kojac 0.9.0 → 0.9.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ODkyNDY5NGExYzM1NmM2MGU0NTU0ZGM1YTk0Mjk0MzU1OTQwMmQzZQ==
4
+ ZmY3Y2Y3OTRjODA1NDQ4MGFkNzQ2YjBlNzEyZWVhZjg1NzI3NWYxNA==
5
5
  data.tar.gz: !binary |-
6
- NTRlMTFjNTliMDcxMGU5MTQ0OGYzMWY0ZjM5NmY5M2NhNTJmZDM5Yw==
6
+ MDQyNTNmYmM4YzA4ZTBiYmYzYmI2NWExYmRkNDBjODk5ZjJjNjhlMg==
7
7
  !binary "U0hBNTEy":
8
8
  metadata.gz: !binary |-
9
- NzY3MGYyODk2YzJmMzgzYjY0ZTkzNGJmYjgyYTliMTg4NzQ1ZjdlMzFmMmU4
10
- ZTljYjQxZTQzNGQxZjZhMTZlYmYyMjA4NTM5MzQ5MjdjODJmNDIxMDVkNDU3
11
- Njk5NDJhNGZlMGViY2NkNmU4ZWZlOWVlM2IxZjRlZThkOGY2YWU=
9
+ ZjllODJmMWIzMGJjNjlkODNhZTM1N2Q3MTRjOTUyZTI3Y2Q4YzQ1OTgxYTAw
10
+ MWM5YTAxMDQ1MzAyYTkwMGM4NGQ2YTQ1MmIxM2Y3MzMyZTllMTc4NjA3NThh
11
+ Y2JlMWM3ODUyYWZhNGEyYWVmNWRjNmI3OTk1MmRjMTEzYTE4Mjc=
12
12
  data.tar.gz: !binary |-
13
- NDkyOWZmOWVjYmUwYmExMDIxNGJlOTM4M2JmY2U0Y2Q4MWRjOTk1YzA5NTNj
14
- NTQzZmI0MmQ4NGE2OWYwMmQ1ZjY2ZDg1ZWQ3YzFlNTQwNzQxZDdhODMwNWM4
15
- NjQyNGVhZDc3ZTdlMWIxNjk5OTY3OWJkNDE2MTVjYWRiNjBkY2I=
13
+ NWQ5OTQ4ZGM4ZDc3M2EyOTBmMjRmZmRhODI3ZDhiYWVlNTE5Y2MwNzhiMDcw
14
+ MmZhNDk3YTZjNDk1MWE0NGFlMDQyOWM4NzUwODk4NzMyMDY1MGNhZGVkYmQy
15
+ NzEwMDdiN2I1ZmY4MWFmNjg1Mzc1ZTM4OTIyY2IzYWUzZTAxYzU=
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- kojac (0.9.0)
4
+ kojac (0.9.0.1)
5
5
  jquery-rails
6
6
  json2-rails
7
7
  strong_parameters
@@ -63,370 +63,372 @@ module KojacUtils
63
63
 
64
64
  end
65
65
 
66
- module Kojac::ModelMethods
67
-
68
- def self.included(aClass)
69
- aClass.send :extend, ClassMethods
70
- end
71
-
72
- module ClassMethods
73
- def by_key(aKey,aContext=nil)
74
- r,id,a = aKey.split_kojac_key
75
- model = self
76
- model = self.rescope(model,aContext) if self.respond_to? :rescope
77
- if id
78
- model.where(id: id).first
79
- else
80
- model.all
66
+ module Kojac
67
+ module ModelMethods
68
+
69
+ def self.included(aClass)
70
+ aClass.send :extend, ClassMethods
71
+ end
72
+
73
+ module ClassMethods
74
+ def by_key(aKey,aContext=nil)
75
+ r,id,a = aKey.split_kojac_key
76
+ model = self
77
+ model = self.rescope(model,aContext) if self.respond_to? :rescope
78
+ if id
79
+ model.where(id: id).first
80
+ else
81
+ model.all
82
+ end
81
83
  end
82
84
  end
83
- end
84
85
 
85
- def kojac_key
86
- self.class.to_s.snake_case.pluralize+'__'+self.id.to_s
87
- end
86
+ def kojac_key
87
+ self.class.to_s.snake_case.pluralize+'__'+self.id.to_s
88
+ end
88
89
 
