respond_for_helper 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.github/workflows/ci.yml +47 -0
- data/.gitignore +9 -0
- data/.rspec +4 -0
- data/CHANGELOG.md +5 -0
- data/Gemfile +3 -0
- data/LICENSE +21 -0
- data/README.md +205 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/gemfiles/rails50.gemfile +6 -0
- data/gemfiles/rails51.gemfile +6 -0
- data/gemfiles/rails52.gemfile +6 -0
- data/gemfiles/rails60.gemfile +5 -0
- data/gemfiles/rails61.gemfile +5 -0
- data/lib/respond_for_helper/config.rb +32 -0
- data/lib/respond_for_helper/controller.rb +23 -0
- data/lib/respond_for_helper/flashes/base.rb +47 -0
- data/lib/respond_for_helper/flashes/timestamp.rb +12 -0
- data/lib/respond_for_helper/formats/base.rb +40 -0
- data/lib/respond_for_helper/formats/html.rb +88 -0
- data/lib/respond_for_helper/formats/json.rb +53 -0
- data/lib/respond_for_helper/helper.rb +7 -0
- data/lib/respond_for_helper/locales/en.yml +20 -0
- data/lib/respond_for_helper/locales/ja.yml +20 -0
- data/lib/respond_for_helper/railtie.rb +17 -0
- data/lib/respond_for_helper/version.rb +5 -0
- data/lib/respond_for_helper.rb +25 -0
- data/respond_for_helper.gemspec +27 -0
- metadata +155 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: f7f17979b8269d9fad0e710e0f593e6688477368e32a8d229f4ca8aeda7058e9
|
4
|
+
data.tar.gz: 7995b1e3ae788d7332f4cf3885a78946ad1980731e89cb7a2658e88608aba35c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d87eb932d5b4b2e7f9cbe8a5d9630a7633c40827bd6596b907e5094d801f685ed86d43b895be7564702547edb10b4f87ea91e9a4a509ee6b174be0bb45244c12
|
7
|
+
data.tar.gz: 33a6df7945b5d6acd9cf5da7bb50b3376533eb3746c669f4a4e2f31ee25a8dbfde5fa04cb5b92ee269d840e622e24fd8de817c48897a3431d8fdaa58ba6241dd
|
@@ -0,0 +1,47 @@
|
|
1
|
+
name: CI
|
2
|
+
|
3
|
+
on: [push, pull_request]
|
4
|
+
|
5
|
+
jobs:
|
6
|
+
test:
|
7
|
+
runs-on: ubuntu-20.04
|
8
|
+
strategy:
|
9
|
+
fail-fast: false
|
10
|
+
matrix:
|
11
|
+
ruby: [2.3, 2.4, 2.5, 2.6, 2.7, 3.0]
|
12
|
+
gemfile: ['rails50', 'rails51', 'rails52', 'rails60', 'rails61']
|
13
|
+
exclude:
|
14
|
+
- ruby: 2.3
|
15
|
+
gemfile: rails60
|
16
|
+
- ruby: 2.3
|
17
|
+
gemfile: rails61
|
18
|
+
- ruby: 2.4
|
19
|
+
gemfile: rails60
|
20
|
+
- ruby: 2.4
|
21
|
+
gemfile: rails61
|
22
|
+
- ruby: 3.0
|
23
|
+
gemfile: rails50
|
24
|
+
- ruby: 3.0
|
25
|
+
gemfile: rails51
|
26
|
+
- ruby: 3.0
|
27
|
+
gemfile: rails52
|
28
|
+
|
29
|
+
name: ruby ${{ matrix.ruby }}, ${{ matrix.gemfile }}
|
30
|
+
|
31
|
+
env:
|
32
|
+
BUNDLE_GEMFILE: gemfiles/${{ matrix.gemfile }}.gemfile
|
33
|
+
|
34
|
+
steps:
|
35
|
+
- uses: actions/checkout@v2
|
36
|
+
- uses: ruby/setup-ruby@v1
|
37
|
+
with:
|
38
|
+
ruby-version: ${{ matrix.ruby }}
|
39
|
+
bundler-cache: true
|
40
|
+
- name: Prepare test
|
41
|
+
run: |
|
42
|
+
cd spec/dummy
|
43
|
+
BUNDLE_GEMFILE=../../${{ env.BUNDLE_GEMFILE }} RAILS_ENV=test bundle exec rake db:create db:migrate db:seed
|
44
|
+
cd ../..
|
45
|
+
- name: Run test
|
46
|
+
run: |
|
47
|
+
DEBUG=1 bundle exec rspec
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2021 Yoshikazu Kaneta
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,205 @@
|
|
1
|
+
# RespondForHelper
|
2
|
+
|
3
|
+
A rails helper for responding request format.
|
4
|
+
|
5
|
+
## Dependencies
|
6
|
+
|
7
|
+
* ruby 2.3+
|
8
|
+
* rails 5.0+
|
9
|
+
|
10
|
+
## Installation
|
11
|
+
|
12
|
+
Add this line to your application's Gemfile:
|
13
|
+
|
14
|
+
```ruby
|
15
|
+
gem 'respond_for_helper'
|
16
|
+
```
|
17
|
+
|
18
|
+
Then execute:
|
19
|
+
|
20
|
+
$ bundle
|
21
|
+
|
22
|
+
## Usage
|
23
|
+
|
24
|
+
This gem adds `respond_for` method in your controller.
|
25
|
+
`respond_for` performs default behaviour such as template rendering or redirection for each action.
|
26
|
+
Default behaviour is like `respond_to` which is generated by scaffold of rails.
|
27
|
+
For example:
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
class ItemsController < ActionController::Base
|
31
|
+
def create
|
32
|
+
@item = Item.new
|
33
|
+
@item.save
|
34
|
+
respond_for @item
|
35
|
+
end
|
36
|
+
end
|
37
|
+
```
|
38
|
+
|
39
|
+
this action is succeeded when `@item.errors` is empty, otherwise this action is failed.
|
40
|
+
This action returns html or json corresponding to the request format.
|
41
|
+
|
42
|
+
`respond_for` allows some options as follows:
|
43
|
+
|
44
|
+
```ruby
|
45
|
+
# Specify redirect location when succeeded.
|
46
|
+
# The value should be one of string, symbol (action name) or proc.
|
47
|
+
respond_for @item, location: url_for(@item)
|
48
|
+
|
49
|
+
# Specify template when failed.
|
50
|
+
# The value should be one of symbol (action name) or proc.
|
51
|
+
respond_for @item, failure_template: :some_template
|
52
|
+
|
53
|
+
# Specify notice message when succeeded.
|
54
|
+
respond_for @item, notice: 'Create was succceeded'
|
55
|
+
|
56
|
+
# Specify alert message when failed.
|
57
|
+
respond_for @item, alert: 'Create was failed'
|
58
|
+
|
59
|
+
# Always success without checking error existence.
|
60
|
+
respond_for @item, success: true
|
61
|
+
|
62
|
+
# Block is called when succeeded.
|
63
|
+
respond_for @item do
|
64
|
+
puts "succeeded"
|
65
|
+
end
|
66
|
+
```
|
67
|
+
|
68
|
+
### Flash message
|
69
|
+
|
70
|
+
Flash message will be generated for each action automatically.
|
71
|
+
Its format is customizable by i18n translations.
|
72
|
+
Translations below shows the default english message:
|
73
|
+
|
74
|
+
```yaml
|
75
|
+
en:
|
76
|
+
respond_for:
|
77
|
+
format: "%{message}%{success_num}%{failure_num}%{timestamp}"
|
78
|
+
message:
|
79
|
+
defaults:
|
80
|
+
default:
|
81
|
+
notice: Succeeded
|
82
|
+
alert: Failed
|
83
|
+
create:
|
84
|
+
notice: Successfully created
|
85
|
+
alert: Failed to create
|
86
|
+
update:
|
87
|
+
notice: Successfully updated
|
88
|
+
alert: Failed to update
|
89
|
+
destroy:
|
90
|
+
notice: Successfully destroyed
|
91
|
+
alert: Failed to destroy
|
92
|
+
success_num: " (%{value} succeeded)"
|
93
|
+
failure_num: " (%{value} failed)"
|
94
|
+
timestamp: " (%{value})"
|
95
|
+
```
|
96
|
+
|
97
|
+
If you want to generate a flash message in your own context,
|
98
|
+
you can use `respond_for_message` as follows:
|
99
|
+
|
100
|
+
```ruby
|
101
|
+
class ItemsController < ActionController::Base
|
102
|
+
def create
|
103
|
+
respond_for_mesasge :notice
|
104
|
+
#=> Successfully created (2021-01-01 10:00)
|
105
|
+
|
106
|
+
respond_for_mesasge :notice, success_num: 10
|
107
|
+
#=> Successfully created (10 succeeded) (2021-01-01 10:00)
|
108
|
+
|
109
|
+
respond_for_mesasge :notice, action_name: :update
|
110
|
+
#=> Successfully updated (2021-01-01 10:00)
|
111
|
+
|
112
|
+
respond_for_mesasge :alert
|
113
|
+
#=> Failed to create (2021-01-01 10:00)
|
114
|
+
end
|
115
|
+
end
|
116
|
+
```
|
117
|
+
|
118
|
+
#### Controller-specific message
|
119
|
+
|
120
|
+
You can also define controller-specific message:
|
121
|
+
|
122
|
+
```yaml
|
123
|
+
en:
|
124
|
+
respond_for:
|
125
|
+
message:
|
126
|
+
items:
|
127
|
+
create:
|
128
|
+
notice: Item was Successfully created
|
129
|
+
alert: Item was failed to create
|
130
|
+
```
|
131
|
+
|
132
|
+
Note that `items` is a path of controller you want to define.
|
133
|
+
|
134
|
+
### Configurations
|
135
|
+
|
136
|
+
#### default behaviours
|
137
|
+
|
138
|
+
You can customize default behaviours for each action.
|
139
|
+
For example:
|
140
|
+
|
141
|
+
```ruby
|
142
|
+
RespondForHelper.configure do |config|
|
143
|
+
config.default_behaviours = {
|
144
|
+
template: {},
|
145
|
+
location: {},
|
146
|
+
failure_template: { create: :new, update: :edit, destroy: :show, _default: :show },
|
147
|
+
failure_location: { _default: :show }
|
148
|
+
}
|
149
|
+
end
|
150
|
+
```
|
151
|
+
|
152
|
+
`template` and `location` is used when current action is succeeded,
|
153
|
+
while `failure_template` and `failure_location` is used when current action is failed.
|
154
|
+
All of them contains action name mapping:
|
155
|
+
the key of mapping is a current action and the value of mapping is a next action after succeeded or failed.
|
156
|
+
`_default` is a common action which is applied to all of actions which have not specific mapping.
|
157
|
+
|
158
|
+
#### Controller-specific behaviours
|
159
|
+
|
160
|
+
You can also customize controller-specific behaviours:
|
161
|
+
|
162
|
+
```ruby
|
163
|
+
class ItemsController < ActionController::Base
|
164
|
+
self.respond_for_behaviours = {
|
165
|
+
location: { _default: :show }
|
166
|
+
}
|
167
|
+
end
|
168
|
+
```
|
169
|
+
|
170
|
+
The way of setting is same as `config.default_behaviours`.
|
171
|
+
|
172
|
+
#### Format processors
|
173
|
+
|
174
|
+
you can also set your own format processors as you like:
|
175
|
+
|
176
|
+
```ruby
|
177
|
+
RespondForHelper.configure do |config|
|
178
|
+
config.formats = {
|
179
|
+
html: RespondForHelper::Formats::Html,
|
180
|
+
json: RespondForHelper::Formats::Json
|
181
|
+
}
|
182
|
+
config.flash = RespondForHelper::Flashes::Timestamp
|
183
|
+
end
|
184
|
+
```
|
185
|
+
|
186
|
+
Format processors should be extended by `RespondForHelper::Formats::Base`.
|
187
|
+
|
188
|
+
you can also set your own flash message generators:
|
189
|
+
|
190
|
+
```ruby
|
191
|
+
RespondForHelper.configure do |config|
|
192
|
+
config.flash = RespondForHelper::Flashes::Timestamp
|
193
|
+
end
|
194
|
+
```
|
195
|
+
|
196
|
+
Flash message generators should be extended by `RespondForHelper::Flashes::Base`.
|
197
|
+
|
198
|
+
|
199
|
+
## Contributing
|
200
|
+
|
201
|
+
Bug reports and pull requests are welcome at https://github.com/kanety/respond_for_helper.
|
202
|
+
|
203
|
+
## License
|
204
|
+
|
205
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "respond_for_helper"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
class Config
|
5
|
+
class_attribute :data
|
6
|
+
|
7
|
+
self.data = {
|
8
|
+
flash: RespondForHelper::Flashes::Timestamp,
|
9
|
+
formats: {
|
10
|
+
html: RespondForHelper::Formats::Html,
|
11
|
+
json: RespondForHelper::Formats::Json
|
12
|
+
},
|
13
|
+
default_behaviours: {
|
14
|
+
template: {},
|
15
|
+
location: {},
|
16
|
+
failure_template: { create: :new, update: :edit, destroy: :show, _default: :show },
|
17
|
+
failure_location: { _default: :show }
|
18
|
+
},
|
19
|
+
member_actions: [:show, :edit, :update, :destroy]
|
20
|
+
}
|
21
|
+
|
22
|
+
data.keys.each do |key|
|
23
|
+
define_singleton_method "#{key}" do
|
24
|
+
data[key]
|
25
|
+
end
|
26
|
+
|
27
|
+
define_singleton_method "#{key}=" do |val|
|
28
|
+
data[key] = val
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Controller
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
def respond_for(item, options = {})
|
8
|
+
klass = Config.formats[request.format.to_sym] || Config.formats[:html]
|
9
|
+
formatter = klass.new(self, item, options)
|
10
|
+
formatter.call
|
11
|
+
yield if block_given? && formatter.succeeded?
|
12
|
+
end
|
13
|
+
|
14
|
+
def respond_for_message(type, options = {})
|
15
|
+
options = options.reverse_merge(controller_path: controller_path, action_name: action_name)
|
16
|
+
Config.flash.new(type, options).call
|
17
|
+
end
|
18
|
+
|
19
|
+
included do
|
20
|
+
class_attribute :respond_for_behaviours
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +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
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Formats
|
5
|
+
class Base
|
6
|
+
attr_reader :controller
|
7
|
+
delegate :request, :respond_to, :render, :head, :redirect_to, :flash, :url_for, :action_name, to: :controller
|
8
|
+
|
9
|
+
def initialize(controller, item, options)
|
10
|
+
@controller = controller
|
11
|
+
@item = item
|
12
|
+
@options = options
|
13
|
+
end
|
14
|
+
|
15
|
+
def call
|
16
|
+
respond do
|
17
|
+
if respond_to?("_#{action_name}", true)
|
18
|
+
send("_#{action_name}")
|
19
|
+
else
|
20
|
+
default_action
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def succeeded?
|
26
|
+
if @options.key?(:success)
|
27
|
+
@options[:success]
|
28
|
+
else
|
29
|
+
Array(@item).all? { |item| item.errors.blank? }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def respond
|
34
|
+
end
|
35
|
+
|
36
|
+
def default_action
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,88 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Formats
|
5
|
+
class Html < Base
|
6
|
+
def respond
|
7
|
+
respond_to do |format|
|
8
|
+
format.html { yield }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def _index
|
13
|
+
render @options[:template] || :index
|
14
|
+
end
|
15
|
+
|
16
|
+
def _show
|
17
|
+
render @options[:template] || :show
|
18
|
+
end
|
19
|
+
|
20
|
+
def default_action
|
21
|
+
if succeeded?
|
22
|
+
render_or_redirect(:template, :location, :notice)
|
23
|
+
else
|
24
|
+
render_or_redirect(:failure_template, :failure_location, :alert)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def render_or_redirect(template_key, location_key, type)
|
29
|
+
behaviour = resolve_behaviour(template_key, location_key)
|
30
|
+
if behaviour[0] == :template
|
31
|
+
message = resolve_flash(type)
|
32
|
+
flash.now[type] = message if message
|
33
|
+
render resolve_template(behaviour[1]), status: :unprocessable_entity
|
34
|
+
else
|
35
|
+
message = resolve_flash(type)
|
36
|
+
flash[type] = message if message
|
37
|
+
redirect_to resolve_location(behaviour[1])
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def resolve_flash(type)
|
42
|
+
return if request.xhr?
|
43
|
+
@controller.respond_for_message(type, @options)
|
44
|
+
end
|
45
|
+
|
46
|
+
def resolve_behaviour(template_key, location_key)
|
47
|
+
[ [:template, @options[template_key]],
|
48
|
+
[:location, @options[location_key]],
|
49
|
+
[:template, behaviour_config(template_key)],
|
50
|
+
[:location, behaviour_config(location_key)]
|
51
|
+
].each do |behaviour|
|
52
|
+
return behaviour if behaviour[1].present?
|
53
|
+
end
|
54
|
+
[:location, url_for(action: :index)]
|
55
|
+
end
|
56
|
+
|
57
|
+
def behaviour_config(key)
|
58
|
+
[@controller.class.respond_for_behaviours, Config.default_behaviours].each do |behaviours|
|
59
|
+
behaviour = behaviours&.dig(key, action_name.to_sym) || behaviours&.dig(key, :_default)
|
60
|
+
return behaviour if behaviour
|
61
|
+
end
|
62
|
+
return nil
|
63
|
+
end
|
64
|
+
|
65
|
+
def resolve_template(template)
|
66
|
+
if template.respond_to?(:call)
|
67
|
+
@controller.instance_exec(&template)
|
68
|
+
else
|
69
|
+
template
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def resolve_location(location)
|
74
|
+
if location.respond_to?(:call)
|
75
|
+
@controller.instance_exec(&location)
|
76
|
+
elsif location.is_a?(Symbol)
|
77
|
+
if location.in?(Config.member_actions)
|
78
|
+
url_for(action: location, id: @item)
|
79
|
+
else
|
80
|
+
url_for(action: location)
|
81
|
+
end
|
82
|
+
else
|
83
|
+
location
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
end
|
@@ -0,0 +1,53 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
module Formats
|
5
|
+
class Json < Base
|
6
|
+
def respond
|
7
|
+
respond_to do |format|
|
8
|
+
format.json { yield }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def _index
|
13
|
+
render json: @item
|
14
|
+
end
|
15
|
+
|
16
|
+
def _show
|
17
|
+
render json: @item
|
18
|
+
end
|
19
|
+
|
20
|
+
def _create
|
21
|
+
if succeeded?
|
22
|
+
render json: @item, status: :created
|
23
|
+
else
|
24
|
+
render json: @item.errors, status: :unprocessable_entity
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def _update
|
29
|
+
if succeeded?
|
30
|
+
render json: @item, status: :ok
|
31
|
+
else
|
32
|
+
render json: @item.errors, status: :unprocessable_entity
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def _destroy
|
37
|
+
if succeeded?
|
38
|
+
head :no_content
|
39
|
+
else
|
40
|
+
head :unprocessable_entity
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def default_action
|
45
|
+
if succeeded?
|
46
|
+
render json: @item, status: :ok
|
47
|
+
else
|
48
|
+
render json: @item.errors, status: :unprocessable_entity
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
en:
|
2
|
+
respond_for:
|
3
|
+
format: "%{message}%{success_num}%{failure_num}%{timestamp}"
|
4
|
+
message:
|
5
|
+
defaults:
|
6
|
+
default:
|
7
|
+
notice: Succeeded
|
8
|
+
alert: Failed
|
9
|
+
create:
|
10
|
+
notice: Successfully created
|
11
|
+
alert: Failed to create
|
12
|
+
update:
|
13
|
+
notice: Successfully updated
|
14
|
+
alert: Failed to update
|
15
|
+
destroy:
|
16
|
+
notice: Successfully destroyed
|
17
|
+
alert: Failed to destroy
|
18
|
+
success_num: " (%{value} succeeded)"
|
19
|
+
failure_num: " (%{value} failed)"
|
20
|
+
timestamp: " (%{value})"
|
@@ -0,0 +1,20 @@
|
|
1
|
+
ja:
|
2
|
+
respond_for:
|
3
|
+
format: "%{message}%{success_num}%{failure_num}%{timestamp}"
|
4
|
+
message:
|
5
|
+
defaults:
|
6
|
+
default:
|
7
|
+
notice: 処理が完了しました。
|
8
|
+
alert: 処理に失敗しました。
|
9
|
+
create:
|
10
|
+
notice: 登録処理が完了しました。
|
11
|
+
alert: 登録処理に失敗しました。
|
12
|
+
update:
|
13
|
+
notice: 更新処理が完了しました。
|
14
|
+
alert: 更新処理に失敗しました。
|
15
|
+
destroy:
|
16
|
+
notice: 削除処理が完了しました。
|
17
|
+
alert: 削除処理に失敗しました。
|
18
|
+
success_num: (%{value}件成功)
|
19
|
+
failure_num: (%{value}件失敗)
|
20
|
+
timestamp: (%{value})
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RespondForHelper
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
ActiveSupport.on_load :action_controller do
|
6
|
+
ActionController::Base.send :include, RespondForHelper::Controller
|
7
|
+
end
|
8
|
+
|
9
|
+
ActiveSupport.on_load :action_view do
|
10
|
+
ActionView::Base.send :include, RespondForHelper::Helper
|
11
|
+
end
|
12
|
+
|
13
|
+
ActiveSupport.on_load :i18n do
|
14
|
+
I18n.load_path += Dir[File.join(__dir__, 'locales', '*.yml')]
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'active_support/all'
|
4
|
+
|
5
|
+
require 'respond_for_helper/formats/base'
|
6
|
+
require 'respond_for_helper/formats/html'
|
7
|
+
require 'respond_for_helper/formats/json'
|
8
|
+
require 'respond_for_helper/flashes/base'
|
9
|
+
require 'respond_for_helper/flashes/timestamp'
|
10
|
+
require 'respond_for_helper/config'
|
11
|
+
require 'respond_for_helper/controller'
|
12
|
+
require 'respond_for_helper/helper'
|
13
|
+
require 'respond_for_helper/railtie' if defined?(Rails)
|
14
|
+
|
15
|
+
module RespondForHelper
|
16
|
+
class << self
|
17
|
+
def configure
|
18
|
+
yield Config
|
19
|
+
end
|
20
|
+
|
21
|
+
def config
|
22
|
+
Config
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'respond_for_helper/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "respond_for_helper"
|
8
|
+
spec.version = RespondForHelper::VERSION
|
9
|
+
spec.authors = ["Yoshikazu Kaneta"]
|
10
|
+
spec.email = ["kaneta@sitebridge.co.jp"]
|
11
|
+
spec.summary = %q{A rails helper for responding request format}
|
12
|
+
spec.description = %q{A rails helper for responding request format}
|
13
|
+
spec.homepage = "https://github.com/kanety/respond_for_helper"
|
14
|
+
|
15
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
16
|
+
spec.bindir = "exe"
|
17
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
18
|
+
spec.require_paths = ["lib"]
|
19
|
+
|
20
|
+
spec.add_dependency "rails", ">= 5.0"
|
21
|
+
spec.add_dependency "i18n"
|
22
|
+
|
23
|
+
spec.add_development_dependency "sqlite3"
|
24
|
+
spec.add_development_dependency "rake"
|
25
|
+
spec.add_development_dependency "rspec-rails"
|
26
|
+
spec.add_development_dependency "simplecov"
|
27
|
+
end
|
metadata
ADDED
@@ -0,0 +1,155 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: respond_for_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 1.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Yoshikazu Kaneta
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2021-09-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: i18n
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: sqlite3
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec-rails
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: simplecov
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: A rails helper for responding request format
|
98
|
+
email:
|
99
|
+
- kaneta@sitebridge.co.jp
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".github/workflows/ci.yml"
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- CHANGELOG.md
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- bin/console
|
113
|
+
- bin/setup
|
114
|
+
- gemfiles/rails50.gemfile
|
115
|
+
- gemfiles/rails51.gemfile
|
116
|
+
- gemfiles/rails52.gemfile
|
117
|
+
- gemfiles/rails60.gemfile
|
118
|
+
- gemfiles/rails61.gemfile
|
119
|
+
- lib/respond_for_helper.rb
|
120
|
+
- lib/respond_for_helper/config.rb
|
121
|
+
- lib/respond_for_helper/controller.rb
|
122
|
+
- lib/respond_for_helper/flashes/base.rb
|
123
|
+
- lib/respond_for_helper/flashes/timestamp.rb
|
124
|
+
- lib/respond_for_helper/formats/base.rb
|
125
|
+
- lib/respond_for_helper/formats/html.rb
|
126
|
+
- lib/respond_for_helper/formats/json.rb
|
127
|
+
- lib/respond_for_helper/helper.rb
|
128
|
+
- lib/respond_for_helper/locales/en.yml
|
129
|
+
- lib/respond_for_helper/locales/ja.yml
|
130
|
+
- lib/respond_for_helper/railtie.rb
|
131
|
+
- lib/respond_for_helper/version.rb
|
132
|
+
- respond_for_helper.gemspec
|
133
|
+
homepage: https://github.com/kanety/respond_for_helper
|
134
|
+
licenses: []
|
135
|
+
metadata: {}
|
136
|
+
post_install_message:
|
137
|
+
rdoc_options: []
|
138
|
+
require_paths:
|
139
|
+
- lib
|
140
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
141
|
+
requirements:
|
142
|
+
- - ">="
|
143
|
+
- !ruby/object:Gem::Version
|
144
|
+
version: '0'
|
145
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
146
|
+
requirements:
|
147
|
+
- - ">="
|
148
|
+
- !ruby/object:Gem::Version
|
149
|
+
version: '0'
|
150
|
+
requirements: []
|
151
|
+
rubygems_version: 3.1.2
|
152
|
+
signing_key:
|
153
|
+
specification_version: 4
|
154
|
+
summary: A rails helper for responding request format
|
155
|
+
test_files: []
|