hanami-controller 1.3.0 → 2.0.0.alpha2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (48) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +83 -0
  3. data/LICENSE.md +1 -1
  4. data/README.md +297 -538
  5. data/hanami-controller.gemspec +6 -5
  6. data/lib/hanami/action.rb +129 -73
  7. data/lib/hanami/action/application_action.rb +111 -0
  8. data/lib/hanami/action/application_configuration.rb +92 -0
  9. data/lib/hanami/action/application_configuration/cookies.rb +29 -0
  10. data/lib/hanami/action/application_configuration/sessions.rb +46 -0
  11. data/lib/hanami/action/base_params.rb +2 -2
  12. data/lib/hanami/action/cache.rb +1 -139
  13. data/lib/hanami/action/cache/cache_control.rb +4 -4
  14. data/lib/hanami/action/cache/conditional_get.rb +7 -2
  15. data/lib/hanami/action/cache/directives.rb +1 -1
  16. data/lib/hanami/action/cache/expires.rb +3 -3
  17. data/lib/hanami/action/configuration.rb +430 -0
  18. data/lib/hanami/action/cookie_jar.rb +3 -3
  19. data/lib/hanami/action/cookies.rb +3 -62
  20. data/lib/hanami/action/csrf_protection.rb +214 -0
  21. data/lib/hanami/action/flash.rb +102 -207
  22. data/lib/hanami/action/glue.rb +5 -31
  23. data/lib/hanami/action/halt.rb +12 -0
  24. data/lib/hanami/action/mime.rb +78 -485
  25. data/lib/hanami/action/params.rb +3 -3
  26. data/lib/hanami/action/rack/file.rb +1 -1
  27. data/lib/hanami/action/request.rb +30 -20
  28. data/lib/hanami/action/response.rb +193 -0
  29. data/lib/hanami/action/session.rb +11 -128
  30. data/lib/hanami/action/standalone_action.rb +581 -0
  31. data/lib/hanami/action/validatable.rb +2 -2
  32. data/lib/hanami/action/view_name_inferrer.rb +46 -0
  33. data/lib/hanami/controller.rb +0 -227
  34. data/lib/hanami/controller/version.rb +1 -1
  35. data/lib/hanami/http/status.rb +2 -2
  36. metadata +47 -30
  37. data/lib/hanami-controller.rb +0 -1
  38. data/lib/hanami/action/callable.rb +0 -92
  39. data/lib/hanami/action/callbacks.rb +0 -214
  40. data/lib/hanami/action/configurable.rb +0 -50
  41. data/lib/hanami/action/exposable.rb +0 -126
  42. data/lib/hanami/action/exposable/guard.rb +0 -104
  43. data/lib/hanami/action/head.rb +0 -121
  44. data/lib/hanami/action/rack.rb +0 -399
  45. data/lib/hanami/action/rack/callable.rb +0 -47
  46. data/lib/hanami/action/redirect.rb +0 -59
  47. data/lib/hanami/action/throwable.rb +0 -196
  48. data/lib/hanami/controller/configuration.rb +0 -763
@@ -1,7 +1,7 @@
1
1
  require 'hanami/action/params'
2
2
 
3
3
  module Hanami
4
- module Action
4
+ class Action
5
5
  module Validatable
6
6
  # Defines the class name for anonymous params
7
7
  #
@@ -50,7 +50,7 @@ module Hanami
50
50
  # @since 0.3.0
51
51
  #
52
52
  # @see Hanami::Action::Params
53
- # @see http://hanamirb.org/guides/validations/overview/
53
+ # @see https://guides.hanamirb.org//validations/overview
54
54
  #
55
55
  # @example Anonymous Block
56
56
  # require 'hanami/controller'
