locale_flash 0.0.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -87,5 +87,4 @@ Given a nested Admin::UsersController, the order of fallbacks will be as follows
87
87
 
88
88
  ## TODO
89
89
  + Add an option to configure the wrapping html tags/classes
90
- + Allow options to be passed to the I18n.t call (like :locale => :fr for example)
91
90
  + Add the option of predefining common messages (like resource_created for example)
@@ -0,0 +1,66 @@
1
+ module LocaleFlash
2
+ class Flash
3
+ attr_reader :type, :controller, :action, :options, :message
4
+
5
+ def initialize(args={})
6
+ @type = args[:type]
7
+ @controller = args[:controller]
8
+ @action = args[:action]
9
+ @options = args[:options] || {}
10
+ @message = args[:message]
11
+ end
12
+
13
+ def self.from_params(type, params={})
14
+ if params.is_a?(Hash) && params[:controller] && params[:action]
15
+ new(params.merge(:type => type))
16
+ else
17
+ new(:type => type, :message => params.to_s)
18
+ end
19
+ end
20
+
21
+ def to_params
22
+ { :controller => controller,
23
+ :action => action,
24
+ :options => options }
25
+ end
26
+
27
+ def legacy?
28
+ !message.nil?
29
+ end
30
+
31
+ def controllers
32
+ controller.split('/')
33
+ end
34
+
35
+ def key
36
+ ['controllers', controllers.join('.'), action, 'flash', type].join('.').to_sym
37
+ end
38
+
39
+ def fallbacks
40
+ if controllers.size > 1
41
+ defaults = []
42
+ controllers.each_with_index do |current, i|
43
+ current = controllers[0, (controllers.size - i)].join('.')
44
+ parent = controllers[0, (controllers.size - i - 1)].join('.')
45
+ defaults << ['controllers', current, 'flash', type]
46
+ defaults << ['controllers', parent, 'flash', action, type] unless parent.empty?
47
+ end
48
+ else
49
+ defaults = [['controllers', controller, 'flash', type]]
50
+ end
51
+
52
+ defaults << ['controllers', 'flash', action, type]
53
+ defaults << ['controllers', 'flash', type]
54
+ defaults.map { |i| i.join('.').to_sym }
55
+ end
56
+
57
+ def to_html
58
+ str = if legacy?
59
+ message
60
+ else
61
+ I18n.t(key, options.merge(:default => fallbacks))
62
+ end
63
+ %Q{<div class="#{type}">#{str}</div>}
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,31 @@
1
+ module LocaleFlash
2
+ module Rails
3
+ module ActionController
4
+
5
+ private
6
+
7
+ def locale_flash(type, options={})
8
+ flash[type] = LocaleFlash::Flash.new(
9
+ :controller => controller_path,
10
+ :action => action_name,
11
+ :type => type,
12
+ :options => options
13
+ ).to_params
14
+ end
15
+
16
+ def locale_notice(options={})
17
+ locale_flash :notice, options
18
+ end
19
+
20
+ def locale_alert(options={})
21
+ locale_flash :alert, options
22
+ end
23
+ end
24
+ end
25
+ end
26
+
27
+ module ActionController
28
+ class Base
29
+ include LocaleFlash::Rails::ActionController
30
+ end
31
+ end
@@ -0,0 +1,22 @@
1
+ module LocaleFlash
2
+ module Rails
3
+ module ActionView
4
+
5
+ def locale_flash
6
+ output = ''
7
+ flash.each do |type, args|
8
+ output << LocaleFlash::Flash.from_params(type, args).to_html
9
+ end
10
+ output.respond_to?(:html_safe) ? output.html_safe : output
11
+ end
12
+
13
+ end
14
+ end
15
+ end
16
+
17
+
18
+ module ActionView
19
+ class Base
20
+ include LocaleFlash::Rails::ActionView
21
+ end
22
+ end
@@ -1,3 +1,3 @@
1
1
  module LocaleFlash
2
- VERSION = "0.0.4"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/locale_flash.rb CHANGED
@@ -1,2 +1,3 @@
1
- require 'locale_flash/action_controller'
2
- require 'locale_flash/action_view'
1
+ require 'locale_flash/flash'
2
+ require 'locale_flash/rails/action_controller'
3
+ require 'locale_flash/rails/action_view'
data/spec/fixtures/en.yml CHANGED
@@ -9,4 +9,7 @@ en:
9
9
  notice: Found in controllers.users.flash.notice
10
10
  create:
11
11
  flash:
12
- notice: Found in controllers.users.create.flash.notice
12
+ notice: Found in controllers.users.create.flash.notice
13
+ show:
14
+ flash:
15
+ notice: Showing profile for %{name}
@@ -0,0 +1,203 @@
1
+ require 'spec_helper'
2
+
3
+ describe LocaleFlash::Flash do
4
+ describe '.from_params(type, params)' do
5
+ context "when a complete Hash is passed" do
6
+ before do
7
+ @flash = LocaleFlash::Flash.from_params(:notice, {
8
+ :controller => 'admin/users',
9
+ :action => 'update',
10
+ :options => {:name => 'Chris'}
11
+ })
12
+ end
13
+
14
+ it "returns a non legacy Flash" do
15
+ @flash.should_not be_legacy
16
+ end
17
+
18
+ it "sets the type" do
19
+ @flash.type.should == :notice
20
+ end
21
+
22
+ it "sets the action" do
23
+ @flash.action.should == 'update'
24
+ end
25
+
26
+ it "sets the controller" do
27
+ @flash.controller.should == 'admin/users'
28
+ end
29
+
30
+ it "sets the options" do
31
+ @flash.options.should == {:name => 'Chris'}
32
+ end
33
+ end
34
+
35
+ context "when an invalid Hash is passed" do
36
+ before do
37
+ @flash = LocaleFlash::Flash.from_params(:notice, {
38
+ :controller => 'admin/users',
39
+ :options => {:name => 'Chris'}
40
+ # NOTE no :action
41
+ })
42
+ end
43
+
44
+ it "returns a legacy Flash" do
45
+ @flash.should be_legacy
46
+ end
47
+
48
+ it "sets the type" do
49
+ @flash.type.should == :notice
50
+ end
51
+
52
+ it "sets the message to the string version of the hash" do
53
+ @flash.message.should_not be_nil
54
+ @flash.message.should be_kind_of(String)
55
+ end
56
+ end
57
+
58
+ context "when a String is passed" do
59
+ before do
60
+ @flash = LocaleFlash::Flash.from_params(:notice, "I'm a flash")
61
+ end
62
+
63
+ it "returns a legacy Flash" do
64
+ @flash.should be_legacy
65
+ end
66
+
67
+ it "sets the type" do
68
+ @flash.type.should == :notice
69
+ end
70
+
71
+ it "sets message to the String" do
72
+ @flash.message.should == "I'm a flash"
73
+ end
74
+ end
75
+ end
76
+
77
+ describe '#to_params' do
78
+ it "returns a hash of attributes" do
79
+ LocaleFlash::Flash.new(
80
+ :type => :notice,
81
+ :controller => 'admin/users',
82
+ :action => 'show',
83
+ :options => {:foo => :bar}
84
+ ).to_params.should == {
85
+ :controller => 'admin/users',
86
+ :action => 'show',
87
+ :options => {:foo => :bar}
88
+ }
89
+ end
90
+ end
91
+
92
+ describe '#legacy?' do
93
+ it "returns true when message is set" do
94
+ LocaleFlash::Flash.new(:message => 'foo').should be_legacy
95
+ end
96
+
97
+ it "returns false when message is NOT set" do
98
+ LocaleFlash::Flash.new.should_not be_legacy
99
+ end
100
+ end
101
+
102
+ describe '#controllers' do
103
+ it "returns an array with 1 element when controller is simple" do
104
+ LocaleFlash::Flash.new(:controller => 'users').controllers.should == ['users']
105
+ end
106
+
107
+ it "returns an array of controllers when controller is nested" do
108
+ LocaleFlash::Flash.new(:controller => 'admin/users').controllers.should == ['admin', 'users']
109
+ end
110
+ end
111
+
112
+ describe '#key' do
113
+ it "returns the correct key for a simple controller" do
114
+ LocaleFlash::Flash.new(
115
+ :controller => 'users',
116
+ :action => 'update',
117
+ :type => :notice
118
+ ).key.should == :'controllers.users.update.flash.notice'
119
+ end
120
+
121
+ it "returns the correct key for a newsted controller" do
122
+ LocaleFlash::Flash.new(
123
+ :controller => 'admin/users',
124
+ :action => 'update',
125
+ :type => :notice
126
+ ).key.should == :'controllers.admin.users.update.flash.notice'
127
+ end
128
+ end
129
+
130
+ describe '#fallbacks' do
131
+ it "should be correct for simple controllers" do
132
+ LocaleFlash::Flash.new(
133
+ :controller => 'users',
134
+ :action => 'show',
135
+ :type => :notice
136
+ ).fallbacks.should == [
137
+ :"controllers.users.flash.notice",
138
+ :"controllers.flash.show.notice",
139
+ :"controllers.flash.notice"
140
+ ]
141
+ end
142
+
143
+ it "should be correct for nested controllers" do
144
+ LocaleFlash::Flash.new(
145
+ :controller => 'admin/users',
146
+ :action => 'show',
147
+ :type => :notice
148
+ ).fallbacks.should == [
149
+ :"controllers.admin.users.flash.notice",
150
+ :"controllers.admin.flash.show.notice",
151
+ :"controllers.admin.flash.notice",
152
+ :"controllers.flash.show.notice",
153
+ :"controllers.flash.notice"
154
+ ]
155
+ end
156
+
157
+ it "should be correct for multiply nested controllers" do
158
+ LocaleFlash::Flash.new(
159
+ :controller => 'admin/users/projects',
160
+ :action => 'show',
161
+ :type => :notice
162
+ ).fallbacks.should == [
163
+ :"controllers.admin.users.projects.flash.notice",
164
+ :"controllers.admin.users.flash.show.notice",
165
+ :"controllers.admin.users.flash.notice",
166
+ :"controllers.admin.flash.show.notice",
167
+ :"controllers.admin.flash.notice",
168
+ :"controllers.flash.show.notice",
169
+ :"controllers.flash.notice"
170
+ ]
171
+ end
172
+ end
173
+
174
+ describe '#to_html' do
175
+ def wrap(str)
176
+ %Q{<div class="notice">#{str}</div>}
177
+ end
178
+
179
+ context "when flash is legacy" do
180
+ it "returns the message" do
181
+ flash = LocaleFlash::Flash.new(:message => 'Hello there!', :type => :notice)
182
+ flash.to_html.should == wrap('Hello there!')
183
+ end
184
+ end
185
+
186
+ context "when flash is a locale_flash" do
187
+ it "returns the translated string" do
188
+ flash = LocaleFlash::Flash.new(:controller => 'users', :action => 'create', :type => :notice)
189
+ flash.to_html.should == wrap('Found in controllers.users.create.flash.notice')
190
+ end
191
+
192
+ it "passes options to I18n" do
193
+ flash = LocaleFlash::Flash.new(:controller => 'users', :action => 'show', :type => :notice, :options => {:name => 'Chris'})
194
+ flash.to_html.should == wrap('Showing profile for Chris')
195
+ end
196
+
197
+ it "passes fallbacks to I18n" do
198
+ flash = LocaleFlash::Flash.new(:controller => 'users', :action => 'destroy', :type => :notice)
199
+ flash.to_html.should == wrap('Found in controllers.users.flash.notice')
200
+ end
201
+ end
202
+ end
203
+ end
@@ -6,10 +6,10 @@ describe ActionController::Base do
6
6
  @controller = UsersController.new
7
7
  end
8
8
 
9
- describe '#locale_flash(key)' do
10
- it "sets flash[key] to a hash containing the controller name and the action name" do
9
+ describe '#locale_flash(key, options)' do
10
+ it "sets flash[key] to a hash containing the controller name, the action name and passed options" do
11
11
  @controller.edit
12
- @controller.flash.should == {:warning => {:controller => 'users', :action => 'create'}}
12
+ @controller.flash.should == {:warning => {:controller => 'users', :action => 'create', :options => {:name => 'Chris'}}}
13
13
  end
14
14
 
15
15
  context "when the controller is nested" do
@@ -19,22 +19,22 @@ describe ActionController::Base do
19
19
 
20
20
  it "sets the flash[key] to a hash containing the full path to the controller and the action name" do
21
21
  @controller.show
22
- @controller.flash.should == {:alert => {:controller => 'admin/users', :action => 'show'}}
22
+ @controller.flash.should == {:alert => {:controller => 'admin/users', :action => 'show', :options => {}}}
23
23
  end
24
24
  end
25
25
  end
26
26
 
27
- describe '#locale_notice' do
28
- it "sets flash[:notice] to a hash contianing the controller name and the action name" do
27
+ describe '#locale_notice(options)' do
28
+ it "sets flash[:notice] to a hash contianing the controller name, the action name and passed options" do
29
29
  @controller.create
30
- @controller.flash.should == {:notice => {:controller => 'users', :action => 'create'}}
30
+ @controller.flash.should == {:notice => {:controller => 'users', :action => 'create', :options => {:name => 'John'}}}
31
31
  end
32
32
  end
33
33
 
34
- describe '#locale_alert' do
35
- it "sets flash[:alert] to a hash contianing the controller name and the action name" do
34
+ describe '#locale_alert(options)' do
35
+ it "sets flash[:alert] to a hash contianing the controller name, the action name and passed options" do
36
36
  @controller.destroy
37
- @controller.flash.should == {:alert => {:controller => 'users', :action => 'create'}}
37
+ @controller.flash.should == {:alert => {:controller => 'users', :action => 'create', :options => {:name => 'Martin'}}}
38
38
  end
39
39
  end
40
40
  end
@@ -51,44 +51,4 @@ describe ActionView::Base do
51
51
  end
52
52
  end
53
53
 
54
- describe "#locale_flash_default(type, msg)" do
55
- it "should be correct for simple controllers" do
56
- @template.send(
57
- :locale_flash_default,
58
- :notice, {:controller => 'users', :action => 'show'}
59
- ).should == [
60
- :"controllers.users.flash.notice",
61
- :"controllers.flash.show.notice",
62
- :"controllers.flash.notice"
63
- ]
64
- end
65
-
66
- it "should be correct for nested controllers" do
67
- @template.send(
68
- :locale_flash_default,
69
- :notice, {:controller => 'admin/users', :action => 'show'}
70
- ).should == [
71
- :"controllers.admin.users.flash.notice",
72
- :"controllers.admin.flash.show.notice",
73
- :"controllers.admin.flash.notice",
74
- :"controllers.flash.show.notice",
75
- :"controllers.flash.notice"
76
- ]
77
- end
78
-
79
- it "should be correct for multiply nested controllers" do
80
- @template.send(
81
- :locale_flash_default,
82
- :notice, {:controller => 'admin/users/projects', :action => 'show'}
83
- ).should == [
84
- :"controllers.admin.users.projects.flash.notice",
85
- :"controllers.admin.users.flash.show.notice",
86
- :"controllers.admin.users.flash.notice",
87
- :"controllers.admin.flash.show.notice",
88
- :"controllers.admin.flash.notice",
89
- :"controllers.flash.show.notice",
90
- :"controllers.flash.notice"
91
- ]
92
- end
93
- end
94
54
  end
@@ -10,15 +10,15 @@ end
10
10
 
11
11
  class UsersController < ActionController::Base
12
12
  def edit
13
- locale_flash(:warning)
13
+ locale_flash(:warning, :name => 'Chris')
14
14
  end
15
15
 
16
16
  def create
17
- locale_notice
17
+ locale_notice(:name => 'John')
18
18
  end
19
19
 
20
20
  def destroy
21
- locale_alert
21
+ locale_alert(:name => 'Martin')
22
22
  end
23
23
 
24
24
  private
metadata CHANGED
@@ -2,7 +2,7 @@
2
2
  name: locale_flash
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.4
5
+ version: 1.0.0
6
6
  platform: ruby
7
7
  authors:
8
8
  - Christopher Dell
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2011-05-13 00:00:00 +02:00
13
+ date: 2011-05-29 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -64,13 +64,15 @@ files:
64
64
  - README.markdown
65
65
  - Rakefile
66
66
  - lib/locale_flash.rb
67
- - lib/locale_flash/action_controller.rb
68
- - lib/locale_flash/action_view.rb
67
+ - lib/locale_flash/flash.rb
68
+ - lib/locale_flash/rails/action_controller.rb
69
+ - lib/locale_flash/rails/action_view.rb
69
70
  - lib/locale_flash/version.rb
70
71
  - locale_flash.gemspec
71
72
  - spec/fixtures/en.yml
72
- - spec/locale_flash/action_controller_spec.rb
73
- - spec/locale_flash/action_view_spec.rb
73
+ - spec/locale_flash/flash_spec.rb
74
+ - spec/locale_flash/rails/action_controller_spec.rb
75
+ - spec/locale_flash/rails/action_view_spec.rb
74
76
  - spec/spec_helper.rb
75
77
  - spec/support/controllers.rb
76
78
  - spec/support/templates.rb
@@ -98,14 +100,15 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
100
  requirements: []
99
101
 
100
102
  rubyforge_project: locale_flash
101
- rubygems_version: 1.5.0
103
+ rubygems_version: 1.6.2
102
104
  signing_key:
103
105
  specification_version: 3
104
106
  summary: Super charge your flash messages with I18n
105
107
  test_files:
106
108
  - spec/fixtures/en.yml
107
- - spec/locale_flash/action_controller_spec.rb
108
- - spec/locale_flash/action_view_spec.rb
109
+ - spec/locale_flash/flash_spec.rb
110
+ - spec/locale_flash/rails/action_controller_spec.rb
111
+ - spec/locale_flash/rails/action_view_spec.rb
109
112
  - spec/spec_helper.rb
110
113
  - spec/support/controllers.rb
111
114
  - spec/support/templates.rb
@@ -1,24 +0,0 @@
1
- module LocaleFlash
2
- module ActionController
3
-
4
- private
5
-
6
- def locale_flash(key)
7
- flash[key] = {:controller => controller_path, :action => action_name}
8
- end
9
-
10
- def locale_notice
11
- locale_flash :notice
12
- end
13
-
14
- def locale_alert
15
- locale_flash :alert
16
- end
17
- end
18
- end
19
-
20
- module ActionController
21
- class Base
22
- include LocaleFlash::ActionController
23
- end
24
- end
@@ -1,55 +0,0 @@
1
- module LocaleFlash
2
- module ActionView
3
-
4
- def locale_flash
5
- output = ''
6
- flash.each do |key, value|
7
- if value.is_a?(Hash) && value[:controller] && value[:action]
8
- output << locale_flash_wrap(key, t(locale_flash_key(key, value), :default => locale_flash_default(key, value)))
9
- else
10
- output << locale_flash_wrap(key, value)
11
- end
12
- end
13
-
14
- output.respond_to?(:html_safe) ? output.html_safe : output
15
- end
16
-
17
- private
18
-
19
- def locale_flash_wrap(key, value)
20
- %Q{<div class="#{key}">#{value}</div>}
21
- end
22
-
23
- def locale_flash_key(key, value)
24
- ['controllers', value[:controller].split('/').join('.'), value[:action], 'flash', key].join('.').to_sym
25
- end
26
-
27
- def locale_flash_default(type, msg)
28
- controllers = msg[:controller].split('/')
29
-
30
- if controllers.size > 1
31
- defaults = []
32
- controllers.each_with_index do |controller, i|
33
- controller = controllers[0, (controllers.size - i)].join('.')
34
- parent = controllers[0, (controllers.size - i - 1)].join('.')
35
- defaults << ['controllers', controller, 'flash', type]
36
- defaults << ['controllers', parent, 'flash', msg[:action], type] unless parent.empty?
37
- end
38
- else
39
- defaults = [['controllers', controllers.first, 'flash', type]]
40
- end
41
-
42
- defaults << ['controllers', 'flash', msg[:action], type]
43
- defaults << ['controllers', 'flash', type]
44
- defaults.map { |i| i.join('.').to_sym }
45
- end
46
-
47
- end
48
- end
49
-
50
-
51
- module ActionView
52
- class Base
53
- include LocaleFlash::ActionView
54
- end
55
- end