Module: ActsAsTable::ValueProviderAssociationMethods

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/acts_as_table/value_provider_association_methods.rb

Overview

ActsAsTable value provider association methods (concern).

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.reflect_on_acts_as_table_value_provider_associations(macro = nil, **options) ⇒ Array<ActiveRecord::Reflection::MacroReflection>

Note:

If you only want to reflect on a certain association type, pass in the symbol (:has_many, :has_one, :belongs_to) as the first parameter.

Returns an array of ActiveRecord::Reflection::MacroReflection objects for all ActsAsTable value provider associations in the class.

Parameters:

  • macro (Symbol, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :only (Array<Symbol>, nil)
  • :except (Array<Symbol>, nil)

Returns:

  • (Array<ActiveRecord::Reflection::MacroReflection>)


16
17
18
19
20
21
22
23
24
# File 'app/models/concerns/acts_as_table/value_provider_association_methods.rb', line 16

def reflect_on_acts_as_table_value_provider_associations(macro = nil, **options)
  options.assert_valid_keys(:except, :only)

  self.reflect_on_all_associations(macro).select { |reflection|
    reflection.klass.acts_as_table_value_provider?
  }.select { |reflection|
    (options[:except].nil? || !options[:except].collect(&:to_sym).include?(reflection.name.to_sym)) && (options[:only].nil? || options[:only].collect(&:to_sym).include?(reflection.name.to_sym))
  }
end

Instance Method Details

#each_acts_as_table_value_provider(macro = nil, **options, &block) ⇒ Enumerable<ActsAsTable::ValueProvider::InstanceMethods>

Note:

If you only want to reflect on a certain association type, pass in the symbol (:has_many, :has_one, :belongs_to) as the first parameter.

Enumerates all associated records for all ActsAsTable value provider associations in the class.

Parameters:

  • macro (Symbol, nil) (defaults to: nil)
  • options (Hash<Symbol, Object>)

Options Hash (**options):

  • :only (Array<Symbol>, nil)
  • :except (Array<Symbol>, nil)

Returns:



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'app/models/concerns/acts_as_table/value_provider_association_methods.rb', line 36

def each_acts_as_table_value_provider(macro = nil, **options, &block)
  ::Enumerator.new { |enumerator|
    {
      belongs_to: [],
      has_one: [],
      has_many: [:each],
      has_and_belongs_to_many: [:each],
    }.each do |method_name, args|
      if macro.nil? || (macro == method_name)
        reflections = self.class.reflect_on_acts_as_table_value_provider_associations(method_name, **options)

        reflections.each do |reflection|
          self.send(reflection.name).try(*args) { |value_provider|
            enumerator << value_provider
          }
        end
      end
    end
  }.each(&block)
end