locale_flash 0.0.3 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -1,6 +1,6 @@
1
1
  # locale_flash
2
2
 
3
- ## Intallation
3
+ ## Installation
4
4
 
5
5
  Add locale_flash to your Gemfile :
6
6
 
@@ -76,8 +76,16 @@ Given that I call *locale_notice* from the create action in a UsersController, t
76
76
  controllers.flash.create.notice # fallback to the notice message for all create actions
77
77
  controllers.flash.notice # fallback to the generic notice message
78
78
 
79
+ Given a nested Admin::UsersController, the order of fallbacks will be as follows :
80
+
81
+ controllers.admin.users.create.flash.notice
82
+ controllers.admin.create.flash.show.notice
83
+ controllers.admin.create.flash.notice
84
+ controllers.admin.flash.notice
85
+ controllers.flash.create.notice
86
+ controllers.flash.notice
87
+
79
88
  ## TODO
80
89
  + Add an option to configure the wrapping html tags/classes
81
- + Make sure that nested controllers have the correct fallbacks
82
90
  + Allow options to be passed to the I18n.t call (like :locale => :fr for example)
83
91
  + Add the option of predefining common messages (like resource_created for example)
@@ -4,7 +4,7 @@ module LocaleFlash
4
4
  private
5
5
 
6
6
  def locale_flash(key)
7
- flash[key] = {:controller => controller_name, :action => action_name}
7
+ flash[key] = {:controller => controller_path, :action => action_name}
8
8
  end
9
9
 
10
10
  def locale_notice
@@ -21,14 +21,27 @@ module LocaleFlash
21
21
  end
22
22
 
23
23
  def locale_flash_key(key, value)
24
- ['controllers', value[:controller], value[:action], 'flash', key].join('.').to_sym
24
+ ['controllers', value[:controller].split('/').join('.'), value[:action], 'flash', key].join('.').to_sym
25
25
  end
26
26
 
27
- def locale_flash_default(key, value)
28
- [ ['controllers', value[:controller], 'flash', key],
29
- ['controllers', 'flash', value[:action], key],
30
- ['controllers', 'flash', key]
31
- ].map { |i| i.join('.').to_sym }
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 }
32
45
  end
33
46
 
34
47
  end
@@ -1,3 +1,3 @@
1
1
  module LocaleFlash
2
- VERSION = "0.0.3"
2
+ VERSION = "0.0.4"
3
3
  end
@@ -11,6 +11,17 @@ describe ActionController::Base do
11
11
  @controller.edit
12
12
  @controller.flash.should == {:warning => {:controller => 'users', :action => 'create'}}
13
13
  end
14
+
15
+ context "when the controller is nested" do
16
+ before(:each) do
17
+ @controller = Admin::UsersController.new
18
+ end
19
+
20
+ it "sets the flash[key] to a hash containing the full path to the controller and the action name" do
21
+ @controller.show
22
+ @controller.flash.should == {:alert => {:controller => 'admin/users', :action => 'show'}}
23
+ end
24
+ end
14
25
  end
15
26
 
16
27
  describe '#locale_notice' do
@@ -49,6 +49,46 @@ describe ActionView::Base do
49
49
  @template.locale_flash.should == %Q{<div class="notice">Found in controllers.flash.notice</div>}
50
50
  end
51
51
  end
52
+ end
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
52
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
53
93
  end
54
94
  end
@@ -9,7 +9,6 @@ module ActionController
9
9
  end
10
10
 
11
11
  class UsersController < ActionController::Base
12
-
13
12
  def edit
14
13
  locale_flash(:warning)
15
14
  end
@@ -24,11 +23,28 @@ class UsersController < ActionController::Base
24
23
 
25
24
  private
26
25
 
27
- def controller_name
26
+ def controller_path
28
27
  'users'
29
28
  end
30
29
 
31
30
  def action_name
32
31
  'create'
33
32
  end
33
+ end
34
+
35
+ module Admin
36
+ end
37
+
38
+ class Admin::UsersController < ActionController::Base
39
+ def show
40
+ locale_alert
41
+ end
42
+
43
+ def controller_path
44
+ 'admin/users'
45
+ end
46
+
47
+ def action_name
48
+ 'show'
49
+ end
34
50
  end
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.3
5
+ version: 0.0.4
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-07 00:00:00 +02:00
13
+ date: 2011-05-13 00:00:00 +02:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency