responders 2.0.0 → 3.0.1

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.
Files changed (37) hide show
  1. checksums.yaml +5 -5
  2. data/CHANGELOG.md +54 -1
  3. data/MIT-LICENSE +2 -1
  4. data/README.md +117 -48
  5. data/lib/action_controller/respond_with.rb +44 -15
  6. data/lib/action_controller/responder.rb +33 -24
  7. data/lib/generators/rails/responders_controller_generator.rb +4 -26
  8. data/lib/generators/rails/templates/api_controller.rb.tt +51 -0
  9. data/lib/generators/rails/templates/controller.rb.tt +59 -0
  10. data/lib/generators/responders/install_generator.rb +10 -0
  11. data/lib/responders/collection_responder.rb +2 -0
  12. data/lib/responders/controller_method.rb +7 -2
  13. data/lib/responders/flash_responder.rb +32 -18
  14. data/lib/responders/http_cache_responder.rb +5 -3
  15. data/lib/responders/location_responder.rb +3 -1
  16. data/lib/responders/version.rb +3 -1
  17. data/lib/responders.rb +13 -16
  18. metadata +24 -52
  19. data/lib/generators/rails/templates/controller.rb +0 -55
  20. data/test/action_controller/respond_with_test.rb +0 -717
  21. data/test/locales/en.yml +0 -28
  22. data/test/responders/collection_responder_test.rb +0 -82
  23. data/test/responders/controller_method_test.rb +0 -72
  24. data/test/responders/flash_responder_test.rb +0 -260
  25. data/test/responders/http_cache_responder_test.rb +0 -120
  26. data/test/test_helper.rb +0 -87
  27. data/test/views/addresses/create.js.erb +0 -1
  28. data/test/views/addresses/edit.html.erb +0 -1
  29. data/test/views/addresses/new.html.erb +0 -1
  30. data/test/views/locations/new.html.erb +0 -1
  31. data/test/views/respond_with/edit.html.erb +0 -1
  32. data/test/views/respond_with/new.html.erb +0 -1
  33. data/test/views/respond_with/respond_with_additional_params.html.erb +0 -0
  34. data/test/views/respond_with/using_invalid_resource_with_template.xml.erb +0 -1
  35. data/test/views/respond_with/using_options_with_template.xml.erb +0 -1
  36. data/test/views/respond_with/using_resource.js.erb +0 -1
  37. data/test/views/respond_with/using_resource_with_block.html.erb +0 -1
@@ -0,0 +1,59 @@
1
+ <% if namespaced? -%>
2
+ require_dependency "<%= namespaced_file_path %>/application_controller"
3
+
4
+ <% end -%>
5
+ <% module_namespacing do -%>
6
+ class <%= controller_class_name %>Controller < ApplicationController
7
+ before_action :set_<%= singular_table_name %>, only: [:show, :edit, :update, :destroy]
8
+
9
+ respond_to :html
10
+
11
+ <% unless options[:singleton] -%>
12
+ def index
13
+ @<%= plural_table_name %> = <%= orm_class.all(class_name) %>
14
+ respond_with(@<%= plural_table_name %>)
15
+ end
16
+ <% end -%>
17
+
18
+ def show
19
+ respond_with(@<%= singular_table_name %>)
20
+ end
21
+
22
+ def new
23
+ @<%= singular_table_name %> = <%= orm_class.build(class_name) %>
24
+ respond_with(@<%= singular_table_name %>)
25
+ end
26
+
27
+ def edit
28
+ end
29
+
30
+ def create
31
+ @<%= singular_table_name %> = <%= orm_class.build(class_name, attributes_params) %>
32
+ <%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
33
+ respond_with(@<%= singular_table_name %>)
34
+ end
35
+
36
+ def update
37
+ <%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance.update(attributes_params) %>
38
+ respond_with(@<%= singular_table_name %>)
39
+ end
40
+
41
+ def destroy
42
+ @<%= orm_instance.destroy %>
43
+ respond_with(@<%= singular_table_name %>)
44
+ end
45
+
46
+ private
47
+ def set_<%= singular_table_name %>
48
+ @<%= singular_table_name %> = <%= orm_class.find(class_name, "params[:id]") %>
49
+ end
50
+
51
+ def <%= "#{singular_table_name}_params" %>
52
+ <%- if attributes_names.empty? -%>
53
+ params[:<%= singular_table_name %>]
54
+ <%- else -%>
55
+ params.require(:<%= singular_table_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
56
+ <%- end -%>
57
+ end
58
+ end
59
+ <% end -%>
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Responders
2
4
  module Generators
3
5
  class InstallGenerator < Rails::Generators::Base
@@ -18,6 +20,14 @@ end
18
20
  RUBY
19
21
  end
20
22
 
23
+ def update_application
24
+ inject_into_class "config/application.rb", "Application", <<-RUBY
25
+ # Use the responders controller from the responders gem
26
+ config.app_generators.scaffold_controller :responders_controller
27
+
28
+ RUBY
29
+ end
30
+
21
31
  def update_application_controller
22
32
  prepend_file "app/controllers/application_controller.rb", %{require "application_responder"\n\n}
23
33
  inject_into_class "app/controllers/application_controller.rb", "ApplicationController", <<-RUBY
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Responders
2
4
  # This responder modifies your current responder to redirect
3
5
  # to the collection page on POST/PUT/DELETE.
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Responders
2
4
  module ControllerMethod
3
5
  # Adds the given responders to the current controller's responder, allowing you to cherry-pick
@@ -18,7 +20,8 @@ module Responders
18
20
  #
19
21
  def responders(*responders)
20
22
  self.responder = responders.inject(Class.new(responder)) do |klass, responder|
21
- responder = case responder
23
+ responder = \
24
+ case responder
22
25
  when Module
23
26
  responder
24
27
  when String, Symbol
@@ -34,4 +37,6 @@ module Responders
34
37
  end
35
38
  end
36
39
 
37
- ActionController::Base.extend Responders::ControllerMethod
40
+ ActiveSupport.on_load(:action_controller) do
41
+ ActionController::Base.extend Responders::ControllerMethod
42
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Responders
2
4
  # Responder to automatically set flash messages based on I18n API. It checks for
3
5
  # message based on the current action, but also allows defaults to be set, using
@@ -33,9 +35,9 @@ module Responders
33
35
  # notice: "Hooray! You just tuned your %{car_brand}!"
34
36
  #
35
37
  # Since :car_name is not available for interpolation by default, you have
36
- # to overwrite interpolation_options in your controller.
38
+ # to overwrite `flash_interpolation_options` in your controller.
37
39
  #
38
- # def interpolation_options
40
+ # def flash_interpolation_options
39
41
  # { :car_brand => @car.brand }
40
42
  # end
41
43
  #
@@ -94,7 +96,7 @@ module Responders
94
96
  ActionView::Helpers::TagHelper
95
97
  )
96
98
 
97
- def initialize(controller, resources, options={})
99
+ def initialize(controller, resources, options = {})
98
100
  super
99
101
  @flash = options.delete(:flash)
100
102
  @notice = options.delete(:notice)
@@ -126,7 +128,7 @@ module Responders
126
128
  return if controller.flash[status].present?
127
129
 
128
130
  options = mount_i18n_options(status)
129
- message = Responders::FlashResponder.helper.t options[:default].shift, options
131
+ message = Responders::FlashResponder.helper.t options[:default].shift, **options
130
132
  set_flash(status, message)
131
133
  end
132
134
 
@@ -147,34 +149,46 @@ module Responders
147
149
  end
148
150
 
149
151
  def mount_i18n_options(status) #:nodoc:
150
- resource_name = if resource.class.respond_to?(:model_name)
151
- resource.class.model_name.human
152
- else
153
- resource.class.name.underscore.humanize
154
- end
155
-
156
152
  options = {
157
- :default => flash_defaults_by_namespace(status),
158
- :resource_name => resource_name,
159
- :downcase_resource_name => resource_name.downcase
153
+ default: flash_defaults_by_namespace(status),
154
+ resource_name: resource_name,
155
+ downcase_resource_name: resource_name.downcase
160
156
  }
161
157
 
162
- if controller.respond_to?(:interpolation_options, true)
163
- options.merge!(controller.send(:interpolation_options))
158
+ controller_options = controller_interpolation_options
159
+ if controller_options
160
+ options.merge!(controller_options)
164
161
  end
165
162
 
166
163
  options
167
164
  end
168
165
 
166
+ def controller_interpolation_options
167
+ if controller.respond_to?(:flash_interpolation_options, true)
168
+ controller.send(:flash_interpolation_options)
169
+ elsif controller.respond_to?(:interpolation_options, true)
170
+ ActiveSupport::Deprecation.warn("[responders] `#{controller.class}#interpolation_options` is deprecated, please rename it to `flash_interpolation_options`.")
171
+ controller.send(:interpolation_options)
172
+ end
173
+ end
174
+
175
+ def resource_name
176
+ if resource.class.respond_to?(:model_name)
177
+ resource.class.model_name.human
178
+ else
179
+ resource.class.name.underscore.humanize
180
+ end
181
+ end
182
+
169
183
  def flash_defaults_by_namespace(status) #:nodoc:
170
184
  defaults = []
171
- slices = controller.controller_path.split('/')
185
+ slices = controller.controller_path.split("/")
172
186
  lookup = Responders::FlashResponder.namespace_lookup
173
187
 
174
188
  begin
175
- controller_scope = :"flash.#{slices.fill(controller.controller_name, -1).join('.')}.#{controller.action_name}.#{status}"
189
+ controller_scope = :"flash.#{slices.fill(controller.controller_name, -1).join(".")}.#{controller.action_name}.#{status}"
176
190
 
177
- actions_scope = lookup ? slices.fill('actions', -1).join('.') : :actions
191
+ actions_scope = lookup ? slices.fill("actions", -1).join(".") : :actions
178
192
  actions_scope = :"flash.#{actions_scope}.#{controller.action_name}.#{status}"
179
193
 
180
194
  defaults << :"#{controller_scope}_html"
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Responders
2
4
  # Set HTTP Last-Modified headers based on the given resource. It's used only
3
5
  # on API behavior (to_format) and is useful for a client to check in the server
@@ -9,7 +11,7 @@ module Responders
9
11
  # the digest of the body.
10
12
  #
11
13
  module HttpCacheResponder
12
- def initialize(controller, resources, options={})
14
+ def initialize(controller, resources, options = {})
13
15
  super
14
16
  @http_cache = options.delete(:http_cache)
15
17
  end
@@ -34,11 +36,11 @@ module Responders
34
36
 
35
37
  def do_http_cache?
36
38
  get? && @http_cache != false && ActionController::Base.perform_caching &&
37
- persisted? && resourceful? && resource.respond_to?(:updated_at)
39
+ persisted? && resource.respond_to?(:updated_at)
38
40
  end
39
41
 
40
42
  def persisted?
41
43
  resource.respond_to?(:persisted?) ? resource.persisted? : true
42
44
  end
43
45
  end
44
- end
46
+ end
@@ -1,7 +1,9 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Responders
2
4
  module LocationResponder
3
5
  def self.included(_base)
4
- ActiveSupport::Deprecation.warn "Responders::LocationResponder is enabled by default, "
6
+ ActiveSupport::Deprecation.warn "Responders::LocationResponder is enabled by default, " \
5
7
  "no need to include it", caller
6
8
  end
7
9
  end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Responders
2
- VERSION = "2.0.0".freeze
4
+ VERSION = "3.0.1"
3
5
  end
data/lib/responders.rb CHANGED
@@ -1,32 +1,29 @@
1
- require 'action_controller/base'
1
+ # frozen_string_literal: true
2
+
3
+ require "action_controller"
4
+ require "rails/railtie"
2
5
 
3
6
  module ActionController
4
- autoload :Responder, 'action_controller/responder'
5
- autoload :RespondWith, 'action_controller/respond_with'
7
+ autoload :Responder, "action_controller/responder"
8
+ autoload :RespondWith, "action_controller/respond_with"
6
9
  end
7
10
 
8
11
  module Responders
