Skip to content

Instantly share code, notes, and snippets.

@obie
Created February 28, 2012 19:47
Show Gist options
  • Save obie/1934672 to your computer and use it in GitHub Desktop.
Save obie/1934672 to your computer and use it in GitHub Desktop.
# See http://blog.obiefernandez.com/content/2012/02/metaprogramming-your-activerecord-objects-at-runtime.html for an explanation
require 'spec_helper'
describe Nomination do
let(:user1) { FactoryGirl.create(:user) }
let(:user2) { FactoryGirl.create(:user_with_nominations_received) }
let(:user3) { FactoryGirl.create(:user_with_nominations_received) }
let(:user4) { FactoryGirl.create(:user) }
let(:user5) { FactoryGirl.create(:user) }
let(:params) { {:description => "Great Work!", :submitter => user1} }
context "Teamwork Props" do
let(:teamwork_props) do
returning(FactoryGirl.create(:award, name: "Teamwork")) do |award|
award.modifier_expr = <<-EOF
def normal_validations
if users.size == 1 && users.include?(submitter)
errors.add(:user_ids, "You can't give Teamwork Props to only yourself")
end
end
EOF
end
end
it "can include the submitter and another recipient" do
nomination = Nomination.create params.merge(award: teamwork_props, :user_ids => [user1.id, user2.id])
nomination.should be_valid
end
it "cannot include only the submitter" do
nomination = Nomination.create params.merge(award: teamwork_props, :user_ids => [user1.id])
nomination.should_not be_valid
end
it "mod doesn't affect another unrelated award" do
nomination = Nomination.create params.merge(award: FactoryGirl.create(:award, name: "Thank You"), :user_ids => [user1.id, user2.id])
nomination.should_not be_valid
end
end
context "Dynamic Duo" do
let(:dynamic_duo_props) do
returning(FactoryGirl.create(:award, name: "Dynamic Duo")) do |award|
award.modifier_expr = <<-EOF
def normal_validations
if users.size == 1 && users.include?(submitter)
errors.add(:user_ids, "You can't give Teamwork Props to only yourself")
end
if users.size != 2
errors.add(:user_ids, "You can only give Dynamic Duo to two recipients at a time.")
end
end
EOF
end
end
it "can include the submitter and another recipient" do
nomination = Nomination.create params.merge(award: dynamic_duo_props, :user_ids => [user1.id, user2.id])
nomination.should be_valid
end
it "cannot include only the submitter" do
nomination = Nomination.create params.merge(award: dynamic_duo_props, :user_ids => [user1.id])
nomination.should_not be_valid
end
it "cannot have more than two recipients" do
nomination = Nomination.create params.merge(award: dynamic_duo_props, :user_ids => [user1.id, user2.id, user3.id])
nomination.should_not be_valid
end
it "mod doesn't affect another unrelated award" do
nomination = Nomination.create params.merge(award: FactoryGirl.create(:award, name: "Thank You"), :user_ids => [user1.id, user2.id])
nomination.should_not be_valid
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment