naranya_ecm-sdk 0.0.59 → 0.0.61

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: a6e047f2e11b3f979a614497fab55f5509a91f82
4
- data.tar.gz: 1c0e0b24fe627d4e6950c0ad08ccdf1aa51e270a
3
+ metadata.gz: 1181f4986de39fadb4461af21904a7e6a7909e0f
4
+ data.tar.gz: a0b0b0ecafeb47e0d5107c477f3c937ae7274998
5
5
  SHA512:
6
- metadata.gz: 4d1beca3428f756addca77e0d3e8e1911e8f35526a3447937826e658f54c093432c6c656a74f31d365065494362b2994db1dcf4bb3593249d10eb8e654df5589
7
- data.tar.gz: 67af8129323fd6c47becb28842f49c25f1a3fb6765fb688cc7709d48219e08271b8f3b3ded397949cd669c221b2b330cd1a3cc59a4aac407292e9e8c387be926
6
+ metadata.gz: 2479a6d36d005d8e95861c794da10b8ac50c1be35b14e48362bad62a833daf5736c33cf140bb9f86d24b1ba9e8ff2e6c4549ff5601478210b05bc68b3253a51a
7
+ data.tar.gz: de993f6677215f8fdc0b75631723892fb33c1db89f24adaa8b730bafc3a3a85c1c660fe98304eb17d56f400cb4bd659e56f77faec81f04f6ac6f7e4676fe0a74
@@ -1,6 +1,6 @@
1
1
  module AASM
2
2
  module Persistence
3
- module RestPersistence
3
+ module NContentRestPersistence
4
4
  # This method:
5
5
  #
6
6
  # * extends the model with ClassMethods
@@ -28,8 +28,8 @@ module AASM
28
28
  #
29
29
  def self.included(base)
30
30
  base.send(:include, AASM::Persistence::Base)
31
- base.extend AASM::Persistence::RestPersistence::ClassMethods
32
- base.send(:include, AASM::Persistence::RestPersistence::InstanceMethods)
31
+ base.extend AASM::Persistence::NContentRestPersistence::ClassMethods
32
+ base.send(:include, AASM::Persistence::NContentRestPersistence::InstanceMethods)
33
33
 
34
34
  base.before_validation(:aasm_ensure_initial_state, :on => :create)
35
35
 
@@ -70,55 +70,23 @@ module AASM
70
70
 
71
71
  module InstanceMethods
72
72
 
73
- # # Writes <tt>state</tt> to the state column and persists it to the database
74
- # #
75
- # # foo = Foo.find(1)
76
- # # foo.aasm.current_state # => :opened
77
- # # foo.close!
78
- # # foo.aasm.current_state # => :closed
79
- # # Foo.find(1).aasm.current_state # => :closed
80
- # #
81
- # # NOTE: intended to be called from an event
82
- # def aasm_write_state(state)
83
- # old_value = self.send self.class.aasm.attribute_name.to_sym
84
- # write_attribute(self.class.aasm.attribute_name, state.to_s)
85
-
86
- # unless self.save(:validate => false)
87
- # write_attribute(self.class.aasm.attribute_name, old_value)
88
- # return false
89
- # end
90
-
91
- # true
92
- # end
93
-
94
- # # Writes <tt>state</tt> to the state column, but does not persist it to the database
95
- # #
96
- # # foo = Foo.find(1)
97
- # # foo.aasm.current_state # => :opened
98
- # # foo.close
99
- # # foo.aasm.current_state # => :closed
100
- # # Foo.find(1).aasm.current_state # => :opened
101
- # # foo.save
102
- # # foo.aasm.current_state # => :closed
103
- # # Foo.find(1).aasm.current_state # => :closed
104
- # #
105
- # # NOTE: intended to be called from an event
106
- # def aasm_write_state_without_persistence(state)
107
- # self.off_band_changes["#{self.class.aasm.attribute_name}_event"] = transition.event
108
- # end
109
-
110
- private
111
-
112
- def aasm_ensure_initial_state
113
- send("#{self.class.aasm.attribute_name}=", aasm.enter_initial_state.to_s) \
114
- if send(self.class.aasm.attribute_name).blank?
115
- end
116
-
117
- def aasm_fire_event(name, options, *args, &block)
118
- local_success = super
119
- remote_success = if local_success
73
+ # Writes <tt>state</tt> to the state column and persists it to the server
74
+ #
75
+ # foo = Foo.find(1)
76
+ # foo.aasm.current_state # => :opened
77
+ # foo.close!
78
+ # foo.aasm.current_state # => :closed
79
+ # Foo.find(1).aasm.current_state # => :closed
80
+ #
81
+ # NOTE: intended to be called from an event
82
+ def aasm_write_state(new_state)
83
+
84
+ previous_state = self.public_send self.class.aasm.attribute_name
85
+ self.public_send "#{self.class.aasm.attribute_name}=", new_state
86
+
87
+ success = begin
120
88
  data = {
121
- name: name,
89
+ name: aasm.current_event.to_s.tr('!',''),
122
90
  target_type: self.class.name.demodulize,
123
91
  target_id: self.id
124
92
  }
