Module: NetSuiteRails::RecordSync::InstanceMethods

Defined in:
lib/netsuite_rails/record_sync.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#netsuite_manual_fieldsObject



103
104
105
# File 'lib/netsuite_rails/record_sync.rb', line 103

def netsuite_manual_fields
  @netsuite_manual_fields ||= []
end

Instance Method Details

#netsuite_async_jobs?Boolean

Returns:

  • (Boolean)


139
140
141
# File 'lib/netsuite_rails/record_sync.rb', line 139

def netsuite_async_jobs?
  self.netsuite_sync_options[:sync_mode] == :async || (self.netsuite_sync_options[:sync_mode].blank? && NetSuiteRails::Configuration.netsuite_sync_mode == :async)
end

#netsuite_custom_record?Boolean

Returns:

  • (Boolean)


239
240
241
# File 'lib/netsuite_rails/record_sync.rb', line 239

def netsuite_custom_record?
  self.netsuite_record_class == NetSuite::Records::CustomRecord
end

#netsuite_execute_callbacks(list, record) ⇒ Object

TODO this should be protected; it needs to be pushed down to the Push/Pull manager level



245
246
247
248
249
250
251
252
253
# File 'lib/netsuite_rails/record_sync.rb', line 245

def netsuite_execute_callbacks(list, record)
  list.each do |callback|
    if callback.is_a?(Symbol)
      self.send(callback, record)
    else
      instance_exec(record, &callback)
    end
  end
end

#netsuite_extract_from_record(netsuite_record) ⇒ Object

TODO move this login into separate service object



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/netsuite_rails/record_sync.rb', line 171

def netsuite_extract_from_record(netsuite_record)
  Rails.logger.info "NetSuite: Pull #{netsuite_record.class} #{netsuite_record.internal_id}"

  @netsuite_pulling = true

  field_hints = self.netsuite_field_hints

  custom_field_list = self.netsuite_field_map[:custom_field_list] || {}

  all_field_list = self.netsuite_field_map.except(:custom_field_list) || {}
  all_field_list.merge!(custom_field_list)

  # TODO should have a helper module for common push/pull methods
  reflection_attributes = NetSuiteRails::RecordSync::PushManager.relationship_attributes_list(self)

  # handle non-collection associations
  association_keys = reflection_attributes.values.reject(&:collection?).map(&:name)

  all_field_list.each do |local_field, netsuite_field|
    is_custom_field = custom_field_list.keys.include?(local_field)

    if netsuite_field.is_a?(Proc)
      netsuite_field.call(self, netsuite_record, :pull)
      next
    end

    field_value = if is_custom_field
      netsuite_record.custom_field_list.send(netsuite_field).value rescue ""
    else
      netsuite_record.send(netsuite_field)
    end

    if field_value.nil?
      # TODO possibly nil out the local value?
      next
    end

    if association_keys.include?(local_field)
      field_value = reflection_attributes[local_field].
        klass.
        where(netsuite_id: field_value.internal_id).
        first_or_initialize
    elsif is_custom_field
      field_value = NetSuiteRails::RecordSync::PullManager.extract_custom_field_value(field_value)
    else
      # then it's not a custom field
    end

    # TODO should we just check for nil? vs present?

    if field_hints.has_key?(local_field) && !field_value.nil?
      field_value = NetSuiteRails::Transformations.transform(field_hints[local_field], field_value, :pull)
    end

    self.send(:"#{local_field}=", field_value)
  end

  netsuite_execute_callbacks(self.class.after_netsuite_pull, netsuite_record)

  @netsuite_pulling = false
  @netsuite_pulled = true
end

#netsuite_field_hintsObject



125
126
127
# File 'lib/netsuite_rails/record_sync.rb', line 125

def netsuite_field_hints
  self.class.netsuite_field_hints
end

#netsuite_field_mapObject



121
122
123
# File 'lib/netsuite_rails/record_sync.rb', line 121

def netsuite_field_map
  self.class.netsuite_field_map
end

#netsuite_pull(opts = {}) ⇒ Object

TODO need to support the opts hash



144
145
146
147
148
149
150
151
# File 'lib/netsuite_rails/record_sync.rb', line 144

def netsuite_pull(opts = {})
  netsuite_extract_from_record(netsuite_pull_record)

  if self.netsuite_async_jobs?
    # without callbacks?
    self.save
  end
end

#netsuite_pull_recordObject



153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/netsuite_rails/record_sync.rb', line 153

def netsuite_pull_record
  # TODO support use_external_id / netsuite_external_id

  if netsuite_custom_record?
    NetSuite::Records::CustomRecord.get(
      internal_id: self.netsuite_id,
      type_id: self.class.netsuite_custom_record_type_id
    )
  else
    self.netsuite_record_class.get(self.netsuite_id)
  end
end

#netsuite_pulled?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'lib/netsuite_rails/record_sync.rb', line 135

def netsuite_pulled?
  @netsuite_pulled ||= false
end

#netsuite_pulling?Boolean

assumes netsuite_id field on activerecord

Returns:

  • (Boolean)


131
132
133
# File 'lib/netsuite_rails/record_sync.rb', line 131

def netsuite_pulling?
  @netsuite_pulling ||= false
end

#netsuite_push(opts = {}) ⇒ Object



166
167
168
# File 'lib/netsuite_rails/record_sync.rb', line 166

def netsuite_push(opts = {})
  NetSuiteRails::RecordSync::PushManager.push(self, opts)
end

#netsuite_record_classObject



117
118
119
# File 'lib/netsuite_rails/record_sync.rb', line 117

def netsuite_record_class
  self.class.netsuite_record_class
end

#netsuite_syncObject



113
114
115
# File 'lib/netsuite_rails/record_sync.rb', line 113

def netsuite_sync
  self.class.netsuite_sync
end

#netsuite_sync_optionsObject

these methods are here for easy model override



109
110
111
# File 'lib/netsuite_rails/record_sync.rb', line 109

def netsuite_sync_options
  self.class.netsuite_sync_options
end

#new_netsuite_record?Boolean

Returns:

  • (Boolean)


235
236
237
# File 'lib/netsuite_rails/record_sync.rb', line 235

def new_netsuite_record?
  self.netsuite_id.blank?
end