Class: Importu::Backends::ActiveRecord::AssignmentContext
- Inherits:
-
Object
- Object
- Importu::Backends::ActiveRecord::AssignmentContext
- Defined in:
- lib/importu/backends/active_record.rb
Overview
Context for assigning field values to a model instance.
This is the execution context for before_save hooks, providing access to the record data, model object, and current action.
Instance Attribute Summary collapse
-
#action ⇒ Symbol
readonly
The action (:create or :update).
-
#object ⇒ ActiveRecord::Base
readonly
The model instance.
-
#record ⇒ Importu::Record
readonly
The import record.
Instance Method Summary collapse
-
#apply { ... } ⇒ Object
private
Executes a block in this context.
-
#assign_values ⇒ Object
private
Assigns field values from the record to the object.
-
#initialize(record, object, action) ⇒ AssignmentContext
constructor
private
A new instance of AssignmentContext.
Constructor Details
#initialize(record, object, action) ⇒ AssignmentContext
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Returns a new instance of AssignmentContext.
134 135 136 |
# File 'lib/importu/backends/active_record.rb', line 134 def initialize(record, object, action) @record, @object, @action = record, object, action end |
Instance Attribute Details
#action ⇒ Symbol (readonly)
Returns the action (:create or :update).
131 132 133 |
# File 'lib/importu/backends/active_record.rb', line 131 def action @action end |
#object ⇒ ActiveRecord::Base (readonly)
Returns the model instance.
128 129 130 |
# File 'lib/importu/backends/active_record.rb', line 128 def object @object end |
#record ⇒ Importu::Record (readonly)
Returns the import record.
125 126 127 |
# File 'lib/importu/backends/active_record.rb', line 125 def record @record end |
Instance Method Details
#apply { ... } ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Executes a block in this context.
142 143 144 |
# File 'lib/importu/backends/active_record.rb', line 142 def apply(&block) instance_eval(&block) if block end |
#assign_values ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Assigns field values from the record to the object.
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/importu/backends/active_record.rb', line 150 def assign_values field_names = record.assignable_fields_for(action) begin field_names.each do |name| object.send("#{name}=", record[name]) end rescue NoMethodError raise_unassignable_fields!(field_names) end end |