@@ -134,25 +102,49 @@ module AASM
134
102
  req.body = ActiveSupport::JSON.encode(state_event: data)
135
103
  end
136
104
 
137
- state_attribute = self.class.aasm.attribute_name
138
- state_before_reload = self.send(state_attribute)
139
-
140
105
  reload
141
- state_after_reload = self.send(state_attribute)
142
106
 
143
- msg = "Event: #{name} - #{state_attribute} #{state_before_reload}"
144
- NContent::SDK.logger.debug "#{msg} -> #{state_after_reload}"
107
+ # Return true if the actual state is the same as the expected state:
108
+ self.public_send(self.class.aasm.attribute_name) == new_state.to_s
109
+ end
110
+
111
+ unless success
112
+ self.public_send "#{self.class.aasm.attribute_name}=", previous_state
113
+ return false
114
+ end
115
+
116
+ true
117
+ end
118
+
119
+ def aasm_write_state_without_persistence(new_state)
120
+ self.public_send(self.class.aasm.attribute_name) == new_state.to_s
121
+ true
122
+ end
123
+
124
+ private
145
125
 
146
- # Check that the state changed (and return the result of that check)
147
- state_before_reload != state_after_reload
148
- else
149
- false
126
+ def aasm_fire_event(name, options, *args, &block)
127
+ success = super
128
+
129
+ if success
130
+ if options[:persist]
131
+ event = self.class.aasm.state_machine.events[name]
132
+ event.fire_callbacks(:after_commit, self, *args)
133
+ else
134
+ # TODO: Implement a "state change queue" to make .save send the state changes
135
+ # to the server when no bang (options[:persist] == false) was given...
136
+ end
150
137
  end
151
138
 
152
- local_success && remote_success
139
+ success
140
+ end
141
+
142
+ def aasm_ensure_initial_state
143
+ send("#{self.class.aasm.attribute_name}=", aasm.enter_initial_state.to_s) \
144
+ if send(self.class.aasm.attribute_name).blank?
153
145
  end
154
146
 
155
- end # InstanceMethods
147
+ end
156
148
 
157
149
  end
158
150
  end
@@ -1,3 +1,3 @@
1
1
  module NaranyaEcm
2
- VERSION = "0.0.59"
2
+ VERSION = "0.0.61"
3
3
  end
@@ -1,3 +1,7 @@
1
+ require 'aasm'
2
+ require 'aasm/persistence/base'
3
+ require 'aasm/persistence/ncontent_rest_persistence'
4
+
1
5
  module NaranyaEcm
2
6
 
3
7
  ##
@@ -164,7 +168,7 @@ module NaranyaEcm
164
168
  ###############################################################
165
169
  # StateMachine:
166
170
  include AASM
167
- include AASM::Persistence::RestPersistence
171
+ include AASM::Persistence::NContentRestPersistence
168
172
 
