effective_resources 0.4.9 → 0.4.10
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 +4 -4
- data/README.md +24 -12
- data/app/controllers/concerns/effective/crud_controller.rb +8 -8
- data/app/controllers/concerns/effective/flash_messages.rb +47 -0
- data/app/helpers/effective_resources_helper.rb +1 -1
- data/app/models/effective/resources/actions.rb +1 -1
- data/lib/effective_resources/engine.rb +7 -0
- data/lib/effective_resources/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 57a9fea58220e51746d9c8467b84e884a3039049
|
4
|
+
data.tar.gz: 3252c3a12d1dbff7591df1f8cbab3d9ea4961ddf
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e869b9eb37fe264c797927817fb67f30abf8415217e572ac2dd628ac6e15105e4f425f807bca12f690d01c908cc61b2fad5fe3f67eb2178a7c61b0e88486d2b4
|
7
|
+
data.tar.gz: cd024f08251b9b0ebcbfa22e7e3671b281bcaa603675c10eea7ca89fc05f90f32eddbae710e68a95277c13da844a2e08384f79b4ad6f936bd029abe1710a4e4a
|
data/README.md
CHANGED
@@ -32,21 +32,31 @@ Add to your contoller:
|
|
32
32
|
class PostsController < ApplicationController
|
33
33
|
include Effective::CrudController
|
34
34
|
|
35
|
-
|
35
|
+
# Sets the @page_title in a before_filter
|
36
|
+
page_title 'My Posts', only: [:index]
|
36
37
|
|
37
|
-
|
38
|
+
# All queries and objects will be built with this scope
|
39
|
+
resource_scope -> { current_user.posts }
|
38
40
|
|
39
|
-
#
|
40
|
-
|
41
|
-
|
42
|
-
# If it's an ActiveRecord scope, or symbol, we initialize a datatable with {resource_scope: true}
|
43
|
-
# and leave it as a TODO for the datatable to do the right thin .
|
44
|
-
def post_scope
|
45
|
-
{client_id: current_user.client_id}
|
46
|
-
# Post.where(client_id: current_user.client_id)
|
47
|
-
# :approved
|
41
|
+
# Similar to above, with block syntax
|
42
|
+
resource_scope do
|
43
|
+
Post.active.where(user: current_user)
|
48
44
|
end
|
49
45
|
|
46
|
+
# When GET request, will render the approve page
|
47
|
+
# When POST|PATCH|PUT request, will call @post.approve! and do the right thing
|
48
|
+
member_action :approve
|
49
|
+
|
50
|
+
# When GET request, will render an index page scoped to this method (if it's a scope on the model i.e. Post.approved)
|
51
|
+
collection_action :approved
|
52
|
+
|
53
|
+
# When POST|PATCH|PUT request, will call @post.approve! on each post as per params[:ids]
|
54
|
+
# Created with effective_datatables bulk actions in mind
|
55
|
+
collection_action :bulk_approve
|
56
|
+
|
57
|
+
protected
|
58
|
+
|
59
|
+
# The permitted parameters for this post. Other recognized method names are posts_params and permitted_params
|
50
60
|
def post_params
|
51
61
|
params.require(:post).permit(:id, :title, :body)
|
52
62
|
end
|
@@ -58,11 +68,13 @@ end
|
|
58
68
|
|
59
69
|
Implements the 7 RESTful actions: `index`, `new`, `create`, `show`, `edit`, `update`, `destroy`.
|
60
70
|
|
61
|
-
- Loads an appropriate `@post` type instance
|
71
|
+
- Loads an appropriate `@posts` or `@post` type instance variable.
|
62
72
|
- Sets a `@page_title` (effective_pages).
|
63
73
|
- Calls authorize as per the configured `EffectiveResources.authorization_method` (flow through to CanCan or Pundit)
|
64
74
|
- Does the create/update save
|
65
75
|
- Sets a `flash[:success]` and redirects on success, or sets a `flash.now[:danger]` and renders on error.
|
76
|
+
- Does the right thing with member and collection actions
|
77
|
+
- Intelligently redirects based on commit message
|
66
78
|
|
67
79
|
## Helpers
|
68
80
|
|
@@ -101,10 +101,10 @@ module Effective
|
|
101
101
|
resource.created_by ||= current_user if resource.respond_to?(:created_by=)
|
102
102
|
|
103
103
|
if resource.save
|
104
|
-
flash[:success] =
|
104
|
+
flash[:success] = flash_success(resource)
|
105
105
|
redirect_to(resource_redirect_path)
|
106
106
|
else
|
107
|
-
flash.now[:danger] =
|
107
|
+
flash.now[:danger] = flash_danger(resource)
|
108
108
|
render :new
|
109
109
|
end
|
110
110
|
end
|
@@ -130,10 +130,10 @@ module Effective
|
|
130
130
|
EffectiveResources.authorized?(self, :update, resource)
|
131
131
|
|
132
132
|
if resource.update_attributes(send(resource_params_method_name))
|
133
|
-
flash[:success] =
|
133
|
+
flash[:success] = flash_success(resource)
|
134
134
|
redirect_to(resource_redirect_path)
|
135
135
|
else
|
136
|
-
flash.now[:danger] =
|
136
|
+
flash.now[:danger] = flash_danger(resource)
|
137
137
|
render :edit
|
138
138
|
end
|
139
139
|
end
|
@@ -145,9 +145,9 @@ module Effective
|
|
145
145
|
EffectiveResources.authorized?(self, :destroy, resource)
|
146
146
|
|
147
147
|
if resource.destroy
|
148
|
-
flash[:success] =
|
148
|
+
flash[:success] = flash_success(resource, :delete)
|
149
149
|
else
|
150
|
-
flash[:danger] =
|
150
|
+
flash[:danger] = flash_danger(resource, :delete)
|
151
151
|
end
|
152
152
|
|
153
153
|
if request.referer.present? && !request.referer.include?(effective_resource.show_path)
|
@@ -164,10 +164,10 @@ module Effective
|
|
164
164
|
begin
|
165
165
|
resource.public_send("#{action}!") || raise("failed to #{action} #{resource}")
|
166
166
|
|
167
|
-
flash[:success] =
|
167
|
+
flash[:success] = flash_success(resource, action)
|
168
168
|
redirect_back(fallback_location: resource_redirect_path)
|
169
169
|
rescue => e
|
170
|
-
flash.now[:danger] =
|
170
|
+
flash.now[:danger] = flash_danger(resource, action, e: e)
|
171
171
|
|
172
172
|
referer = request.referer.to_s
|
173
173
|
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module Effective
|
2
|
+
module FlashMessages
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
# flash[:success] = flash_success(@post)
|
6
|
+
def flash_success(resource, action = nil, name: nil)
|
7
|
+
raise 'expected an ActiveRecord resource' unless (name || resource.class.respond_to?(:model_name))
|
8
|
+
|
9
|
+
action ||= :save
|
10
|
+
name ||= resource.class.model_name.human
|
11
|
+
|
12
|
+
"#{name.to_s.titleize} was successfully #{action}#{(action.to_s.end_with?('e') ? 'd' : 'ed')}"
|
13
|
+
end
|
14
|
+
|
15
|
+
# flash.now[:danger] = flash_danger(@post)
|
16
|
+
def flash_danger(resource, action = nil, e: nil, name: nil)
|
17
|
+
raise 'expected an ActiveRecord resource' unless resource.respond_to?(:errors) && (name || resource.class.respond_to?(:model_name))
|
18
|
+
|
19
|
+
action ||= resource.respond_to?(:new_record?) ? (resource.new_record? ? :create : :update) : :save
|
20
|
+
name ||= resource.class.model_name.human
|
21
|
+
messages = flash_errors(resource, e: e)
|
22
|
+
|
23
|
+
["Unable to #{action} #{name.to_s.downcase}", (": #{messages}." if messages)].compact.join.html_safe
|
24
|
+
end
|
25
|
+
|
26
|
+
# flash.now[:danger] = "Unable to accept: #{flash_errors(@post)}"
|
27
|
+
def flash_errors(resource, e: nil)
|
28
|
+
raise 'expected an ActiveRecord resource' unless resource.respond_to?(:errors)
|
29
|
+
|
30
|
+
messages = resource.errors.map do |attribute, message|
|
31
|
+
if message[0] == message[0].upcase # If the error begins with a capital letter
|
32
|
+
message
|
33
|
+
elsif attribute == :base
|
34
|
+
"#{resource.class.model_name.human.downcase} #{message}"
|
35
|
+
elsif attribute.to_s.end_with?('_ids')
|
36
|
+
"#{resource.class.human_attribute_name(attribute.to_s[0..-5].pluralize).downcase} #{message}"
|
37
|
+
else
|
38
|
+
"#{resource.class.human_attribute_name(attribute).downcase} #{message}"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
messages << e.message if messages.blank? && e && e.respond_to?(:message)
|
43
|
+
|
44
|
+
messages.to_sentence.presence
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -15,7 +15,7 @@ module EffectiveResourcesHelper
|
|
15
15
|
|
16
16
|
def simple_form_save(form, options = {class: 'form-actions'}, &block)
|
17
17
|
content_tag(:div, class: options[:class]) do
|
18
|
-
form.button(:submit, 'Save', data: { disable_with: 'Saving...' }) + (capture(
|
18
|
+
form.button(:submit, 'Save', data: { disable_with: 'Saving...' }) + (capture(&block) if block_given?)
|
19
19
|
end
|
20
20
|
end
|
21
21
|
|
@@ -23,7 +23,7 @@ module Effective
|
|
23
23
|
controller_routes.map { |route| route.defaults[:action] if is_get_member?(route) }.compact - crud_actions
|
24
24
|
end
|
25
25
|
|
26
|
-
#
|
26
|
+
# POST/PUT/PATCH actions
|
27
27
|
def member_post_actions
|
28
28
|
controller_routes.map { |route| route.defaults[:action] if is_post_member?(route) }.compact - crud_actions
|
29
29
|
end
|
@@ -9,5 +9,12 @@ module EffectiveResources
|
|
9
9
|
eval File.read("#{config.root}/config/effective_resources.rb")
|
10
10
|
end
|
11
11
|
|
12
|
+
# Register the flash_messages concern so that it can be called in ActionController
|
13
|
+
initializer 'effective_resources.action_controller' do |app|
|
14
|
+
ActiveSupport.on_load :action_controller do
|
15
|
+
include(Effective::FlashMessages)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
12
19
|
end
|
13
20
|
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.
|
4
|
+
version: 0.4.10
|
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-10-
|
11
|
+
date: 2017-10-10 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -36,6 +36,7 @@ files:
|
|
36
36
|
- app/assets/javascripts/effective_resources.js
|
37
37
|
- app/assets/javascripts/effective_resources/effective_ujs.js
|
38
38
|
- app/controllers/concerns/effective/crud_controller.rb
|
39
|
+
- app/controllers/concerns/effective/flash_messages.rb
|
39
40
|
- app/helpers/effective_resources_helper.rb
|
40
41
|
- app/models/effective/access_denied.rb
|
41
42
|
- app/models/effective/attribute.rb
|