Sha256: d3766b1006ad7ad524ddb35e20b7824b26d3200daac7f0b525789cea8536988a
Contents?: true
Size: 1.71 KB
Versions: 38
Compression:
Stored size: 1.71 KB
Contents
module OpenStax module Accounts module HasManyThroughGroups module ActiveRecord module Base def self.included(base) base.extend(ClassMethods) end module ClassMethods def has_many_through_groups(groups_name, name, options = {}) options = {class_name: name.to_s.classify}.merge(options) association_name = "direct_#{name.to_s}".to_sym OpenStax::Accounts::Group.class_exec do has_many association_name, options define_method(name) do OpenStax::Accounts::Group.includes(association_name) .where(openstax_uid: supertree_group_ids) .collect{|g| g.send(association_name).to_a}.flatten.uniq end end class_exec do has_many association_name, options if options[:as] define_method(name) do direct_records = respond_to?(association_name) ? \ send(association_name).to_a : [] indirect_records = OpenStax::Accounts::Group .includes(association_name).where( openstax_uid: send(groups_name).collect{|g| g.supertree_group_ids }.flatten.uniq ) .collect{|g| g.send(association_name).to_a} (direct_records + indirect_records).flatten.uniq end end end end end end end end end ::ActiveRecord::Base.send( :include, OpenStax::Accounts::HasManyThroughGroups::ActiveRecord::Base )
Version data entries
38 entries across 38 versions & 1 rubygems