169
173
  ##
170
174
  # :method: request_an_acceptance_review
@@ -248,29 +252,29 @@ module NaranyaEcm
248
252
  #########################################
249
253
  # Eventos:
250
254
 
251
- event :request_an_acceptance_review do
255
+ event :request_an_acceptance_review, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
252
256
  # Iniciar la transición de :draft a :awaiting_acceptance,
253
257
  transitions from: :draft, to: :awaiting_acceptance, guard: :validatable_lifecycle?
254
258
  end
255
259
 
256
- event :accept do
260
+ event :accept, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
257
261
  # Iniciar la transición de :awaiting_acceptance a :accepted:
258
262
  transitions from: :awaiting_acceptance, to: :accepted
259
263
  end
260
264
 
261
- event :reject do
265
+ event :reject, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
262
266
  # Iniciar la transición de :awaiting_acceptance a :rejected:
263
267
  transitions from: :awaiting_acceptance, to: :rejected
264
268
  end
265
269
 
266
- event :publish do
270
+ event :publish, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
267
271
  # Iniciar la transición de :draft a :published,
268
272
  # si el lifecycle es sencillo:
269
273
  transitions from: :draft, to: :published, guard: :simple_lifecycle?
270
274
  transitions from: :validated, to: :published, guard: :validatable_lifecycle?
271
275
  end
272
276
 
273
- event :deactivate do
277
+ event :deactivate, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
274
278
  # Iniciar la transición de :published a :deactivated:
275
279
  transitions from: :published, to: :deactivated
276
280
  end
@@ -1,6 +1,6 @@
1
1
  require 'aasm'
2
2
  require 'aasm/persistence/base'
3
- require 'aasm/persistence/rest_persistence'
3
+ require 'aasm/persistence/ncontent_rest_persistence'
4
4
 
5
5
  module NaranyaEcm
6
6
  class ContentVersion
@@ -57,7 +57,7 @@ module NaranyaEcm
57
57
  ###############################################################
58
58
  # StateMachine:
59
59
  include AASM
60
- include AASM::Persistence::RestPersistence
60
+ include AASM::Persistence::NContentRestPersistence
61
61
 
62
62
  aasm column: :lifecycle_state do
63
63
 
@@ -72,22 +72,22 @@ module NaranyaEcm
72
72
  #########################################
73
73
  # Eventos:
74
74
 
75
- event :request_an_acceptance_review do
75
+ event :request_an_acceptance_review, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
76
76
  # Iniciar la transición de :draft a :awaiting_acceptance,
77
77
  transitions from: :draft, to: :awaiting_acceptance, guard: :validatable_lifecycle?
78
78
  end
79
79
 
80
- event :accept do
80
+ event :accept, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
81
81
  # Iniciar la transición de :awaiting_acceptance a :accepted:
82
82
  transitions from: :awaiting_acceptance, to: :accepted
83
83
  end
84
84
 
85
- event :reject do
85
+ event :reject, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
86
86
  # Iniciar la transición de :awaiting_acceptance a :rejected:
87
87
  transitions from: :awaiting_acceptance, to: :rejected
88
88
  end
89
89
 
90
- event :publish do
90
+ event :publish, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
91
91
  # Iniciar la transición de :draft a :published,
92
92
  # si el lifecycle es sencillo:
93
93
  transitions from: :draft, to: :published, guard: :simple_lifecycle?
@@ -95,12 +95,12 @@ module NaranyaEcm
95
95
  transitions from: :deactivated, to: :published
96
96
  end
97
97
 
98
- event :supersede do
98
+ event :supersede, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
99
99
  # Iniciar la transición de :published a :superseded:
100
100
  transitions from: :published, to: :superseded
101
101
  end
102
102
 
103
- event :deactivate do
103
+ event :deactivate, before: :start_notify_state_change!, after_commit: :finish_notify_state_change! do
104
104
  # Iniciar la transición de :published a :deactivated:
105
105
  transitions from: :published, to: :deactivated
