Class: Flipper::UI::Action
- Inherits:
-
Object
- Object
- Flipper::UI::Action
- Extended by:
- Forwardable
- Defined in:
- lib/flipper/ui/action.rb
Direct Known Subclasses
Flipper::UI::Actions::ActorsGate, Flipper::UI::Actions::AddFeature, Flipper::UI::Actions::BooleanGate, Flipper::UI::Actions::Feature, Flipper::UI::Actions::Features, Flipper::UI::Actions::File, Flipper::UI::Actions::Gate, Flipper::UI::Actions::GroupsGate, Flipper::UI::Actions::Home, Flipper::UI::Actions::PercentageOfActorsGate, Flipper::UI::Actions::PercentageOfTimeGate
Defined Under Namespace
Classes: Breadcrumb
Instance Attribute Summary collapse
-
#flipper ⇒ Object
readonly
Public: The instance of the Flipper::DSL the middleware was initialized with.
-
#request ⇒ Object
readonly
Public: The Rack::Request to provide a response for.
Class Method Summary collapse
-
.public_path ⇒ Object
Private: The path to the public folder.
-
.regex ⇒ Object
Internal: The regex that matches which routes this action will work for.
-
.route(regex) ⇒ Object
Public: Call this in subclasses so the action knows its route.
-
.run(flipper, request) ⇒ Object
Internal: Initializes and runs an action for a given request.
-
.views_path ⇒ Object
Private: The path to the views folder.
Instance Method Summary collapse
-
#breadcrumb(text, href = nil) ⇒ Object
Public: Add a breadcrumb to the trail.
- #csrf_input_tag ⇒ Object
-
#halt(response) ⇒ Object
Public: Call this with a response to immediately stop the current action and respond however you want.
-
#header(name, value) ⇒ Object
Public: Set a header.
-
#initialize(flipper, request) ⇒ Action
constructor
A new instance of Action.
-
#json_response(object) ⇒ Object
Public: Dumps an object as json and returns rack response with that as the body.
-
#public_path ⇒ Object
Private.
-
#redirect_to(location) ⇒ Object
Public: Redirect to a new location.
-
#request_method_name ⇒ Object
Private: Returns the request method converted to an action method.
-
#run ⇒ Object
Public: Runs the request method for the provided request.
-
#run_other_action(action_class) ⇒ Object
Public: Runs another action from within the request method of a different action.
-
#script_name ⇒ Object
Internal: The path the app is mounted at.
-
#status(code) ⇒ Object
Public: Set the status code for the response.
-
#view(name) ⇒ Object
Private.
-
#view_response(name) ⇒ Object
Public: Compiles a view and returns rack response with that as the body.
-
#view_with_layout(&block) ⇒ Object
Private.
-
#view_without_layout(name) ⇒ Object
Private.
-
#views_path ⇒ Object
Private.
Constructor Details
#initialize(flipper, request) ⇒ Action
Returns a new instance of Action.
55 56 57 58 59 60 61 62 63 64 |
# File 'lib/flipper/ui/action.rb', line 55 def initialize(flipper, request) @flipper, @request = flipper, request @code = 200 @headers = {"Content-Type" => "text/plain"} @breadcrumbs = if Flipper::UI. [Breadcrumb.new("App", Flipper::UI.)] else [] end end |
Instance Attribute Details
#flipper ⇒ Object (readonly)
Public: The instance of the Flipper::DSL the middleware was initialized with.
47 48 49 |
# File 'lib/flipper/ui/action.rb', line 47 def flipper @flipper end |
#request ⇒ Object (readonly)
Public: The Rack::Request to provide a response for.
50 51 52 |
# File 'lib/flipper/ui/action.rb', line 50 def request @request end |
Class Method Details
.public_path ⇒ Object
Private: The path to the public folder.
41 42 43 |
# File 'lib/flipper/ui/action.rb', line 41 def self.public_path @public_path ||= Flipper::UI.root.join('public') end |
.regex ⇒ Object
Internal: The regex that matches which routes this action will work for.
31 32 33 |
# File 'lib/flipper/ui/action.rb', line 31 def self.regex @regex || raise("#{name}.route is not set") end |
.route(regex) ⇒ Object
Public: Call this in subclasses so the action knows its route.
regex - The Regexp that this action should run for.
Returns nothing.
16 17 18 |
# File 'lib/flipper/ui/action.rb', line 16 def self.route(regex) @regex = regex end |
.run(flipper, request) ⇒ Object
Internal: Initializes and runs an action for a given request.
flipper - The Flipper::DSL instance. request - The Rack::Request that was sent.
Returns result of Action#run.
26 27 28 |
# File 'lib/flipper/ui/action.rb', line 26 def self.run(flipper, request) new(flipper, request).run end |
Instance Method Details
#breadcrumb(text, href = nil) ⇒ Object
Public: Add a breadcrumb to the trail.
text - The String text for the breadcrumb. href - The String href for the anchor tag (optional). If nil, breadcrumb
is assumed to be the end of the trail.
165 166 167 168 |
# File 'lib/flipper/ui/action.rb', line 165 def (text, href = nil) = href.nil? ? href : "#{script_name}#{href}" @breadcrumbs << Breadcrumb.new(text, ) end |
#csrf_input_tag ⇒ Object
213 214 215 |
# File 'lib/flipper/ui/action.rb', line 213 def csrf_input_tag %Q(<input type="hidden" name="authenticity_token" value="#{@request.session[:csrf]}">) end |
#halt(response) ⇒ Object
Public: Call this with a response to immediately stop the current action and respond however you want.
response - The response you would like to return.
96 97 98 |
# File 'lib/flipper/ui/action.rb', line 96 def halt(response) throw :halt, response end |
#header(name, value) ⇒ Object
Public: Set a header.
name - The String name of the header. value - The value of the header.
143 144 145 |
# File 'lib/flipper/ui/action.rb', line 143 def header(name, value) @headers[name] = value end |
#json_response(object) ⇒ Object
Public: Dumps an object as json and returns rack response with that as the body. Automatically sets Content-Type to “application/json”.
object - The Object that should be dumped as json.
Returns a response.
117 118 119 120 121 |
# File 'lib/flipper/ui/action.rb', line 117 def json_response(object) header 'Content-Type', 'application/json' body = JSON.dump(object) halt [@code, @headers, [body]] end |
#public_path ⇒ Object
Private
204 205 206 |
# File 'lib/flipper/ui/action.rb', line 204 def public_path self.class.public_path end |
#redirect_to(location) ⇒ Object
Public: Redirect to a new location.
location - The String location to set the Location header to.
126 127 128 129 130 |
# File 'lib/flipper/ui/action.rb', line 126 def redirect_to(location) status 302 header "Location", "#{script_name}#{location}" halt [@code, @headers, [""]] end |
#request_method_name ⇒ Object
Private: Returns the request method converted to an action method.
209 210 211 |
# File 'lib/flipper/ui/action.rb', line 209 def request_method_name @request_method_name ||= @request.request_method.downcase end |
#run ⇒ Object
Public: Runs the request method for the provided request.
Returns whatever the request method returns in the action.
69 70 71 72 73 74 75 |
# File 'lib/flipper/ui/action.rb', line 69 def run if respond_to?(request_method_name) catch(:halt) { send(request_method_name) } else raise UI::RequestMethodNotSupported, "#{self.class} does not support request method #{request_method_name.inspect}" end end |
#run_other_action(action_class) ⇒ Object
Public: Runs another action from within the request method of a different action.
action_class - The class of the other action to run.
Examples
run_other_action Home
# => result of running Home action
Returns result of other action.
88 89 90 |
# File 'lib/flipper/ui/action.rb', line 88 def run_other_action(action_class) action_class.new(flipper, request).run end |
#script_name ⇒ Object
Internal: The path the app is mounted at.
194 195 196 |
# File 'lib/flipper/ui/action.rb', line 194 def script_name request.env['SCRIPT_NAME'] end |
#status(code) ⇒ Object
Public: Set the status code for the response.
code - The Integer code you would like the response to return.
135 136 137 |
# File 'lib/flipper/ui/action.rb', line 135 def status(code) @code = code.to_i end |
#view(name) ⇒ Object
Private
181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/flipper/ui/action.rb', line 181 def view(name) path = views_path.join("#{name}.erb") unless path.exist? raise "Template does not exist: #{path}" end contents = path.read compiled = Eruby.new(contents) compiled.result Proc.new {}.binding end |
#view_response(name) ⇒ Object
Public: Compiles a view and returns rack response with that as the body.
name - The Symbol name of the view.
Returns a response.
105 106 107 108 109 |
# File 'lib/flipper/ui/action.rb', line 105 def view_response(name) header "Content-Type", "text/html" body = view_with_layout { view_without_layout name } halt [@code, @headers, [body]] end |
#view_with_layout(&block) ⇒ Object
Private
171 172 173 |
# File 'lib/flipper/ui/action.rb', line 171 def view_with_layout(&block) view :layout, &block end |
#view_without_layout(name) ⇒ Object
Private
176 177 178 |
# File 'lib/flipper/ui/action.rb', line 176 def view_without_layout(name) view name end |
#views_path ⇒ Object
Private
199 200 201 |
# File 'lib/flipper/ui/action.rb', line 199 def views_path self.class.views_path end |