Sha256: 9f76ad8db210153a3b1eed3b4ee3fec224b560b62e662fa5d3da875efcb7cf36

Contents?: true

Size: 1.06 KB

Versions: 9

Compression:

Stored size: 1.06 KB

Contents

# Helper methods for `comma` method
Comma::Extractor.class_eval do
  def __association__(hash)
    return unless hash.is_a? Hash

    association = hash.keys.first
    method_path = hash_to_method_path(hash)

    block = -> (record) { method_path[1..-1].inject(record, &:send) }
    klass = @instance.is_a?(ActiveRecord::Base) ? @instance.class : @instance
    send(association, nested_human_attribute_name(klass, method_path), &block)
  end

  private

  # { foo: { bar: :sample } } => [:foo, :bar, :sample]
  def hash_to_method_path(hash)
    key, value = hash.first
    value = value.is_a?(Hash) ? hash_to_method_path(value) : [value]
    [key] + value
  end

  # [:foo, :bar, :sample] => "Foo/Bar/Sample"
  def nested_human_attribute_name(klass, method_path)
    method_name = method_path.first
    return unless method_name
    association = klass.reflect_on_association(method_name).try(:klass) || klass
    human_method_name = klass.human_attribute_name(method_name)
    [human_method_name, nested_human_attribute_name(association, method_path[1..-1])].compact.join('/')
  end
end

Version data entries

9 entries across 9 versions & 2 rubygems

Version Path
comable-core-0.7.1 lib/comma_extractor_extentions.rb
comable-core-0.7.0 lib/comma_extractor_extentions.rb
comable-core-0.7.0.beta2 lib/comma_extractor_extentions.rb
comable-core-0.7.0.beta1 lib/comma_extractor_extentions.rb
comable-core-0.6.0 lib/comma_extractor_extentions.rb
comable_core-0.5.0 lib/comma_extractor_extentions.rb
comable_core-0.4.2 lib/comma_extractor_extentions.rb
comable_core-0.4.1 lib/comma_extractor_extentions.rb
comable_core-0.4.0 lib/comma_extractor_extentions.rb