106
106
  end
@@ -33,6 +33,18 @@ module NaranyaEcm
33
33
  has_many :media_resources, class_name: :MediaResource, inverse_of: :media_resource
34
34
 
35
35
  #belongs_to :parent_document
36
+ field :parent_document_id, type: String
37
+ field :parent_document_type, type: String
38
+ def as_notification_payload
39
+ instance_payload = super.merge(roles: self.roles)
40
+
41
+ if self.parent_document_id.present? && self.parent_document_type.present?
42
+ instance_payload[:parent_id] = self.parent_document_id
43
+ instance_payload[:parent_class] = "NaranyaEcm::#{self.parent_document_type}"
44
+ end
45
+
46
+ instance_payload
47
+ end
36
48
 
37
49
  # Returns true if the Media Resource is private
38
50
  def private?
@@ -1,5 +1,7 @@
1
1
  require 'active_model'
2
2
 
3
+ require 'naranya_ecm/rest/notifications'
4
+
3
5
  module NaranyaEcm::Rest
4
6
  module Model
5
7
 
@@ -62,6 +64,8 @@ module NaranyaEcm::Rest
62
64
  # Ignore the server resource's url attribute:
63
65
  attr_accessor :url
64
66
 
67
+ include NaranyaEcm::Rest::Notifications
68
+
65
69
  include NaranyaEcm::Rest::Associations
66
70
  include NaranyaEcm::Rest::Persistence
67
71
 
@@ -0,0 +1,38 @@
1
+ require 'active_support/notifications'
2
+
3
+ module NaranyaEcm
4
+ module Rest
5
+ module Notifications
6
+ extend ActiveSupport::Concern
7
+
8
+ module ClassMethods
9
+ def as_notification_payload
10
+ { class: self.name }
11
+ end
12
+ end
13
+
14
+ def as_notification_payload
15
+ instance_payload = self.class.as_notification_payload.merge({ id: self.id })
16
+
17
+ # Include belongs_to references:
18
+ self.class.reflect_on_all_associations.values.select do |association|
19
+ association.class.name.demodulize == 'BelongsTo'
20
+ end.map(&:foreign_key).each do |reference_name|
21
+ instance_payload[reference_name] = self.public_send reference_name
22
+ end
23
+
24
+ instance_payload
25
+ end
26
+
27
+ def start_notify_state_change!
28
+ ActiveSupport::Notifications.instrumenter
29
+ .start("ncontent_sdk.#{aasm.current_event.to_s.tr('!', '').underscore}", self.as_notification_payload)
30
+ end
31
+
32
+ def finish_notify_state_change!
33
+ ActiveSupport::Notifications.instrumenter
34
+ .finish("ncontent_sdk.#{aasm.current_event.to_s.tr('!', '').underscore}", self.as_notification_payload)
35
+ end
36
+ end
37
+ end
38
+ end
@@ -56,7 +56,10 @@ module NaranyaEcm::Rest
56
56
  end
57
57
 
58
58
  def delete(id, options={})
59
- response = NContent::SDK::RESTClient.delete("#{self.path}/#{id}.json")
59
+ ActiveSupport::Notifications.instrument('ncontent_sdk.destroy') do |notification_payload|
60
+ response = NContent::SDK::RESTClient.delete("#{self.path}/#{id}.json")
61
+ notification_payload.merge!({ id: id }.merge(self.as_notification_payload))
62
+ end
60
63
  true
61
64
  end
62
65
 
@@ -272,25 +275,29 @@ module NaranyaEcm::Rest
272
275
  create_path = "#{self.class.path}.json"
273
276
 
274
277
  begin
