effective_resources 0.4.5 → 0.4.6

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: 4d2f4f585bbb5859066bf26049f670a026ad0df2
4
- data.tar.gz: e10b256b516418051208158bc06a8dc76b46f1e6
3
+ metadata.gz: 3603bb31768aaa66451280ed995ff7ffb0267317
4
+ data.tar.gz: cefdd8ab23fb2caf9cea8a8072a5924a98bf2ed7
5
5
  SHA512:
6
- metadata.gz: 33229026829624749822b3d54d0188b1979faf226d425b88f97dee7fe9618309d7f0630c7402ae36de704aabe5c9370af76e98f112de9b46583c240242f85835
7
- data.tar.gz: 54286cafb8da006ef388c2d0a2ac0cf95c0b582e2f2bdb0f591188289bb05c32350f8452d11efd9ef23f3095ec3ada32a8a2fba7717bb85d3ccc0ffe8db94f01
6
+ metadata.gz: 6a0ee609b5828105deafbfcb6998582b9b41f96cfccfd4225ab3b725e1c73e9d487172d34fd574618f3938b3bb5b059fecf249b3b3a279df812fd84a95ac96a8
7
+ data.tar.gz: 1b0f62769e126fd7d32eb3c66b955cff2e2137d66bebae0e0fd1054721652b09882886e9923a994e94b993dd343d57ed901d189bf7ba1158f4ba55c6029e57cc
@@ -14,44 +14,29 @@ module Effective
14
14
 
15
15
  EffectiveResources.authorized?(self, action, resource)
16
16
 
17
- if (request.post? || request.patch? || request.put?)
18
- raise "expected @#{resource_name} to respond to #{action}!" unless resource.respond_to?("#{action}!")
19
-
20
- begin
21
- resource.send("#{action}!") || raise('exception')
22
-
23
- flash[:success] = "Successfully #{action}#{action.to_s.end_with?('e') ? 'd' : 'ed'} #{resource_human_name}"
24
- redirect_back fallback_location: resource_redirect_path
25
- rescue => e
26
- flash.now[:danger] = "Unable to #{action} #{resource_human_name}: #{resource.errors.full_messages.to_sentence.presence || e.message}"
27
-
28
- referer = request.referer.to_s
29
-
30
- edit_path = send(effective_resource.edit_path, resource) if respond_to?(effective_resource.edit_path)
31
- new_path = effective_resource.new_path if respond_to?(effective_resource.new_path)
32
- show_path = effective_resource.show_path if respond_to?(effective_resource.show_path)
33
-
34
- if edit_path && referer.end_with?(edit_path)
35
- @page_title ||= "Edit #{resource}"
36
- render :edit
37
- elsif new_path && referer.end_with?(new_path)
38
- @page_title ||= "New #{resource_name.titleize}"
39
- render :new
40
- elsif show_path && referer.end_with?(show_path)
41
- @page_title ||= resource_name.titleize
42
- render :show
43
- else
44
- @page_title ||= resource.to_s
45
- flash[:danger] = flash.now[:danger]
46
-
47
- if referer.present? && (Rails.application.routes.recognize_path(URI(referer).path) rescue false)
48
- redirect_back fallback_location: resource_redirect_path
49
- else
50
- redirect_to(resource_redirect_path)
51
- end
52
- end
53
- end
17
+ @page_title ||= "#{action.to_s.titleize} #{resource}"
18
+
19
+ member_post_action(action) unless request.get?
20
+ end
21
+ end
22
+
23
+ def collection_action(action)
24
+ define_method(action) do
25
+ if params[:ids].present?
26
+ self.resources ||= resource_class.where(id: params[:ids])
27
+ end
28
+
29
+ if effective_resource.scope?(action)
30
+ self.resources ||= resource_class.send(action)
54
31
  end
32
+
33
+ self.resources ||= resource_class.all
34
+
35
+ EffectiveResources.authorized?(self, action, resource_class)
36
+
37
+ @page_title ||= "#{action.to_s.titleize} #{resource_plural_name.titleize}"
38
+
39
+ collection_post_action(action) unless request.get?
55
40
  end
56
41
  end
57
42
 
@@ -66,7 +51,7 @@ module Effective
66
51
 
67
52
  def index
68
53
  @page_title ||= resource_plural_name.titleize
69
- EffectiveResources.authorized?(self, :index, resource_class.new)
54
+ EffectiveResources.authorized?(self, :index, resource_class)
70
55
 
71
56
  self.resources ||= resource_class.all
72
57
 
@@ -149,7 +134,67 @@ module Effective
149
134
  else
150
135
  redirect_to(send(resource_index_path))
151
136
  end
