Module: PortableModel::ClassMethods
- Defined in:
- lib/portable_model.rb
Instance Method Summary collapse
- #excluded_export_attrs ⇒ Object
-
#import_from_hash(record_hash, options = {}) ⇒ Object
Import a record from a hash.
-
#import_from_yml(filename, additional_attrs = {}, options = {}) ⇒ Object
Export a record from a YAML file.
- #included_association_keys ⇒ Object
- #order_associations ⇒ Object
- #overridden_export_attrs ⇒ Object
- #overridden_import_attrs ⇒ Object
-
#portable_associations ⇒ Object
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.
-
#portable_attributes ⇒ Object
Returns the names of portable attributes, which are any attributes that are not primary or foreign keys.
-
#start_exporting(&block) ⇒ Object
Starts an export session and yields a hash of currently exported records in the session to the specified block.
-
#start_importing(&block) ⇒ Object
Starts an import session and yields a hash of currently imported records in the session to the specified block.
Instance Method Details
#excluded_export_attrs ⇒ Object
212 213 214 |
# File 'lib/portable_model.rb', line 212 def excluded_export_attrs @excluded_export_attrs ||= superclass.include?(PortableModel) ? superclass.excluded_export_attrs.dup : Set.new end |
#import_from_hash(record_hash, options = {}) ⇒ Object
Import a record from a hash.
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 144 145 146 147 148 149 150 151 152 153 |
# File 'lib/portable_model.rb', line 99 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. overridden_import_attrs.each do |attr, overridden_value| record_hash[attr] = overridden_value.is_a?(Proc) ? overridden_value.call(record_hash) : overridden_value end 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] if record update_record_associations(record, record_hash, ) else 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 .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, ) 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.
157 158 159 160 |
# File 'lib/portable_model.rb', line 157 def import_from_yml(filename, additional_attrs = {}, = {}) record_hash = YAML::load_file(filename) import_from_hash(record_hash.merge(additional_attrs), ) end |
#included_association_keys ⇒ Object
208 209 210 |
# File 'lib/portable_model.rb', line 208 def included_association_keys @included_association_keys ||= superclass.include?(PortableModel) ? superclass.included_association_keys.dup : Set.new end |
#order_associations ⇒ Object
224 225 226 |
# File 'lib/portable_model.rb', line 224 def order_associations @order_associations ||= superclass.include?(PortableModel) ? superclass.order_associations.dup : [] end |
#overridden_export_attrs ⇒ Object
216 217 218 |
# File 'lib/portable_model.rb', line 216 def overridden_export_attrs @overridden_export_attrs ||= superclass.include?(PortableModel) ? superclass.overridden_export_attrs.dup : {} end |
#overridden_import_attrs ⇒ Object
220 221 222 |
# File 'lib/portable_model.rb', line 220 def overridden_import_attrs @overridden_import_attrs ||= superclass.include?(PortableModel) ? superclass.overridden_import_attrs.dup : {} end |
#portable_associations ⇒ Object
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.
200 201 202 203 204 205 206 |
# File 'lib/portable_model.rb', line 200 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_attributes ⇒ Object
Returns the names of portable attributes, which are any attributes that are not primary or foreign keys.
179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/portable_model.rb', line 179 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.
165 166 167 |
# File 'lib/portable_model.rb', line 165 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.
172 173 174 |
# File 'lib/portable_model.rb', line 172 def start_importing(&block) start_porting(:imported_records, &block) end |