278
+ ActiveSupport::Notifications.instrument('ncontent_sdk.create') do |notification_payload|
279
+ response = NContent::SDK::RESTClient.post create_path do |req|
280
+ req.headers[:content_type] = 'application/json'
281
+
282
+ # Obtener solo los cambios con respecto al objeto recién
283
+ # inicializado:
284
+ attributes_to_post = changes.inject({}) do |attr_hash, attribute_change|
285
+ attribute_name, attribute_change_values = attribute_change
286
+ previous_attribute_value, new_attribute_value = attribute_change_values
287
+ attr_hash[attribute_name] = new_attribute_value
288
+ attr_hash
289
+ end
290
+
291
+ req.body = ActiveSupport::JSON.encode(
292
+ self.class.name.demodulize.underscore => attributes_to_post
293
+ )
294
+ end
275
295
 
276
- response = NContent::SDK::RESTClient.post create_path do |req|
277
- req.headers[:content_type] = 'application/json'
296
+ send :_load_from_fetched_attributes!, response.body
278
297
 
279
- # Obtener solo los cambios con respecto al objeto recién
280
- # inicializado:
281
- attributes_to_post = changes.inject({}) do |attr_hash, attribute_change|
282
- attribute_name, attribute_change_values = attribute_change
283
- previous_attribute_value, new_attribute_value = attribute_change_values
284
- attr_hash[attribute_name] = new_attribute_value
285
- attr_hash
286
- end
298
+ notification_payload.merge! self.as_notification_payload
287
299
 
288
- req.body = ActiveSupport::JSON.encode(
289
- self.class.name.demodulize.underscore => attributes_to_post
290
- )
291
300
  end
292
-
293
- send :_load_from_fetched_attributes!, response.body
294
301
  true
295
302
  rescue UnprocessableEntity => e
296
303
  self.load_server_errors(e.response.body)
@@ -320,15 +327,18 @@ module NaranyaEcm::Rest
320
327
  update_path = "#{self.path}.json"
321
328
 
322
329
  begin
323
- response = NContent::SDK::RESTClient.put update_path do |req|
324
- req.headers[:content_type] = 'application/json'
325
- req.body = ActiveSupport::JSON.encode(
326
- self.class.name.demodulize.underscore => attributes_to_save
327
- )
328
- end
330
+ ActiveSupport::Notifications.instrument('ncontent_sdk.update') do |notification_payload|
331
+ response = NContent::SDK::RESTClient.put update_path do |req|
332
+ req.headers[:content_type] = 'application/json'
333
+ req.body = ActiveSupport::JSON.encode(
334
+ self.class.name.demodulize.underscore => attributes_to_save
335
+ )
336
+ end
329
337
 
330
- self.reload!
331
- off_band_changes.clear if off_band_changes.present?
338
+ self.reload!
339
+ off_band_changes.clear if off_band_changes.present?
340
+ notification_payload.merge!(self.as_notification_payload)
341
+ end
332
342
  true
333
343
  rescue UnprocessableEntity
334
344
  self.load_server_errors(response.body)
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: naranya_ecm-sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.59
4
+ version: 0.0.61
5
5
  platform: ruby
6
6
  authors:
7
7
  - Roberto Quintanilla
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2015-06-26 00:00:00.000000000 Z
11
+ date: 2015-06-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport
@@ -209,7 +209,7 @@ files:
209
209
  - dev/search.rb
210
210
  - dev/thread-safety-tests.rb
211
211
  - doc/media_processing.md
212
- - lib/aasm/persistence/rest_persistence.rb
212
+ - lib/aasm/persistence/ncontent_rest_persistence.rb
213
213
  - lib/naranya_ecm-sdk.rb
214
214
  - lib/naranya_ecm-sdk/version.rb
215
215
  - lib/naranya_ecm/behaviors/localizable.rb
@@ -228,6 +228,7 @@ files:
228
228
  - lib/naranya_ecm/rest/errors.rb
229
229
  - lib/naranya_ecm/rest/finder_methods.rb
230
230
  - lib/naranya_ecm/rest/model.rb
231
+ - lib/naranya_ecm/rest/notifications.rb
231
232
  - lib/naranya_ecm/rest/persistence.rb
232
233
  - lib/naranya_ecm/rest/relation.rb
233
234
  - lib/naranya_ecm/search/hit.rb