Permalink
Browse files

add interface for setting roles properly

  • Loading branch information...
1 parent 27c64d7 commit a1145be6237e1108c9e8a491709889f0add72e45 @iffyuva iffyuva committed Jun 18, 2012
Showing with 38 additions and 0 deletions.
  1. +19 −0 app/models/attendee.rb
  2. +19 −0 spec/models/attendees_spec.rb
View
19 app/models/attendee.rb
@@ -32,6 +32,13 @@ class Attendee < ActiveRecord::Base
validates :roles, :inclusion => { :in => ROLES.values.min..ROLES.values.sum }
+ class UnknownRole < ActiveRecord::ActiveRecordError
+ def initialize(message)
+ @message = message
+ end
+ attr_reader :message
+ end
+
def get_roles
# bitwise-and with each role value, and pick them
ROLES.reject { |k, v| (self.roles & v) == 0 }.keys
@@ -45,4 +52,16 @@ def #{role.to_s}?
end
RUBY
end
+
+ def set_roles!(roles)
+ self.roles = 0
+ roles.each do |role|
+ unless ROLES.keys.include?(role)
+ raise Attendee::UnknownRole.new("unknown role=#{role.to_s}")
+ end
+
+ self.roles |= ROLES[role]
+ end
+ save!
+ end
end
View
19 spec/models/attendees_spec.rb
@@ -43,5 +43,24 @@
roles.each { |r| user.send("#{r}?").should be_true }
end
end
+
+ it "should set valid roles properly" do
+ attendee = FactoryGirl.create(:attendee)
+ expected = [
+ [:presenter, :keynote],
+ [:presenter, :chair],
+ [:attendee, :chair, :admin]
+ ]
+
+ expected.each do |roles|
+ attendee.set_roles!(roles)
+ attendee.get_roles.should == roles
+ end
+ end
+
+ it "should not set invalid roles" do
+ attendee = FactoryGirl.create(:attendee)
+ expect { attendee.set_roles!([:coordinator]) }.to raise_error(Attendee::UnknownRole)
+ end
end
end

0 comments on commit a1145be

Please sign in to comment.