0

I'm working on an engine where any model can have a has_many association with Permit as Permissible:

class Permit < ActiveRecord::Base
  belongs_to :permissible, polymorphic: true
end

module Permissible
  def self.included(base)
    base.class_eval do
    has_many :permits, as: :permissible
  end
end

class Group < ActiveRecord::Base
  include Permissible
end

class GroupAllocation < ActiveRecord::Base
  belongs_to :person
  belongs_to :group
end

class Person < ActiveRecord::Base
  include Permissible
  has_many :group_allocations
  has_many :groups, through: :group_allocations
end

class User < ActiveRecord::Base
  belongs_to :person
end

So, Group has_many :permits and Person has_many :permits. What I am trying to do is dynamically create associations on User that uses the permits association as a source, and chain associations on other models down to User by doing the same. This can be done manually (in rails 3.1+) with:

class Person
  has_many :group_permits, through: :person, source: :permits
end

class User
  has_many :person_permits, through: :person, source: :permits, class_name: Permit
  has_many :person_group_permits, through: :person, source: :group_permits, class_name: Permit
end

However, in practice, Permissible will be included on many models, so I'm trying to write a class method on User (actually within another module, but no need to confuse things more) that can traverse User.reflect_on_all_associations and create an array of new associations, which may be many associations deep each.

Looking for input on how to do this cleanly in rails 3.2.8.

iliveinapark
  • 327
  • 4
  • 13

1 Answers1

0

Here is how I did it (implementation code varies slightly from the details given in the question):

module Authorisable def self.included(base) base.class_eval do base.extend ClassMethods end end

module ClassMethods
  class PermissionAssociationBuilder
    def build_permissions_associations(klass)
      chains = build_chains_from(klass)
      chains.select! {|c| c.last.klass.included_modules.include? DistributedAuthorisation::Permissible}
      permissions_associations = []
      chains.each do |chain|
        source_name = :permissions
        chain.reverse.each do |r|
          assoc_name = :"#{r.name}_#{source_name}"
          r.active_record.has_many assoc_name, through: r.name.to_sym, source: source_name, class_name: DistributedAuthorisation::Permission
          source_name = assoc_name
        end
        permissions_associations << source_name
      end
      return permissions_associations
    end

    private

    def build_chains_from(klass)
      chains = reflections_to_follow(klass).map {|r| [r]}
      chains.each do |chain|
        models = chain.map {|r| r.klass}.unshift klass
        reflections_to_follow(models.last).each do |r|
          chains << (chain.clone << r) unless models.include? r.klass
        end
      end
    end

    def reflections_to_follow(klass)
      refs = klass.reflect_on_all_associations
      refs.reject {|r| r.options[:polymorphic] or r.is_a? ActiveRecord::Reflection::ThroughReflection}
    end
  end

  def permissions_associations
    @permissions_associations ||= PermissionAssociationBuilder.new.build_permissions_associations(self)
  end
end

Probably not the most efficient method, but it adds the chains I'm after with Klass.permissions_associations, and stores their symbols in class instance variable.

I'd be happy to hear suggestions on how to improve it.

iliveinapark
  • 327
  • 4
  • 13