89
- def update_permitted_attributes!(aChanges, aRing)
90
- aChanges = KojacUtils.upgrade_hashes_to_params(aChanges)
91
- permitted_fields = self.class.permitted_fields(:write, aRing)
92
- permitted_fields = aChanges.permit(*permitted_fields)
93
- assign_attributes(permitted_fields, :without_protection => true)
94
- save!
95
- end
90
+ def update_permitted_attributes!(aChanges, aRing)
91
+ aChanges = KojacUtils.upgrade_hashes_to_params(aChanges)
92
+ permitted_fields = self.class.permitted_fields(:write, aRing)
93
+ permitted_fields = aChanges.permit(*permitted_fields)
94
+ assign_attributes(permitted_fields, :without_protection => true)
95
+ save!
96
+ end
96
97
 
98
+ end
97
99
  end
98
100
 
99
- module Kojac::ControllerOpMethods
100
-
101
- def self.included(aClass)
102
- #aClass.send :extend, ClassMethods
103
- aClass.send :include, ActiveSupport::Callbacks
104
- aClass.send :define_callbacks, :update_op, :scope => [:kind, :name]
105
- end
101
+ module Kojac
102
+ module ControllerOpMethods
106
103
 
107
- #module ClassMethods
108
- #end
104
+ def self.included(aClass)
105
+ #aClass.send :extend, ClassMethods
106
+ aClass.send :include, ActiveSupport::Callbacks
107
+ aClass.send :define_callbacks, :update_op, :scope => [:kind, :name]
108
+ end
109
109
 
110
- module_function
110
+ #module ClassMethods
111
+ #end
111
112
 
112
- public
113
+ module_function
113
114
 
114
- attr_accessor :item
115
+ public
115
116
 
116
- def results
117
- @results ||= {}
118
- end
117
+ attr_accessor :item
119
118
 
120
- def deduce_model_class
121
- KojacUtils.model_class_for_key(self.kojac_resource)
122
- end
119
+ def results
120
+ @results ||= {}
121
+ end
123
122
 
124
- def kojac_resource
125
- self.class.to_s.chomp('Controller').snake_case
126
- end
123
+ def deduce_model_class
124
+ KojacUtils.model_class_for_key(self.kojac_resource)
125
+ end
127
126
 
128
- def create_on_association(aItem,aAssoc,aValues,aRing)
129
- raise "User does not have permission for create on #{aAssoc}" unless aItem.class.permitted_associations(:create,aRing).include?(aAssoc.to_sym)
130
-
131
- return nil unless ma = aItem.class.reflect_on_association(aAssoc.to_sym)
132
- a_model_class = ma.klass
133
-
134
- aValues = KojacUtils.upgrade_hashes_to_params(aValues || {})
135
-
136
- case ma.macro
137
- when :belongs_to
138
- return nil if !aValues.is_a?(Hash)
139
- fields = aValues.permit( *a_model_class.permitted_fields(:write,aRing) )
140
- return aItem.send("build_#{aAssoc}".to_sym,fields)
141
- when :has_many
142
- aValues = [aValues] if aValues.is_a?(Hash)
143
- return nil unless aValues.is_a? Array
144
- aValues.each do |v|
145
- fields = v.permit( *a_model_class.permitted_fields(:write,aRing) )
146
- new_sub_item = nil
147
- case ma.macro
148
- when :has_many
149
- new_sub_item = aItem.send(aAssoc.to_sym).create(fields)
150
- else
151
- raise "#{ma.macro} association unsupported in CREATE"
152
- end
153
- merge_model_into_results(new_sub_item)
154
- end
127
+ def kojac_resource
128
+ self.class.to_s.chomp('Controller').snake_case
155
129
  end
156
- #
157
- #
158
- #
159
- #a_value = op[:value][a] # get data for this association, assume {}
160
- #if a_value.is_a?(Hash)
161
- # a_model_class = ma.klass
162
- # fields = a_value.permit( *permitted_fields(:write,a_model_class) )
163
- # item.send("build_#{a}".to_sym,fields)
164
- # included_assocs << a.to_sym
165
- #elsif a_value.is_a?(Array)
166
- # raise "association collections not yet implemented for create"
167
- #else
168
- # next
169
- #end
170
- end
171
130
 