@@ -0,0 +1,46 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hanami
4
+ class Action
5
+ class ViewNameInferrer
6
+ ALTERNATIVE_NAMES = {
7
+ "create" => "new",
8
+ "update" => "edit"
9
+ }.freeze
10
+
11
+ class << self
12
+ def call(action_name:, provider:)
13
+ application = provider.respond_to?(:application) ? provider.application : Hanami.application
14
+
15
+ action_identifier_base = application.config.actions.name_inference_base
16
+ view_identifier_base = application.config.actions.view_name_inference_base
17
+
18
+ identifier = action_identifier_name(action_name, provider, action_identifier_base)
19
+
20
+ view_name = [view_identifier_base, identifier].compact.join(".")
21
+
22
+ [view_name, alternative_view_name(view_name)].compact
23
+ end
24
+
25
+ private
26
+
27
+ def action_identifier_name(action_name, provider, name_base)
28
+ provider
29
+ .inflector
30
+ .underscore(action_name)
31
+ .sub(/^#{provider.namespace_path}\//, "")
32
+ .sub(/^#{name_base}\//, "")
33
+ .gsub("/", ".")
34
+ end
35
+
36
+ def alternative_view_name(view_name)
37
+ parts = view_name.split(".")
38
+
39
+ alternative_name = ALTERNATIVE_NAMES[parts.last]
40
+
41
+ [parts[0..-2], alternative_name].join(".") if alternative_name
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,6 +1,4 @@
1
- require 'hanami/utils/class_attribute'
2
1
  require 'hanami/action'
3
- require 'hanami/controller/configuration'
4
2
  require 'hanami/controller/version'
5
3
  require 'hanami/controller/error'
6
4
 
@@ -46,230 +44,5 @@ module Hanami
46
44
  super("Cannot find a corresponding Mime type for '#{ format }'. Please configure it with Hanami::Controller::Configuration#format.")
47
45
  end
48
46
  end
49
-
50
- # Missing session error
51
- #
52
- # This error is raised when an action sends either `session` or `flash` to
53
- # itself and it does not include `Hanami::Action::Session`.
54
- #
55
- # @since 1.2.0
56
- #
57
- # @see Hanami::Action::Session
58
- # @see Hanami::Action#session
59
- # @see Hanami::Action#flash
60
- class MissingSessionError < Hanami::Controller::Error
61
- def initialize(session_method)
62
- super("To use `#{session_method}', add `include Hanami::Action::Session`.")
63
- end
64
- end
65
-
66
- include Utils::ClassAttribute
67
-
68
- # Framework configuration
69
- #
70
- # @since 0.2.0
71
- # @api private
72
- class_attribute :configuration
73
- self.configuration = Configuration.new
74
-
75
- # Configure the framework.
76
- # It yields the given block in the context of the configuration
77
- #
78
- # @param blk [Proc] the configuration block
79
- #
80
- # @since 0.2.0
81
- #
82
- # @see Hanami::Controller::Configuration
83
- #
84
- # @example
85
- # require 'hanami/controller'
86
- #
87
- # Hanami::Controller.configure do
88
- # handle_exceptions false
89
- # end
90
- def self.configure(&blk)
91
- configuration.instance_eval(&blk)
92
- end
93
-
94
- # Duplicate Hanami::Controller in order to create a new separated instance
95
- # of the framework.
96
- #
97
- # The new instance of the framework will be completely decoupled from the
98
- # original. It will inherit the configuration, but all the changes that
99
- # happen after the duplication, won't be reflected on the other copies.
100
- #
101
- # @return [Module] a copy of Hanami::Controller
102
- #
103
- # @since 0.2.0
104
- # @api private
105
- #
106
- # @example Basic usage
107
- # require 'hanami/controller'
108
- #
109
- # module MyApp
110
- # Controller = Hanami::Controller.dupe
111
- # end
112
- #
113
- # MyApp::Controller == Hanami::Controller # => false
114
- #
115
- # MyApp::Controller.configuration ==
116
- # Hanami::Controller.configuration # => false
117
- #
118
- # @example Inheriting configuration
119
- # require 'hanami/controller'
120
- #
121
- # Hanami::Controller.configure do
122
- # handle_exceptions false
123
- # end
124
- #
125
- # module MyApp
126
- # Controller = Hanami::Controller.dupe
127
- # end
128
- #
129
- # module MyApi
130
- # Controller = Hanami::Controller.dupe
131
- # Controller.configure do
132
- # handle_exceptions true
133
- # end
134
- # end
135
- #
136
- # Hanami::Controller.configuration.handle_exceptions # => false
137
- # MyApp::Controller.configuration.handle_exceptions # => false
138
- # MyApi::Controller.configuration.handle_exceptions # => true
139
- def self.dupe
140
- dup.tap do |duplicated|
141
- duplicated.configuration = configuration.duplicate
142
- end
143
- end
144
-
145
- # Duplicate the framework and generate modules for the target application
146
- #
147
- # @param mod [Module] the Ruby namespace of the application
148
- # @param controllers [String] the optional namespace where the application's
149
- # controllers will live
150
- # @param blk [Proc] an optional block to configure the framework
151
- #
152
- # @return [Module] a copy of Hanami::Controller
153
- #
154
- # @since 0.2.0
155
- #
156
- # @see Hanami::Controller#dupe
157
- # @see Hanami::Controller::Configuration
158
- # @see Hanami::Controller::Configuration#action_module
159
- #
160
- # @example Basic usage
161
- # require 'hanami/controller'
162
- #
163
- # module MyApp
164
- # Controller = Hanami::Controller.duplicate(self)
165
- # end
166
- #
167
- # # It will:
168
- # #
169
- # # 1. Generate MyApp::Controller
170
- # # 2. Generate MyApp::Action
171
- # # 3. Generate MyApp::Controllers
172
- # # 4. Configure MyApp::Action as the default module for actions
173
- #
174
- # module MyApp::Controllers::Dashboard
175
- # include MyApp::Controller
176
- #
177
- # action 'Index' do # this will inject MyApp::Action
178
- # def call(params)
179
- # # ...
180
- # end
181
- # end
182
- # end
183
- #
184
- # @example Compare code
185
- # require 'hanami/controller'
186
- #
187
- # module MyApp
188
- # Controller = Hanami::Controller.duplicate(self) do
189
- # # ...
190
- # end
191
- # end
192
- #
193
- # # it's equivalent to:
194
- #
195
- # module MyApp
196
- # Controller = Hanami::Controller.dupe
197
- # Action = Hanami::Action.dup
198
- #
199
- # module Controllers
200
- # end
201
- #
202
- # Controller.configure do
203
- # action_module MyApp::Action
204
- # end
205
- #
206
- # Controller.configure do
207
- # # ...
208
- # end
209
- # end
210
- #
211
- # @example Custom controllers module
212
- # require 'hanami/controller'
213
- #
214
- # module MyApp
215
- # Controller = Hanami::Controller.duplicate(self, 'Ctrls')
216
- # end
217
- #
218
- # defined?(MyApp::Controllers) # => nil
219
- # defined?(MyApp::Ctrls) # => "constant"
220
- #
221
- # # Developers can namespace controllers under Ctrls
222
- # module MyApp::Ctrls::Dashboard
223
- # # ...
224
- # end
225
- #
226
- # @example Nil controllers module
227
- # require 'hanami/controller'
228
- #
229
- # module MyApp
230
- # Controller = Hanami::Controller.duplicate(self, nil)
231
- # end
232
- #
233
- # defined?(MyApp::Controllers) # => nil
234
- #
235
- # # Developers can namespace controllers under MyApp
236
- # module MyApp::DashboardController
237
- # # ...
238
- # end
239
- #
240
- # @example Block usage
241
- # require 'hanami/controller'
242
- #
243
- # module MyApp
244
- # Controller = Hanami::Controller.duplicate(self) do
245
- # handle_exceptions false
246
- # end
247
- # end
248
- #
249
- # Hanami::Controller.configuration.handle_exceptions # => true
250
- # MyApp::Controller.configuration.handle_exceptions # => false
251
- def self.duplicate(mod, controllers = 'Controllers', &blk)
252
- dupe.tap do |duplicated|
253
- mod.module_eval %{ module #{ controllers }; end } if controllers
254
- mod.module_eval %{ Action = Hanami::Action.dup }
255
-
256
- duplicated.module_eval %{
257
- configure do
258
- action_module #{ mod }::Action
259
- end
260
- }
261
-
262
- duplicated.configure(&blk) if block_given?
263
- end
264
- end
265
-
266
- # Framework loading entry point
267
- #
268
- # @return [void]
269
- #
270
- # @since 0.3.0
271
- def self.load!
272
- configuration.load!
273
- end
274
47
  end
275
48
  end
@@ -3,6 +3,6 @@ module Hanami
3
3
  # Defines the version
4
4
  #
5
5
  # @since 0.1.0
6
- VERSION = '1.3.0'.freeze
6
+ VERSION = '2.0.0.alpha2'.freeze
7
7
  end
8
8
  end
@@ -31,7 +31,7 @@ module Hanami
31
31
 
32
32
  # Return a status for the given code
33
33
  #
34
- # @param code [Fixnum] a valid HTTP code
34
+ # @param code [Integer] a valid HTTP code
35
35
  #
36
36
  # @return [Array] a pair of code and message for an HTTP status
37
37
  #
@@ -48,7 +48,7 @@ module Hanami
48
48
 
49
49
  # Return a message for the given status code
50
50
  #
51
- # @param code [Fixnum] a valid HTTP code
51
+ # @param code [Integer] a valid HTTP code
52
52
  #
53
53
  # @return [String] a message for the given status code
54
54
  #
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hanami-controller
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 2.0.0.alpha2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luca Guidi
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-10-24 00:00:00.000000000 Z
11
+ date: 2021-05-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -30,28 +30,48 @@ dependencies:
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '1.3'
33
+ version: 2.0.alpha
34
34
  type: :runtime
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '1.3'
40
+ version: 2.0.alpha
41
41
  - !ruby/object:Gem::Dependency
42
- name: bundler
42
+ name: dry-configurable
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '0.12'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '0.12'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
46
60
  - !ruby/object:Gem::Version
47
61
  version: '1.6'
62
+ - - "<"
63
+ - !ruby/object:Gem::Version
64
+ version: '3'
48
65
  type: :development
49
66
  prerelease: false
50
67
  version_requirements: !ruby/object:Gem::Requirement
51
68
  requirements:
52
- - - "~>"
69
+ - - ">="
53
70
  - !ruby/object:Gem::Version
54
71
  version: '1.6'
72
+ - - "<"
73
+ - !ruby/object:Gem::Version
74
+ version: '3'
55
75
  - !ruby/object:Gem::Dependency
56
76
  name: rack-test
57
77
  requirement: !ruby/object:Gem::Requirement
@@ -72,28 +92,28 @@ dependencies:
72
92
  requirements:
73
93
  - - "~>"
74
94
  - !ruby/object:Gem::Version
75
- version: '12'
95
+ version: '13'
76
96
  type: :development
77
97
  prerelease: false
78
98
  version_requirements: !ruby/object:Gem::Requirement
79
99
  requirements:
80
100
  - - "~>"
81
101
  - !ruby/object:Gem::Version
82
- version: '12'
102
+ version: '13'
83
103
  - !ruby/object:Gem::Dependency
84
104
  name: rspec
85
105
  requirement: !ruby/object:Gem::Requirement
86
106
  requirements:
87
107
  - - "~>"
88
108
  - !ruby/object:Gem::Version
89
- version: '3.7'
109
+ version: '3.9'
90
110
  type: :development
91
111
  prerelease: false
92
112
  version_requirements: !ruby/object:Gem::Requirement
93
113
  requirements:
94
114
  - - "~>"
95
115
  - !ruby/object:Gem::Version
96
- version: '3.7'
116
+ version: '3.9'
97
117
  description: Complete, fast and testable actions for Rack
98
118
  email:
99
119
  - me@lucaguidi.com
@@ -105,36 +125,34 @@ files:
105
125
  - LICENSE.md
106
126
  - README.md
107
127
  - hanami-controller.gemspec
108
- - lib/hanami-controller.rb
109
128
  - lib/hanami/action.rb
129
+ - lib/hanami/action/application_action.rb
130
+ - lib/hanami/action/application_configuration.rb
131
+ - lib/hanami/action/application_configuration/cookies.rb
132
+ - lib/hanami/action/application_configuration/sessions.rb
110
133
  - lib/hanami/action/base_params.rb
111
134
  - lib/hanami/action/cache.rb
112
135
  - lib/hanami/action/cache/cache_control.rb
113
136
  - lib/hanami/action/cache/conditional_get.rb
114
137
  - lib/hanami/action/cache/directives.rb
115
138
  - lib/hanami/action/cache/expires.rb
116
- - lib/hanami/action/callable.rb
117
- - lib/hanami/action/callbacks.rb
118
- - lib/hanami/action/configurable.rb
139
+ - lib/hanami/action/configuration.rb
119
140
  - lib/hanami/action/cookie_jar.rb
120
141
  - lib/hanami/action/cookies.rb
121
- - lib/hanami/action/exposable.rb
122
- - lib/hanami/action/exposable/guard.rb
142
+ - lib/hanami/action/csrf_protection.rb
123
143
  - lib/hanami/action/flash.rb
124
144
  - lib/hanami/action/glue.rb
125
- - lib/hanami/action/head.rb
145
+ - lib/hanami/action/halt.rb
126
146
  - lib/hanami/action/mime.rb
127
147
  - lib/hanami/action/params.rb
128
- - lib/hanami/action/rack.rb
129
- - lib/hanami/action/rack/callable.rb
130
148
  - lib/hanami/action/rack/file.rb
131
- - lib/hanami/action/redirect.rb
132
149
  - lib/hanami/action/request.rb
150
+ - lib/hanami/action/response.rb
133
151
  - lib/hanami/action/session.rb
134
- - lib/hanami/action/throwable.rb
152
+ - lib/hanami/action/standalone_action.rb
135
153
  - lib/hanami/action/validatable.rb
154
+ - lib/hanami/action/view_name_inferrer.rb
136
155
  - lib/hanami/controller.rb
137
- - lib/hanami/controller/configuration.rb
138
156
  - lib/hanami/controller/error.rb
139
157
  - lib/hanami/controller/version.rb
140
158
  - lib/hanami/http/status.rb
@@ -142,7 +160,7 @@ homepage: http://hanamirb.org
142
160
  licenses:
143
161
  - MIT
144
162
  metadata: {}
145
- post_install_message:
163
+ post_install_message:
146
164
  rdoc_options: []
147
165
  require_paths:
148
166
  - lib
@@ -150,16 +168,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
150
168
  requirements:
151
169
  - - ">="
152
170
  - !ruby/object:Gem::Version
153
- version: 2.3.0
171
+ version: 2.6.0
154
172
  required_rubygems_version: !ruby/object:Gem::Requirement
155
173
  requirements:
156
- - - ">="
174
+ - - ">"
157
175
  - !ruby/object:Gem::Version
158
- version: '0'
176
+ version: 1.3.1
159
177
  requirements: []
160
- rubyforge_project:
161
- rubygems_version: 2.7.7
162
- signing_key:
178
+ rubygems_version: 3.2.4
179
+ signing_key:
163
180
  specification_version: 4
164
181
  summary: Complete, fast and testable actions for Rack and Hanami
165
182
  test_files: []