Module: Seedable::Exporter::ClassMethods

Defined in:
lib/seedable/exporter.rb

Overview

Extensions to all ActiveRecord classes to enable this class for import and export of data.

Use acts_as_seedable here to enable your classes to use this functionality.

Use from_seedable and to_seedable here to perform the serialization and deserialization.

Instance Method Summary collapse

Instance Method Details

#filter_attributes(attributes) ⇒ Object

Sets which attributes should be fitlered from the serialization of this object.

Parameters

  • attributes - Array of symbols representing attributes.

Examples

Garage.filter_attributes([:id, :name])


63
64
65
# File 'lib/seedable/exporter.rb', line 63

def filter_attributes(attributes)
  self.send(:filterable_attributes=, attributes)
end

#from_seedable_attributes(attributes) ⇒ Object

Create object from attributes without a root node, since it’s assumed to be the type this method is being called on.

Parameters

  • attributes - Hash of attributes, without a root node.

Examples

Garage.from_seedable_attributes({ :id => '1', :name => 'Name' })


101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/seedable/exporter.rb', line 101

def from_seedable_attributes(attributes) # :nodoc:
  object = self.new

  # Handling for rails-3.2.x vs. rails-3.0.x changes.
  #
  if object.respond_to?(:assign_attributes)
    object.assign_attributes(
      Helpers.convert_to_nested_attributes(self, attributes), 
      :without_protection => true
    )
  else
    object.send(
      :attributes=,
      Helpers.convert_to_nested_attributes(self, attributes), 
      false
    )
  end

  object.save!(:validate => false)
  object
end

#include_associations(associations) ⇒ Object

Sets which associations should be traversed when performing serialization.

Parameters

  • associations - Array of symbols representing associations.

Examples

Garage.include_associations([:cars])

Car.include_associations([:garage, :drivers])


80
81
82
83
84
85
86
87
88
# File 'lib/seedable/exporter.rb', line 80

def include_associations(associations)
  self.send(:includable_associations=, associations)

  unless Rails.env.production?
    associations.each do |association|
      self.accepts_nested_attributes_for association
    end
  end
end

#seedable(options = {}) ⇒ Object

Enable seedable export and import for the including class.

Options

  • filter_attributes - Attributes to filter from the export.

  • include_associations - Associations that should be traversed during export.

Examples

class Garage < ActiveRecord::Base 
  acts_as_seedable :include_associations => [:cars]
end


31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/seedable/exporter.rb', line 31

def seedable(options = {})
  cattr_accessor :filterable_attributes
  cattr_accessor :includable_associations

  if options[:filter_attributes]
    self.send(:filter_attributes, options.delete(:filter_attributes))
  end

  if options[:include_associations]
    self.send(:include_associations, options.delete(:include_associations))
  else
    self.send(:include_associations, Helpers.associations_for(self))
  end
end

#seedable?Boolean

Return seedable status

Returns:

  • (Boolean)


48
49
50
# File 'lib/seedable/exporter.rb', line 48

def seedable?
  self.respond_to?(:filterable_attributes) && self.respond_to?(:includable_associations)
end