locale_flash 0.0.1 → 0.0.2

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/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ -I ./lib
2
+ -I ./spec
data/Guardfile ADDED
@@ -0,0 +1,3 @@
1
+ guard 'rspec', :cli => "--color --format nested" do
2
+ watch(%r{^spec/.+_spec\.rb})
3
+ end
data/Rakefile CHANGED
@@ -1,2 +1,7 @@
1
1
  require 'bundler'
2
- Bundler::GemHelper.install_tasks
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rspec/core/rake_task'
5
+ RSpec::Core::RakeTask.new
6
+
7
+ task :default => :spec
@@ -0,0 +1,24 @@
1
+ module LocaleFlash
2
+ module ActionController
3
+
4
+ private
5
+
6
+ def locale_flash(key)
7
+ flash[key] = {:controller => controller_name, :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
@@ -0,0 +1,41 @@
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
+ output
14
+ end
15
+
16
+ private
17
+
18
+ def locale_flash_wrap(key, value)
19
+ %Q{<div class="#{key}">#{value}</div>}
20
+ end
21
+
22
+ def locale_flash_key(key, value)
23
+ ['controllers', value[:controller], value[:action], 'flash', key].join('.').to_sym
24
+ end
25
+
26
+ def locale_flash_default(key, value)
27
+ [ ['controllers', value[:controller], 'flash', key],
28
+ ['controllers', 'flash', value[:action], key],
29
+ ['controllers', 'flash', key]
30
+ ].map { |i| i.join('.').to_sym }
31
+ end
32
+
33
+ end
34
+ end
35
+
36
+
37
+ module ActionView
38
+ class Base
39
+ include LocaleFlash::ActionView
40
+ end
41
+ end
@@ -1,3 +1,3 @@
1
1
  module LocaleFlash
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/locale_flash.rb CHANGED
@@ -1,2 +1,2 @@
1
- module LocaleFlash
2
- end
1
+ require 'locale_flash/action_controller'
2
+ require 'locale_flash/action_view'
data/locale_flash.gemspec CHANGED
@@ -18,4 +18,8 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
+
22
+ s.add_development_dependency('i18n')
23
+ s.add_development_dependency('rspec')
24
+ s.add_development_dependency('guard-rspec')
21
25
  end
@@ -0,0 +1,12 @@
1
+ en:
2
+ controllers:
3
+ flash:
4
+ notice: Found in controllers.flash.notice
5
+ show:
6
+ notice: Found in controllers.flash.show.notice
7
+ users:
8
+ flash:
9
+ notice: Found in controllers.users.flash.notice
10
+ create:
11
+ flash:
12
+ notice: Found in controllers.users.create.flash.notice
@@ -0,0 +1,29 @@
1
+ require 'spec_helper'
2
+ require 'support/controllers'
3
+
4
+ describe ActionController::Base do
5
+ before(:each) do
6
+ @controller = UsersController.new
7
+ end
8
+
9
+ describe '#locale_flash(key)' do
10
+ it "sets flash[key] to a hash containing the controller name and the action name" do
11
+ @controller.edit
12
+ @controller.flash.should == {:warning => {:controller => 'users', :action => 'create'}}
13
+ end
14
+ end
15
+
16
+ describe '#locale_notice' do
17
+ it "sets flash[:notice] to a hash contianing the controller name and the action name" do
18
+ @controller.create
19
+ @controller.flash.should == {:notice => {:controller => 'users', :action => 'create'}}
20
+ end
21
+ end
22
+
23
+ describe '#locale_alert' do
24
+ it "sets flash[:alert] to a hash contianing the controller name and the action name" do
25
+ @controller.destroy
26
+ @controller.flash.should == {:alert => {:controller => 'users', :action => 'create'}}
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ require 'spec_helper'
2
+ require 'support/templates'
3
+
4
+ describe ActionView::Base do
5
+ before(:each) do
6
+ @template = UsersTemplate.new
7
+ end
8
+
9
+ describe '#locale_flash' do
10
+ context "when flash is empty" do
11
+ it "returns an empty string" do
12
+ @template.flash = {}
13
+ @template.locale_flash.should == ''
14
+ end
15
+ end
16
+
17
+ context "when flash is a string" do
18
+ it "returns an html flash" do
19
+ @template.flash = {:notice => 'This is a string'}
20
+ @template.locale_flash.should == %Q{<div class="notice">This is a string</div>}
21
+ end
22
+
23
+ it "returns multiple html flashes" do
24
+ @template.flash = {:notice => 'This is a string', :alert => 'This is another string'}
25
+ html = @template.locale_flash
26
+ html.should include(%Q{<div class="notice">This is a string</div>})
27
+ html.should include(%Q{<div class="alert">This is another string</div>})
28
+ end
29
+ end
30
+
31
+ context "when flash is a hash containg controller and action keys" do
32
+ it "returns the flash for the controller and action" do
33
+ @template.flash = {:notice => {:controller => 'users', :action => 'create'}}
34
+ @template.locale_flash.should == %Q{<div class="notice">Found in controllers.users.create.flash.notice</div>}
35
+ end
36
+
37
+ it "returns the flash falling back to the controller flash" do
38
+ @template.flash = {:notice => {:controller => 'users', :action => 'update'}}
39
+ @template.locale_flash.should == %Q{<div class="notice">Found in controllers.users.flash.notice</div>}
40
+ end
41
+
42
+ it "returns the flash falling back to the action flash" do
43
+ @template.flash = {:notice => {:controller => 'pages', :action => 'show'}}
44
+ @template.locale_flash.should == %Q{<div class="notice">Found in controllers.flash.show.notice</div>}
45
+ end
46
+
47
+ it "returns the flash falling back to the key flash" do
48
+ @template.flash = {:notice => {:controller => 'other', :action => 'index'}}
49
+ @template.locale_flash.should == %Q{<div class="notice">Found in controllers.flash.notice</div>}
50
+ end
51
+ end
52
+
53
+ end
54
+ end
@@ -0,0 +1,4 @@
1
+ require 'locale_flash'
2
+ require 'i18n'
3
+
4
+ I18n.load_path << 'spec/fixtures/en.yml'
@@ -0,0 +1,34 @@
1
+ module ActionController
2
+ class Base
3
+ attr_accessor :flash
4
+
5
+ def initialize
6
+ @flash = {}
7
+ end
8
+ end
9
+ end
10
+
11
+ class UsersController < ActionController::Base
12
+
13
+ def edit
14
+ locale_flash(:warning)
15
+ end
16
+
17
+ def create
18
+ locale_notice
19
+ end
20
+
21
+ def destroy
22
+ locale_alert
23
+ end
24
+
25
+ private
26
+
27
+ def controller_name
28
+ 'users'
29
+ end
30
+
31
+ def action_name
32
+ 'create'
33
+ end
34
+ end
@@ -0,0 +1,12 @@
1
+ module ActionView
2
+ class Base
3
+ attr_accessor :flash
4
+
5
+ def t(key, args={})
6
+ I18n.t(key, args)
7
+ end
8
+ end
9
+ end
10
+
11
+ class UsersTemplate < ActionView::Base
12
+ 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.1
5
+ version: 0.0.2
6
6
  platform: ruby
7
7
  authors:
8
8
  - Christopher Dell
@@ -12,8 +12,40 @@ cert_chain: []
12
12
 
13
13
  date: 2011-05-07 00:00:00 +02:00
14
14
  default_executable:
15
- dependencies: []
16
-
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: i18n
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :development
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: guard-rspec
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: "0"
47
+ type: :development
48
+ version_requirements: *id003
17
49
  description: locale_flash lets you create flash messages easily with I18n fallbacks
18
50
  email:
19
51
  - chris@tigrish.com
@@ -25,12 +57,22 @@ extra_rdoc_files: []
25
57
 
26
58
  files:
27
59
  - .gitignore
60
+ - .rspec
28
61
  - .rvmrc
29
62
  - Gemfile
63
+ - Guardfile
30
64
  - Rakefile
31
65
  - lib/locale_flash.rb
66
+ - lib/locale_flash/action_controller.rb
67
+ - lib/locale_flash/action_view.rb
32
68
  - lib/locale_flash/version.rb
33
69
  - locale_flash.gemspec
70
+ - spec/fixtures/en.yml
71
+ - spec/locale_flash/action_controller_spec.rb
72
+ - spec/locale_flash/action_view_spec.rb
73
+ - spec/spec_helper.rb
74
+ - spec/support/controllers.rb
75
+ - spec/support/templates.rb
34
76
  has_rdoc: true
35
77
  homepage: ""
36
78
  licenses: []
@@ -55,9 +97,14 @@ required_rubygems_version: !ruby/object:Gem::Requirement
55
97
  requirements: []
56
98
 
57
99
  rubyforge_project: locale_flash
58
- rubygems_version: 1.6.2
100
+ rubygems_version: 1.5.0
59
101
  signing_key:
60
102
  specification_version: 3
61
103
  summary: Super charge your flash messages with I18n
62
- test_files: []
63
-
104
+ test_files:
105
+ - spec/fixtures/en.yml
106
+ - spec/locale_flash/action_controller_spec.rb
107
+ - spec/locale_flash/action_view_spec.rb
108
+ - spec/spec_helper.rb
109
+ - spec/support/controllers.rb
110
+ - spec/support/templates.rb