172
- def create_op
173
- ring = current_user.try(:ring)
174
- op = params[:op]
175
- options = op[:options] || {}
176
- model_class = deduce_model_class
177
- resource,id,assoc = op['key'].split_kojac_key
178
- if assoc # create operation on an association eg. {verb: "CREATE", key: "order.items"}
179
- raise "User does not have permission for #{op[:verb]} operation on #{model_class.to_s}.#{assoc}" unless model_class.permitted_associations(:create,ring).include?(assoc.to_sym)
180
- item = KojacUtils.model_for_key(key_join(resource,id))
181
- ma = model_class.reflect_on_association(assoc.to_sym)
182
- a_value = op[:value] # get data for this association, assume {}
183
- raise "create multiple not yet implemented for associations" unless a_value.is_a?(Hash)
131
+ def create_on_association(aItem,aAssoc,aValues,aRing)
132
+ raise "User does not have permission for create on #{aAssoc}" unless aItem.class.permitted_associations(:create,aRing).include?(aAssoc.to_sym)
184
133
 
134
+ return nil unless ma = aItem.class.reflect_on_association(aAssoc.to_sym)
185
135
  a_model_class = ma.klass
186
- p_fields = a_model_class.permitted_fields(:write,ring)
187
- fields = a_value.permit( *p_fields )
188
- new_sub_item = nil
136
+
137
+ aValues = KojacUtils.upgrade_hashes_to_params(aValues || {})
138
+
189
139
  case ma.macro
140
+ when :belongs_to
141
+ return nil if !aValues.is_a?(Hash)
142
+ fields = aValues.permit( *a_model_class.permitted_fields(:write,aRing) )
143
+ return aItem.send("build_#{aAssoc}".to_sym,fields)
190
144
  when :has_many
191
- new_sub_item = item.send(assoc.to_sym).create(fields)
192
- else
193
- raise "#{ma.macro} association unsupported in CREATE"
145
+ aValues = [aValues] if aValues.is_a?(Hash)
146
+ return nil unless aValues.is_a? Array
147
+ aValues.each do |v|
148
+ fields = v.permit( *a_model_class.permitted_fields(:write,aRing) )
149
+ new_sub_item = nil
150
+ case ma.macro
151
+ when :has_many
152
+ new_sub_item = aItem.send(aAssoc.to_sym).create(fields)
153
+ else
154
+ raise "#{ma.macro} association unsupported in CREATE"
155
+ end
156
+ merge_model_into_results(new_sub_item)
157
+ end
194
158
  end
195
- result_key = op[:result_key] || new_sub_item.kojac_key
196
- merge_model_into_results(new_sub_item)
197
- else # create operation on a resource eg. {verb: "CREATE", key: "order_items"} but may have embedded association values
198
- p_fields = model_class.permitted_fields(:write,ring)
199
- raise "User does not have permission for #{op[:verb]} operation on #{model_class.to_s}" unless model_class.ring_can?(:create,ring)
200
-
201
- p_fields = op[:value].permit( *p_fields )
202
- item = model_class.create!(p_fields)
203
-
204
- options_include = options['include'] || []
205
- included_assocs = []
206
- p_assocs = model_class.permitted_associations(:write,ring)
207
- if p_assocs
208
- p_assocs.each do |a|
209
- next unless (a_value = op[:value][a]) || options_include.include?(a.to_s)
210
- create_on_association(item,a,a_value,ring)
211
- included_assocs << a.to_sym
159
+ #
160
+ #
161
+ #
162
+ #a_value = op[:value][a] # get data for this association, assume {}
163
+ #if a_value.is_a?(Hash)
164
+ # a_model_class = ma.klass
165
+ # fields = a_value.permit( *permitted_fields(:write,a_model_class) )
166
+ # item.send("build_#{a}".to_sym,fields)
167
+ # included_assocs << a.to_sym
168
+ #elsif a_value.is_a?(Array)
169
+ # raise "association collections not yet implemented for create"
170
+ #else
171
+ # next
172
+ #end
173
+ end
174
+
175
+ def create_op
176
+ ring = current_user.try(:ring)
177
+ op = params[:op]
178
+ options = op[:options] || {}
179
+ model_class = deduce_model_class
180
+ resource,id,assoc = op['key'].split_kojac_key
181
+ if assoc # create operation on an association eg. {verb: "CREATE", key: "order.items"}
182
+ raise "User does not have permission for #{op[:verb]} operation on #{model_class.to_s}.#{assoc}" unless model_class.permitted_associations(:create,ring).include?(assoc.to_sym)
183
+ item = KojacUtils.model_for_key(key_join(resource,id))
184
+ ma = model_class.reflect_on_association(assoc.to_sym)
185
+ a_value = op[:value] # get data for this association, assume {}
186
+ raise "create multiple not yet implemented for associations" unless a_value.is_a?(Hash)
187
+
188
+ a_model_class = ma.klass
189
+ p_fields = a_model_class.permitted_fields(:write,ring)
190
+ fields = a_value.permit( *p_fields )
191
+ new_sub_item = nil
192
+ case ma.macro
193
+ when :has_many
194
+ new_sub_item = item.send(assoc.to_sym).create(fields)
195
+ else
196
+ raise "#{ma.macro} association unsupported in CREATE"
212
197
  end
