Class: Flipper::UI::Action

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Defined in:
lib/flipper/ui/action.rb

Defined Under Namespace

Classes: Breadcrumb

Constant Summary collapse

VALID_REQUEST_METHOD_NAMES =
Set.new([
  "get".freeze,
  "post".freeze,
  "put".freeze,
  "delete".freeze,
]).freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(flipper, request) ⇒ Action

Returns a new instance of Action.



62
63
64
65
66
67
68
69
70
71
# File 'lib/flipper/ui/action.rb', line 62

def initialize(flipper, request)
  @flipper, @request = flipper, request
  @code = 200
  @headers = {"Content-Type" => "text/plain"}
  @breadcrumbs = if Flipper::UI.application_breadcrumb_href
    [Breadcrumb.new("App", Flipper::UI.application_breadcrumb_href)]
  else
    []
  end
end

Instance Attribute Details

#flipperObject (readonly)

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



54
55
56
# File 'lib/flipper/ui/action.rb', line 54

def flipper
  @flipper
end

#requestObject (readonly)

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



57
58
59
# File 'lib/flipper/ui/action.rb', line 57

def request
  @request
end

Class Method Details

.public_pathObject

Private: The path to the public folder.



48
49
50
# File 'lib/flipper/ui/action.rb', line 48

def self.public_path
  @public_path ||= Flipper::UI.root.join('public')
end

.regexObject

Internal: The regex that matches which routes this action will work for.



38
39
40
# File 'lib/flipper/ui/action.rb', line 38

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.



23
24
25
# File 'lib/flipper/ui/action.rb', line 23

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.



33
34
35
# File 'lib/flipper/ui/action.rb', line 33

def self.run(flipper, request)
  new(flipper, request).run
end

.views_pathObject

Private: The path to the views folder.



43
44
45
# File 'lib/flipper/ui/action.rb', line 43

def self.views_path
  @views_path ||= Flipper::UI.root.join('views')
end

Instance Method Details

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.


172
173
174
175
# File 'lib/flipper/ui/action.rb', line 172

def breadcrumb(text, href = nil)
  breadcrumb_href = href.nil? ? href : "#{script_name}#{href}"
  @breadcrumbs << Breadcrumb.new(text, breadcrumb_href)
end

#csrf_input_tagObject



220
221
222
# File 'lib/flipper/ui/action.rb', line 220

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.



103
104
105
# File 'lib/flipper/ui/action.rb', line 103

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.



150
151
152
# File 'lib/flipper/ui/action.rb', line 150

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.



124
125
126
127
128
# File 'lib/flipper/ui/action.rb', line 124

def json_response(object)
  header 'Content-Type', 'application/json'
  body = JSON.dump(object)
  halt [@code, @headers, [body]]
end

#public_pathObject

Private



211
212
213
# File 'lib/flipper/ui/action.rb', line 211

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.



133
134
135
136
137
# File 'lib/flipper/ui/action.rb', line 133

def redirect_to(location)
  status 302
  header "Location", "#{script_name}#{location}"
  halt [@code, @headers, [""]]
end

#request_method_nameObject

Private: Returns the request method converted to an action method.



216
217
218
# File 'lib/flipper/ui/action.rb', line 216

def request_method_name
  @request_method_name ||= @request.request_method.downcase
end

#runObject

Public: Runs the request method for the provided request.

Returns whatever the request method returns in the action.



76
77
78
79
80
81
82
# File 'lib/flipper/ui/action.rb', line 76

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) ⇒ 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.



95
96
97
# File 'lib/flipper/ui/action.rb', line 95

def run_other_action(action_class)
  action_class.new(flipper, request).run
end

#script_nameObject

Internal: The path the app is mounted at.



201
202
203
# File 'lib/flipper/ui/action.rb', line 201

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.



142
143
144
# File 'lib/flipper/ui/action.rb', line 142

def status(code)
  @code = code.to_i
end

#valid_request_method?Boolean

Returns:

  • (Boolean)


224
225
226
# File 'lib/flipper/ui/action.rb', line 224

def valid_request_method?
  VALID_REQUEST_METHOD_NAMES.include?(request_method_name)
end

#view(name) ⇒ Object

Private



188
189
190
191
192
193
194
195
196
197
198
# File 'lib/flipper/ui/action.rb', line 188

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.



112
113
114
115
116
# File 'lib/flipper/ui/action.rb', line 112

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



178
179
180
# File 'lib/flipper/ui/action.rb', line 178

def view_with_layout(&block)
  view :layout, &block
end

#view_without_layout(name) ⇒ Object

Private



183
184
185
# File 'lib/flipper/ui/action.rb', line 183

def view_without_layout(name)
  view name
end

#views_pathObject

Private



206
207
208
# File 'lib/flipper/ui/action.rb', line 206

def views_path
  self.class.views_path
end