respond_for_helper 1.1.3 → 1.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +5 -0
- data/README.md +10 -3
- data/lib/respond_for_helper/behaviour.rb +9 -1
- data/lib/respond_for_helper/flashes/base.rb +47 -47
- data/lib/respond_for_helper/formats/base.rb +61 -61
- data/lib/respond_for_helper/formats/html.rb +55 -44
- data/lib/respond_for_helper/formats/json.rb +31 -31
- data/lib/respond_for_helper/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9fd3094966984698be9e2a6dee739ac76fe42e610c8073831d5ae31e6a3df84c
|
4
|
+
data.tar.gz: 92883a30c2c987214cdd876cc1eb506df21731f6ef3231df14c5e93c86c4df45
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6808c53b8b8897ef912ac647e021964242f0b54e1cffbd176cf5679b2e61436015ed8f7f581a0962c1d8f4d5c2c9e06a492e0c9f100222076e486a2743918f74
|
7
|
+
data.tar.gz: e0fcdafe477eb29958a0ca29b46b8e463820828da1daacf3d69b2bf8441c60a4a4fb369d55436e7cdaa46dc6c57a8e7599835489b3f8eeeb14bcf1e6df548ef1
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -54,18 +54,25 @@ respond_for @item, html: true, json: true, any: false
|
|
54
54
|
|
55
55
|
```ruby
|
56
56
|
# Specify redirect location when succeeded.
|
57
|
-
# The value should be one of
|
57
|
+
# The value should be one of symbol (action name), string or proc.
|
58
|
+
respond_for @item, location: :index
|
58
59
|
respond_for @item, location: url_for(@item)
|
60
|
+
respond_for @item, location: -> { url_for(@item) }
|
59
61
|
|
60
62
|
# Specify template when failed.
|
61
63
|
# The value should be one of symbol (action name) or proc.
|
62
64
|
respond_for @item, failure_template: :some_template
|
65
|
+
respond_for @item, failure_template: -> { :some_template }
|
63
66
|
|
64
|
-
# Specify notice message when succeeded.
|
67
|
+
# Specify notice message when succeeded by symbol, string or proc.
|
68
|
+
respond_for @item, notice: :notice
|
65
69
|
respond_for @item, notice: 'Create was succceeded'
|
70
|
+
respond_for @item, notice: -> { 'Create was succceeded' }
|
66
71
|
|
67
|
-
# Specify alert message when failed.
|
72
|
+
# Specify alert message when failed by symbol, string or proc.
|
73
|
+
respond_for @item, alert: :alert
|
68
74
|
respond_for @item, alert: 'Create was failed'
|
75
|
+
respond_for @item, alert: -> { 'Create was failed' }
|
69
76
|
```
|
70
77
|
|
71
78
|
`respond_for` also supports block like `respond_to`:
|
@@ -4,12 +4,13 @@ module RespondForHelper
|
|
4
4
|
class Behaviour
|
5
5
|
TYPES = [:render, :redirect, :head]
|
6
6
|
|
7
|
-
attr_accessor :type, :target, :flash, :options
|
7
|
+
attr_accessor :type, :target, :flash, :flash_message, :options
|
8
8
|
|
9
9
|
def initialize(attrs)
|
10
10
|
@type = attrs.delete(:type)
|
11
11
|
@target = attrs.delete(:target)
|
12
12
|
@flash = attrs.delete(:flash)
|
13
|
+
@flash_message = attrs.delete(:flash_message)
|
13
14
|
@options = attrs
|
14
15
|
end
|
15
16
|
|
@@ -46,6 +47,7 @@ module RespondForHelper
|
|
46
47
|
|
47
48
|
if @format == :html
|
48
49
|
merge_html_options(behaviour)
|
50
|
+
merge_flash_options(behaviour)
|
49
51
|
end
|
50
52
|
|
51
53
|
behaviour
|
@@ -110,6 +112,12 @@ module RespondForHelper
|
|
110
112
|
end
|
111
113
|
end
|
112
114
|
|
115
|
+
def merge_flash_options(behaviour)
|
116
|
+
if @options[behaviour.flash]
|
117
|
+
behaviour.flash_message = @options[behaviour.flash]
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
113
121
|
def change_to_render(behaviour)
|
114
122
|
behaviour.type = :render
|
115
123
|
behaviour.options[:status] = @result == :success ? :ok : :unprocessable_entity
|
@@ -1,47 +1,47 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RespondForHelper
|
4
|
-
module Flashes
|
5
|
-
class Base
|
6
|
-
def initialize(type, options = {})
|
7
|
-
@type = type
|
8
|
-
@options = options
|
9
|
-
end
|
10
|
-
|
11
|
-
def call
|
12
|
-
I18n.t("respond_for.format", **translations.merge(message: message))
|
13
|
-
end
|
14
|
-
|
15
|
-
private
|
16
|
-
|
17
|
-
def translations
|
18
|
-
format_keys.each_with_object({}) do |key, map|
|
19
|
-
map[key] = if @options[key]
|
20
|
-
I18n.t("respond_for.#{key}", value: @options[key], default: '')
|
21
|
-
else
|
22
|
-
''
|
23
|
-
end
|
24
|
-
end
|
25
|
-
end
|
26
|
-
|
27
|
-
def format_keys
|
28
|
-
I18n.t("respond_for.format").scan(/\%\{(\w+)\}/).flatten.map(&:to_sym)
|
29
|
-
end
|
30
|
-
|
31
|
-
def message
|
32
|
-
@options[@type] || I18n.t(i18n_path, default: i18n_extra_paths)
|
33
|
-
end
|
34
|
-
|
35
|
-
def i18n_path
|
36
|
-
"respond_for.message.#{@options[:controller_path]}.#{@options[:action_name]}.#{@type}"
|
37
|
-
end
|
38
|
-
|
39
|
-
def i18n_extra_paths
|
40
|
-
[ :"respond_for.message.defaults.#{@options[:action_name]}.#{@type}",
|
41
|
-
:"respond_for.message.#{@options[:controller_path]}.default.#{@type}",
|
42
|
-
:"respond_for.message.defaults.default.#{@type}",
|
43
|
-
'' ]
|
44
|
-
end
|
45
|
-
end
|
46
|
-
end
|
47
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Flashes
|
5
|
+
class Base
|
6
|
+
def initialize(type, options = {})
|
7
|
+
@type = type
|
8
|
+
@options = options
|
9
|
+
end
|
10
|
+
|
11
|
+
def call
|
12
|
+
I18n.t("respond_for.format", **translations.merge(message: message))
|
13
|
+
end
|
14
|
+
|
15
|
+
private
|
16
|
+
|
17
|
+
def translations
|
18
|
+
format_keys.each_with_object({}) do |key, map|
|
19
|
+
map[key] = if @options[key]
|
20
|
+
I18n.t("respond_for.#{key}", value: @options[key], default: '')
|
21
|
+
else
|
22
|
+
''
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def format_keys
|
28
|
+
I18n.t("respond_for.format").scan(/\%\{(\w+)\}/).flatten.map(&:to_sym)
|
29
|
+
end
|
30
|
+
|
31
|
+
def message
|
32
|
+
@options[@type] || I18n.t(i18n_path, default: i18n_extra_paths)
|
33
|
+
end
|
34
|
+
|
35
|
+
def i18n_path
|
36
|
+
"respond_for.message.#{@options[:controller_path]}.#{@options[:action_name]}.#{@type}"
|
37
|
+
end
|
38
|
+
|
39
|
+
def i18n_extra_paths
|
40
|
+
[ :"respond_for.message.defaults.#{@options[:action_name]}.#{@type}",
|
41
|
+
:"respond_for.message.#{@options[:controller_path]}.default.#{@type}",
|
42
|
+
:"respond_for.message.defaults.default.#{@type}",
|
43
|
+
'' ]
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -1,61 +1,61 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RespondForHelper
|
4
|
-
module Formats
|
5
|
-
class Base
|
6
|
-
class_attribute :format
|
7
|
-
|
8
|
-
attr_reader :controller, :item, :format, :result, :behaviour
|
9
|
-
delegate :request, :render, :head, :redirect_to, :flash, :url_for, :action_name, to: :controller
|
10
|
-
|
11
|
-
def initialize(controller, item, success, options)
|
12
|
-
@controller = controller
|
13
|
-
@item = item
|
14
|
-
@result = success ? :success : :failure
|
15
|
-
@options = options
|
16
|
-
|
17
|
-
@format = self.class.format.to_sym
|
18
|
-
@behaviour = Behaviours.new(@controller, @format, @result, @options).call
|
19
|
-
end
|
20
|
-
|
21
|
-
def call
|
22
|
-
if respond_to?("_#{action_name}", true)
|
23
|
-
send("_#{action_name}")
|
24
|
-
else
|
25
|
-
perform
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
private
|
30
|
-
|
31
|
-
def perform
|
32
|
-
end
|
33
|
-
|
34
|
-
def succeeded?
|
35
|
-
@result == :success
|
36
|
-
end
|
37
|
-
|
38
|
-
def resolve_target(target)
|
39
|
-
if target.respond_to?(:call)
|
40
|
-
@controller.instance_exec(&target)
|
41
|
-
elsif target.is_a?(Symbol)
|
42
|
-
if respond_to?(target)
|
43
|
-
send(target)
|
44
|
-
elsif @behaviour.redirect?
|
45
|
-
resolve_url(target)
|
46
|
-
else
|
47
|
-
target
|
48
|
-
end
|
49
|
-
else
|
50
|
-
target
|
51
|
-
end
|
52
|
-
end
|
53
|
-
|
54
|
-
def resolve_url(target)
|
55
|
-
url_for(action: target)
|
56
|
-
rescue ActionController::UrlGenerationError
|
57
|
-
url_for(action: target, id: @item)
|
58
|
-
end
|
59
|
-
end
|
60
|
-
end
|
61
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Formats
|
5
|
+
class Base
|
6
|
+
class_attribute :format
|
7
|
+
|
8
|
+
attr_reader :controller, :item, :format, :result, :behaviour
|
9
|
+
delegate :request, :render, :head, :redirect_to, :flash, :url_for, :action_name, to: :controller
|
10
|
+
|
11
|
+
def initialize(controller, item, success, options)
|
12
|
+
@controller = controller
|
13
|
+
@item = item
|
14
|
+
@result = success ? :success : :failure
|
15
|
+
@options = options
|
16
|
+
|
17
|
+
@format = self.class.format.to_sym
|
18
|
+
@behaviour = Behaviours.new(@controller, @format, @result, @options).call
|
19
|
+
end
|
20
|
+
|
21
|
+
def call
|
22
|
+
if respond_to?("_#{action_name}", true)
|
23
|
+
send("_#{action_name}")
|
24
|
+
else
|
25
|
+
perform
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def perform
|
32
|
+
end
|
33
|
+
|
34
|
+
def succeeded?
|
35
|
+
@result == :success
|
36
|
+
end
|
37
|
+
|
38
|
+
def resolve_target(target)
|
39
|
+
if target.respond_to?(:call)
|
40
|
+
@controller.instance_exec(&target)
|
41
|
+
elsif target.is_a?(Symbol)
|
42
|
+
if respond_to?(target)
|
43
|
+
send(target)
|
44
|
+
elsif @behaviour.redirect?
|
45
|
+
resolve_url(target)
|
46
|
+
else
|
47
|
+
target
|
48
|
+
end
|
49
|
+
else
|
50
|
+
target
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def resolve_url(target)
|
55
|
+
url_for(action: target)
|
56
|
+
rescue ActionController::UrlGenerationError
|
57
|
+
url_for(action: target, id: @item)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
@@ -1,44 +1,55 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RespondForHelper
|
4
|
-
module Formats
|
5
|
-
class Html < Base
|
6
|
-
self.format = :html
|
7
|
-
|
8
|
-
private
|
9
|
-
|
10
|
-
def perform
|
11
|
-
if @behaviour.flash? && !request.xhr?
|
12
|
-
perform_flash
|
13
|
-
end
|
14
|
-
|
15
|
-
if @behaviour.render?
|
16
|
-
perform_render
|
17
|
-
elsif @behaviour.redirect?
|
18
|
-
perform_redirect
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def perform_render
|
23
|
-
render resolve_target(@behaviour.target), @behaviour.options
|
24
|
-
end
|
25
|
-
|
26
|
-
def perform_redirect
|
27
|
-
redirect_to resolve_target(@behaviour.target), @behaviour.options
|
28
|
-
end
|
29
|
-
|
30
|
-
def perform_flash
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Formats
|
5
|
+
class Html < Base
|
6
|
+
self.format = :html
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def perform
|
11
|
+
if @behaviour.flash? && !request.xhr?
|
12
|
+
perform_flash
|
13
|
+
end
|
14
|
+
|
15
|
+
if @behaviour.render?
|
16
|
+
perform_render
|
17
|
+
elsif @behaviour.redirect?
|
18
|
+
perform_redirect
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def perform_render
|
23
|
+
render resolve_target(@behaviour.target), @behaviour.options
|
24
|
+
end
|
25
|
+
|
26
|
+
def perform_redirect
|
27
|
+
redirect_to resolve_target(@behaviour.target), @behaviour.options
|
28
|
+
end
|
29
|
+
|
30
|
+
def perform_flash
|
31
|
+
flash_content[@behaviour.flash] ||= flash_message
|
32
|
+
end
|
33
|
+
|
34
|
+
def flash_message
|
35
|
+
if @behaviour.flash_message.is_a?(Proc)
|
36
|
+
@controller.instance_exec(&@behaviour.flash_message)
|
37
|
+
elsif @behaviour.flash_message.is_a?(Symbol)
|
38
|
+
@controller.respond_for_message(@behaviour.flash_message, @options)
|
39
|
+
elsif @behaviour.flash_message.is_a?(String)
|
40
|
+
@behaviour.flash_message
|
41
|
+
else
|
42
|
+
@controller.respond_for_message(@behaviour.flash, @options)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
def flash_content
|
47
|
+
if @behaviour.render?
|
48
|
+
flash.now
|
49
|
+
elsif @behaviour.redirect?
|
50
|
+
flash
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
@@ -1,31 +1,31 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module RespondForHelper
|
4
|
-
module Formats
|
5
|
-
class Json < Base
|
6
|
-
self.format = :json
|
7
|
-
|
8
|
-
def item_errors
|
9
|
-
@item.errors
|
10
|
-
end
|
11
|
-
|
12
|
-
private
|
13
|
-
|
14
|
-
def perform
|
15
|
-
if @behaviour.render?
|
16
|
-
perform_render
|
17
|
-
elsif @behaviour.head?
|
18
|
-
perform_head
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
def perform_render
|
23
|
-
render @behaviour.options.merge(json: resolve_target(@behaviour.target))
|
24
|
-
end
|
25
|
-
|
26
|
-
def perform_head
|
27
|
-
head resolve_target(@behaviour.target), @behaviour.options
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
31
|
-
end
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Formats
|
5
|
+
class Json < Base
|
6
|
+
self.format = :json
|
7
|
+
|
8
|
+
def item_errors
|
9
|
+
@item.errors
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def perform
|
15
|
+
if @behaviour.render?
|
16
|
+
perform_render
|
17
|
+
elsif @behaviour.head?
|
18
|
+
perform_head
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
def perform_render
|
23
|
+
render @behaviour.options.merge(json: resolve_target(@behaviour.target))
|
24
|
+
end
|
25
|
+
|
26
|
+
def perform_head
|
27
|
+
head resolve_target(@behaviour.target), @behaviour.options
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: respond_for_helper
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Yoshikazu Kaneta
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-
|
11
|
+
date: 2024-08-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|