Class: Uchi::ActionResponse

Inherits:
Object
  • Object
show all
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").message("Try again later")
ActionResponse.success("Exported").download(file_path: path, filename: "data.csv")

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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 = message
end

Instance Attribute Details

#file_pathObject (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

#filenameObject (readonly)

Returns the value of attribute filename.



12
13
14
# File 'lib/uchi/action_response.rb', line 12

def filename
  @filename
end

#message_textObject (readonly)

Returns the value of attribute message_text.



12
13
14
# File 'lib/uchi/action_response.rb', line 12

def message_text
  @message_text
end

#redirect_pathObject (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

#statusObject (readonly)

Returns the value of attribute status.



12
13
14
# File 'lib/uchi/action_response.rb', line 12

def status
  @status
end

#turbo_stream_blockObject (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.

Parameters:

  • message (String, nil) (defaults to: nil)
    • Error message to display

Returns:



26
27
28
# File 'lib/uchi/action_response.rb', line 26

def self.error(message = nil)
  new(status: :error, message: message)
end

.success(message = nil) ⇒ ActionResponse

Creates a successful response with an optional message.

Parameters:

  • message (String, nil) (defaults to: nil)
    • Success message to display

Returns:



18
19
20
# File 'lib/uchi/action_response.rb', line 18

def self.success(message = nil)
  new(status: :success, message: message)
end

Instance Method Details

#download(file_path:, filename:) ⇒ ActionResponse

Sets a file to download.

Parameters:

  • file_path (String)
    • Path to the file
  • filename (String)
    • Name for the downloaded file

Returns:



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.

Returns:

  • (Boolean)


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.

Returns:

  • (Boolean)


83
84
85
# File 'lib/uchi/action_response.rb', line 83

def error?
  status == :error
end

#message(text) ⇒ ActionResponse

Sets the message to display.

Parameters:

  • text (String)
    • The message text

Returns:



39
40
41
42
# File 'lib/uchi/action_response.rb', line 39

def message(text)
  @message_text = text
  self
end

#redirect?Boolean

Returns true if this response includes a redirect.

Returns:

  • (Boolean)


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.

Parameters:

  • path (String)
    • The path to redirect to

Returns:



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.

Returns:

  • (Boolean)


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.

Yields:

  • (stream)

    Provides a Turbo Stream builder

Returns:



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.

Returns:

  • (Boolean)


104
105
106
# File 'lib/uchi/action_response.rb', line 104

def turbo_stream?
  !turbo_stream_block.nil?
end