Module: IronTrail::Reifier
- Defined in:
- lib/iron_trail/reifier.rb
Class Method Summary collapse
Class Method Details
.model_from_table_name(table_name, sti_type = nil) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/iron_trail/reifier.rb', line 28 def self.model_from_table_name(table_name, sti_type = nil) candidates = ActiveRecord::Base.descendants .reject(&:abstract_class) .select { |klass| klass.table_name == table_name } raise "Cannot infer model from table named '#{table_name}'" if candidates.empty? return candidates.first if candidates.one? if sti_type.present? klass = candidates.find { |c| c.name == sti_type } return klass if klass raise "Cannot infer STI model for table #{table_name} and type '#{sti_type}'" end # When sti_type is nil and multiple classes share the table, # filter out STI subclasses to find the base class. bases = candidates.reject { |c| candidates.any? { |other| other != c && c < other } } return bases.first if bases.one? # If multiple base classes remain, prefer the one that is an STI base # (i.e., has subclasses among the original candidates). sti_bases = bases.select { |c| candidates.any? { |other| other != c && other < c } } return sti_bases.first if sti_bases.one? raise "Cannot infer model from table named '#{table_name}'" end |
.reify(trail) ⇒ Object
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/iron_trail/reifier.rb', line 5 def self.reify(trail) source_attributes = (trail.delete_operation? ? trail.rec_old : trail.rec_new) klass = model_from_table_name(trail.rec_table, source_attributes['type']) record = klass.where(id: trail.rec_id).first || klass.new source_attributes.each do |name, value| if record.has_attribute?(name) attr_type = record.type_for_attribute(name) record[name] = attr_type.deserialize(value) else ghost = record.instance_variable_get(:@irontrail_reified_ghost_attributes) unless ghost ghost = HashWithIndifferentAccess.new record.instance_variable_set(:@irontrail_reified_ghost_attributes, ghost) end ghost[name] = value end end record end |