198
+ result_key = op[:result_key] || new_sub_item.kojac_key
199
+ merge_model_into_results(new_sub_item)
200
+ else # create operation on a resource eg. {verb: "CREATE", key: "order_items"} but may have embedded association values
201
+ p_fields = model_class.permitted_fields(:write,ring)
202
+ raise "User does not have permission for #{op[:verb]} operation on #{model_class.to_s}" unless model_class.ring_can?(:create,ring)
203
+
204
+ p_fields = op[:value].permit( *p_fields )
205
+ item = model_class.create!(p_fields)
206
+
207
+ options_include = options['include'] || []
208
+ included_assocs = []
209
+ p_assocs = model_class.permitted_associations(:write,ring)
210
+ if p_assocs
211
+ p_assocs.each do |a|
212
+ next unless (a_value = op[:value][a]) || options_include.include?(a.to_s)
213
+ create_on_association(item,a,a_value,ring)
214
+ included_assocs << a.to_sym
215
+ end
216
+ end
217
+ item.save!
218
+ result_key = op[:result_key] || item.kojac_key
219
+ merge_model_into_results(item,result_key,:include => included_assocs)
213
220
  end
214
- item.save!
215
- result_key = op[:result_key] || item.kojac_key
216
- merge_model_into_results(item,result_key,:include => included_assocs)
221
+ {
222
+ key: op[:key],
223
+ verb: op[:verb],
224
+ result_key: result_key,
225
+ results: results
226
+ }
217
227
  end
218
- {
219
- key: op[:key],
220
- verb: op[:verb],
221
- result_key: result_key,
222
- results: results
223
- }
224
- end
225
228
 
226
- protected
227
-
228
- def merge_model_into_results(aItem,aResultKey=nil,aOptions=nil)
229
- ring = current_user.try(:ring)
230
- aResultKey ||= aItem.kojac_key
231
- aOptions ||= {}
232
- results[aResultKey] = aItem.sanitized_hash(ring)
233
- if included_assocs = aOptions[:include]
234
- included_assocs = included_assocs.split(',') if included_assocs.is_a?(String)
235
- included_assocs = [included_assocs] unless included_assocs.is_a?(Array)
236
- included_assocs.map!(&:to_sym) if included_assocs.is_a?(Array)
237
- p_assocs = aItem.class.permitted_associations(:read,ring)
238
- use_assocs = p_assocs.delete_if do |a|
239
- if included_assocs.include?(a) and ma = aItem.class.reflect_on_association(a)
240
- ![:belongs_to,:has_many].include?(ma.macro) # is supported association type
241
- else
242
- true # no such assoc
229
+ protected
230
+
231
+ def merge_model_into_results(aItem,aResultKey=nil,aOptions=nil)
232
+ ring = current_user.try(:ring)
233
+ aResultKey ||= aItem.kojac_key
234
+ aOptions ||= {}
235
+ results[aResultKey] = aItem.sanitized_hash(ring)
236
+ if included_assocs = aOptions[:include]
237
+ included_assocs = included_assocs.split(',') if included_assocs.is_a?(String)
238
+ included_assocs = [included_assocs] unless included_assocs.is_a?(Array)
239
+ included_assocs.map!(&:to_sym) if included_assocs.is_a?(Array)
240
+ p_assocs = aItem.class.permitted_associations(:read,ring)
241
+ use_assocs = p_assocs.delete_if do |a|
242
+ if included_assocs.include?(a) and ma = aItem.class.reflect_on_association(a)
243
+ ![:belongs_to,:has_many].include?(ma.macro) # is supported association type
244
+ else
245
+ true # no such assoc
246
+ end
243
247
  end
