class Flipper::UI::Action
Constants
- CONTENT_SECURITY_POLICY
- SCRIPT_SRCS
- SOURCES
- STYLE_SRCS
- VALID_REQUEST_METHOD_NAMES
Attributes
Public: The instance of the Flipper::DSL the middleware was initialized with.
Public: The Rack::Request to provide a response for.
Public Class Methods
# File lib/flipper/ui/action.rb, line 108 def initialize(flipper, request) @flipper = flipper @request = request @code = 200 @headers = { 'Content-Type' => 'text/plain' } @breadcrumbs = if Flipper::UI.configuration.application_breadcrumb_href [Breadcrumb.new('App', Flipper::UI.configuration.application_breadcrumb_href)] else [] end end
Private: The path to the public folder.
# File lib/flipper/ui/action.rb, line 94 def self.public_path @public_path ||= Flipper::UI.root.join('public') end
Public: Call this in subclasses so the action knows its route.
regex - The Regexp that this action should run for.
Returns nothing.
# File lib/flipper/ui/action.rb, line 64 def self.route(regex) @route_regex = regex end
Internal: Does this action's route match the path.
# File lib/flipper/ui/action.rb, line 69 def self.route_match?(path) path.match(route_regex) end
Internal: The regex that matches which routes this action will work for.
# File lib/flipper/ui/action.rb, line 74 def self.route_regex @route_regex || raise("#{name}.route is not set") end
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
.
# File lib/flipper/ui/action.rb, line 84 def self.run(flipper, request) new(flipper, request).run end
Private: The path to the views folder.
# File lib/flipper/ui/action.rb, line 89 def self.views_path @views_path ||= Flipper::UI.root.join('views') end
Public Instance Methods
# File lib/flipper/ui/action.rb, line 272 def bootstrap_css SOURCES[:bootstrap_css] end
# File lib/flipper/ui/action.rb, line 276 def bootstrap_js SOURCES[:bootstrap_js] end
# File lib/flipper/ui/action.rb, line 264 def csrf_input_tag %(<input type="hidden" name="authenticity_token" value="#{@request.session[:csrf]}">) end
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.
# File lib/flipper/ui/action.rb, line 152 def halt(response) throw :halt, response end
Public: Set a header.
name - The String name of the header. value - The value of the header.
# File lib/flipper/ui/action.rb, line 200 def header(name, value) @headers[name] = value end
# File lib/flipper/ui/action.rb, line 284 def jquery_js SOURCES[:jquery_js] end
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.
# File lib/flipper/ui/action.rb, line 174 def json_response(object) header 'Content-Type', 'application/json' body = JSON.dump(object) halt [@code, @headers, [body]] end
# File lib/flipper/ui/action.rb, line 280 def popper_js SOURCES[:popper_js] end
Private
# File lib/flipper/ui/action.rb, line 255 def public_path self.class.public_path end
Public: Redirect to a new location.
location - The String location to set the Location header to.
# File lib/flipper/ui/action.rb, line 183 def redirect_to(location) status 302 header 'Location', "#{script_name}#{Rack::Utils.escape_path(location)}" halt [@code, @headers, ['']] end
Private: Returns the request method converted to an action method.
# File lib/flipper/ui/action.rb, line 260 def request_method_name @request_method_name ||= @request.request_method.downcase end
Public: Runs the request method for the provided request.
Returns whatever the request method returns in the action.
# File lib/flipper/ui/action.rb, line 124 def run if valid_request_method? && 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
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.
# File lib/flipper/ui/action.rb, line 144 def run_other_action(action_class) action_class.new(flipper, request).run end
Internal: The path the app is mounted at.
# File lib/flipper/ui/action.rb, line 245 def script_name request.env['SCRIPT_NAME'] end
Public: Set the status code for the response.
code - The Integer code you would like the response to return.
# File lib/flipper/ui/action.rb, line 192 def status(code) @code = code.to_i end
# File lib/flipper/ui/action.rb, line 268 def valid_request_method? VALID_REQUEST_METHOD_NAMES.include?(request_method_name) end
Private
# File lib/flipper/ui/action.rb, line 238 def view(name) path = views_path.join("#{name}.erb") raise "Template does not exist: #{path}" unless path.exist? eval(Erubi::Engine.new(path.read, escape: true).src) end
Public: Compiles a view and returns rack response with that as the body.
name - The Symbol name of the view.
Returns a response.
# File lib/flipper/ui/action.rb, line 161 def view_response(name) header 'Content-Type', 'text/html' header 'Content-Security-Policy', CONTENT_SECURITY_POLICY body = view_with_layout { view_without_layout name } halt [@code, @headers, [body]] end
Private
# File lib/flipper/ui/action.rb, line 228 def view_with_layout(&block) view :layout, &block end
Private
# File lib/flipper/ui/action.rb, line 233 def view_without_layout(name) view name end
Private
# File lib/flipper/ui/action.rb, line 250 def views_path self.class.views_path end