Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 205 lines (174 sloc) 6.113 kB
db045db @dhh Initial
dhh authored
1 module ActionController #:nodoc:
2 # The flash provides a way to pass temporary objects between actions. Anything you place in the flash will be exposed
6b0a647 @dhh Removed ActionController::Base#keep_flash (use flash.keep instead)
dhh authored
3 # to the very next action and then cleared out. This is a great way of doing notices and alerts, such as a create
4 # action that sets <tt>flash[:notice] = "Successfully created"</tt> before redirecting to a display action that can
5 # then expose the flash to its template. Actually, that exposure is automatically done. Example:
db045db @dhh Initial
dhh authored
6 #
39e1ac6 @lifo Merge docrails
lifo authored
7 # class PostsController < ActionController::Base
db045db @dhh Initial
dhh authored
8 # def create
9 # # save post
1c057b7 @jamis Update/clean up AP documentation (rdoc)
jamis authored
10 # flash[:notice] = "Successfully created post"
39e1ac6 @lifo Merge docrails
lifo authored
11 # redirect_to posts_path(@post)
db045db @dhh Initial
dhh authored
12 # end
13 #
39e1ac6 @lifo Merge docrails
lifo authored
14 # def show
db045db @dhh Initial
dhh authored
15 # # doesn't need to assign the flash notice to the template, that's done automatically
16 # end
17 # end
18 #
39e1ac6 @lifo Merge docrails
lifo authored
19 # show.html.erb
20 # <% if flash[:notice] %>
21 # <div class="notice"><%= flash[:notice] %></div>
22 # <% end %>
db045db @dhh Initial
dhh authored
23 #
6b0a647 @dhh Removed ActionController::Base#keep_flash (use flash.keep instead)
dhh authored
24 # This example just places a string in the flash, but you can put any object in there. And of course, you can put as
25 # many as you like at a time too. Just remember: They'll be gone by the time the next action has been performed.
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
26 #
27 # See docs on the FlashHash class for more details about the flash.
db045db @dhh Initial
dhh authored
28 module Flash
4e50a35 @josh Break up DependencyModule's dual function of providing a "depend_on" …
josh authored
29 extend ActiveSupport::Concern
8e7a87d @lifo Make ActionController::Flash work with new_base
lifo authored
30
55ee0ba Cleaning up if defined?(ActionController::Http) blocks from the pre n…
Yehuda Katz + Carl Lerche authored
31 include Session
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
32
fa57597 @dhh Added alert/notice from 2-3-stable and refactored redirect_to into ju…
dhh authored
33 included do
34 helper_method :alert, :notice
35 end
36
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
37 class FlashNow #:nodoc:
ff986f1 @dhh Flash shouldnt depend on components either [DHH]
dhh authored
38 def initialize(flash)
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
39 @flash = flash
40 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
41
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
42 def []=(k, v)
43 @flash[k] = v
44 @flash.discard(k)
45 v
46 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
47
07c494a @dhh Added a reader for flash.now, so it's possible to do stuff like flash…
dhh authored
48 def [](k)
49 @flash[k]
50 end
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
51 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
52
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
53 class FlashHash < Hash
54 def initialize #:nodoc:
55 super
df06e0b @wycats Clean up flash a bit
wycats authored
56 @used = Set.new
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
57 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
58
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
59 def []=(k, v) #:nodoc:
60 keep(k)
61 super
62 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
63
050c396 @dhh Stopped the massive bleeding of concerns into ActionController::Base.…
dhh authored
64 def update(h) #:nodoc:
dac6aae Sweep flash when filter chain is halted. Closes #6175. [Caio Chassot…
Marcel Molina authored
65 h.keys.each { |k| keep(k) }
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
66 super
67 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
68
050c396 @dhh Stopped the massive bleeding of concerns into ActionController::Base.…
dhh authored
69 alias :merge! :update
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
70
050c396 @dhh Stopped the massive bleeding of concerns into ActionController::Base.…
dhh authored
71 def replace(h) #:nodoc:
df06e0b @wycats Clean up flash a bit
wycats authored
72 @used = Set.new
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
73 super
74 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
75
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
76 # Sets a flash that will not be available to the next action, only to the current.
77 #
1c057b7 @jamis Update/clean up AP documentation (rdoc)
jamis authored
78 # flash.now[:message] = "Hello current action"
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
79 #
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
80 # This method enables you to use the flash as a central messaging system in your app.
81 # When you need to pass an object to the next action, you use the standard flash assign (<tt>[]=</tt>).
82 # When you need to pass an object to the current action, you use <tt>now</tt>, and your object will
83 # vanish when the current action is done.
84 #
85 # Entries set via <tt>now</tt> are accessed the same way as standard entries: <tt>flash['my-key']</tt>.
86 def now
6b0a647 @dhh Removed ActionController::Base#keep_flash (use flash.keep instead)
dhh authored
87 FlashNow.new(self)
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
88 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
89
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
90 # Keeps either the entire current flash or a specific flash entry available for the next action:
91 #
92 # flash.keep # keeps the entire flash
1c057b7 @jamis Update/clean up AP documentation (rdoc)
jamis authored
93 # flash.keep(:notice) # keeps only the "notice" entry, the rest of the flash is discarded
6d88a99 @dhh Added deprecation language for in_place_editor and auto_complete_fiel…
dhh authored
94 def keep(k = nil)
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
95 use(k, false)
96 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
97
ed09b62 @jeremy Documentation tweaks and fixes. Closes #9454 [sur, kampers]
jeremy authored
98 # Marks the entire flash or a single flash entry to be discarded by the end of the current action:
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
99 #
d3017e2 @jeremy Correct flash discard rdoc. Closes #8502 [kampers]
jeremy authored
100 # flash.discard # discard the entire flash at the end of the current action
101 # flash.discard(:warning) # discard only the "warning" entry at the end of the current action
6d88a99 @dhh Added deprecation language for in_place_editor and auto_complete_fiel…
dhh authored
102 def discard(k = nil)
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
103 use(k)
104 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
105
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
106 # Mark for removal entries that were kept, and delete unkept ones.
107 #
108 # This method is called automatically by filters, so you generally don't need to care about it.
109 def sweep #:nodoc:
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
110 keys.each do |k|
df06e0b @wycats Clean up flash a bit
wycats authored
111 unless @used.include?(k)
112 @used << k
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
113 else
114 delete(k)
115 @used.delete(k)
116 end
db045db @dhh Initial
dhh authored
117 end
6d88a99 @dhh Added deprecation language for in_place_editor and auto_complete_fiel…
dhh authored
118
6b0a647 @dhh Removed ActionController::Base#keep_flash (use flash.keep instead)
dhh authored
119 # clean up after keys that could have been left over by calling reject! or shift on the flash
df06e0b @wycats Clean up flash a bit
wycats authored
120 (@used - keys).each{ |k| @used.delete(k) }
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
121 end
95c839b @josh Session objects are always a hash, so we need to ensure a flash hash …
josh authored
122
df06e0b @wycats Clean up flash a bit
wycats authored
123 def store(session)
72cb6f5 @js The FlashHash and friends causes a lot of needless session storing, w…
js authored
124 return if self.empty?
df06e0b @wycats Clean up flash a bit
wycats authored
125 session["flash"] = self
72cb6f5 @js The FlashHash and friends causes a lot of needless session storing, w…
js authored
126 end
127
df06e0b @wycats Clean up flash a bit
wycats authored
128 private
129 # Used internally by the <tt>keep</tt> and <tt>discard</tt> methods
130 # use() # marks the entire flash as used
131 # use('msg') # marks the "msg" entry as used
132 # use(nil, false) # marks the entire flash as unused (keeps it around for one more action)
133 # use('msg', false) # marks the "msg" entry as unused (keeps it around for one more action)
134 # Returns the single value for the key you asked to be marked (un)used or the FlashHash itself
135 # if no key is passed.
136 def use(key = nil, used = true)
137 Array(key || keys).each { |k| used ? @used << k : @used.delete(k) }
138 return key ? self[key] : self
139 end
324fa68 @josevalim Make controller.flash public to be used in responders.
josevalim authored
140 end
141
142 # Access the contents of the flash. Use <tt>flash["notice"]</tt> to
143 # read a notice you put there or <tt>flash["notice"] = "hello"</tt>
144 # to put a new one.
145 def flash #:doc:
146 unless @_flash
147 @_flash = session["flash"] || FlashHash.new
148 @_flash.sweep
a35cf34 @dhh Added a much improved Flash module that allows for finer-grained cont…
dhh authored
149 end
6580662 @jeremy Don't put flash in session if sessions are disabled.
jeremy authored
150
324fa68 @josevalim Make controller.flash public to be used in responders.
josevalim authored
151 @_flash
152 end
153
fa57597 @dhh Added alert/notice from 2-3-stable and refactored redirect_to into ju…
dhh authored
154 # Convenience accessor for flash[:alert]
155 def alert
156 flash[:alert]
157 end
158
159 # Convenience accessor for flash[:alert]=
160 def alert=(message)
161 flash[:alert] = message
162 end
163
164 # Convenience accessor for flash[:notice]
165 def notice
166 flash[:notice]
167 end
168
169 # Convenience accessor for flash[:notice]=
170 def notice=(message)
171 flash[:notice] = message
172 end
173
174
55ee0ba Cleaning up if defined?(ActionController::Http) blocks from the pre n…
Yehuda Katz + Carl Lerche authored
175 protected
176 def process_action(method_name)
1c52bca @josh Fix warning in AC flash
josh authored
177 @_flash = nil
55ee0ba Cleaning up if defined?(ActionController::Http) blocks from the pre n…
Yehuda Katz + Carl Lerche authored
178 super
df06e0b @wycats Clean up flash a bit
wycats authored
179 @_flash.store(session) if @_flash
180 @_flash = nil
8e7a87d @lifo Make ActionController::Flash work with new_base
lifo authored
181 end
182
55ee0ba Cleaning up if defined?(ActionController::Http) blocks from the pre n…
Yehuda Katz + Carl Lerche authored
183 def reset_session
184 super
df06e0b @wycats Clean up flash a bit
wycats authored
185 @_flash = nil
55ee0ba Cleaning up if defined?(ActionController::Http) blocks from the pre n…
Yehuda Katz + Carl Lerche authored
186 end
fa57597 @dhh Added alert/notice from 2-3-stable and refactored redirect_to into ju…
dhh authored
187
188 def redirect_to(options = {}, response_status_and_flash = {}) #:doc:
189 if alert = response_status_and_flash.delete(:alert)
190 flash[:alert] = alert
191 end
192
193 if notice = response_status_and_flash.delete(:notice)
194 flash[:notice] = notice
195 end
196
197 if other_flashes = response_status_and_flash.delete(:flash)
198 flash.update(other_flashes)
199 end
200
201 super(options, response_status_and_flash)
202 end
db045db @dhh Initial
dhh authored
203 end
0ee1cb2 @jeremy Ruby 1.9 compat, consistent load paths
jeremy authored
204 end
Something went wrong with that request. Please try again.