Module: AASM::Persistence::NContentRestPersistence::InstanceMethods

Defined in:
lib/aasm/persistence/ncontent_rest_persistence.rb

Instance Method Summary collapse

Instance Method Details

#aasm_write_state(new_state) ⇒ Object

Writes state to the state column and persists it to the server

foo = Foo.find(1) foo.aasm.current_state # => :opened foo.close! foo.aasm.current_state # => :closed Foo.find(1).aasm.current_state # => :closed

NOTE: intended to be called from an event



82
83
84
85
86
87
88
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/aasm/persistence/ncontent_rest_persistence.rb', line 82

def aasm_write_state(new_state)

  previous_state = self.public_send self.class.aasm.attribute_name
  self.public_send "#{self.class.aasm.attribute_name}=", new_state

  success = begin
    data = {
      name: aasm.current_event.to_s.tr('!',''),
      target_type: self.class.name.demodulize,
      target_id: self.id
    }

    unless NContent::SDK.config.state_event_delay < 1
      msg = "Will wait #{NContent::SDK.config.state_event_delay} seconds"
      NContent::SDK.logger.debug "#{msg} before posting the event..."
      sleep NContent::SDK.config.state_event_delay
    end

    response = NContent::SDK::RESTClient.post "/events.json" do |req|
      req.headers[:content_type] = 'application/json'
      req.body = ActiveSupport::JSON.encode(state_event: data)
    end

    reload

    # Return true if the actual state is the same as the expected state:
    self.public_send(self.class.aasm.attribute_name) == new_state.to_s
  end

  unless success
    self.public_send "#{self.class.aasm.attribute_name}=", previous_state
    return false
  end

  true
end

#aasm_write_state_without_persistence(new_state) ⇒ Object



119
120
121
122
# File 'lib/aasm/persistence/ncontent_rest_persistence.rb', line 119

def aasm_write_state_without_persistence(new_state)
  self.public_send(self.class.aasm.attribute_name) == new_state.to_s
  true
end