forked from heartcombo/responders
/
flash_responder.rb
167 lines (150 loc) · 4.95 KB
/
flash_responder.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
module Responders
# Responder to automatically set flash messages based on I18n API. It checks for
# message based on the current action, but also allows defaults to be set, using
# the following order:
#
# flash.controller_name.action_name.status
# flash.actions.action_name.status
#
# So, if you have a CarsController, create action, it will check for:
#
# flash.cars.create.status
# flash.actions.create.status
#
# The statuses by default are :notice (when the object can be created, updated
# or destroyed with success) and :alert (when the objecy cannot be created
# or updated).
#
# On I18n, the resource_name given is available as interpolation option,
# this means you can set:
#
# flash:
# actions:
# create:
# notice: "Hooray! %{resource_name} was successfully created!"
#
# But sometimes, flash messages are not that simple. Going back
# to cars example, you might want to say the brand of the car when it's
# updated. Well, that's easy also:
#
# flash:
# cars:
# update:
# notice: "Hooray! You just tuned your %{car_brand}!"
#
# Since :car_name is not available for interpolation by default, you have
# to overwrite interpolation_options in your controller.
#
# def interpolation_options
# { :car_brand => @car.brand }
# end
#
# Then you will finally have:
#
# 'Hooray! You just tuned your Aston Martin!'
#
# If your controller is namespaced, for example Admin::CarsController,
# the messages will be checked in the following order:
#
# flash.admin.cars.create.status
# flash.admin.actions.create.status
# flash.cars.create.status
# flash.actions.create.status
#
# == Options
#
# FlashResponder also accepts some options through respond_with API.
#
# * :flash - When set to false, no flash message is set.
#
# respond_with(@user, :flash => true)
#
# * :notice - Supply the message to be set if the record has no errors.
# * :alert - Supply the message to be set if the record has errors.
#
# respond_with(@user, :notice => "Hooray! Welcome!", :alert => "Woot! You failed.")
#
# * :flash_now - Sets the flash message using flash.now. Accepts true, :on_failure or :on_sucess.
#
# == Configure status keys
#
# As said previously, FlashResponder by default use :notice and :alert
# keys. You can change that by setting the status_keys:
#
# Responders::FlashResponder.flash_keys = [ :success, :failure ]
#
# However, the options :notice and :alert to respond_with are kept :notice
# and :alert.
#
module FlashResponder
mattr_accessor :flash_keys
@@flash_keys = [ :notice, :alert ]
def initialize(controller, resources, options={})
super
@flash = options.delete(:flash)
@notice = options.delete(:notice)
@alert = options.delete(:alert)
@flash_now = options.delete(:flash_now) { :on_failure }
end
def to_html
set_flash_message! if set_flash_message?
super
end
def to_js
set_flash_message! if set_flash_message?
defined?(super) ? super : to_format
end
protected
def set_flash_message!
if has_errors?
set_flash(:alert, @alert)
status = Responders::FlashResponder.flash_keys.last
else
set_flash(:notice, @notice)
status = Responders::FlashResponder.flash_keys.first
end
return if controller.flash[status].present?
options = mount_i18n_options(status)
message = ::I18n.t options[:default].shift, options
set_flash(status, message)
end
def set_flash(key, value)
return if value.blank?
flash = controller.flash
flash = flash.now if set_flash_now?
flash[key] ||= value
end
def set_flash_now?
(@flash_now == true) || (has_errors? ? @flash_now == :on_failure : @flash_now == :on_success) || (format == :js)
end
def set_flash_message? #:nodoc:
!get? && @flash != false
end
def mount_i18n_options(status) #:nodoc:
resource_name = if resource.class.respond_to?(:model_name)
resource.class.model_name.human
else
resource.class.name.underscore.humanize
end
options = {
:default => flash_defaults_by_namespace(status),
:resource_name => resource_name,
:downcase_resource_name => resource_name.downcase
}
if controller.respond_to?(:interpolation_options, true)
options.merge!(controller.send(:interpolation_options))
end
options
end
def flash_defaults_by_namespace(status) #:nodoc:
defaults = []
slices = controller.controller_path.split('/')
while slices.size > 0
defaults << :"flash.#{slices.fill(controller.controller_name, -1).join('.')}.#{controller.action_name}.#{status}"
defaults << :"flash.#{slices.fill(:actions, -1).join('.')}.#{controller.action_name}.#{status}"
slices.shift
end
defaults << ""
end
end
end