Module: PortableModel::ClassMethods

Defined in:
lib/portable_model.rb

Instance Method Summary collapse

Instance Method Details

#excluded_export_attrsObject



202
203
204
# File 'lib/portable_model.rb', line 202

def excluded_export_attrs
  @excluded_export_attrs ||= Set.new
end

#import_from_hash(record_hash, options = {}) ⇒ Object

Import a record from a hash.

Raises:

  • (ArgumentError)


93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/portable_model.rb', line 93

def import_from_hash(record_hash, options = {})
  raise ArgumentError.new('specified argument is not a hash') unless record_hash.is_a?(Hash)

  # Override any necessary attributes before importing.
  record_hash.merge!(overridden_import_attrs)

  if (columns_hash.include?(inheritance_column) &&
      (record_type_name = record_hash[inheritance_column.to_s]) &&
      !record_type_name.blank? &&
      record_type_name != sti_name)
    # The model implements STI and the record type points to a different
    # class; call the method in that class instead.
    compute_type(record_type_name).import_from_hash(record_hash, options)
  else
    start_importing do |imported_records|
      # If the hash had already been imported during the current session,
      # use the result of that previous import.
      record = imported_records[record_hash.object_id]

      unless record
        transaction do
          # First split out the attributes that correspond to portable
          # associations.
          assoc_attrs = portable_associations.inject({}) do |hash, assoc_name|
            hash[assoc_name] = record_hash.delete(assoc_name) if record_hash.has_key?(assoc_name)
            hash
          end

          if options.fetch(:skip_validations, false)
            # Create a new record and save, skipping validations.
            record = new(record_hash.merge(:importing_record => true))
            record.save(false)
          else
            record = create!(record_hash.merge(:importing_record => true))
          end

          # Import each of the record's associations into the record.
          assoc_attrs = assoc_attrs.sort_by { |assoc_name, assoc_value| order_associations.index(assoc_name) ? order_associations.index(assoc_name) : order_associations.size } unless order_associations.empty?
          assoc_attrs.each do |assoc_name, assoc_value|
            record.import_into_association(assoc_name, assoc_value, options)
          end

        end

        imported_records[record_hash.object_id] = record
      end

      record
    end
  end
end

#import_from_yml(filename, additional_attrs = {}, options = {}) ⇒ Object

Export a record from a YAML file.



147
148
149
150
# File 'lib/portable_model.rb', line 147

def import_from_yml(filename, additional_attrs = {}, options = {})
  record_hash = YAML::load_file(filename)
  import_from_hash(record_hash.merge(additional_attrs), options)
end

#included_association_keysObject



198
199
200
# File 'lib/portable_model.rb', line 198

def included_association_keys
  @included_association_keys ||= Set.new
end

#order_associationsObject



214
215
216
# File 'lib/portable_model.rb', line 214

def order_associations
  @order_associations ||= []
end

#overridden_export_attrsObject



206
207
208
# File 'lib/portable_model.rb', line 206

def overridden_export_attrs
  @overridden_export_attrs ||= {}
end

#overridden_import_attrsObject



210
211
212
# File 'lib/portable_model.rb', line 210

def overridden_import_attrs
  @overridden_import_attrs ||= {}
end

#portable_associationsObject

Returns names of portable associations, which are has_one and has_many associations that do not go through other associations and that also include PortableModel.

Because has_and_belongs_to_many associations are bi-directional, they are not portable.



190
191
192
193
194
195
196
# File 'lib/portable_model.rb', line 190

def portable_associations
  reflect_on_all_associations.select do |assoc_reflection|
    assoc_reflection.macro.in?([:has_one, :has_many]) &&
      !assoc_reflection.is_a?(ActiveRecord::Reflection::ThroughReflection) &&
      assoc_reflection.klass.include?(PortableModel)
  end.map(&:name).map(&:to_s)
end

#portable_attributesObject

Returns the names of portable attributes, which are any attributes that are not primary or foreign keys.



169
170
171
172
173
174
175
176
177
178
179
180
181
# File 'lib/portable_model.rb', line 169

def portable_attributes
  columns.reject do |column|
    # TODO: Consider rejecting counter_cache columns as well; this will involve retrieving a has_many association's corresponding belongs_to association to retrieve its counter_cache_column.
    (
      column.primary ||
      column.name.in?(excluded_export_attrs) && !overridden_export_attrs.has_key?(column.name) ||
      (
        column.name.in?(reflect_on_all_associations(:belongs_to).map(&:association_foreign_key)) &&
        !column.name.in?(included_association_keys)
      )
    )
  end.map(&:name).map(&:to_s) | overridden_export_attrs.keys
end

#start_exporting(&block) ⇒ Object

Starts an export session and yields a hash of currently exported records in the session to the specified block.



155
156
157
# File 'lib/portable_model.rb', line 155

def start_exporting(&block)
  start_porting(:exported_records, &block)
end

#start_importing(&block) ⇒ Object

Starts an import session and yields a hash of currently imported records in the session to the specified block.



162
163
164
# File 'lib/portable_model.rb', line 162

def start_importing(&block)
  start_porting(:imported_records, &block)
end