Module: PortableModel::ClassMethods

Defined in:
lib/portable_model.rb

Instance Method Summary collapse

Instance Method Details

#excluded_export_attrsObject



189
190
191
# File 'lib/portable_model.rb', line 189

def excluded_export_attrs
  @excluded_export_attrs ||= Set.new
end

#import_from_hash(record_hash) ⇒ Object

Import a record from a hash.

Raises:

  • (ArgumentError)


87
88
89
90
91
92
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
# File 'lib/portable_model.rb', line 87

def import_from_hash(record_hash)
  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)
  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

          # Create a new record.
          record = create!(record_hash.merge(:importing_record => true))

          # Import each of the record's associations into the record.
          assoc_attrs.each do |assoc_name, assoc_value|
            record.import_into_association(assoc_name, assoc_value)
          end
        end

        imported_records[record_hash.object_id] = record
      end

      record
    end
  end
end

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

Export a record from a YAML file.



134
135
136
137
# File 'lib/portable_model.rb', line 134

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

#included_association_keysObject



185
186
187
# File 'lib/portable_model.rb', line 185

def included_association_keys
  @included_association_keys ||= Set.new
end

#overridden_export_attrsObject



193
194
195
# File 'lib/portable_model.rb', line 193

def overridden_export_attrs
  @overridden_export_attrs ||= {}
end

#overridden_import_attrsObject



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

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.



177
178
179
180
181
182
183
# File 'lib/portable_model.rb', line 177

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.



156
157
158
159
160
161
162
163
164
165
166
167
168
# File 'lib/portable_model.rb', line 156

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)
end

#start_exporting(&block) ⇒ Object

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



142
143
144
# File 'lib/portable_model.rb', line 142

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.



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

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