lib/monarchy/acts_as_resource.rb in monarchy-1.1.5 vs lib/monarchy/acts_as_resource.rb in monarchy-2.0.0.pre.beta.1

- old
+ new

@@ -11,94 +11,107 @@ parent_as(options[:parent_as]) if options[:parent_as] after_update :assign_parent after_create :ensure_hierarchy, :assign_parent - has_many :members, through: :hierarchy, class_name: "::#{Monarchy.member_class}" - has_many :users, through: :members, class_name: "::#{Monarchy.user_class}" - has_one :hierarchy, as: :resource, dependent: :destroy, class_name: 'Monarchy::Hierarchy' - + include_relationships include_scopes include Monarchy::ActsAsResource::InstanceMethods end end module SupportMethods - attr_accessor :parentize, :acting_as_resource, :automatic_hierarchy + attr_accessor :automatic_hierarchy + attr_reader :acting_as_resource, :parentize_name + def default_role_name + Monarchy.configuration.inherited_default_role + end + + def default_role + @default_role ||= Monarchy.role_class.find_by(name: default_role_name) + end + private def setup_acting Monarchy.resource_classes << self @acting_as_resource = true @automatic_hierarchy = true end def parent_as(name) - self.parentize = name + @parentize_name = name end def include_scopes - scope :in, (lambda do |resource| - joins(:hierarchy).where(monarchy_hierarchies: { parent_id: resource.hierarchy.self_and_descendants }) + scope :in, (lambda do |resource, descendants = true| + Monarchy::Validators.resource(resource) + hierarchies = Monarchy.hierarchy_class.in(resource.hierarchy, descendants) + joins(:hierarchy).where(monarchy_hierarchies: { id: hierarchies }) end) scope :accessible_for, (lambda do |user| - joins(:hierarchy).where(monarchy_hierarchies: { id: Monarchy::Hierarchy.accessible_for(user) }) + joins(:hierarchy).where(monarchy_hierarchies: { id: Monarchy.hierarchy_class.accessible_for(user) }) end) end + + def include_relationships + has_many :members, through: :hierarchy, class_name: "::#{Monarchy.member_class}" + has_many :users, through: :members, class_name: "::#{Monarchy.user_class}" + has_one :hierarchy, as: :resource, dependent: :destroy, class_name: "::#{Monarchy.hierarchy_class}" + end end module InstanceMethods def parent @parent = hierarchy.try(:parent).try(:resource) || @parent end def parent=(resource) - if hierarchy - hierarchy.update(parent: resource.try(:ensure_hierarchy)) - else - @parent = resource - end + Monarchy::Validators.resource(resource, true) + hierarchy&.update(parent: resource.try(:ensure_hierarchy)) + @parent = resource end def children @children ||= children_resources end def children=(array) - hierarchy.update(children: hierarchies_for(array)) if hierarchy + hierarchy&.update(children: hierarchies_for(array)) @children = array end def ensure_hierarchy(force = false) - self.hierarchy ||= Monarchy::Hierarchy.create( + self.hierarchy ||= Monarchy.hierarchy_class.create( resource: self, parent: parent.try(:hierarchy), children: hierarchies_for(children) ) if self.class.automatic_hierarchy || force end private def assign_parent(force = false) - parentize = self.class.parentize + parentize = self.class.parentize_name + return unless parentize - if parentize - was_changed = changes["#{parentize}_id"] || changes["#{parentize}_type"] - self.parent = send(parentize) if was_changed || force - end + was_changed = changes["#{parentize}_id"] || changes["#{parentize}_type"] + Monarchy::Validators.resource(send(parentize), true, false) + self.parent = send(parentize) if was_changed || force end def children_resources c = hierarchy.try(:children) return nil if c.nil? c.includes(:resource).map(&:resource) end def hierarchies_for(array) - Array(array).map(&:hierarchy) + array&.compact! + Array(array).map { |resource| Monarchy::Validators.resource(resource).hierarchy } end end end end