Class: Manufactory::DataMapperAdapter

Inherits:
GenericModelAdapter show all
Defined in:
lib/manufactory/adapters/datamapper.rb

Instance Attribute Summary

Attributes inherited from Adapter

#callables, #klass, #name

Instance Method Summary collapse

Methods inherited from GenericModelAdapter

#initialize_object

Methods inherited from Adapter

#initialize, #run

Constructor Details

This class inherits a constructor from Manufactory::Adapter

Instance Method Details

#assigned_attributes_without_associations(dsl) ⇒ 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.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/manufactory/adapters/datamapper.rb', line 41

def assigned_attributes_without_associations(dsl)
  attributes = {}
  dsl.assigned_attributes.each_pair do |attribute, value|
    association = dsl.object.class.relationships[attribute]
    if association && association_is_many_to_one?(association)
      # DataMapper child_key can have more than one property, but I'm not
      # sure in what circumstances this would be the case. I'm assuming
      # here that there's only one property.
      key = association.child_key.map(&:field).first.to_sym
      attributes[key] = value.id
    else
      attributes[attribute] = value
    end
  end
  attributes
end

#association_is_many_to_one?(association) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
21
22
23
24
25
26
# File 'lib/manufactory/adapters/datamapper.rb', line 18

def association_is_many_to_one?(association)
  if defined?(DataMapper::Associations::ManyToOne::Relationship)
    # We're using the next branch of DM
    association.class == DataMapper::Associations::ManyToOne::Relationship
  else
    # We're using the 0.9 or less branch.
    association.options[:max].nil?
  end
end

#class_for_association(object, attribute) ⇒ Object



13
14
15
16
# File 'lib/manufactory/adapters/datamapper.rb', line 13

def class_for_association(object, attribute)
  association = object.class.relationships[attribute]
  association && association.parent_model
end

#has_association?(object, attribute) ⇒ Boolean

Returns:

  • (Boolean)


9
10
11
# File 'lib/manufactory/adapters/datamapper.rb', line 9

def has_association?(object, attribute)
  object.class.relationships.has_key?(attribute)
end