diff options
Diffstat (limited to 'app/models')
-rw-r--r-- | app/models/group.rb | 11 | ||||
-rw-r--r-- | app/models/group_membership.rb | 37 | ||||
-rw-r--r-- | app/models/group_permission.rb | 24 |
3 files changed, 72 insertions, 0 deletions
diff --git a/app/models/group.rb b/app/models/group.rb new file mode 100644 index 0000000..e0cfaab --- /dev/null +++ b/app/models/group.rb @@ -0,0 +1,11 @@ +class Group < ActiveRecord::Base + attr_accessible :name, :active, :comment + + has_many :group_memberships, :dependent => :destroy + has_many :group_permissions, :dependent => :destroy + has_many :permittances, :foreign_key => :target_group_id, :class_name => "GroupPermission", :dependent => :destroy + + def to_s + self.name + end +end 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 diff --git a/app/models/group_permission.rb b/app/models/group_permission.rb new file mode 100644 index 0000000..fe988ba --- /dev/null +++ b/app/models/group_permission.rb @@ -0,0 +1,24 @@ +class GroupPermission < ActiveRecord::Base + attr_accessible :group_id, :permission, :target_group_id + + PERMISSION_TYPES = ['pickup'] + + belongs_to :group + belongs_to :target_group, :class_name => "Group" + + validates :target_group_id, + :presence => true, + :uniqueness => { :scope => [:group_id, :permission] } + + validates :target_group, + :presence => true + + validates :permission, + :presence => true, + :uniqueness => { :scope => [:group_id, :target_group_id] }, + :inclusion => { :in => PERMISSION_TYPES } + + def to_s + "#{self.permission} => #{self.target_group}" + end +end |