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
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/iron_trail/reifier.rb', line 29 def self.model_from_table_name(table_name, sti_type=nil) index = ActiveRecord::Base.descendants.reject(&:abstract_class).chunk(&:table_name).to_h do |key, val| v = \ if val.length == 1 val[0] else val.to_h { |k| [k.to_s, k] } end [key, v] end klass = index[table_name] raise "Cannot infer model from table named '#{table_name}'" unless klass return klass unless klass.is_a?(Hash) klass = klass[sti_type] return klass if klass raise "Cannot infer STI model for table #{table_name} and type '#{sti_type}'" 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 27 |
# 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) record[name.to_sym] = value elsif record.respond_to?("#{name}=") record.send("#{name}=", 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 |