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
72
73
# File 'lib/flipper/ui/action.rb', line 62

def initialize(flipper, request)
  @flipper = flipper
  @request = 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.


175
176
177
178
# File 'lib/flipper/ui/action.rb', line 175

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

#csrf_input_tagObject



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

def csrf_input_tag
  %(<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.



106
107
108
# File 'lib/flipper/ui/action.rb', line 106

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.



153
154
155
# File 'lib/flipper/ui/action.rb', line 153

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.



127
128
129
130
131
# File 'lib/flipper/ui/action.rb', line 127

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

#public_pathObject

Private



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

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.



136
137
138
139
140
# File 'lib/flipper/ui/action.rb', line 136

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.



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

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.



78
79
80
81
82
83
84
85
# File 'lib/flipper/ui/action.rb', line 78

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.



98
99
100
# File 'lib/flipper/ui/action.rb', line 98

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

#script_nameObject

Internal: The path the app is mounted at.



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

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.



145
146
147
# File 'lib/flipper/ui/action.rb', line 145

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

#valid_request_method?Boolean

Returns:

  • (Boolean)


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

def valid_request_method?
  VALID_REQUEST_METHOD_NAMES.include?(request_method_name)
end

#view(name) ⇒ Object

Private



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

def view(name)
  path = views_path.join("#{name}.erb")

  raise "Template does not exist: #{path}" unless path.exist?

  contents = path.read
  compiled = Eruby.new(contents)
  compiled.result proc {}.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.



115
116
117
118
119
# File 'lib/flipper/ui/action.rb', line 115

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



181
182
183
# File 'lib/flipper/ui/action.rb', line 181

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

#view_without_layout(name) ⇒ Object

Private



186
187
188
# File 'lib/flipper/ui/action.rb', line 186

def view_without_layout(name)
  view name
end

#views_pathObject

Private



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

def views_path
  self.class.views_path
end