mime_actor 0.6.4 → 0.7.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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +197 -0
- data/COMPARE.md +40 -32
- data/README.md +130 -25
- data/lib/mime_actor/action.rb +23 -15
- data/lib/mime_actor/callbacks.rb +101 -126
- data/lib/mime_actor/deprecator.rb +1 -11
- data/lib/mime_actor/errors.rb +2 -2
- data/lib/mime_actor/logging.rb +12 -2
- data/lib/mime_actor/railtie.rb +1 -1
- data/lib/mime_actor/rescue.rb +6 -74
- data/lib/mime_actor/scene.rb +77 -68
- data/lib/mime_actor/stage.rb +3 -47
- data/lib/mime_actor/validator.rb +4 -0
- data/lib/mime_actor/version.rb +2 -2
- metadata +13 -26
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '094172ea0e5fcae9e39f4afb809b70a3726685ea811394a9b44b131479054c31'
|
4
|
+
data.tar.gz: c1e62426da71b3864e14cbbcc5002c6d89e6b63b26c14f22bb0978ce78ce7157
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 67e13f2740eba0faba943d6ed74ba57e0b6ce27e8359fde9258ad94ba270613a871b39b628fe8ea3975f36695003272387f0243f3027242ab8b9f608bdd7005d
|
7
|
+
data.tar.gz: c14df5ab058a41f6e2f259bfb7b5ee8471d17bd88e130abc9763622eb79bcc2e326172962edaee74a29efb8920bb1d076c509df0753bc2b823eb6c3aff2bdded
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,197 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [Unreleased](https://github.com/ryancyq/mime_actor/tree/HEAD)
|
4
|
+
|
5
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.7.0...HEAD)
|
6
|
+
|
7
|
+
**Implemented enhancements:**
|
8
|
+
|
9
|
+
- use tagged logging [\#64](https://github.com/ryancyq/mime_actor/issues/64)
|
10
|
+
|
11
|
+
**Merged pull requests:**
|
12
|
+
|
13
|
+
- feat: tagged logging [\#66](https://github.com/ryancyq/mime_actor/pull/66) ([ryancyq](https://github.com/ryancyq))
|
14
|
+
- refactor: code generator with heredoc [\#65](https://github.com/ryancyq/mime_actor/pull/65) ([ryancyq](https://github.com/ryancyq))
|
15
|
+
|
16
|
+
## [v0.7.0](https://github.com/ryancyq/mime_actor/tree/v0.7.0) (2024-07-30)
|
17
|
+
|
18
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.6.4...v0.7.0)
|
19
|
+
|
20
|
+
**Merged pull requests:**
|
21
|
+
|
22
|
+
- fix: callback chain sequence respect definition ordering [\#63](https://github.com/ryancyq/mime_actor/pull/63) ([ryancyq](https://github.com/ryancyq))
|
23
|
+
- refactor: callback generator [\#62](https://github.com/ryancyq/mime_actor/pull/62) ([ryancyq](https://github.com/ryancyq))
|
24
|
+
- feat: \#act\_on\_action to replace \#respond\_act\_to [\#61](https://github.com/ryancyq/mime_actor/pull/61) ([ryancyq](https://github.com/ryancyq))
|
25
|
+
- fix: action/format validation [\#60](https://github.com/ryancyq/mime_actor/pull/60) ([ryancyq](https://github.com/ryancyq))
|
26
|
+
|
27
|
+
## [v0.6.4](https://github.com/ryancyq/mime_actor/tree/v0.6.4) (2024-07-26)
|
28
|
+
|
29
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.6.3...v0.6.4)
|
30
|
+
|
31
|
+
**Implemented enhancements:**
|
32
|
+
|
33
|
+
- Use actor name generator [\#5](https://github.com/ryancyq/mime_actor/issues/5)
|
34
|
+
|
35
|
+
**Merged pull requests:**
|
36
|
+
|
37
|
+
- feat: actor delegator [\#57](https://github.com/ryancyq/mime_actor/pull/57) ([ryancyq](https://github.com/ryancyq))
|
38
|
+
- lib: add railtie for deprecation config [\#56](https://github.com/ryancyq/mime_actor/pull/56) ([ryancyq](https://github.com/ryancyq))
|
39
|
+
- lib: extract deprecation into separate file [\#55](https://github.com/ryancyq/mime_actor/pull/55) ([ryancyq](https://github.com/ryancyq))
|
40
|
+
- refactor: rename with validator into callable validator [\#54](https://github.com/ryancyq/mime_actor/pull/54) ([ryancyq](https://github.com/ryancyq))
|
41
|
+
- chore\(deps-dev\): bump rubocop from 1.64.1 to 1.65.0 [\#35](https://github.com/ryancyq/mime_actor/pull/35) ([dependabot[bot]](https://github.com/apps/dependabot))
|
42
|
+
|
43
|
+
## [v0.6.3](https://github.com/ryancyq/mime_actor/tree/v0.6.3) (2024-07-25)
|
44
|
+
|
45
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.6.2...v0.6.3)
|
46
|
+
|
47
|
+
**Merged pull requests:**
|
48
|
+
|
49
|
+
- fix: validate format arg for \#cue\_actor [\#53](https://github.com/ryancyq/mime_actor/pull/53) ([ryancyq](https://github.com/ryancyq))
|
50
|
+
- fix: use controller\#action\_name [\#52](https://github.com/ryancyq/mime_actor/pull/52) ([ryancyq](https://github.com/ryancyq))
|
51
|
+
- feat: rescue act callbacks [\#51](https://github.com/ryancyq/mime_actor/pull/51) ([ryancyq](https://github.com/ryancyq))
|
52
|
+
- lib: allow actor of any visibility to be called [\#50](https://github.com/ryancyq/mime_actor/pull/50) ([ryancyq](https://github.com/ryancyq))
|
53
|
+
- feat: run act callbacks during `start_scene` [\#49](https://github.com/ryancyq/mime_actor/pull/49) ([ryancyq](https://github.com/ryancyq))
|
54
|
+
- spec: add act callbacks test sequence [\#48](https://github.com/ryancyq/mime_actor/pull/48) ([ryancyq](https://github.com/ryancyq))
|
55
|
+
- fix: support collection argument for action/format filters in act\_callacbks configuration [\#47](https://github.com/ryancyq/mime_actor/pull/47) ([ryancyq](https://github.com/ryancyq))
|
56
|
+
- refactor: move action collection vs single action rule into a composed rule in validator [\#46](https://github.com/ryancyq/mime_actor/pull/46) ([ryancyq](https://github.com/ryancyq))
|
57
|
+
- refactor: dispatcher callable API [\#45](https://github.com/ryancyq/mime_actor/pull/45) ([ryancyq](https://github.com/ryancyq))
|
58
|
+
- Revert "Revert "feat: act callbacks"" [\#44](https://github.com/ryancyq/mime_actor/pull/44) ([ryancyq](https://github.com/ryancyq))
|
59
|
+
- doc: example controller spec [\#31](https://github.com/ryancyq/mime_actor/pull/31) ([ryancyq](https://github.com/ryancyq))
|
60
|
+
|
61
|
+
## [v0.6.2](https://github.com/ryancyq/mime_actor/tree/v0.6.2) (2024-07-21)
|
62
|
+
|
63
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.6.1...v0.6.2)
|
64
|
+
|
65
|
+
**Implemented enhancements:**
|
66
|
+
|
67
|
+
- feat: act callbacks [\#32](https://github.com/ryancyq/mime_actor/issues/32)
|
68
|
+
|
69
|
+
**Merged pull requests:**
|
70
|
+
|
71
|
+
- Revert "feat: act callbacks" [\#43](https://github.com/ryancyq/mime_actor/pull/43) ([ryancyq](https://github.com/ryancyq))
|
72
|
+
- feat: act callbacks [\#42](https://github.com/ryancyq/mime_actor/pull/42) ([ryancyq](https://github.com/ryancyq))
|
73
|
+
- chore: deprecate methods before removal [\#41](https://github.com/ryancyq/mime_actor/pull/41) ([ryancyq](https://github.com/ryancyq))
|
74
|
+
- lib: instance method rescue\_actor to public [\#40](https://github.com/ryancyq/mime_actor/pull/40) ([ryancyq](https://github.com/ryancyq))
|
75
|
+
- refactor: replace dispatch\_act rescue with dispatch\_actor rescue [\#39](https://github.com/ryancyq/mime_actor/pull/39) ([ryancyq](https://github.com/ryancyq))
|
76
|
+
- refactor: dispatcher call [\#38](https://github.com/ryancyq/mime_actor/pull/38) ([ryancyq](https://github.com/ryancyq))
|
77
|
+
- lib: add gem deprecation message [\#37](https://github.com/ryancyq/mime_actor/pull/37) ([ryancyq](https://github.com/ryancyq))
|
78
|
+
- lib: keep version as string [\#36](https://github.com/ryancyq/mime_actor/pull/36) ([ryancyq](https://github.com/ryancyq))
|
79
|
+
- fix: use `module_eval` inside module [\#34](https://github.com/ryancyq/mime_actor/pull/34) ([ryancyq](https://github.com/ryancyq))
|
80
|
+
- fix: use `#dispatch_act` [\#33](https://github.com/ryancyq/mime_actor/pull/33) ([ryancyq](https://github.com/ryancyq))
|
81
|
+
|
82
|
+
## [v0.6.1](https://github.com/ryancyq/mime_actor/tree/v0.6.1) (2024-07-03)
|
83
|
+
|
84
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.6.0...v0.6.1)
|
85
|
+
|
86
|
+
**Implemented enhancements:**
|
87
|
+
|
88
|
+
- allow block via scene composition [\#2](https://github.com/ryancyq/mime_actor/issues/2)
|
89
|
+
- feat: actor responder [\#28](https://github.com/ryancyq/mime_actor/pull/28) ([ryancyq](https://github.com/ryancyq))
|
90
|
+
- fix: error message should use `#inspect` for param [\#27](https://github.com/ryancyq/mime_actor/pull/27) ([ryancyq](https://github.com/ryancyq))
|
91
|
+
- fix: rubocop access modifier [\#26](https://github.com/ryancyq/mime_actor/pull/26) ([ryancyq](https://github.com/ryancyq))
|
92
|
+
- refactor: rescue validation + dispatch [\#25](https://github.com/ryancyq/mime_actor/pull/25) ([ryancyq](https://github.com/ryancyq))
|
93
|
+
- fix: switch back to ClassMethods [\#24](https://github.com/ryancyq/mime_actor/pull/24) ([ryancyq](https://github.com/ryancyq))
|
94
|
+
- fix: require active support `nil` class extension [\#23](https://github.com/ryancyq/mime_actor/pull/23) ([ryancyq](https://github.com/ryancyq))
|
95
|
+
- fix: ensure validation message use \#inspect [\#22](https://github.com/ryancyq/mime_actor/pull/22) ([ryancyq](https://github.com/ryancyq))
|
96
|
+
- refactor: act\_on\_format with respond\_act\_to [\#21](https://github.com/ryancyq/mime_actor/pull/21) ([ryancyq](https://github.com/ryancyq))
|
97
|
+
|
98
|
+
**Merged pull requests:**
|
99
|
+
|
100
|
+
- fix: ensure `actor` passed to `#cue_actor` is executed within the instance [\#30](https://github.com/ryancyq/mime_actor/pull/30) ([ryancyq](https://github.com/ryancyq))
|
101
|
+
- fix: update type check logic to raise `TypeError` instead [\#29](https://github.com/ryancyq/mime_actor/pull/29) ([ryancyq](https://github.com/ryancyq))
|
102
|
+
|
103
|
+
## [v0.6.0](https://github.com/ryancyq/mime_actor/tree/v0.6.0) (2024-07-01)
|
104
|
+
|
105
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.5.4...v0.6.0)
|
106
|
+
|
107
|
+
**Implemented enhancements:**
|
108
|
+
|
109
|
+
- fix: refactor rescue validations [\#20](https://github.com/ryancyq/mime_actor/pull/20) ([ryancyq](https://github.com/ryancyq))
|
110
|
+
- fix: switch over to active support class methods [\#19](https://github.com/ryancyq/mime_actor/pull/19) ([ryancyq](https://github.com/ryancyq))
|
111
|
+
- fix: use `#rescue_act_from` [\#17](https://github.com/ryancyq/mime_actor/pull/17) ([ryancyq](https://github.com/ryancyq))
|
112
|
+
- fix: promote `#act_on_format` to the public API [\#16](https://github.com/ryancyq/mime_actor/pull/16) ([ryancyq](https://github.com/ryancyq))
|
113
|
+
|
114
|
+
**Merged pull requests:**
|
115
|
+
|
116
|
+
- refactor: top level comments [\#18](https://github.com/ryancyq/mime_actor/pull/18) ([ryancyq](https://github.com/ryancyq))
|
117
|
+
|
118
|
+
## [v0.5.4](https://github.com/ryancyq/mime_actor/tree/v0.5.4) (2024-06-29)
|
119
|
+
|
120
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.5.3...v0.5.4)
|
121
|
+
|
122
|
+
**Implemented enhancements:**
|
123
|
+
|
124
|
+
- fix: alias compose\_scene to act\_on\_format [\#15](https://github.com/ryancyq/mime_actor/pull/15) ([ryancyq](https://github.com/ryancyq))
|
125
|
+
|
126
|
+
## [v0.5.3](https://github.com/ryancyq/mime_actor/tree/v0.5.3) (2024-06-29)
|
127
|
+
|
128
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.5.2...v0.5.3)
|
129
|
+
|
130
|
+
## [v0.5.2](https://github.com/ryancyq/mime_actor/tree/v0.5.2) (2024-06-29)
|
131
|
+
|
132
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.5.1...v0.5.2)
|
133
|
+
|
134
|
+
**Implemented enhancements:**
|
135
|
+
|
136
|
+
- fix: ensure block is passed when calling dispatch\_cue [\#13](https://github.com/ryancyq/mime_actor/pull/13) ([ryancyq](https://github.com/ryancyq))
|
137
|
+
|
138
|
+
**Closed issues:**
|
139
|
+
|
140
|
+
- document API [\#4](https://github.com/ryancyq/mime_actor/issues/4)
|
141
|
+
|
142
|
+
**Merged pull requests:**
|
143
|
+
|
144
|
+
- doc: update class/methods with rdoc compatible comments [\#14](https://github.com/ryancyq/mime_actor/pull/14) ([ryancyq](https://github.com/ryancyq))
|
145
|
+
- chore\(deps-dev\): bump simplecov from 0.21.2 to 0.22.0 [\#12](https://github.com/ryancyq/mime_actor/pull/12) ([dependabot[bot]](https://github.com/apps/dependabot))
|
146
|
+
|
147
|
+
## [v0.5.1](https://github.com/ryancyq/mime_actor/tree/v0.5.1) (2024-06-28)
|
148
|
+
|
149
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.4.0...v0.5.1)
|
150
|
+
|
151
|
+
**Implemented enhancements:**
|
152
|
+
|
153
|
+
- fix: refine logging message to be clearer in context [\#10](https://github.com/ryancyq/mime_actor/pull/10) ([ryancyq](https://github.com/ryancyq))
|
154
|
+
- spec: add tests for different type of error class [\#9](https://github.com/ryancyq/mime_actor/pull/9) ([ryancyq](https://github.com/ryancyq))
|
155
|
+
- spec: add tests to logging module [\#7](https://github.com/ryancyq/mime_actor/pull/7) ([ryancyq](https://github.com/ryancyq))
|
156
|
+
- spec: improve coverage [\#6](https://github.com/ryancyq/mime_actor/pull/6) ([ryancyq](https://github.com/ryancyq))
|
157
|
+
|
158
|
+
**Merged pull requests:**
|
159
|
+
|
160
|
+
- spec: add action controller specific test [\#11](https://github.com/ryancyq/mime_actor/pull/11) ([ryancyq](https://github.com/ryancyq))
|
161
|
+
- spec: add tests around rescue handler invocation context [\#8](https://github.com/ryancyq/mime_actor/pull/8) ([ryancyq](https://github.com/ryancyq))
|
162
|
+
|
163
|
+
## [v0.4.0](https://github.com/ryancyq/mime_actor/tree/v0.4.0) (2024-06-27)
|
164
|
+
|
165
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.3.3...v0.4.0)
|
166
|
+
|
167
|
+
**Implemented enhancements:**
|
168
|
+
|
169
|
+
- handle Mime::ALL priority [\#3](https://github.com/ryancyq/mime_actor/issues/3)
|
170
|
+
|
171
|
+
## [v0.3.3](https://github.com/ryancyq/mime_actor/tree/v0.3.3) (2024-06-24)
|
172
|
+
|
173
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.3.2...v0.3.3)
|
174
|
+
|
175
|
+
## [v0.3.2](https://github.com/ryancyq/mime_actor/tree/v0.3.2) (2024-06-24)
|
176
|
+
|
177
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.3.1...v0.3.2)
|
178
|
+
|
179
|
+
## [v0.3.1](https://github.com/ryancyq/mime_actor/tree/v0.3.1) (2024-06-23)
|
180
|
+
|
181
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.3.0...v0.3.1)
|
182
|
+
|
183
|
+
## [v0.3.0](https://github.com/ryancyq/mime_actor/tree/v0.3.0) (2024-06-23)
|
184
|
+
|
185
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.2.1...v0.3.0)
|
186
|
+
|
187
|
+
## [v0.2.1](https://github.com/ryancyq/mime_actor/tree/v0.2.1) (2024-06-23)
|
188
|
+
|
189
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.2.0...v0.2.1)
|
190
|
+
|
191
|
+
## [v0.2.0](https://github.com/ryancyq/mime_actor/tree/v0.2.0) (2024-06-23)
|
192
|
+
|
193
|
+
[Full Changelog](https://github.com/ryancyq/mime_actor/compare/v0.1.0...v0.2.0)
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
\* *This Changelog was automatically generated by [github_changelog_generator](https://github.com/github-changelog-generator/github-changelog-generator)*
|
data/COMPARE.md
CHANGED
@@ -56,18 +56,18 @@ end
|
|
56
56
|
#### before
|
57
57
|
```rb
|
58
58
|
class EventsController < ActionController::Base
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
59
|
+
def index
|
60
|
+
@events = Event.all
|
61
|
+
respond_to do |format|
|
62
|
+
format.html do
|
63
|
+
@event_categories = EventCategory.all
|
64
|
+
|
65
|
+
# render html using @events and @event_categories
|
66
|
+
render :index
|
67
|
+
end
|
68
|
+
format.json { render json: @events } # render json using #as_json
|
70
69
|
end
|
70
|
+
end
|
71
71
|
end
|
72
72
|
```
|
73
73
|
#### after
|
@@ -75,21 +75,21 @@ end
|
|
75
75
|
class EventsController < ActionController::Base
|
76
76
|
include MimeActor::Action
|
77
77
|
|
78
|
-
|
78
|
+
act_before -> { @events = Event.all }, action: :index
|
79
79
|
|
80
80
|
# dynamically defines the action method according to on: argument
|
81
|
-
|
81
|
+
act_on_action :index, format: %i[html json]
|
82
82
|
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
# render html using @events and @event_categories
|
87
|
-
render :index
|
88
|
-
end
|
83
|
+
act_after :render_index_html, action: :index, format: :html
|
84
|
+
act_after -> { render json: @events }, action: :index, format: :json
|
89
85
|
|
90
|
-
def
|
91
|
-
|
92
|
-
|
86
|
+
def index
|
87
|
+
@event_categories = EventCategory.all
|
88
|
+
end
|
89
|
+
|
90
|
+
def render_index_html
|
91
|
+
# render html using @events and @event_categories
|
92
|
+
render :index
|
93
93
|
end
|
94
94
|
end
|
95
95
|
```
|
@@ -145,28 +145,36 @@ end
|
|
145
145
|
class EventsController < ActionController::Base
|
146
146
|
include MimeActor::Action
|
147
147
|
|
148
|
-
|
148
|
+
act_before :load_event, action: %i[show update]
|
149
149
|
|
150
|
-
|
151
|
-
|
150
|
+
act_on_action :show, :update, format: :html
|
151
|
+
act_on_action :show, :update, format: :json, with: -> { render json: @event } # render json using #as_json
|
152
152
|
|
153
|
-
|
153
|
+
act_after :render_show_html, action: :show, format: :html
|
154
|
+
act_after :redirect_to_show, action: :update, format: :html
|
154
155
|
|
156
|
+
rescue_act_from ActiveRecord::RecordNotFound, format: :json, with: :handle_json_error
|
155
157
|
rescue_act_from ActiveRecord::RecordNotFound, format: :html, action: :show do
|
156
158
|
redirect_to events_path
|
157
159
|
end
|
160
|
+
rescue_act_from ActiveRecord::RecrodNotSaved, format: :html, action: :update, with: -> { render :edit }
|
161
|
+
|
162
|
+
def show
|
163
|
+
# ...
|
164
|
+
end
|
165
|
+
|
166
|
+
def update
|
167
|
+
# ...
|
168
|
+
end
|
158
169
|
|
159
170
|
private
|
160
|
-
|
161
|
-
def
|
171
|
+
|
172
|
+
def render_show_html
|
162
173
|
render :show # render html using @event
|
163
174
|
end
|
164
175
|
|
165
|
-
def
|
166
|
-
# ...
|
176
|
+
def redirect_to_show
|
167
177
|
redirect_to event_path(@event.id) # redirect to show upon sucessful update
|
168
|
-
rescue ActiveRecord::RecordNotFound
|
169
|
-
render :edit
|
170
178
|
end
|
171
179
|
|
172
180
|
def handle_json_error(error)
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# mime_actor
|
2
2
|
|
3
3
|
[![Version][rubygems_badge]][rubygems]
|
4
4
|
[![CI][ci_badge]][ci_workflows]
|
@@ -29,47 +29,151 @@ However, it can be a litte bit messy when some actions render more than a single
|
|
29
29
|
## Usage
|
30
30
|
|
31
31
|
**Mime Actor** allows you to do something like:
|
32
|
+
|
33
|
+
#### Customize Action for certain MIME type
|
32
34
|
```rb
|
33
|
-
|
34
|
-
|
35
|
+
act_on_action :index, :show, format: %i[html json]
|
36
|
+
act_on_action :create, format: :json
|
37
|
+
|
38
|
+
def index
|
39
|
+
@events = Event.all
|
40
|
+
end
|
41
|
+
def show
|
42
|
+
@event = Event.find(params[:id])
|
43
|
+
end
|
44
|
+
def create
|
45
|
+
@event = Event.new.save!
|
46
|
+
end
|
47
|
+
```
|
48
|
+
The above is equivalent to the following:
|
49
|
+
```rb
|
50
|
+
def index
|
51
|
+
respond_to do |format|
|
52
|
+
format.html { @events = Event.all }
|
53
|
+
format.json { @events = Event.all }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
def show
|
57
|
+
respond_to do |format|
|
58
|
+
format.html { @event = Event.find(params[:id]) }
|
59
|
+
format.json { @event = Event.find(params[:id]) }
|
60
|
+
end
|
61
|
+
end
|
62
|
+
def create
|
63
|
+
respond_to do |format|
|
64
|
+
format.json { @event = Event.new.save! }
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
35
68
|
|
36
|
-
|
37
|
-
|
38
|
-
|
69
|
+
#### Callback registration for certain Action + MIME type
|
70
|
+
```rb
|
71
|
+
act_before :load_event_categories, action: :index, format: :html
|
39
72
|
|
40
|
-
|
41
|
-
respond_act_to :html, on: %i[index show], with: :render_html
|
42
|
-
respond_act_to :json, on: %i[index show], with: -> { render json: { action: action_name } }
|
73
|
+
act_on_action :index, :show, format: %i[html json]
|
43
74
|
|
44
|
-
|
45
|
-
|
75
|
+
def index
|
76
|
+
@events = Event.all
|
77
|
+
end
|
46
78
|
|
47
|
-
|
79
|
+
def show
|
80
|
+
@event = Event.find(params[:id])
|
81
|
+
end
|
48
82
|
|
49
|
-
def
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
83
|
+
def load_event_categories
|
84
|
+
@event_categories = EventCategory.all
|
85
|
+
end
|
86
|
+
```
|
87
|
+
The above is equivalent to the following:
|
88
|
+
```rb
|
89
|
+
def index
|
90
|
+
respond_to do |format|
|
91
|
+
format.html do
|
92
|
+
load_event_categories
|
93
|
+
@events = Event.all
|
94
|
+
end
|
95
|
+
format.json { @events = Event.all }
|
96
|
+
end
|
54
97
|
end
|
55
98
|
|
56
|
-
def
|
99
|
+
def show
|
100
|
+
respond_to do |format|
|
101
|
+
format.html { @event = Event.find(params[:id]) }
|
102
|
+
format.json { @event = Event.find(params[:id]) }
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def load_event_categories
|
107
|
+
@event_categories = EventCategory.all
|
108
|
+
end
|
109
|
+
```
|
110
|
+
|
111
|
+
#### Rescue handler registration for certain Action + MIME type
|
112
|
+
```rb
|
113
|
+
act_before :load_event, action: %i[show update]
|
114
|
+
|
115
|
+
act_on_action :show, :update, format: :json
|
116
|
+
act_on_action :show, format: :html
|
117
|
+
|
118
|
+
rescue_act_from ActiveRecord::RecordNotFound, ActiveRecord::RecordNotSaved, format: :json, with: :handle_json_error
|
119
|
+
rescue_act_from ActiveRecord::RecordNotSaved, action: :show, format: :html, with: -> { redirect_to "/events" }
|
120
|
+
rescue_act_from ActiveRecord::RecordNotSaved, action: :update, format: :html, with: -> { render :edit }
|
121
|
+
|
122
|
+
def show
|
57
123
|
# ...
|
58
|
-
render json: @event # render with #to_json
|
59
124
|
end
|
60
125
|
|
61
|
-
def
|
62
|
-
|
126
|
+
def update
|
127
|
+
@event.name = params[:name]
|
128
|
+
@event.save!
|
129
|
+
redirect_to "/events/#{@event.id}"
|
63
130
|
end
|
64
131
|
|
65
132
|
def load_event
|
66
|
-
@event = Event.find(params
|
133
|
+
@event = Event.find(params[:id])
|
134
|
+
end
|
135
|
+
|
136
|
+
def handle_json_error(error)
|
137
|
+
render status: :bad_request, json: { error: error.message }
|
138
|
+
end
|
139
|
+
```
|
140
|
+
The above is equivalent to the following:
|
141
|
+
```rb
|
142
|
+
before_action :load_event, only: %i[show update]
|
143
|
+
|
144
|
+
rescue_from ActiveRecord::RecordNotFound, with: :handle_not_found_error
|
145
|
+
rescue_from ActiveRecord::RecordNotSaved, with: :handle_not_saved_error
|
146
|
+
|
147
|
+
def show
|
148
|
+
respond_to do |format|
|
149
|
+
format.html { ... }
|
150
|
+
format.json { ... }
|
151
|
+
end
|
152
|
+
end
|
153
|
+
|
154
|
+
def update
|
155
|
+
@event.name = params[:name]
|
156
|
+
@event.save!
|
157
|
+
redirect_to "/events/#{@event.id}"
|
158
|
+
end
|
159
|
+
|
160
|
+
def handle_not_found_error(error)
|
161
|
+
respond_to do |format|
|
162
|
+
format.html { redirect_to "/events" }
|
163
|
+
format.json { handle_json_error(error) }
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
def handle_not_saved_error(error)
|
168
|
+
respond_to do |format|
|
169
|
+
format.html { render :edit }
|
170
|
+
format.json { handle_json_error(error) }
|
171
|
+
end
|
67
172
|
end
|
68
173
|
|
69
174
|
def handle_json_error(error)
|
70
175
|
render status: :bad_request, json: { error: error.message }
|
71
176
|
end
|
72
|
-
end
|
73
177
|
```
|
74
178
|
|
75
179
|
## Features
|
@@ -87,7 +191,8 @@ You can find the documentation on [RubyDoc][doc_mime_actor].
|
|
87
191
|
## Requirements
|
88
192
|
|
89
193
|
- Ruby: MRI 3.1+
|
90
|
-
-
|
194
|
+
- ActiveSupport: 7.0+
|
195
|
+
- ActionPack: 7.0+
|
91
196
|
|
92
197
|
## Installation
|
93
198
|
|
@@ -117,7 +222,7 @@ Bug reports and pull requests are welcome on GitHub at [https://github.com/ryanc
|
|
117
222
|
[rubygems_badge]: https://img.shields.io/gem/v/mime_actor.svg
|
118
223
|
[rubygems]: https://rubygems.org/gems/mime_actor
|
119
224
|
[ci_badge]: https://github.com/ryancyq/mime_actor/actions/workflows/build.yml/badge.svg
|
120
|
-
[ci_workflows]: https://github.com/ryancyq/mime_actor/actions/workflows/
|
225
|
+
[ci_workflows]: https://github.com/ryancyq/mime_actor/actions/workflows/build.yml
|
121
226
|
[coverage_badge]: https://codecov.io/gh/ryancyq/mime_actor/graph/badge.svg?token=4C091RHXC3
|
122
227
|
[coverage]: https://codecov.io/gh/ryancyq/mime_actor
|
123
228
|
[maintainability_badge]: https://api.codeclimate.com/v1/badges/06689606dc3f3945dc1b/maintainability
|
data/lib/mime_actor/action.rb
CHANGED
@@ -7,8 +7,6 @@ require "mime_actor/scene"
|
|
7
7
|
require "mime_actor/stage"
|
8
8
|
|
9
9
|
require "active_support/concern"
|
10
|
-
require "active_support/core_ext/module/attribute_accessors"
|
11
|
-
require "active_support/core_ext/object/blank"
|
12
10
|
require "active_support/lazy_load_hooks"
|
13
11
|
require "abstract_controller/rendering"
|
14
12
|
require "action_controller/metal/mime_responds"
|
@@ -30,38 +28,48 @@ module MimeActor
|
|
30
28
|
include Stage
|
31
29
|
include Logging
|
32
30
|
|
33
|
-
included do
|
34
|
-
mattr_accessor :actor_delegator, instance_writer: false, default: ->(action, format) { "#{action}_#{format}" }
|
35
|
-
end
|
36
|
-
|
37
31
|
# The core logic where rendering logics are collected as `Proc` and passed over to `ActionController::MimeResponds`
|
38
32
|
#
|
33
|
+
# @param block the block to be evaluated
|
34
|
+
#
|
39
35
|
# @example process `create` action
|
40
36
|
# # it uses AbstractController#action_name to process
|
41
|
-
# start_scene
|
37
|
+
# start_scene { create_internal }
|
42
38
|
#
|
43
39
|
# # it is equivalent to the following if `create` action is configured with `html` and `json` formats
|
44
40
|
# def create
|
45
41
|
# respond_to |format|
|
46
|
-
# format.html {
|
47
|
-
# format.json {
|
42
|
+
# format.html { create_internal }
|
43
|
+
# format.json { create_internal }
|
48
44
|
# end
|
49
45
|
# end
|
50
46
|
#
|
51
|
-
def start_scene
|
47
|
+
def start_scene(&block)
|
52
48
|
action = action_name.to_sym
|
53
49
|
formats = acting_scenes.fetch(action, {})
|
54
50
|
|
55
51
|
if formats.empty?
|
56
|
-
logger.warn { "format
|
57
|
-
|
52
|
+
logger.warn { "no format found for action: #{action_name.inspect}" }
|
53
|
+
yield if block_given?
|
54
|
+
else
|
55
|
+
respond_to_scene(action, formats, block)
|
58
56
|
end
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
59
60
|
|
61
|
+
def respond_to_scene(action, formats, block)
|
60
62
|
respond_to do |collector|
|
61
63
|
formats.each do |format, actor|
|
62
|
-
callable = actor.presence ||
|
63
|
-
|
64
|
-
|
64
|
+
callable = actor.presence || block
|
65
|
+
if callable.nil?
|
66
|
+
logger.warn { "no #respond_to handler found for action: #{action.inspect} format: #{format.inspect}" }
|
67
|
+
next
|
68
|
+
end
|
69
|
+
|
70
|
+
collector.public_send(format) do
|
71
|
+
fill_run_sheet(action, format) { cue_actor(callable, action, format, action:, format:) }
|
72
|
+
end
|
65
73
|
end
|
66
74
|
end
|
67
75
|
end
|