Class: Machinist::ActiveRecordAdapter

Inherits:
Object
  • Object
show all
Defined in:
lib/machinist/active_record.rb

Class Method Summary collapse

Class Method Details

.assigned_attributes_without_associations(lathe) ⇒ Object

This method takes care of converting any associated objects, in the hash returned by Lathe#assigned_attributes, into their object ids.

For example, let’s say we have blueprints like this:

Post.blueprint { }
Comment.blueprint { post }

Lathe#assigned_attributes will return { :post => … }, but we want to pass { :post_id => 1 } to a controller.

This method takes care of cleaning this up.



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/machinist/active_record.rb', line 30

def self.assigned_attributes_without_associations(lathe)
  attributes = {}
  lathe.assigned_attributes.each_pair do |attribute, value|
    association = lathe.object.class.reflect_on_association(attribute)
    if association && association.macro == :belongs_to
      attributes[association.primary_key_name.to_sym] = value.id
    else
      attributes[attribute] = value
    end
  end
  attributes
end

.class_for_association(object, attribute) ⇒ Object



12
13
14
15
# File 'lib/machinist/active_record.rb', line 12

def self.class_for_association(object, attribute)
  association = object.class.reflect_on_association(attribute)
  association && association.klass
end

.has_association?(object, attribute) ⇒ Boolean

Returns:

  • (Boolean)


8
9
10
# File 'lib/machinist/active_record.rb', line 8

def self.has_association?(object, attribute)
  object.class.reflect_on_association(attribute)
end