Class: Importu::Backends::ActiveRecord::AssignmentContext

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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

#actionSymbol (readonly)

Returns the action (:create or :update).

Returns:

  • (Symbol)

    the action (:create or :update)



131
132
133
# File 'lib/importu/backends/active_record.rb', line 131

def action
  @action
end

#objectActiveRecord::Base (readonly)

Returns the model instance.

Returns:

  • (ActiveRecord::Base)

    the model instance



128
129
130
# File 'lib/importu/backends/active_record.rb', line 128

def object
  @object
end

#recordImportu::Record (readonly)

Returns the import record.

Returns:



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.

Yields:

  • block to execute with access to record, object, and action



142
143
144
# File 'lib/importu/backends/active_record.rb', line 142

def apply(&block)
  instance_eval(&block) if block
end

#assign_valuesObject

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.

Raises:



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