9
- autoload :FlashResponder, 'responders/flash_responder'
10
- autoload :HttpCacheResponder, 'responders/http_cache_responder'
11
- autoload :CollectionResponder, 'responders/collection_responder'
12
- autoload :LocationResponder, 'responders/location_responder'
12
+ autoload :FlashResponder, "responders/flash_responder"
13
+ autoload :HttpCacheResponder, "responders/http_cache_responder"
14
+ autoload :CollectionResponder, "responders/collection_responder"
15
+ autoload :LocationResponder, "responders/location_responder"
13
16
 
14
- require 'responders/controller_method'
17
+ require "responders/controller_method"
15
18
 
16
19
  class Railtie < ::Rails::Railtie
17
20
  config.responders = ActiveSupport::OrderedOptions.new
18
21
  config.responders.flash_keys = [:notice, :alert]
19
22
  config.responders.namespace_lookup = false
20
23
 
21
- if config.respond_to?(:app_generators)
22
- config.app_generators.scaffold_controller = :responders_controller
23
- else
24
- config.generators.scaffold_controller = :responders_controller
25
- end
26
-
27
24
  # Add load paths straight to I18n, so engines and application can overwrite it.
28
- require 'active_support/i18n'
29
- I18n.load_path << File.expand_path('../responders/locales/en.yml', __FILE__)
25
+ require "active_support/i18n"
26
+ I18n.load_path << File.expand_path("../responders/locales/en.yml", __FILE__)
30
27
 
31
28
  initializer "responders.flash_responder" do |app|
32
29
  Responders::FlashResponder.flash_keys = app.config.responders.flash_keys
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: responders
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.0
4
+ version: 3.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - José Valim
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-17 00:00:00.000000000 Z
11
+ date: 2020-05-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: railties
@@ -16,22 +16,30 @@ dependencies:
16
16
  requirements:
17
17
  - - ">="
18
18
  - !ruby/object:Gem::Version
19
- version: 4.2.0.alpha
20
- - - "<"
21
- - !ruby/object:Gem::Version
22
- version: '5'
19
+ version: '5.0'
23
20
  type: :runtime
24
21
  prerelease: false
25
22
  version_requirements: !ruby/object:Gem::Requirement
26
23
  requirements:
27
24
  - - ">="
28
25
  - !ruby/object:Gem::Version
29
- version: 4.2.0.alpha
30
- - - "<"
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: actionpack
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '5.0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
31
39
  - !ruby/object:Gem::Version
32
- version: '5'
40
+ version: '5.0'
33
41
  description: A set of Rails responders to dry up your application
34
- email: contact@plataformatec.com.br
42
+ email: heartcombo@googlegroups.com
35
43
  executables: []
36
44
  extensions: []
37
45
  extra_rdoc_files: []
@@ -43,7 +51,8 @@ files:
43
51
  - lib/action_controller/responder.rb
44
52
  - lib/generators/rails/USAGE
45
53
  - lib/generators/rails/responders_controller_generator.rb
46
- - lib/generators/rails/templates/controller.rb
54
+ - lib/generators/rails/templates/api_controller.rb.tt
55
+ - lib/generators/rails/templates/controller.rb.tt
47
56
  - lib/generators/responders/install_generator.rb
48
57
  - lib/responders.rb
49
58
  - lib/responders/collection_responder.rb
@@ -53,25 +62,7 @@ files:
53
62
  - lib/responders/locales/en.yml
54
63
  - lib/responders/location_responder.rb
55
64
  - lib/responders/version.rb
56
- - test/action_controller/respond_with_test.rb
57
- - test/locales/en.yml
58
- - test/responders/collection_responder_test.rb
59
- - test/responders/controller_method_test.rb
60
- - test/responders/flash_responder_test.rb
61
- - test/responders/http_cache_responder_test.rb
62
- - test/test_helper.rb
63
- - test/views/addresses/create.js.erb
64
- - test/views/addresses/edit.html.erb
65
- - test/views/addresses/new.html.erb
66
- - test/views/locations/new.html.erb
67
- - test/views/respond_with/edit.html.erb
68
- - test/views/respond_with/new.html.erb
69
- - test/views/respond_with/respond_with_additional_params.html.erb
70
- - test/views/respond_with/using_invalid_resource_with_template.xml.erb
71
- - test/views/respond_with/using_options_with_template.xml.erb
72
- - test/views/respond_with/using_resource.js.erb
73
- - test/views/respond_with/using_resource_with_block.html.erb
74
- homepage: http://github.com/plataformatec/responders
65
+ homepage: https://github.com/heartcombo/responders
75
66
  licenses:
76
67
  - MIT
77
68
  metadata: {}
@@ -83,34 +74,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
83
74
  requirements:
84
75
  - - ">="
85
76
  - !ruby/object:Gem::Version
86
- version: '0'
77
+ version: 2.4.0
87
78
  required_rubygems_version: !ruby/object:Gem::Requirement
88
79
  requirements:
89
80
  - - ">="
90
81
  - !ruby/object:Gem::Version
91
82
  version: '0'
92
83
  requirements: []
93
- rubyforge_project: responders
94
- rubygems_version: 2.2.2
84
+ rubygems_version: 3.1.2
95
85
  signing_key:
96
86
  specification_version: 4
97
87
  summary: A set of Rails responders to dry up your application
98
- test_files:
99
- - test/action_controller/respond_with_test.rb
100
- - test/locales/en.yml
101
- - test/responders/collection_responder_test.rb
102
- - test/responders/controller_method_test.rb
103
- - test/responders/flash_responder_test.rb
104
- - test/responders/http_cache_responder_test.rb
105
- - test/test_helper.rb
106
- - test/views/addresses/create.js.erb
107
- - test/views/addresses/edit.html.erb
108
- - test/views/addresses/new.html.erb
109
- - test/views/locations/new.html.erb
110
- - test/views/respond_with/edit.html.erb
111
- - test/views/respond_with/new.html.erb
112
- - test/views/respond_with/respond_with_additional_params.html.erb
113
- - test/views/respond_with/using_invalid_resource_with_template.xml.erb
114
- - test/views/respond_with/using_options_with_template.xml.erb
115
- - test/views/respond_with/using_resource.js.erb
116
- - test/views/respond_with/using_resource_with_block.html.erb
88
+ test_files: []
@@ -1,55 +0,0 @@
1
- <% module_namespacing do -%>
2
- class <%= controller_class_name %>Controller < ApplicationController
3
- <%= controller_before_filter %> :set_<%= file_name %>, only: [:show, :edit, :update, :destroy]
4
-
5
- <% unless options[:singleton] -%>
6
- def index
7
- @<%= table_name %> = <%= orm_class.all(class_name) %>
8
- respond_with(@<%= table_name %>)
9
- end
10
- <% end -%>
11
-
12
- def show
13
- respond_with(@<%= file_name %>)
14
- end
15
-
16
- def new
17
- @<%= file_name %> = <%= orm_class.build(class_name) %>
18
- respond_with(@<%= file_name %>)
19
- end
20
-
21
- def edit
22
- end
23
-
24
- def create
25
- @<%= file_name %> = <%= orm_class.build(class_name, attributes_params) %>
26
- <%= "flash[:notice] = '#{class_name} was successfully created.' if " if flash? %>@<%= orm_instance.save %>
27
- respond_with(@<%= file_name %>)
28
- end
29
-
30
- def update
31
- <%= "flash[:notice] = '#{class_name} was successfully updated.' if " if flash? %>@<%= orm_instance_update(attributes_params) %>
32
- respond_with(@<%= file_name %>)
33
- end
34
-
35
- def destroy
36
- @<%= orm_instance.destroy %>
37
- respond_with(@<%= file_name %>)
38
- end
39
-
40
- private
41
- def set_<%= file_name %>
42
- @<%= file_name %> = <%= orm_class.find(class_name, "params[:id]") %>
43
- end
44
- <%- if strong_parameters_defined? -%>
45
-
46
- def <%= "#{file_name}_params" %>
47
- <%- if attributes_names.empty? -%>
48
- params[:<%= file_name %>]
49
- <%- else -%>
50
- params.require(:<%= file_name %>).permit(<%= attributes_names.map { |name| ":#{name}" }.join(', ') %>)
51
- <%- end -%>
52
- end
53
- <%- end -%>
54
- end
55
- <% end -%>