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 +8 -8
- data/Gemfile.lock +1 -1
- data/lib/kojac/kojac_rails.rb +318 -316
- data/lib/kojac/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZmY3Y2Y3OTRjODA1NDQ4MGFkNzQ2YjBlNzEyZWVhZjg1NzI3NWYxNA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MDQyNTNmYmM4YzA4ZTBiYmYzYmI2NWExYmRkNDBjODk5ZjJjNjhlMg==
|
7
7
|
!binary "U0hBNTEy":
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
ZjllODJmMWIzMGJjNjlkODNhZTM1N2Q3MTRjOTUyZTI3Y2Q4YzQ1OTgxYTAw
|
10
|
+
MWM5YTAxMDQ1MzAyYTkwMGM4NGQ2YTQ1MmIxM2Y3MzMyZTllMTc4NjA3NThh
|
11
|
+
Y2JlMWM3ODUyYWZhNGEyYWVmNWRjNmI3OTk1MmRjMTEzYTE4Mjc=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
NWQ5OTQ4ZGM4ZDc3M2EyOTBmMjRmZmRhODI3ZDhiYWVlNTE5Y2MwNzhiMDcw
|
14
|
+
MmZhNDk3YTZjNDk1MWE0NGFlMDQyOWM4NzUwODk4NzMyMDY1MGNhZGVkYmQy
|
15
|
+
NzEwMDdiN2I1ZmY4MWFmNjg1Mzc1ZTM4OTIyY2IzYWUzZTAxYzU=
|
data/Gemfile.lock
CHANGED
data/lib/kojac/kojac_rails.rb
CHANGED
@@ -63,370 +63,372 @@ module KojacUtils
|
|
63
63
|
|
64
64
|
end
|
65
65
|
|
66
|
-
module Kojac
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
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
|
-
|
86
|
-
|
87
|
-
|
86
|
+
def kojac_key
|
87
|
+
self.class.to_s.snake_case.pluralize+'__'+self.id.to_s
|
88
|
+
end
|
88
89
|
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
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
|
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
|
-
|
108
|
-
|
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
|
-
|
110
|
+
#module ClassMethods
|
111
|
+
#end
|
111
112
|
|
112
|
-
|
113
|
+
module_function
|
113
114
|
|
114
|
-
|
115
|
+
public
|
115
116
|
|
116
|
-
|
117
|
-
@results ||= {}
|
118
|
-
end
|
117
|
+
attr_accessor :item
|
119
118
|
|
120
|
-
|
121
|
-
|
122
|
-
|
119
|
+
def results
|
120
|
+
@results ||= {}
|
121
|
+
end
|
123
122
|
|
124
|
-
|
125
|
-
|
126
|
-
|
123
|
+
def deduce_model_class
|
124
|
+
KojacUtils.model_class_for_key(self.kojac_resource)
|
125
|
+
end
|
127
126
|
|
128
|
-
|
129
|
-
|
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
|
-
|
173
|
-
|
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
|
-
|
187
|
-
|
188
|
-
|
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
|
-
|
192
|
-
|
193
|
-
|
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
|
-
|
196
|
-
|
197
|
-
|
198
|
-
|
199
|
-
|
200
|
-
|
201
|
-
|
202
|
-
item
|
203
|
-
|
204
|
-
|
205
|
-
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
|
211
|
-
|
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
|
-
|
215
|
-
|
216
|
-
|
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
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
240
|
-
|
241
|
-
|
242
|
-
|
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
|
-
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
|
251
|
-
|
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
|
-
|
262
|
-
|
263
|
-
|
264
|
-
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
270
|
-
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
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] =
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
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
|
-
|
300
|
-
|
301
|
-
|
302
|
-
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
316
|
-
|
317
|
-
|
318
|
-
|
319
|
-
|
320
|
-
|
321
|
-
|
322
|
-
|
323
|
-
|
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
|
-
|
328
|
-
|
330
|
+
result_key = item.kojac_key
|
331
|
+
results[result_key] = item
|
329
332
|
|
330
|
-
|
331
|
-
|
332
|
-
|
333
|
-
|
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
|
-
|
361
|
-
|
362
|
-
|
363
|
-
|
364
|
-
|
365
|
-
|
366
|
-
|
367
|
-
|
368
|
-
|
369
|
-
|
370
|
-
|
371
|
-
|
372
|
-
|
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
|
-
|
398
|
-
|
399
|
-
|
400
|
-
|
401
|
-
|
402
|
-
|
403
|
-
|
404
|
-
|
405
|
-
|
406
|
-
|
407
|
-
|
408
|
-
|
409
|
-
|
410
|
-
|
411
|
-
|
412
|
-
|
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
|
-
|
415
|
-
|
416
|
-
|
417
|
-
|
418
|
-
|
419
|
-
|
420
|
-
|
421
|
-
|
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