244
- end
245
- use_assocs.each do |a|
246
- next unless a_contents = aItem.send(a)
247
- if a_contents.is_a? Array
248
- contents_h = []
249
- a_contents.each do |sub_item|
250
- results[sub_item.kojac_key] = sub_item.sanitized_hash(ring)
251
- #contents_h << sub_item.id
248
+ use_assocs.each do |a|
249
+ next unless a_contents = aItem.send(a)
250
+ if a_contents.is_a? Array
251
+ contents_h = []
252
+ a_contents.each do |sub_item|
253
+ results[sub_item.kojac_key] = sub_item.sanitized_hash(ring)
254
+ #contents_h << sub_item.id
255
+ end
256
+ #results[aResultKey] = contents_h
257
+ else
258
+ results[a_contents.kojac_key] = a_contents.sanitized_hash(ring)
252
259
  end
253
- #results[aResultKey] = contents_h
254
- else
255
- results[a_contents.kojac_key] = a_contents.sanitized_hash(ring)
256
260
  end
257
261
  end
258
262
  end
259
- end
260
263
 
261
- public
262
-
263
- def read_op
264
- op = params[:op]
265
- key = op[:key]
266
- result_key = nil
267
- resource,id = key.split '__'
268
- model = deduce_model_class
269
-
270
- if id # item
271
- if model
272
- item = model.by_key(key,op)
273
- result_key = op[:result_key] || (item && item.kojac_key) || op[:key]
274
- merge_model_into_results(item,result_key,op[:options])
275
- else
264
+ public
265
+
266
+ def read_op
267
+ op = params[:op]
268
+ key = op[:key]
269
+ result_key = nil
270
+ resource,id = key.split '__'
271
+ model = deduce_model_class
272
+
273
+ if id # item
274
+ if model
275
+ item = model.by_key(key,op)
276
+ result_key = op[:result_key] || (item && item.kojac_key) || op[:key]
277
+ merge_model_into_results(item,result_key,op[:options])
278
+ else
279
+ result_key = op[:result_key] || op[:key]
280
+ results[result_key] = null
281
+ end
282
+ else # collection
276
283
  result_key = op[:result_key] || op[:key]
277
- results[result_key] = null
278
- end
279
- else # collection
280
- result_key = op[:result_key] || op[:key]
281
- results[result_key] = []
282
- if model
283
- items = model.by_key(key,op)
284
- items.each do |m|
285
- item_key = m.kojac_key
286
- results[result_key] << item_key.bite(resource+'__')
287
- merge_model_into_results(m,item_key,op[:options])
284
+ results[result_key] = []
285
+ if model
286
+ items = model.by_key(key,op)
287
+ items.each do |m|
288
+ item_key = m.kojac_key
289
+ results[result_key] << item_key.bite(resource+'__')
290
+ merge_model_into_results(m,item_key,op[:options])
291
+ end
288
292
  end
289
293
  end
294
+ {
295
+ key: op[:key],
296
+ verb: op[:verb],
297
+ results: results,
298
+ result_key: result_key
299
+ }
290
300
  end
291
- {
292
- key: op[:key],
293
- verb: op[:verb],
294
- results: results,
295
- result_key: result_key
296
- }
297
- end
298
301
 
299
- def update_op
300
- result = nil
301
- model = deduce_model_class
302
-
303
- ring = current_user.try(:ring)
304
- op = params[:op]
305
- result_key = nil
306
- if self.item = model.by_key(op[:key],op)
307
-
308
- run_callbacks :update_op do
309
- item.update_permitted_attributes!(op[:value], ring)
310
-
311
- associations = model.permitted_associations(:write,ring)
312
- associations.each do |k|
313
- next unless assoc = model.reflect_on_association(k)
314
- next unless op[:value][k]
315
- case assoc.macro
316
- when :belongs_to
317
- if leaf = (item.send(k) || item.send("build_#{k}".to_sym))
318
- #permitted_fields = leaf.class.permitted_fields(:write,ring)
319
- #permitted_fields = op[:value][k].permit( *permitted_fields )
320
- #leaf.assign_attributes(permitted_fields, :without_protection => true)
321
- #leaf.save!
322
- leaf.update_permitted_attributes!(op[:value][k], ring)
323
- end
302
+ def update_op
303
+ result = nil
304
+ model = deduce_model_class
305
+
306
+ ring = current_user.try(:ring)
307
+ op = params[:op]
308
+ result_key = nil
309
+ if self.item = model.by_key(op[:key],op)
310
+
311
+ run_callbacks :update_op do
312
+ item.update_permitted_attributes!(op[:value], ring)
313
+
314
+ associations = model.permitted_associations(:write,ring)
315
+ associations.each do |k|
316
+ next unless assoc = model.reflect_on_association(k)
317
+ next unless op[:value][k]
318
+ case assoc.macro
319
+ when :belongs_to
320
+ if leaf = (item.send(k) || item.send("build_#{k}".to_sym))
321
+ #permitted_fields = leaf.class.permitted_fields(:write,ring)
322
+ #permitted_fields = op[:value][k].permit( *permitted_fields )
323
+ #leaf.assign_attributes(permitted_fields, :without_protection => true)
324
+ #leaf.save!
325
+ leaf.update_permitted_attributes!(op[:value][k], ring)
326
+ end
327
+ end
324
328
  end
325
- end
326
329
 
327
- result_key = item.kojac_key
328
- results[result_key] = item
330
+ result_key = item.kojac_key
331
+ results[result_key] = item
329
332
 
330
- associations.each do |a|
331
- next unless assoc_item = item.send(a)
332
- next unless key = assoc_item.respond_to?(:kojac_key) && assoc_item.kojac_key
333
- results[key] = assoc_item
333
+ associations.each do |a|
334
+ next unless assoc_item = item.send(a)
335
+ next unless key = assoc_item.respond_to?(:kojac_key) && assoc_item.kojac_key
336
+ results[key] = assoc_item
337
+ end
334
338
  end
335
339
  end
340
+ {
341
+ key: op[:key],
342
+ verb: op[:verb],
343
+ result_key: result_key,
344
+ results: results
345
+ }
336
346
  end
337
- {
338
- key: op[:key],
339
- verb: op[:verb],
340
- result_key: result_key,
341
- results: results
342
- }
343
- end
344
-
345
- def destroy_op
346
- ring = current_user.try(:ring)
347
- op = params[:op]
348
- result_key = op[:result_key] || op[:key]
349
- item = KojacUtils.model_for_key(op[:key])
350
- item.destroy if item
351
- results[result_key] = nil
352
- {
353
- key: op[:key],
354
- verb: op[:verb],
355
- result_key: result_key,
356
- results: results
357
- }
358
- end
359
347
 
360
- #def execute_op
361
- # puts 'execute_op'
362
- #end
363
-
364
- def add_op
365
- ring = current_user.try(:ring)
366
- op = params[:op]
367
- model = deduce_model_class
368
- raise "ADD only supports associated collections at present eg order.items" unless op[:key].index('.')
369
-
370
- item = KojacUtils.model_for_key(op[:key].base_key)
371
- assoc = (assoc=op[:key].key_assoc) && assoc.to_sym
372
- id = op[:value]['id']
373
-
374
- ma = item.class.reflect_on_association(assoc)
375
- case ma.macro
376
- when :has_many
377
- assoc_class = ma.klass
378
- assoc_item = assoc_class.find(id)
379
- item.send(assoc) << assoc_item
380
-
381
- #ids_method = assoc.to_s.singularize+'_ids'
382
- #ids = item.send(ids_method.to_sym)
383
- #item.send((ids_method+'=').to_sym,ids + [id])
384
- result_key = assoc_item.kojac_key
385
- merge_model_into_results(assoc_item)
386
- else
387
- raise "ADD does not yet support #{ma.macro} associations"
348
+ def destroy_op
349
+ ring = current_user.try(:ring)
350
+ op = params[:op]
351
+ result_key = op[:result_key] || op[:key]
352
+ item = KojacUtils.model_for_key(op[:key])
353
+ item.destroy if item
354
+ results[result_key] = nil
355
+ {
356
+ key: op[:key],
357
+ verb: op[:verb],
358
+ result_key: result_key,
359
+ results: results
360
+ }
388
361
  end