137
+ end
138
+
139
+ def member_post_action(action)
140
+ raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?)
141
+ raise "expected @#{resource_name} to respond to #{action}!" unless resource.respond_to?("#{action}!")
142
+
143
+ begin
144
+ resource.public_send("#{action}!") || raise("failed to #{action} #{resource}")
145
+
146
+ flash[:success] = "Successfully #{action_verb(action)} #{resource_human_name}"
147
+ redirect_back(fallback_location: resource_redirect_path)
148
+ rescue => e
149
+ flash.now[:danger] = "Unable to #{action} #{resource_human_name}: #{resource.errors.full_messages.to_sentence.presence || e.message}"
150
+
151
+ referer = request.referer.to_s
152
+
153
+ edit_path = send(effective_resource.edit_path, resource) if respond_to?(effective_resource.edit_path)
154
+ new_path = effective_resource.new_path if respond_to?(effective_resource.new_path)
155
+ show_path = effective_resource.show_path if respond_to?(effective_resource.show_path)
156
+
157
+ if edit_path && referer.end_with?(edit_path)
158
+ @page_title ||= "Edit #{resource}"
159
+ render :edit
160
+ elsif new_path && referer.end_with?(new_path)
161
+ @page_title ||= "New #{resource_name.titleize}"
162
+ render :new
163
+ elsif show_path && referer.end_with?(show_path)
164
+ @page_title ||= resource_name.titleize
165
+ render :show
166
+ else
167
+ @page_title ||= resource.to_s
168
+ flash[:danger] = flash.now[:danger]
169
+
170
+ if referer.present? && (Rails.application.routes.recognize_path(URI(referer).path) rescue false)
171
+ redirect_back fallback_location: resource_redirect_path
172
+ else
173
+ redirect_to(resource_redirect_path)
174
+ end
175
+ end
176
+ end
177
+ end
178
+
179
+ def collection_post_action(action)
180
+ action = action.to_s.gsub('bulk_', '').to_sym
181
+
182
+ raise 'expected post, patch or put http action' unless (request.post? || request.patch? || request.put?)
183
+ raise "expected #{resource_name} to respond to #{action}!" if resources.to_a.present? && !resources.first.respond_to?("#{action}!")
184
+
185
+ successes = 0
186
+
187
+ resource_class.transaction do
188
+ successes = resources.select do |resource|
189
+ begin
190
+ resource.public_send("#{action}!") if EffectiveResources.authorized?(self, action, resource)
191
+ rescue => e
192
+ false
193
+ end
194
+ end.length
195
+ end
152
196
 
197
+ render json: { status: 200, message: "Successfully #{action_verb(action)} #{successes} / #{resources.length} selected #{resource_plural_name}" }
153
198
  end
154
199
 
155
200
  protected
@@ -200,6 +245,10 @@ module Effective
200
245
  effective_resource.plural_name
201
246
  end
202
247
 
248
+ def action_verb(action)
249
+ (action.to_s + (action.to_s.end_with?('e') ? 'd' : 'ed'))
250
+ end
251
+
203
252
  # Scoped to resource_scope_method_name
204
253
  def resource_class # Thing
205
254
  @resource_class ||= (
@@ -78,6 +78,23 @@ module Effective
78
78
  nested_resources.find { |ass| ass.name == name }
79
79
  end
80
80
 
81
+ def scope?(name)
82
+ return false unless klass.respond_to?(name)
83
+
84
+ is_scope = false
85
+
86
+ ActiveRecord::Base.transaction do
87
+ begin
88
+ relation = klass.public_send(name).kind_of?(ActiveRecord::Relation)
89
+ rescue => e
90
+ end
91
+
92
+ raise ActiveRecord::Rollback
93
+ end
94
+
95
+ is_scope
96
+ end
97
+
81
98
  end
82
99
  end
83
100
  end
@@ -38,9 +38,9 @@ module Effective
38
38
  name = name.to_s
39
39
 
40
40
  # If the method is named :status, and there is a Class::STATUSES
41
- if (klass || NilClass).const_defined?(name.pluralize.upcase)
41
+ if ((klass || NilClass).const_defined?(name.pluralize.upcase) rescue false)
42
42
  { as: :select, collection: klass.const_get(name.pluralize.upcase) }
43
- elsif (klass || NilClass).const_defined?(name.singularize.upcase)
43
+ elsif ((klass || NilClass).const_defined?(name.singularize.upcase) rescue false)
44
44
  { as: :select, collection: klass.const_get(name.singularize.upcase) }
45
45
  else
46
46
  { as: :string }
@@ -1,3 +1,3 @@
1
1
  module EffectiveResources
2
- VERSION = '0.4.5'.freeze
2
+ VERSION = '0.4.6'.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.5
4
+ version: 0.4.6
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-11 00:00:00.000000000 Z
11
+ date: 2017-09-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails