Module: NetSuiteRails::SyncTrigger

Extended by:
SyncTrigger
Included in:
SyncTrigger
Defined in:
lib/netsuite_rails/sync_trigger.rb

Instance Method Summary collapse

Instance Method Details

#attach(klass) ⇒ Object

TODO think about a flag to push to NS on after_validation vs after_commit TODO think about background async record syncing (re: multiple sales order updates) TODO need to add hook for custom proc to determine if data should be pushed to netsuite

if a model has a pending/complete state we might want to only push on complete


10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
55
# File 'lib/netsuite_rails/sync_trigger.rb', line 10

def attach(klass)
  # don't attach to non-AR backed models
  # it is the user's responsibility to trigger `Model#netsuite_push` when ActiveRecord isn't used
  return if !defined?(::ActiveRecord) || !klass.ancestors.include?(::ActiveRecord::Base)

  if klass.include?(SubListSync)
    klass.after_save { SyncTrigger.sublist_trigger(self) }
    klass.after_destroy { SyncTrigger.sublist_trigger(self) }
  elsif klass.include?(RecordSync)

    # during the initial pull we don't want to push changes up
    klass.before_save do
      @netsuite_sync_record_import = self.new_record? && self.netsuite_id.present?

      if @netsuite_sync_record_import
        # pull the record down if it has't been pulled yet
        # this is useful when this is triggered by a save on a parent record which has this
        # record as a related record

        if !self.netsuite_pulled? && !self.netsuite_async_jobs?
          SyncTrigger.record_pull_trigger(self)
        end
      end

      # if false record will not save
      true
    end

    klass.after_save do
      # this conditional is implemented as a save hook
      # because the coordination class doesn't know about model persistence state

      if @netsuite_sync_record_import
        if !self.netsuite_pulled? && self.netsuite_async_jobs?
          SyncTrigger.record_pull_trigger(self)
        end
      else
        SyncTrigger.record_push_trigger(self)
      end

      @netsuite_sync_record_import = false
    end
  end

  # TODO think on NetSuiteRails::ListSync
end

#record_pull_trigger(local) ⇒ Object



57
58
59
60
61
62
63
64
65
# File 'lib/netsuite_rails/sync_trigger.rb', line 57

def record_pull_trigger(local)
  return if NetSuiteRails::Configuration.netsuite_pull_disabled

  sync_options = local.netsuite_sync_options

  return if sync_options.has_key?(:pull_if) && !local.instance_exec(&sync_options[:pull_if])

  record_trigger_action(local, :netsuite_pull)
end

#record_push_trigger(local) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/netsuite_rails/sync_trigger.rb', line 67

def record_push_trigger(local)
  # don't update when fields are updated because of a netsuite_pull
  if local.netsuite_pulling?
    Rails.logger.info "NetSuite: Push Stopped. Record is pulling. " +
                      "local_record=#{local.class}, local_record_id=#{local.id}"
    return
  end

  return if NetSuiteRails::Configuration.netsuite_push_disabled

  # don't update if a read only record
  return if local.netsuite_sync == :read

  sync_options = local.netsuite_sync_options

  # TODO should migrate to push_if for better self-documentation
  # :if option is a block that returns a boolean
  return if sync_options.has_key?(:if) && !local.instance_exec(&sync_options[:if])

  record_trigger_action(local, :netsuite_push)
end

#record_trigger_action(local, action) ⇒ Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/netsuite_rails/sync_trigger.rb', line 89

def record_trigger_action(local, action)
  sync_options = local.netsuite_sync_options

  action_options = {

  }

  if sync_options.has_key?(:credentials)
    action_options[:credentials] = local.instance_exec(&sync_options[:credentials])
  end

  # TODO need to pass off the credentials to the NS push command

  # You can force sync mode in different envoirnments with the global configuration variables

  if !local.netsuite_async_jobs?
    local.send(action, action_options)
  else
    action_options[:modified_fields] = NetSuiteRails::RecordSync::PushManager.modified_local_fields(local).keys

    # TODO support ActiveJob

    if local.respond_to?(:delay)
      local.delay.send(action, action_options)
    else
      raise 'no supported delayed job method found'
    end
  end
end

#sublist_trigger(sublist_item_rep) ⇒ Object



119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/netsuite_rails/sync_trigger.rb', line 119

def sublist_trigger(sublist_item_rep)
  # TODO don't trigger a push if the parent record is still pulling
  # often sublists are managed in a after_pull hook; we want to prevent auto-pushing
  # if sublist records are being updated. However, the netsuite_pulling? state is not persisted
  # so there is no gaurentee that it isn't being pulled by checking parent.netsuite_pulling?

  parent = sublist_item_rep.send(sublist_item_rep.class.netsuite_sublist_parent)

  if parent.class.include?(RecordSync)
    record_push_trigger(parent)
  end
end