389
- {
390
- key: op[:key],
391
- verb: op[:verb],
392
- result_key: result_key,
393
- results: results
394
- }
395
- end
396
362
 
397
- def remove_op
398
- ring = current_user.try(:ring)
399
- op = params[:op]
400
- model = deduce_model_class
401
- raise "REMOVE only supports associated collections at present eg order.items" unless op[:key].key_assoc
402
-
403
- item = KojacUtils.model_for_key(op[:key].base_key)
404
- assoc = (assoc=op[:key].key_assoc) && assoc.to_sym
405
- id = op[:value]['id']
406
-
407
- ma = item.class.reflect_on_association(assoc)
408
- case ma.macro
409
- when :has_many
410
- assoc_class = ma.klass
411
- if assoc_item = item.send(assoc).find(id)
412
- item.send(assoc).delete(assoc_item)
363
+ #def execute_op
364
+ # puts 'execute_op'
365
+ #end
366
+
367
+ def add_op
368
+ ring = current_user.try(:ring)
369
+ op = params[:op]
370
+ model = deduce_model_class
371
+ raise "ADD only supports associated collections at present eg order.items" unless op[:key].index('.')
372
+
373
+ item = KojacUtils.model_for_key(op[:key].base_key)
374
+ assoc = (assoc=op[:key].key_assoc) && assoc.to_sym
375
+ id = op[:value]['id']
376
+
377
+ ma = item.class.reflect_on_association(assoc)
378
+ case ma.macro
379
+ when :has_many
380
+ assoc_class = ma.klass
381
+ assoc_item = assoc_class.find(id)
382
+ item.send(assoc) << assoc_item
383
+
384
+ #ids_method = assoc.to_s.singularize+'_ids'
385
+ #ids = item.send(ids_method.to_sym)
386
+ #item.send((ids_method+'=').to_sym,ids + [id])
413
387
  result_key = assoc_item.kojac_key
414
- if (assoc_item.destroyed?)
415
- results[result_key] = nil
416
- else
417
- merge_model_into_results(assoc_item,result_key)
418
- end
419
- end
420
- else
421
- raise "REMOVE does not yet support #{ma.macro} associations"
388
+ merge_model_into_results(assoc_item)
389
+ else
390
+ raise "ADD does not yet support #{ma.macro} associations"
391
+ end
392
+ {
393
+ key: op[:key],
394
+ verb: op[:verb],
395
+ result_key: result_key,
396
+ results: results
397
+ }
422
398
  end
423
- {
424
- key: op[:key],
425
- verb: op[:verb],
426
- result_key: result_key,
427
- results: results
428
- }
429
- end
430
399
 
400
+ def remove_op
401
+ ring = current_user.try(:ring)
402
+ op = params[:op]
403
+ model = deduce_model_class
404
+ raise "REMOVE only supports associated collections at present eg order.items" unless op[:key].key_assoc
431
405
 
406
+ item = KojacUtils.model_for_key(op[:key].base_key)
407
+ assoc = (assoc=op[:key].key_assoc) && assoc.to_sym
408
+ id = op[:value]['id']
409
+
410
+ ma = item.class.reflect_on_association(assoc)
411
+ case ma.macro
412
+ when :has_many
413
+ assoc_class = ma.klass
414
+ if assoc_item = item.send(assoc).find(id)
415
+ item.send(assoc).delete(assoc_item)
416
+ result_key = assoc_item.kojac_key
417
+ if (assoc_item.destroyed?)
418
+ results[result_key] = nil
419
+ else
420
+ merge_model_into_results(assoc_item,result_key)
421
+ end
422
+ end
423
+ else
424
+ raise "REMOVE does not yet support #{ma.macro} associations"
425
+ end
426
+ {
427
+ key: op[:key],
428
+ verb: op[:verb],
429
+ result_key: result_key,
430
+ results: results
431
+ }
432
+ end
433
+ end
432
434
  end
data/lib/kojac/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Kojac
2
- VERSION = "0.9.0"
2
+ VERSION = "0.9.0.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: kojac
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.0
4
+ version: 0.9.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gary McGhee