-
Notifications
You must be signed in to change notification settings - Fork 92
/
gflash.rb
86 lines (73 loc) · 2.42 KB
/
gflash.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
module Gritter
module Gflash
def redirect_to(options = {}, response_status_and_flash = {})
if response_status_and_flash.has_key?(:gflash)
gflash(response_status_and_flash[:gflash])
response_status_and_flash.delete(:gflash)
end
super(options, response_status_and_flash)
end
def gflash *args
session[:gflash] ||= {}
options = args.extract_options!
if args.size == 1 && args.first.is_a?(Array)
args = args.first
end
flash_now = args.include?(:now)
args.delete(:now) if flash_now
if args.present?
args.each do |key|
gflash_push(key, gflash_translation(key, options[:locals]), flash_now)
end
end
options.except(:locals).each do |key, value|
if value.is_a?(Hash)
if value.has_key?(:value)
value[:value] = gflash_text(key, value[:value], value[:locals])
else
value[:value] = gflash_translation(key, value[:locals])
end
else
value = gflash_text(key, value, options[:locals])
end
gflash_push(key, value, flash_now)
end
end
private
def gflash_text(key, value, options={})
value == true ? gflash_translation(key, options) : value
end
def gflash_push(key, value, now=false)
session[:gflash][key] ||= []
session[:gflash][key].push(value)
if Gritter.rails_flash_fallback
if now
flash.now[key] ||= []
flash.now[key].push(value.is_a?(Hash) ? value[:value] : value)
else
flash[key] ||= []
flash[key].push(value.is_a?(Hash) ? value[:value] : value)
end
end
end
def gflash_translation(key, options)
options ||= {}
i18n_default_key = "gflash.defaults.#{key}"
i18n_default_action_key = "gflash.defaults.#{params[:action]}.#{key}"
i18n_key = "gflash.#{params[:controller]}.#{params[:action]}.#{key}"
i18n_key.gsub!(/\//, ".")
begin
options[:raise] = true
translation = I18n.t(i18n_key, options)
rescue I18n::MissingTranslationData
begin
translation = I18n.t(i18n_default_action_key, options)
rescue I18n::MissingTranslationData
options.delete(:raise)
translation = I18n.t(i18n_default_key, options)
end
end
translation
end
end
end