locale_flash 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown ADDED
@@ -0,0 +1,83 @@
1
+ # locale_flash
2
+
3
+ ## Intallation
4
+
5
+ Add locale_flash to your Gemfile :
6
+
7
+ gem 'locale_flash'
8
+
9
+ Install the gem :
10
+
11
+ bundle install
12
+
13
+ And off you go!
14
+
15
+ ## Use
16
+
17
+ ### In your controllers
18
+
19
+ Instead of manually assigning a value to flash[:notice] or flash[:alert], just call the *locale_flash* method like so :
20
+
21
+ class UsersController < ActionController::Base
22
+ def create
23
+ @user = User.new(params[:user])
24
+ if @user.save
25
+ locale_flash(:notice)
26
+ # redirect or render...
27
+ else
28
+ locale_flash(:alert)
29
+ # redirect or render...
30
+ end
31
+ end
32
+ end
33
+
34
+ Notice and Alert are such common keys to assign to a flash message that 2 convenience methods are also included : *locale_notice* and *locale_alert*. This means that the previous example can be written like so :
35
+
36
+ class UsersController < ActionController::Base
37
+ def create
38
+ @user = User.new(params[:user])
39
+ if @user.save
40
+ locale_notice
41
+ # redirect or render...
42
+ else
43
+ locale_alert
44
+ # redirect or render...
45
+ end
46
+ end
47
+ end
48
+
49
+ ### In your views
50
+
51
+ Simply call locale_flash from your views like so :
52
+
53
+ <div id="flash">
54
+ <%= locale_flash %>
55
+ </div>
56
+
57
+ This will produce HTML output like this :
58
+
59
+ <div id="flash">
60
+ <div class="notice">My message here</div>
61
+ </div>
62
+
63
+
64
+ Note : if you are adding locale_flash to an existing application with manual assignments to flash[:notice], this will be rendered without I18n lookups.
65
+
66
+ ### I18n fallback order
67
+
68
+ Your actual message text should be stored in your locale files (ie: in config/locales/*.yml).
69
+
70
+ locale_file will first look for the most specific message that matches the controller and action from which the flash was created from, and if that's missing, will fallback to more generic messages.
71
+
72
+ Given that I call *locale_notice* from the create action in a UsersController, the order of fallbacks will be as follows :
73
+
74
+ controllers.users.create.flash.notice # this is the most specific message
75
+ controllers.users.flash.notice # fallback to the notice message for this controller
76
+ controllers.flash.create.notice # fallback to the notice message for all create actions
77
+ controllers.flash.notice # fallback to the generic notice message
78
+
79
+ ## TODO
80
+ + Add an option to configure the wrapping html tags/classes
81
+ + Make sure that nested controllers have the correct fallbacks
82
+ + Allow options to be passed to the I18n.t call (like :locale => :fr for example)
83
+ + Add the option of predefining common messages (like resource_created for example)
@@ -10,7 +10,8 @@ module LocaleFlash
10
10
  output << locale_flash_wrap(key, value)
11
11
  end
12
12
  end
13
- output
13
+
14
+ output.respond_to?(:html_safe) ? output.html_safe : output
14
15
  end
15
16
 
16
17
  private
@@ -1,3 +1,3 @@
1
1
  module LocaleFlash
2
- VERSION = "0.0.2"
2
+ VERSION = "0.0.3"
3
3
  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.2
5
+ version: 0.0.3
6
6
  platform: ruby
7
7
  authors:
8
8
  - Christopher Dell
@@ -61,6 +61,7 @@ files:
61
61
  - .rvmrc
62
62
  - Gemfile
63
63
  - Guardfile
64
+ - README.markdown
64
65
  - Rakefile
65
66
  - lib/locale_flash.rb
66
67
  - lib/locale_flash/action_controller.rb