Class: Uchi::ActionResponse
- Inherits:
-
Object
- Object
- Uchi::ActionResponse
- Defined in:
- lib/uchi/action_response.rb
Overview
Represents the response from executing an action.
ActionResponse uses a builder pattern for fluent chaining:
ActionResponse.success("Done").redirect_to(path: "/posts")
ActionResponse.error("Failed").("Try again later")
ActionResponse.success("Exported").download(file_path: path, filename: "data.csv")
Instance Attribute Summary collapse
-
#file_path ⇒ Object
readonly
Returns the value of attribute file_path.
-
#filename ⇒ Object
readonly
Returns the value of attribute filename.
-
#message_text ⇒ Object
readonly
Returns the value of attribute message_text.
-
#redirect_path ⇒ Object
readonly
Returns the value of attribute redirect_path.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
-
#turbo_stream_block ⇒ Object
readonly
Returns the value of attribute turbo_stream_block.
Class Method Summary collapse
-
.error(message = nil) ⇒ ActionResponse
Creates an error response with an optional message.
-
.success(message = nil) ⇒ ActionResponse
Creates a successful response with an optional message.
Instance Method Summary collapse
-
#download(file_path:, filename:) ⇒ ActionResponse
Sets a file to download.
-
#download? ⇒ Boolean
Returns true if this response includes a download.
-
#error? ⇒ Boolean
Returns true if this is an error response.
-
#initialize(status:, message: nil) ⇒ ActionResponse
constructor
A new instance of ActionResponse.
-
#message(text) ⇒ ActionResponse
Sets the message to display.
-
#redirect? ⇒ Boolean
Returns true if this response includes a redirect.
-
#redirect_to(path:) ⇒ ActionResponse
Sets a redirect path.
-
#success? ⇒ Boolean
Returns true if this is a success response.
-
#turbo_stream {|stream| ... } ⇒ ActionResponse
Sets a custom Turbo Stream response.
-
#turbo_stream? ⇒ Boolean
Returns true if this response includes a custom Turbo Stream.
Constructor Details
#initialize(status:, message: nil) ⇒ ActionResponse
Returns a new instance of ActionResponse.
30 31 32 33 |
# File 'lib/uchi/action_response.rb', line 30 def initialize(status:, message: nil) @status = status @message_text = end |
Instance Attribute Details
#file_path ⇒ Object (readonly)
Returns the value of attribute file_path.
12 13 14 |
# File 'lib/uchi/action_response.rb', line 12 def file_path @file_path end |
#filename ⇒ Object (readonly)
Returns the value of attribute filename.
12 13 14 |
# File 'lib/uchi/action_response.rb', line 12 def filename @filename end |
#message_text ⇒ Object (readonly)
Returns the value of attribute message_text.
12 13 14 |
# File 'lib/uchi/action_response.rb', line 12 def @message_text end |
#redirect_path ⇒ Object (readonly)
Returns the value of attribute redirect_path.
12 13 14 |
# File 'lib/uchi/action_response.rb', line 12 def redirect_path @redirect_path end |
#status ⇒ Object (readonly)
Returns the value of attribute status.
12 13 14 |
# File 'lib/uchi/action_response.rb', line 12 def status @status end |
#turbo_stream_block ⇒ Object (readonly)
Returns the value of attribute turbo_stream_block.
12 13 14 |
# File 'lib/uchi/action_response.rb', line 12 def turbo_stream_block @turbo_stream_block end |
Class Method Details
.error(message = nil) ⇒ ActionResponse
Creates an error response with an optional message.
26 27 28 |
# File 'lib/uchi/action_response.rb', line 26 def self.error( = nil) new(status: :error, message: ) end |
.success(message = nil) ⇒ ActionResponse
Creates a successful response with an optional message.
18 19 20 |
# File 'lib/uchi/action_response.rb', line 18 def self.success( = nil) new(status: :success, message: ) end |
Instance Method Details
#download(file_path:, filename:) ⇒ ActionResponse
Sets a file to download.
58 59 60 61 62 |
# File 'lib/uchi/action_response.rb', line 58 def download(file_path:, filename:) @file_path = file_path @filename = filename self end |
#download? ⇒ Boolean
Returns true if this response includes a download.
97 98 99 |
# File 'lib/uchi/action_response.rb', line 97 def download? !file_path.nil? end |
#error? ⇒ Boolean
Returns true if this is an error response.
83 84 85 |
# File 'lib/uchi/action_response.rb', line 83 def error? status == :error end |
#message(text) ⇒ ActionResponse
Sets the message to display.
39 40 41 42 |
# File 'lib/uchi/action_response.rb', line 39 def (text) @message_text = text self end |
#redirect? ⇒ Boolean
Returns true if this response includes a redirect.
90 91 92 |
# File 'lib/uchi/action_response.rb', line 90 def redirect? !redirect_path.nil? end |
#redirect_to(path:) ⇒ ActionResponse
Sets a redirect path.
48 49 50 51 |
# File 'lib/uchi/action_response.rb', line 48 def redirect_to(path:) @redirect_path = path self end |
#success? ⇒ Boolean
Returns true if this is a success response.
76 77 78 |
# File 'lib/uchi/action_response.rb', line 76 def success? status == :success end |
#turbo_stream {|stream| ... } ⇒ ActionResponse
Sets a custom Turbo Stream response.
68 69 70 71 |
# File 'lib/uchi/action_response.rb', line 68 def turbo_stream(&block) @turbo_stream_block = block self end |
#turbo_stream? ⇒ Boolean
Returns true if this response includes a custom Turbo Stream.
104 105 106 |
# File 'lib/uchi/action_response.rb', line 104 def turbo_stream? !turbo_stream_block.nil? end |