diff options
author | Peter Kozak <spag@golwen.net> | 2013-02-22 07:08:23 -0500 |
---|---|---|
committer | Peter Kozak <spag@golwen.net> | 2013-02-22 07:08:23 -0500 |
commit | 2a9b798f9e122582abb36605a7d4a1da0e70eca9 (patch) | |
tree | 45fe362d677cb243fba57dc7c0c23d01e653863e /app/models/group_membership.rb | |
parent | d6009d77ffa221c14a4536be98a04e8e4b6474a9 (diff) |
pickup group permission model added
Diffstat (limited to 'app/models/group_membership.rb')
-rw-r--r-- | app/models/group_membership.rb | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/app/models/group_membership.rb b/app/models/group_membership.rb new file mode 100644 index 0000000..0f04ae1 --- /dev/null +++ b/app/models/group_membership.rb @@ -0,0 +1,37 @@ +class GroupMembership < ActiveRecord::Base + attr_accessible :group_id, :item_type, :item_id + + belongs_to :group + belongs_to :item, :polymorphic => true + + validates :item_id, + :presence => true, + :uniqueness => { :scope => [:group_id, :item_type] } + + validates :item_type, + :presence => true, + :uniqueness => { :scope => [:group_id, :item_id] } + + validate :validate_item_type_consitency + + validates :item, + :presence => true + + def to_s + "#{self.item_type}: #{self.item}" + end + + def item_type_allowed + fist_item = self.group.group_memberships.first.try(:item) + if fist_item + return fist_item.class.name + end + end + + def validate_item_type_consitency + type_allowed = self.item_type_allowed + if type_allowed && type_allowed != self.item_type + errors.add(:item_type, "must be of type: #{type_allowed}") + end + end +end |