class ActiveShepherd::Aggregate

Attributes

excluded_attributes[R]
model[R]

Public Class Methods

new(model, excluded_attributes = []) click to toggle source
# File lib/active_shepherd/aggregate.rb, line 5
def initialize(model, excluded_attributes = [])
  @model = model

  @excluded_attributes = ["id", "created_at", "updated_at"]
  @excluded_attributes.concat(Array.wrap(excluded_attributes).map(&:to_s))
end

Public Instance Methods

default_attributes() click to toggle source
# File lib/active_shepherd/aggregate.rb, line 12
def default_attributes
  model.class.new.attributes
end
deserialize_value(attribute_name, value) click to toggle source
# File lib/active_shepherd/aggregate.rb, line 32
def deserialize_value(attribute_name, value)
  run_through_serializer(attribute_name, value, :load)
end
in_namespace?(name) click to toggle source
# File lib/active_shepherd/aggregate.rb, line 36
def in_namespace?(name)
  my_namespace = model.class.to_s
  if name == my_namespace
    false
  elsif name.deconstantize == my_namespace
    true
  elsif name.deconstantize == my_namespace.deconstantize && !name.deconstantize.blank?
    true
  else
    false
  end
end
raw_attributes() click to toggle source
# File lib/active_shepherd/aggregate.rb, line 16
def raw_attributes
  model.attributes_before_type_cast
end
serialize_value(attribute_name, value) click to toggle source
# File lib/active_shepherd/aggregate.rb, line 28
def serialize_value(attribute_name, value)
  run_through_serializer(attribute_name, value, :dump)
end
traversable_associations() click to toggle source
# File lib/active_shepherd/aggregate.rb, line 20
def traversable_associations
  associations.traversable
end
untraversable_association_names() click to toggle source
# File lib/active_shepherd/aggregate.rb, line 24
def untraversable_association_names
  associations.untraversable.keys
end

Private Instance Methods

associations() click to toggle source
# File lib/active_shepherd/aggregate.rb, line 51
def associations
  @associations ||= begin
    ostruct = OpenStruct.new untraversable: {}, traversable: {}
    associations_by_table.each_with_object(ostruct) do |(table, associations), ostruct|
      association_reflection = preferred_association_from_set associations
      if traverse_association?(association_reflection)
        key = :traversable
      else
        key = :untraversable
      end
      ostruct.send(key)[association_reflection.name] = association_reflection
    end
  end
end
associations_by_table() click to toggle source
# File lib/active_shepherd/aggregate.rb, line 66
def associations_by_table
  @associations_by_table ||=
    begin
      by_table = Hash.new { |h,k| h[k] = Array.new }
      model.class.reflect_on_all_associations.each do |association_reflection, hash|
        next unless association_reflection.active_record == model.class
        if traverse_association? association_reflection
          by_table[association_reflection.table_name] << association_reflection
        end
      end
      by_table
    end
end
preferred_association_from_set(associations) click to toggle source
# File lib/active_shepherd/aggregate.rb, line 80
def preferred_association_from_set(associations)
  associations.detect { |a| a.macro == :has_many } || associations.first
end
run_through_serializer(attribute_name, value, method) click to toggle source
# File lib/active_shepherd/aggregate.rb, line 84
def run_through_serializer(attribute_name, value, method)
  serializer = model.class.serialized_attributes[attribute_name.to_s]
  if serializer
    serializer.send(method, value)
  else
    value
  end
end
traverse_association?(association) click to toggle source
# File lib/active_shepherd/aggregate.rb, line 93
def traverse_association?(association)
  return false if association.options[:readonly]
  return false if association.macro == :belongs_to
  return false unless in_namespace?(association.klass.to_s)
  return false if association.is_a?(ActiveRecord::Reflection::ThroughReflection)

  true
end