effective_resources 0.4.6 → 0.4.7

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: 3603bb31768aaa66451280ed995ff7ffb0267317
4
- data.tar.gz: cefdd8ab23fb2caf9cea8a8072a5924a98bf2ed7
3
+ metadata.gz: 35fdc868ed7c8871810c8d041776f9d9c264e63b
4
+ data.tar.gz: 4c097429d190542812d01b2dcc2bd4d7cffc6653
5
5
  SHA512:
6
- metadata.gz: 6a0ee609b5828105deafbfcb6998582b9b41f96cfccfd4225ab3b725e1c73e9d487172d34fd574618f3938b3bb5b059fecf249b3b3a279df812fd84a95ac96a8
7
- data.tar.gz: 1b0f62769e126fd7d32eb3c66b955cff2e2137d66bebae0e0fd1054721652b09882886e9923a994e94b993dd343d57ed901d189bf7ba1158f4ba55c6029e57cc
6
+ metadata.gz: 2e85f37e03a17f55b9db2dbe65882ce42ed37503e7c75efe5e4f14aafd4402337eeae6ddca2711ea58126370699180c9f64a42ac1cfec6f7464e8fa14e2b9bc2
7
+ data.tar.gz: 5bf2bdf611049d0d1a32e52aebefb6620f79321913b30fbcf1a65c763c452e784e7d9b5300fac09628a51e7bd782c9dc26078df6b1db33523ca3b3214ae96a7f
@@ -10,7 +10,7 @@ module Effective
10
10
  # member_action :print
11
11
  def member_action(action)
12
12
  define_method(action) do
13
- self.resource ||= resource_class.find(params[:id])
13
+ self.resource ||= resource_scope.find(params[:id])
14
14
 
15
15
  EffectiveResources.authorized?(self, action, resource)
16
16
 
@@ -23,16 +23,16 @@ module Effective
23
23
  def collection_action(action)
24
24
  define_method(action) do
25
25
  if params[:ids].present?
26
- self.resources ||= resource_class.where(id: params[:ids])
26
+ self.resources ||= resource_scope.where(id: params[:ids])
27
27
  end
28
28
 
29
29
  if effective_resource.scope?(action)
30
- self.resources ||= resource_class.send(action)
30
+ self.resources ||= resource_scope.send(action)
31
31
  end
32
32
 
33
- self.resources ||= resource_class.all
33
+ self.resources ||= resource_scope.all
34
34
 
35
- EffectiveResources.authorized?(self, action, resource_class)
35
+ EffectiveResources.authorized?(self, action, resource_klass)
36
36
 
37
37
  @page_title ||= "#{action.to_s.titleize} #{resource_plural_name.titleize}"
38
38
 
@@ -41,9 +41,38 @@ module Effective
41
41
  end
42
42
 
43
43
  # page_title 'My Title', only: [:new]
44
- def page_title(label, opts = {})
44
+ def page_title(label = nil, opts = {}, &block)
45
+ raise 'expected a label or block' unless (label || block_given?)
46
+
47
+ instance_eval do
48
+ before_action(opts) { @page_title ||= (block_given? ? instance_eval(&block) : label) }
49
+ end
50
+ end
51
+
52
+ # resource_scope -> { current_user.things }
53
+ # resource_scope -> { Thing.active.where(user: current_user) }
54
+ # resource_scope do
55
+ # { user_id: current_user.id }
56
+ # end
57
+
58
+ # Return value should be:
59
+ # a Relation: Thing.where(user: current_user)
60
+ # a Hash: { user_id: current_user.id }
61
+ def resource_scope(obj = nil, opts = {}, &block)
62
+ raise 'expected a proc or block' unless (obj.respond_to?(:call) || block_given?)
63
+
45
64
  instance_eval do
46
- before_action(opts) { @page_title = label }
65
+ before_action(opts) do
66
+ @_effective_resource_scope ||= (
67
+ if block_given?
68
+ instance_exec(&block)
69
+ elsif obj.respond_to?(:call)
70
+ instance_exec(&obj)
71
+ else
72
+ obj
73
+ end
74
+ )
75
+ end
47
76
  end
48
77
  end
49
78
 
@@ -51,9 +80,9 @@ module Effective
51
80
 
52
81
  def index
53
82
  @page_title ||= resource_plural_name.titleize
54
- EffectiveResources.authorized?(self, :index, resource_class)
83
+ EffectiveResources.authorized?(self, :index, resource_klass)
55
84
 
56
- self.resources ||= resource_class.all
85
+ self.resources ||= resource_scope.all
57
86
 
58
87
  if resource_datatable_class
59
88
  @datatable ||= resource_datatable_class.new(self, resource_datatable_attributes)
@@ -61,7 +90,7 @@ module Effective
61
90
  end
62
91
 
63
92
  def new
64
- self.resource ||= resource_class.new
93
+ self.resource ||= resource_scope.new
65
94
 
