mime_actor 0.6.3 → 0.6.4.alpha

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c54b7a74eed15ac67b37d8df3879584634955ff4db0275cecc1dd4b7d531ec55
4
- data.tar.gz: da797d40d7f90f5c6f8e23b61f2fe1441aef08860c6af5ff1763f49cee15ccaa
3
+ metadata.gz: 42ba1c04632b3b7ec4c1f1d9c1ab60571eac9e4c44272601d28dccbb45b535d0
4
+ data.tar.gz: 4d0ccbdba192617231ae0405580921e337412d3de9f63106ff772d33ab0d4208
5
5
  SHA512:
6
- metadata.gz: 79eea272d7e8c3b625584eb8ae54790076035b511210302bb948076f54d847f34bb5c1f354d03b5fa319fbcc7c370f4c72a97b806593a732dd6207dfb8b6bbbe
7
- data.tar.gz: 387780b44aafbcd60e79b7f9ebedd3c00edc40faa1acdce0c6946aad5787f053ae17ae62cab3ba84f8dd695874074e66ef867f6cfbb79e9ba947ddff837c7baa
6
+ metadata.gz: e8fa8dc7c8d8438a1dff0a7bed147f0603000e7491b2e3e8a70173acd9d71f65628390cc7778d50de084940aaafd56684a347b59da5a872ce485361398d0743d
7
+ data.tar.gz: 3c87a2ce45ec35aa56786e4fd625a3b689a27aba728d7207c5b3c0cf893afb936c5f944d0408d5ca0149267f46902c7c27ff65845397c9ddccfff12d55233a48
data/COMPARE.md CHANGED
@@ -1,6 +1,57 @@
1
1
  ## Comparison in Rails
2
2
 
3
- ### MIME Action
3
+ When some actions render more than a single MIME type with error handling sfor different MIME type.
4
+ ```rb
5
+ class EventsController < ActionController::Base
6
+ def show
7
+ respond_to do |format|
8
+ format.html { render "show" }
9
+ format.json { render_event_json }
10
+ format.pdf { render_event_pdf }
11
+ end
12
+ end
13
+
14
+ def create
15
+ respond_to do |format|
16
+ format.html do
17
+ create_event ? redirect_to "/events/#{@event.id}" : render "create"
18
+ end
19
+ format.json do
20
+ create_event!
21
+ render_event_json
22
+ rescue StandardError => error
23
+ render status: :bad_request, json: { error: error.message }
24
+ end
25
+ end
26
+ end
27
+
28
+ def update
29
+ # this will be executed regardless of the requested format.
30
+ # e.g. requesting for xml will execute #update_event but responded with :not_acceptable http status
31
+ success = update_event
32
+
33
+ respond_to do |format|
34
+ format.html { success ? redirect_to "/events/#{@event.id}" : render "update" }
35
+ format.json { render_event_json }
36
+ end
37
+ end
38
+
39
+ def render_event_json
40
+ render json: @event
41
+ rescue StandardError => e
42
+ render json: { error: e.message }
43
+ end
44
+
45
+ def render_event_pdf
46
+ # ...
47
+ # render pdf view or call pdf library with options
48
+ rescue StandardError
49
+ # render error page / redirect to 404 page
50
+ end
51
+ end
52
+ ```
53
+
54
+ ### With MIME Action
4
55
 
5
56
  #### before
6
57
  ```rb
@@ -43,12 +94,12 @@ class EventsController < ActionController::Base
43
94
  end
44
95
  ```
45
96
 
46
- ### MIME Rescue
97
+ ### With MIME Rescue
47
98
 
48
99
  #### before
49
100
  ```rb
50
101
  class EventsController < ActionController::Base
51
- before_action only: %i[show update], with: :load_event
102
+ before_action :load_event, action: %i[show update]
52
103
 
53
104
  rescue_from ActiveRecord::RecordNotFound do |ex|
54
105
  case action_name.to_s
@@ -94,7 +145,7 @@ end
94
145
  class EventsController < ActionController::Base
95
146
  include MimeActor::Action
96
147
 
97
- before_action only: %i[show update], with: :load_event
148
+ before_act :load_event, action: %i[show update]
98
149
 
99
150
  respond_act_to :html, on: %i[show update]
100
151
  respond_act_to :json, on: %i[show update], with: -> { render json: @event } # render json using #as_json
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # mime_actor
1
+ # Mime Actor
2
2
 
3
3
  [![Version][rubygems_badge]][rubygems]
4
4
  [![CI][ci_badge]][ci_workflows]
@@ -7,15 +7,35 @@
7
7
 
8
8
  Action processing with Callback + Rescue handlers for different MIME types in Rails.
9
9
 
10
+ In most of the Rails apps, a single `ActionController` is only responsible for rendering a single MIME type. That is usually done by calling `ActionController#respond_to` for each action in the controller.
11
+
12
+ ```rb
13
+ class EventsController < ActionController::Base
14
+ def index
15
+ @events = Event.all
16
+ respond_to :json
17
+ end
18
+
19
+ def show
20
+ respond_to do |format|
21
+ format.json { render json: @event }
22
+ end
23
+ end
24
+ end
25
+ ```
26
+
27
+ However, it can be a litte bit messy when some actions render more than a single MIME type with error handling for different MIME type. See [comparison][doc_comparison] to understand more.
28
+
10
29
  ## Usage
11
30
 
12
- MimeActor allows you to do something like below:
31
+ **Mime Actor** allows you to do something like:
13
32
  ```rb
14
33
  class EventsController < ActionController::Base
15
34
  include MimeActor::Action
16
35
 
17
36
  before_act -> { @events = Event.all }, action: :index
18
37
  before_act :load_event, action: %i[show update]
38
+ before_act -> { @event_categories = EventCategory.all }, action: :show, format: :html
19
39
 
20
40
  respond_act_to :html, :json, on: :update
21
41
  respond_act_to :html, on: %i[index show], with: :render_html
@@ -35,11 +55,10 @@ class EventsController < ActionController::Base
35
55
 
36
56
  def update_json
37
57
  # ...
38
- render json: @event # render json using #as_json
58
+ render json: @event # render with #to_json
39
59
  end
40
60
 
41
61
  def render_html
42
- @event_categories = EventCategory.all if action_name == :index
43
62
  render html: action_name
44
63
  end
45
64
 
@@ -53,8 +72,6 @@ class EventsController < ActionController::Base
53
72
  end
54
73
  ```
55
74
 
56
- Seems useful? See the [Comparison][doc_comparison] on how it can improve your existing code
57
-
58
75
  ## Features
59
76
 
60
77
  - Action customisation for [ActionController][doc_action_controller] per MIME type
@@ -7,6 +7,7 @@ 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"
10
11
  require "active_support/core_ext/object/blank"
11
12
  require "active_support/lazy_load_hooks"
12
13
  require "abstract_controller/rendering"
@@ -29,6 +30,10 @@ module MimeActor
29
30
  include Stage
30
31
  include Logging
31
32
 
33
+ included do
34
+ mattr_accessor :actor_delegator, instance_writer: false, default: ->(action, format) { "#{action}_#{format}" }
35
+ end
36
+
32
37
  # The core logic where rendering logics are collected as `Proc` and passed over to `ActionController::MimeResponds`
33
38
  #
34
39
  # @example process `create` action
@@ -54,7 +59,8 @@ module MimeActor
54
59
 
55
60
  respond_to do |collector|
56
61
  formats.each do |format, actor|
57
- dispatch = -> { cue_actor(actor.presence || "#{action}_#{format}", format:) }
62
+ callable = actor.presence || actor_delegator.call(action, format)
63
+ dispatch = -> { cue_actor(callable, format:) }
58
64
  collector.public_send(format, &dispatch)
59
65
  end
60
66
  end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :markup: markdown
4
+
5
+ require "active_support/deprecation"
6
+
7
+ module MimeActor
8
+ def self.deprecator
9
+ @deprecator ||= ActiveSupport::Deprecation.new("0.7.0", "MimeActor")
10
+ end
11
+ end
12
+
13
+ [
14
+ [MimeActor::Rescue::ClassMethods, { rescue_actor: "use #rescue_actor instance method" }],
15
+ [MimeActor::Scene::ClassMethods, { act_on_format: :respond_act_to }],
16
+ [MimeActor::Stage::ClassMethods, { actor?: "no longer supported, use Object#respond_to?" }],
17
+ [MimeActor::Stage::ClassMethods, { dispatch_cue: "no longer support anonymous proc with rescue" }],
18
+ [MimeActor::Stage::ClassMethods, { dispatch_act: "no longer support anonymous proc with rescue" }]
19
+ ].each do |klazz, *args|
20
+ MimeActor.deprecator.deprecate_methods(klazz, *args)
21
+ end
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ # :markup: markdown
4
+
5
+ module MimeActor
6
+ class Railtie < Rails::Railtie
7
+ initializer "mime_actor.deprecator", before: :load_environment_config do |app|
8
+ app.deprecators[:mime_actor] = MimeActor.deprecator
9
+ end
10
+ end
11
+ end
@@ -47,9 +47,9 @@ module MimeActor
47
47
  #
48
48
  def rescue_act_from(*klazzes, action: nil, format: nil, with: nil, &block)
49
49
  raise ArgumentError, "error filter is required" if klazzes.empty?
50
- raise ArgumentError, "provide either the with: argument or a block" unless with.present? ^ block_given?
50
+ raise ArgumentError, "provide either with: or a block" unless with.present? ^ block_given?
51
51
 
