crud_responder 0.2.6 → 0.3.0
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 +9 -2
- data/lib/crud_responder/caller_extractor.rb +34 -0
- data/lib/crud_responder/default_notification.rb +35 -0
- data/lib/crud_responder/default_options.rb +8 -0
- data/lib/crud_responder/version.rb +1 -1
- data/lib/crud_responder.rb +12 -49
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f1540ab291ed39557bbeadca726c8e779a42efc5
|
4
|
+
data.tar.gz: 5a10265472340364d6827b3b41525e0bdf20532d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0b9269aaca320514db7978f60bc35542fea2131565f58b4f0e6c3cf37fdd51f8ca9420fe7077bce5eab59a7c472d008fe870f2e5e9e8cff91bf499d4eae04d61
|
7
|
+
data.tar.gz: d677683035184027b7da066e8c20c24dced83a3d1e7c2660694191ff0b3e714f9901422d895ab46b6ee10a56e7e51fd48095bc10cda92c647ceb04ab483bdb6a
|
data/README.md
CHANGED
@@ -117,7 +117,7 @@ end
|
|
117
117
|
```
|
118
118
|
By default you will be redirected to object show, objects index or back in this order. And render :new when creating object and :edit when updating.
|
119
119
|
|
120
|
-
You can specify `error_url` instead of `error_action` to be redirected instead of action render in case of error
|
120
|
+
You can specify `error_url` instead of `error_action` to be redirected instead of action render in case of error:
|
121
121
|
```ruby
|
122
122
|
def create
|
123
123
|
@post = Post.new(post_params)
|
@@ -125,6 +125,14 @@ def create
|
|
125
125
|
# will redirect to root_path in case of success or to https://google.com otherwise
|
126
126
|
end
|
127
127
|
```
|
128
|
+
You can specify custom flash messages:
|
129
|
+
```ruby
|
130
|
+
def create
|
131
|
+
@post = Post.new(post_params)
|
132
|
+
crud_respond @post, success_message: 'Yep :)', error_message: 'Nope :('
|
133
|
+
# will render flash 'Yep :)' on success or 'Nope :(' on failure instead of default ones
|
134
|
+
end
|
135
|
+
```
|
128
136
|
|
129
137
|
You can also specify per-controller default options:
|
130
138
|
```ruby
|
@@ -177,7 +185,6 @@ Now your controllers are skinny again! Also, you are forced to think in terms of
|
|
177
185
|
|
178
186
|
## TODO
|
179
187
|
|
180
|
-
* Customizing flash messages
|
181
188
|
* Support for pure API controllers (which is much simpler)
|
182
189
|
* Testing
|
183
190
|
|
@@ -0,0 +1,34 @@
|
|
1
|
+
module CrudResponder
|
2
|
+
class CallerExtractor
|
3
|
+
attr_reader :kaller
|
4
|
+
|
5
|
+
def initialize(kaller)
|
6
|
+
@kaller = kaller
|
7
|
+
end
|
8
|
+
|
9
|
+
def name
|
10
|
+
kaller[0][/`.*'/][1..-2]
|
11
|
+
end
|
12
|
+
|
13
|
+
def method
|
14
|
+
if name =~ /destroy/
|
15
|
+
:destroy
|
16
|
+
else
|
17
|
+
:save
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def action
|
22
|
+
case name
|
23
|
+
when /destroy/
|
24
|
+
:destroy
|
25
|
+
when /update/
|
26
|
+
:update
|
27
|
+
when /create/
|
28
|
+
:create
|
29
|
+
else
|
30
|
+
name.to_sym
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
require 'crud_responder/caller_extractor'
|
2
|
+
|
3
|
+
module CrudResponder
|
4
|
+
class DefaultNotification
|
5
|
+
attr_reader :object, :kaller
|
6
|
+
|
7
|
+
def initialize(object, kaller)
|
8
|
+
@object = object
|
9
|
+
@kaller = kaller
|
10
|
+
end
|
11
|
+
|
12
|
+
def text(ok)
|
13
|
+
t_key = "flash.actions.#{CallerExtractor.new(kaller).action}.#{ok ? 'notice' : 'alert'}"
|
14
|
+
if ok
|
15
|
+
I18n.t(t_key, resource_name: resource_name, resource_desc: resource_desc)
|
16
|
+
else
|
17
|
+
I18n.t(t_key, resource_name: resource_name, resource_desc: resource_desc, errors: errors)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def errors
|
24
|
+
object.errors.full_messages.to_sentence
|
25
|
+
end
|
26
|
+
|
27
|
+
def resource_desc
|
28
|
+
object.to_s
|
29
|
+
end
|
30
|
+
|
31
|
+
def resource_name
|
32
|
+
object.try(:model_name).try(:human) || object.class.name
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
@@ -33,6 +33,14 @@ module CrudResponder
|
|
33
33
|
nil
|
34
34
|
end
|
35
35
|
|
36
|
+
def success_message
|
37
|
+
nil # lazily calcucalted in DefaultNotification
|
38
|
+
end
|
39
|
+
|
40
|
+
def error_message
|
41
|
+
nil # lazily calcucalted in DefaultNotification
|
42
|
+
end
|
43
|
+
|
36
44
|
private
|
37
45
|
|
38
46
|
attr_reader :method, :object
|
data/lib/crud_responder.rb
CHANGED
@@ -1,29 +1,32 @@
|
|
1
|
+
require 'action_view/helpers/text_helper'
|
2
|
+
|
1
3
|
require 'crud_responder/version'
|
2
4
|
require 'crud_responder/default_options'
|
3
|
-
require '
|
5
|
+
require 'crud_responder/default_notification'
|
6
|
+
require 'crud_responder/caller_extractor'
|
4
7
|
|
5
8
|
module CrudResponder
|
6
9
|
include ActionView::Helpers::TextHelper
|
7
10
|
|
8
|
-
protected
|
9
|
-
|
10
11
|
def crud_respond(object, opts = {})
|
11
|
-
method = opts.fetch(:method,
|
12
|
+
method = opts.fetch(:method, CallerExtractor.new(caller).method)
|
12
13
|
options = final_options(opts, method, object)
|
13
|
-
if
|
14
|
-
success(options)
|
14
|
+
if object.public_send(method)
|
15
|
+
success(options, object, caller)
|
15
16
|
else
|
16
|
-
error(options)
|
17
|
+
error(options, object, caller)
|
17
18
|
end
|
18
19
|
end
|
19
20
|
|
20
21
|
private
|
21
22
|
|
22
|
-
def success(options)
|
23
|
+
def success(options, object, kaller)
|
24
|
+
flash_success(options[:success_message] || DefaultNotification.new(object, kaller).text(true))
|
23
25
|
redirect_to options[:success_url]
|
24
26
|
end
|
25
27
|
|
26
|
-
def error(options)
|
28
|
+
def error(options, object, kaller)
|
29
|
+
flash_error(options[:error_message] || DefaultNotification.new(object, kaller).text(false))
|
27
30
|
if options[:error_url]
|
28
31
|
redirect_to options[:error_url]
|
29
32
|
else
|
@@ -56,46 +59,6 @@ module CrudResponder
|
|
56
59
|
result.fetch(opt, nil)
|
57
60
|
end
|
58
61
|
|
59
|
-
def caller_name(kaller)
|
60
|
-
kaller[0][/`.*'/][1..-2]
|
61
|
-
end
|
62
|
-
|
63
|
-
def method_by_caller(kaller)
|
64
|
-
if caller_name(kaller) =~ /destroy/
|
65
|
-
:destroy
|
66
|
-
else
|
67
|
-
:save
|
68
|
-
end
|
69
|
-
end
|
70
|
-
|
71
|
-
def perform(kaller, object, method)
|
72
|
-
ok = object.public_send(method)
|
73
|
-
t_key = "flash.actions.#{action_by_caller(kaller)}.#{ok ? 'notice' : 'alert'}"
|
74
|
-
if ok
|
75
|
-
flash_success I18n.t(t_key, resource_name: resource_name_by_object(object), resource_desc: object.to_s)
|
76
|
-
else
|
77
|
-
flash_error I18n.t(t_key, resource_name: resource_name_by_object(object), resource_desc: object.to_s, errors: object.errors.full_messages.to_sentence)
|
78
|
-
end
|
79
|
-
ok
|
80
|
-
end
|
81
|
-
|
82
|
-
def action_by_caller(kaller)
|
83
|
-
case caller_name(kaller)
|
84
|
-
when /destroy/
|
85
|
-
:destroy
|
86
|
-
when /update/
|
87
|
-
:update
|
88
|
-
when /create/
|
89
|
-
:create
|
90
|
-
else
|
91
|
-
"unknown_action_from_#{caller_name(kaller)}".to_sym
|
92
|
-
end
|
93
|
-
end
|
94
|
-
|
95
|
-
def resource_name_by_object(object)
|
96
|
-
object.try(:model_name).try(:human) || object.class.name
|
97
|
-
end
|
98
|
-
|
99
62
|
def flash_success(msg)
|
100
63
|
flash[:notice] = truncate_message(msg)
|
101
64
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: crud_responder
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Oleg Antonyan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-05-19 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -135,6 +135,8 @@ files:
|
|
135
135
|
- config/locales/ru.yml
|
136
136
|
- crud_responder.gemspec
|
137
137
|
- lib/crud_responder.rb
|
138
|
+
- lib/crud_responder/caller_extractor.rb
|
139
|
+
- lib/crud_responder/default_notification.rb
|
138
140
|
- lib/crud_responder/default_options.rb
|
139
141
|
- lib/crud_responder/version.rb
|
140
142
|
- lib/generators/crud_responder/install_generator.rb
|