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

Version Path
openstax_accounts-7.13.1 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.13.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.12.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.11.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.10.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.9.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.8.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.7.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.6.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.5.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.4.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.3.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.2.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.1.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.0.1 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-7.0.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-6.4.2 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-6.4.1 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-6.4.0 lib/openstax/accounts/has_many_through_groups/active_record/base.rb
openstax_accounts-6.3.2 lib/openstax/accounts/has_many_through_groups/active_record/base.rb