52
- validate!(:with, with) if with.present?
52
+ validate!(:callable, with) if with.present?
53
53
  with = block if block_given?
54
54
 
55
55
  validate!(:action_or_actions, action) if action.present?
@@ -84,9 +84,9 @@ module MimeActor
84
84
  def respond_act_to(*formats, on: nil, with: nil, &block)
85
85
  validate!(:formats, formats)
86
86
 
87
- raise ArgumentError, "provide either the with: argument or a block" if with.present? && block_given?
87
+ raise ArgumentError, "provide either with: or a block" if with.present? && block_given?
88
88
 
89
- validate!(:with, with) if with.present?
89
+ validate!(:callable, with) if with.present?
90
90
  with = block if block_given?
91
91
 
92
92
  raise ArgumentError, "action is required" unless (actions = on).present?
@@ -96,13 +96,13 @@ module MimeActor
96
96
  TypeError.new("#{unchecked.inspect} must be a Class/Module or a String referencing a Class/Module")
97
97
  end
98
98
 
99
- # Validate `with` must be a Symbol or Proc
99
+ # Validate `callable` must be a Symbol or Proc
100
100
  #
101
- # @param unchecked the `with` to be validated
102
- def validate_with(unchecked)
101
+ # @param unchecked the `callable` to be validated
102
+ def validate_callable(unchecked)
103
103
  return if unchecked.is_a?(Proc) || unchecked.is_a?(Symbol)
104
104
 
105
- TypeError.new("with handler must be a Symbol or Proc, got: #{unchecked.inspect}")
105
+ TypeError.new("#{unchecked.inspect} must be a Symbol or Proc")
106
106
  end
107
107
  end
108
108
  end
@@ -14,8 +14,8 @@ module MimeActor
14
14
  module VERSION
15
15
  MAJOR = 0
16
16
  MINOR = 6
17
- BUILD = 3
18
- PRE = nil
17
+ BUILD = 4
18
+ PRE = "alpha"
19
19
 
20
20
  STRING = [MAJOR, MINOR, BUILD, PRE].compact.join(".")
21
21
  end
data/lib/mime_actor.rb CHANGED
@@ -6,30 +6,21 @@
6
6
  require "mime_actor/version"
7
7
  require "mime_actor/errors"
8
8
 
9
- require "active_support/dependencies/autoload"
10
- require "active_support/deprecation"
9
+ # load mime_actor components
10
+ require "mime_actor/action"
11
+ require "mime_actor/callbacks"
12
+ require "mime_actor/logging"
13
+ require "mime_actor/rescue"
14
+ require "mime_actor/scene"
15
+ require "mime_actor/stage"
16
+ require "mime_actor/validator"
11
17
 
12
- module MimeActor
13
- extend ActiveSupport::Autoload
18
+ require "mime_actor/deprecator"
14
19
 
15
- autoload :Action
16
- autoload :Scene
17
- autoload :Stage
18
- autoload :Rescue
19
- autoload :Validator
20
- autoload :Logging
21
-
22
- def self.deprecator
23
- @deprecator ||= ActiveSupport::Deprecation.new("0.7.0", "MimeActor")
24
- end
25
-
26
- [
27
- [MimeActor::Rescue::ClassMethods, { rescue_actor: "use #rescue_actor instance method" }],
28
- [MimeActor::Scene::ClassMethods, { act_on_format: :respond_act_to }],
29
- [MimeActor::Stage::ClassMethods, { actor?: "no longer supported, use Object#respond_to?" }],
30
- [MimeActor::Stage::ClassMethods, { dispatch_cue: "no longer support anonymous proc with rescue" }],
31
- [MimeActor::Stage::ClassMethods, { dispatch_act: "no longer support anonymous proc with rescue" }]
32
- ].each do |klazz, *args|
33
- deprecator.deprecate_methods(klazz, *args)
34
- end
20
+ begin
21
+ require "rails"
22
+ rescue LoadError
23
+ # noop
35
24
  end
25
+
26
+ require "mime_actor/railtie" if defined?(Rails::Railtie)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mime_actor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 0.6.4.alpha
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ryan Chang
@@ -65,9 +65,11 @@ files:
65
65
  - lib/mime_actor.rb
66
66
  - lib/mime_actor/action.rb
67
67
  - lib/mime_actor/callbacks.rb
68
+ - lib/mime_actor/deprecator.rb
68
69
  - lib/mime_actor/dispatcher.rb
69
70
  - lib/mime_actor/errors.rb
70
71
  - lib/mime_actor/logging.rb
72
+ - lib/mime_actor/railtie.rb
71
73
  - lib/mime_actor/rescue.rb
72
74
  - lib/mime_actor/scene.rb
73
75
  - lib/mime_actor/stage.rb