Class: Locomotive::ActionController::Responder

Inherits:
ActionController::Responder
  • Object
show all
Includes:
Responders::FlashResponder
Defined in:
lib/locomotive/action_controller/responder.rb

Instance Method Summary collapse

Instance Method Details

#optionsObject


12
13
14
15
16
17
# File 'lib/locomotive/action_controller/responder.rb', line 12

def options
  membership  = self.controller.send(:current_membership)
  policy      = membership ? Pundit.policy(membership, resource) : nil

  super.merge(policy: policy)
end

#set_flash_now?Boolean

by default flash_now messages if the resource has errors

Returns:


8
9
10
# File 'lib/locomotive/action_controller/responder.rb', line 8

def set_flash_now?
  super || has_errors?
end

#to_htmlObject


19
20
21
22
23
24
25
26
27
# File 'lib/locomotive/action_controller/responder.rb', line 19

def to_html
  add_authenticated_header

  if controller.request.headers['X-Flash'] == 'true'
    with_flash_message(:notice, true) { super }
  else
    super
  end
end

#to_jsObject


29
30
31
32
33
34
35
36
37
# File 'lib/locomotive/action_controller/responder.rb', line 29

def to_js
  add_authenticated_header

  if controller.request.headers['X-Flash'] == 'true'
    with_flash_message(:notice, true) { super }
  else
    super
  end
end

#to_jsonObject


39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/locomotive/action_controller/responder.rb', line 39

def to_json
  add_authenticated_header

  if get?
    add_pagination_header if resource.respond_to?(:total_pages)
    display(resource)
  elsif has_errors?
    with_flash_message(:alert) do
      display resource.errors, status: :unprocessable_entity
    end
  elsif post?
    in_header = controller.request.headers['X-Flash'] == 'true'
    with_flash_message(:notice, in_header) do
      display resource, location: api_location
    end
  elsif put?
    with_flash_message do |message|
      display resource, status: :ok, location: api_location
    end
  elsif delete?
    with_flash_message do |message|
      display resource, status: :ok, location: api_location
    end
  elsif has_empty_resource_definition?
    display empty_resource, status: :ok
  else
    with_flash_message do
      head :ok
    end
  end
end