class Flipper::UI::Action

Constants

CONTENT_SECURITY_POLICY
SCRIPT_SRCS
SOURCES
STYLE_SRCS
VALID_REQUEST_METHOD_NAMES

Attributes

flipper[R]

Public: The instance of the Flipper::DSL the middleware was initialized with.

request[R]

Public: The Rack::Request to provide a response for.

Public Class Methods

new(flipper, request) click to toggle source
# 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
public_path() click to toggle source

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
route(regex) click to toggle source

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
route_match?(path) click to toggle source

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
route_regex() click to toggle source

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
run(flipper, request) click to toggle source

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
views_path() click to toggle source

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

bootstrap_css() click to toggle source
# File lib/flipper/ui/action.rb, line 272
def bootstrap_css
  SOURCES[:bootstrap_css]
end
bootstrap_js() click to toggle source
# File lib/flipper/ui/action.rb, line 276
def bootstrap_js
  SOURCES[:bootstrap_js]
end
breadcrumb(text, href = nil) click to toggle source

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.
csrf_input_tag() click to toggle source
# File lib/flipper/ui/action.rb, line 264
def csrf_input_tag
  %(<input type="hidden" name="authenticity_token" value="#{@request.session[:csrf]}">)
end
halt(response) click to toggle source

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
header(name, value) click to toggle source

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
jquery_js() click to toggle source
# File lib/flipper/ui/action.rb, line 284
def jquery_js
  SOURCES[:jquery_js]
end
json_response(object) click to toggle source

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
popper_js() click to toggle source
# File lib/flipper/ui/action.rb, line 280
def popper_js
  SOURCES[:popper_js]
end
public_path() click to toggle source

Private

# File lib/flipper/ui/action.rb, line 255
def public_path
  self.class.public_path
end
redirect_to(location) click to toggle source

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
request_method_name() click to toggle source

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
run() click to toggle source

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
run_other_action(action_class) click to toggle source

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
script_name() click to toggle source

Internal: The path the app is mounted at.

# File lib/flipper/ui/action.rb, line 245
def script_name
  request.env['SCRIPT_NAME']
end
status(code) click to toggle source

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
valid_request_method?() click to toggle source
# File lib/flipper/ui/action.rb, line 268
def valid_request_method?
  VALID_REQUEST_METHOD_NAMES.include?(request_method_name)
end
view(name) click to toggle source

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
view_response(name) click to toggle source

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
view_with_layout(&block) click to toggle source

Private

# File lib/flipper/ui/action.rb, line 228
def view_with_layout(&block)
  view :layout, &block
end
view_without_layout(name) click to toggle source

Private

# File lib/flipper/ui/action.rb, line 233
def view_without_layout(name)
  view name
end
views_path() click to toggle source

Private

# File lib/flipper/ui/action.rb, line 250
def views_path
  self.class.views_path
end