class Wallaby::ActiveRecord::ModelDecorator::FieldsBuilder::AssociationBuilder

To build the metadata for associations fields

Public Instance Methods

update(metadata, reflection) click to toggle source

Update the metadata @param metadata [Hash] @param reflection [ActiveRecord::Reflection]

# File lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb, line 12
def update(metadata, reflection)
  type = reflection.macro
  metadata[:is_association] = true
  metadata[:sort_disabled] = true
  metadata[:is_through] = through?(reflection)
  metadata[:has_scope] = scope?(reflection)
  metadata[:foreign_key] = foreign_key_for(reflection, type)
end

Protected Instance Methods

foreign_key_for(reflection, type) click to toggle source

@param reflection [ActiveRecord::Reflection] @param type [Symbol] @return [String] foreign key

# File lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb, line 26
def foreign_key_for(reflection, type)
  if type == :belongs_to || reflection.polymorphic?
    reflection.foreign_key
  elsif reflection.collection?
    # @see https://github.com/rails/rails/blob/92703a9ea5d8b96f30e0b706b801c9185ef14f0e/activerecord/lib/active_record/associations/builder/collection_association.rb#L50
    reflection.name.to_s.singularize << '_ids'
  else
    reflection.association_foreign_key
  end
end
scope?(reflection) click to toggle source

@param reflection [ActiveRecord::Reflection] @return [true] if it has scope @return [false] otherwise

# File lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb, line 47
def scope?(reflection)
  reflection.scope.present?
end
through?(reflection) click to toggle source

@param reflection [ActiveRecord::Reflection] @return [true] if it's a through relation @return [false] otherwise

# File lib/adapters/wallaby/active_record/model_decorator/fields_builder/association_builder.rb, line 40
def through?(reflection)
  reflection.is_a? ::ActiveRecord::Reflection::ThroughReflection
end