66
95
  self.resource.assign_attributes(
67
96
  params.to_unsafe_h.except(:controller, :action).select { |k, v| resource.respond_to?("#{k}=") }
@@ -72,7 +101,7 @@ module Effective
72
101
  end
73
102
 
74
103
  def create
75
- self.resource ||= resource_class.new(send(resource_params_method_name))
104
+ self.resource ||= resource_scope.new(send(resource_params_method_name))
76
105
 
77
106
  @page_title ||= "New #{resource_name.titleize}"
78
107
  EffectiveResources.authorized?(self, :create, resource)
@@ -89,21 +118,21 @@ module Effective
89
118
  end
90
119
 
91
120
  def show
92
- self.resource ||= resource_class.find(params[:id])
121
+ self.resource ||= resource_scope.find(params[:id])
93
122
 
94
123
  @page_title ||= resource.to_s
95
124
  EffectiveResources.authorized?(self, :show, resource)
96
125
  end
97
126
 
98
127
  def edit
99
- self.resource ||= resource_class.find(params[:id])
128
+ self.resource ||= resource_scope.find(params[:id])
100
129
 
101
130
  @page_title ||= "Edit #{resource}"
102
131
  EffectiveResources.authorized?(self, :edit, resource)
103
132
  end
104
133
 
105
134
  def update
106
- self.resource ||= resource_class.find(params[:id])
135
+ self.resource ||= resource_scope.find(params[:id])
107
136
 
108
137
  @page_title = "Edit #{resource}"
109
138
  EffectiveResources.authorized?(self, :update, resource)
@@ -118,7 +147,7 @@ module Effective
118
147
  end
119
148
 
120
149
  def destroy
121
- self.resource = resource_class.find(params[:id])
150
+ self.resource = resource_scope.find(params[:id])
122
151
 
123
152
  @page_title ||= "Destroy #{resource}"
124
153
  EffectiveResources.authorized?(self, :destroy, resource)
@@ -184,7 +213,7 @@ module Effective
184
213
 
185
214
  successes = 0
186
215
 
187
- resource_class.transaction do
216
+ resource_klass.transaction do
188
217
  successes = resources.select do |resource|
189
218
  begin
190
219
  resource.public_send("#{action}!") if EffectiveResources.authorized?(self, action, resource)
@@ -211,6 +240,10 @@ module Effective
211
240
  end
212
241
  end
213
242
 
243
+ def resource_index_path
244
+ effective_resource.index_path
245
+ end
246
+
214
247
  def resource # @thing
215
248
  instance_variable_get("@#{resource_name}")
216
249
  end
@@ -237,6 +270,10 @@ module Effective
237
270
  effective_resource.name
238
271
  end
239
272
 
273
+ def resource_klass # Thing
274
+ effective_resource.klass
275
+ end
276
+
240
277
  def resource_human_name
241
278
  effective_resource.human_name
242
279
  end
@@ -249,39 +286,32 @@ module Effective
249
286
  (action.to_s + (action.to_s.end_with?('e') ? 'd' : 'ed'))
250
287
  end
251
288
 
252
- # Scoped to resource_scope_method_name
253
- def resource_class # Thing
254
- @resource_class ||= (
255
- if resource_scope_method_name.blank?
256
- effective_resource.klass
289
+ # Returns an ActiveRecord relation based on the computed value of `resource_scope` dsl method
290
+ def resource_scope # Thing
291
+ @_effective_resource_relation ||= (
292
+ relation = case @_effective_resource_scope # If this was initialized by the resource_scope before_filter
293
+ when ActiveRecord::Relation
294
+ @_effective_resource_scope
295
+ when Hash
296
+ effective_resource.klass.where(@_effective_resource_scope)
297
+ when Symbol
298
+ effective_resource.klass.send(@_effective_resource_scope)
299
+ when nil
300
+ effective_resource.klass.all
257
301
  else
258
- case (resource_scope = send(resource_scope_method_name))
259
- when ActiveRecord::Relation
260
- effective_resource.relation.merge(resource_scope)
261
- when Hash
262
- effective_resource.klass.where(resource_scope)
263
- when Symbol
264
- effective_resource.klass.send(resource_scope)
265
- when nil
266
- effective_resource.klass
267
- else
268
- raise "expected #{resource_scope_method_name} to return a Hash or Symbol"
269
- end
302
+ raise "expected resource_scope method to return an ActiveRecord::Relation or Hash"
303
+ end
304
+
305
+ unless relation.kind_of?(ActiveRecord::Relation)
306
+ raise("unable to build resource relation for #{effective_resource.klass || 'unknown klass'}.")
270
307
  end
308
+
309
+ relation
271
310
  )
272
311
  end
273
312
 
274
313
  def resource_datatable_attributes
275
- return {} unless resource_scope_method_name.present?
276
-
277
- case (resource_scope = send(resource_scope_method_name))
278
- when ActiveRecord::Relation ; {resource_scope: true}
279
- when Hash ; resource_scope
280
- when Symbol ; {resource_scope: true}
281
- when nil ; {}
282
- else
283
- raise "expected #{resource_scope_method_name} to return a Hash or Symbol"
284
- end
314
+ resource_scope.where_values_hash.symbolize_keys
285
315
  end
286
316
 
287
317
  def resource_datatable_class # ThingsDatatable
@@ -292,14 +322,6 @@ module Effective
292
322
  ["#{resource_name}_params", "#{resource_plural_name}_params", 'permitted_params'].find { |name| respond_to?(name, true) } || 'params'
293
323
  end
294
324
 
295
- # Override this with Team.where(team: current_user.team) to build and create with team
296
- def resource_scope_method_name
297
- ["#{resource_name}_scope", "#{resource_plural_name}_scope", 'resource_scope', 'default_scope'].find { |name| respond_to?(name, true) }
298
- end
299
-
300
- def resource_index_path
301
- effective_resource.index_path
302
- end
303
325
 
304
326
  end
305
327
  end
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.4.6'.freeze
2
+ VERSION = '0.4.7'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: effective_resources
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.6
4
+ version: 0.4.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Code and Effect
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-14 00:00:00.000000000 Z
11
+ date: 2017-09-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails