Module: Seedable::Helpers

Defined in:
lib/seedable/helpers.rb

Overview

:nodoc:

Class Method Summary collapse

Class Method Details

.associations_for(klass) ⇒ Object

Return all associations for a particular class.



78
79
80
81
82
# File 'lib/seedable/helpers.rb', line 78

def self.associations_for(klass)
  klass.reflections.map do |reflection| 
    valid_associations_include?(reflection.last.macro) ? reflection.first : nil
  end.compact
end

.attributes_without_root(attributes, root) ⇒ Object

Return all attributes in a hash, without the root node, if present.



96
97
98
# File 'lib/seedable/helpers.rb', line 96

def self.attributes_without_root(attributes, root) 
  attributes.delete(root)
end

.convert_to_nested_attributes(klass, node_attributes) ⇒ Object

Convert a hash of nested model attributes to a valid hash to be passed to assign_attributes by substituting association name for the appropriate association accessors.



128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/seedable/helpers.rb', line 128

def self.convert_to_nested_attributes(klass, node_attributes) 
  node_attributes.keys.inject(node_attributes) do |attributes, key|
    if is_key_association_for_class?(klass, key)
      sub_attributes = attributes_without_root(attributes, key)

      attributes[nested_attributes_key_for(key)] = sub_attributes.is_a?(Enumerable) ?
        sub_attributes.map do |sub_attribute| 
          convert_to_nested_attributes(to_class(key), sub_attribute)
        end :
        convert_to_nested_attributes(to_class(key), sub_attributes)
    end
    attributes
  end
end

.filterable_attributes(klass) ⇒ Object

Filterable attributes for a particular class.



56
57
58
59
60
61
62
# File 'lib/seedable/helpers.rb', line 56

def self.filterable_attributes(klass)
  if klass.respond_to?(:filterable_attributes) 
    klass.filterable_attributes
  else
    []
  end
end

.is_key_association_for_class?(klass, key) ⇒ Boolean

Returns true of a particular association is valid for a particular class.

Returns:

  • (Boolean)


87
88
89
90
91
# File 'lib/seedable/helpers.rb', line 87

def self.is_key_association_for_class?(klass, key)
  klass.reflections.any? do |reflection| 
    valid_associations_include?(reflection.last.macro) && reflection.first == key.to_sym
  end
end

.nested_attributes_key_for(key) ⇒ Object

Return the name of the nested association accessor for a particular association.



103
104
105
# File 'lib/seedable/helpers.rb', line 103

def self.nested_attributes_key_for(key)
  "#{key}_attributes"
end

.parse_seedable(json) ⇒ Object

Parse a seedable object returning a hash.



14
15
16
# File 'lib/seedable/helpers.rb', line 14

def self.parse_seedable(json)
  JSON.parse(json)
end

.reflection_type(klass, association) ⇒ Object

Return the proper active_record reflection type for a nested association.



120
121
122
# File 'lib/seedable/helpers.rb', line 120

def self.reflection_type(klass, association)
  return_reflection(klass, association).collection? ? :collection : :one_to_one
end

.return_reflection(klass, association) ⇒ Object

Return a particular reflection on an object or class.



109
110
111
112
113
114
115
# File 'lib/seedable/helpers.rb', line 109

def self.return_reflection(klass, association) 
  if klass.respond_to?(:reflect_on_association) 
    klass.reflect_on_association(association)
  else 
    klass.class.reflect_on_association(association)
  end
end

.to_class(string) ⇒ Object

Convert a string or symbol to a class in the object space.



8
9
10
# File 'lib/seedable/helpers.rb', line 8

def self.to_class(string)
  string.to_s.classify.constantize
end

.to_class_and_attributes(hash) ⇒ Object

Convert hash root node to class, and return attributes.



20
21
22
23
24
25
26
# File 'lib/seedable/helpers.rb', line 20

def self.to_class_and_attributes(hash)
  node       = hash.keys.first
  attributes = hash.delete(node)
  klass      = Helpers.to_class(node)

  [klass, attributes]
end

.traverse_includable_associations(klass, ancestor_klass) ⇒ Object

Traverse all associations starting from a particular class.



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/seedable/helpers.rb', line 37

def self.traverse_includable_associations(klass, ancestor_klass)
  if klass.respond_to?(:includable_associations) 
    klass.includable_associations.inject({}) do |associations, association|
      descendent_klass = Helpers.to_class(association)
      unless descendent_klass == ancestor_klass || !descendent_klass.seedable?
        associations[association] = { 
          :include => traverse_includable_associations(Helpers.to_class(association), klass),
          :except  => filterable_attributes(Helpers.to_class(association))
        }
      end
      associations
    end
  else 
    {}
  end
end

.traverse_includable_associations_from_instance(klass) ⇒ Object

Traverse all associations starting from an instance of a particular class.



31
32
33
# File 'lib/seedable/helpers.rb', line 31

def self.traverse_includable_associations_from_instance(klass)
  traverse_includable_associations(klass.class, klass.class)
end

.valid_association_typesObject

Valid traversable association types.



66
67
68
# File 'lib/seedable/helpers.rb', line 66

def self.valid_association_types
  [:has_many, :has_one, :belongs_to]
end

.valid_associations_include?(macro) ⇒ Boolean

Determine if a particular association macro is valid.

Returns:

  • (Boolean)


72
73
74
# File 'lib/seedable/helpers.rb', line 72

def self.valid_associations_include?(macro)
  valid_association_types.include?(macro)
end