simply_messages 0.0.1 → 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = simply_messages
2
2
 
3
3
  simply_messages is a Rails plugin providing a unified notice/alert messages handling (like those: flash.now[:notice] = 'Yeah').
4
- Based on the message_block plugin by Ben Hughes (http://github.com/railsgarden/message_block).
4
+ Based on the message_block by Ben Hughes (http://github.com/railsgarden/message_block).
5
5
 
6
6
 
7
7
  == Requirements
@@ -20,17 +20,26 @@ Then bundle:
20
20
  bundle
21
21
 
22
22
 
23
- In `app/views/layouts/application.html.erb`:
23
+ simply_messages uses standard I18n translations to display errors and as such requires at least the following keys to be present:
24
24
 
25
- <%= messages_block %>
25
+ activerecord:
26
+ errors:
27
+ template:
28
+ header:
29
+ one: "1 error prohibited this %{model} from being saved"
30
+ other: "%{count} errors prohibited this %{model} from being saved"
31
+
32
+
33
+ == Usage
26
34
 
35
+ In `app/views/layouts/application.html.erb`:
27
36
 
28
- By default the messages_block function will display all flash[:notice] and flash[:alert] messages and all errors for model object associated by name with the current controller (so it will display all model errors for @user model when 'users' is the current controller).
37
+ <%= messages_block %>
29
38
 
30
- Will also display appropriate messages for :success and :error flash keys.
31
39
 
40
+ By default the messages_block function will display all flash[:notice] and flash[:alert] messages and all errors for model object associated by name with the current controller (so it will display all model errors for @user model when 'users' is the current controller). Will also display appropriate messages for :success and :error flash keys.
32
41
 
33
- We can also specify the model we would like to display errors for, like this:
42
+ We can also explicitly set the model we would like to display errors for, like this:
34
43
 
35
44
  <%= messages_block :for => :comment %>
36
45
 
@@ -52,27 +61,27 @@ view:
52
61
  will result in:
53
62
 
54
63
  <div class="notice">
55
- <p>Post saved.</p>
64
+ <p>Post saved</p>
56
65
  </div>
57
66
 
58
67
 
59
68
  === EXAMPLE 2
60
69
 
61
- users_controller:
70
+ Given a User model instance with one validation error:
62
71
 
63
- @user.errors.add('name', 'name should not be empty')
64
- flash.now[:alert] = 'Unable to add user'
72
+ user.errors
73
+ => #<OrderedHash {:name=>["can't be blank"]}>
65
74
 
66
- view:
75
+ and this in view in the users controller:
67
76
 
68
77
  <%= messages_block %>
69
78
 
70
- will result in:
79
+ will render:
71
80
 
72
81
  <div class="alert">
73
- <p>Unable to add user:</p>
82
+ <p>1 error prohibited this User from being saved:</p>
74
83
  <ul>
75
- <li>name should not be empty</li>
84
+ <li>Name can't be blank</li>
76
85
  </ul>
77
86
  </div>
78
87
 
data/Rakefile CHANGED
@@ -3,10 +3,17 @@
3
3
  require 'bundler'
4
4
  Bundler::GemHelper.install_tasks
5
5
 
6
+ require 'rspec/core'
7
+ require 'rspec/core/rake_task'
8
+ RSpec::Core::RakeTask.new(:spec) do |spec|
9
+ spec.pattern = FileList['spec/**/*_spec.rb']
10
+ end
11
+
12
+ task :default => :spec
13
+
6
14
  require 'rake/rdoctask'
7
15
  Rake::RDocTask.new do |rdoc|
8
16
  require 'simply_messages/version'
9
-
10
17
  rdoc.rdoc_dir = 'rdoc'
11
18
  rdoc.title = "simply_messages #{SimplyMessages::VERSION}"
12
19
  rdoc.rdoc_files.include('README*')
@@ -5,8 +5,8 @@ module SimplyMessages
5
5
  extend ::ActiveSupport::Concern
6
6
 
7
7
  module InstanceMethods
8
- def messages_block(options = {})
9
8
 
9
+ def messages_block(options = {})
10
10
  # make options[:for] an array
11
11
  options[:for] ||= []
12
12
  options[:for] = [options[:for]] unless options[:for].is_a?(Array)
@@ -21,48 +21,51 @@ module SimplyMessages
21
21
  else
22
22
  model_object
23
23
  end
24
- end.select {|m| !m.nil? }.uniq
25
-
26
- # all models errors
27
- models_errors = []
28
- model_objects.each do |m|
29
- errors = m.errors.entries.collect {|field, message| message}.select {|m| m.present?}
30
- models_errors.concat(errors) if errors.any?
31
- end
24
+ end.select { |m| m.present? }.uniq
32
25
 
33
26
  # messages_block
34
27
  messages_block = ''
35
28
 
36
- # flash[:success] or flash[:notice]
37
- if flash[:success].present? or flash[:notice].present?
38
-
39
- key = flash[:success].present? ? :success : :notice
40
-
41
- msg = content_tag(:p, flash[key] + '.')
42
- messages_block += content_tag(:div, msg, :class => key.to_s)
29
+ # notice/success
30
+ flash_msg = ''
31
+ [:notice, :success].each do |key|
32
+ if flash[key].present?
33
+ flash_msg += content_tag(:p, flash[key])
34
+ messages_block += content_tag(:div, flash_msg.html_safe, :class => key.to_s)
35
+ end
43
36
  end
44
37
 
45
- # flash[:error] or flash[:alert] or models errors
46
- if flash[:error].present? or flash[:alert] or models_errors.any?
47
- key = flash[:error].present? ? :error : :alert
48
- msg = ''
49
-
38
+ # alert/error
39
+ flash_msg = ''
40
+ html_class = ''
41
+ [:alert, :error].each do |key|
50
42
  if flash[key].present?
51
- msg += content_tag(:p, flash[key] + (models_errors.any? ? ':' : '.'))
43
+ flash_msg += content_tag(:p, flash[key])
44
+ html_class += key.to_s
52
45
  end
46
+ end
53
47
 
54
- if models_errors.any?
55
- errors = []
56
- models_errors.each do |e|
57
- errors << content_tag(:li, e)
48
+ # all models errors
49
+ errors = ''
50
+ model_objects.each do |model|
51
+ if model.errors.any?
52
+ errors += content_tag(:p, I18n.translate(
53
+ model.errors.count > 1 ? :other : :one,
54
+ :count => model.errors.count,
55
+ :model => model.class.model_name.human,
56
+ :scope => [:activerecord, :errors, :template, :header]) + ':')
57
+
58
+ msg = ''
59
+ model.errors.full_messages.each do |e|
60
+ msg += content_tag(:li, e)
58
61
  end
59
- msg += content_tag(:ul, errors.join)
62
+ errors += content_tag(:ul, msg.html_safe)
60
63
  end
61
-
62
- messages_block += content_tag(:div, msg, :class => key.to_s)
63
64
  end
64
65
 
65
- messages_block
66
+ messages_block += content_tag(:div, flash_msg.html_safe + errors.html_safe, :class => (html_class.presence || 'alert'))
67
+
68
+ messages_block.html_safe
66
69
  end
67
70
 
68
71
  end
@@ -11,4 +11,3 @@ module SimplyMessages
11
11
  end
12
12
  end
13
13
  end
14
-
@@ -1,3 +1,3 @@
1
1
  module SimplyMessages
2
- VERSION = '0.0.1'
2
+ VERSION = '0.0.2'
3
3
  end
@@ -25,4 +25,7 @@ Gem::Specification.new do |s|
25
25
  s.add_dependency 'rails', ['>= 3.0.0']
26
26
  s.add_development_dependency 'bundler', ['>= 1.0.0']
27
27
  s.add_development_dependency 'sqlite3', ['>= 0']
28
+ s.add_development_dependency 'rspec', ['>= 0']
29
+ s.add_development_dependency 'rspec-rails', ['>= 0']
30
+ s.add_development_dependency 'webrat', ['>= 0']
28
31
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: simply_messages
3
3
  version: !ruby/object:Gem::Version
4
- hash: 29
4
+ hash: 27
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 0
9
- - 1
10
- version: 0.0.1
9
+ - 2
10
+ version: 0.0.2
11
11
  platform: ruby
12
12
  authors:
13
13
  - "Pawe\xC5\x82 Go\xC5\x9Bcicki"
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-05-14 00:00:00 +02:00
18
+ date: 2011-05-15 00:00:00 +02:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -64,6 +64,48 @@ dependencies:
64
64
  version: "0"
65
65
  type: :development
66
66
  version_requirements: *id003
67
+ - !ruby/object:Gem::Dependency
68
+ name: rspec
69
+ prerelease: false
70
+ requirement: &id004 !ruby/object:Gem::Requirement
71
+ none: false
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ hash: 3
76
+ segments:
77
+ - 0
78
+ version: "0"
79
+ type: :development
80
+ version_requirements: *id004
81
+ - !ruby/object:Gem::Dependency
82
+ name: rspec-rails
83
+ prerelease: false
84
+ requirement: &id005 !ruby/object:Gem::Requirement
85
+ none: false
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ hash: 3
90
+ segments:
91
+ - 0
92
+ version: "0"
93
+ type: :development
94
+ version_requirements: *id005
95
+ - !ruby/object:Gem::Dependency
96
+ name: webrat
97
+ prerelease: false
98
+ requirement: &id006 !ruby/object:Gem::Requirement
99
+ none: false
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ hash: 3
104
+ segments:
105
+ - 0
106
+ version: "0"
107
+ type: :development
108
+ version_requirements: *id006
67
109
  description: simply_messages is a unified flash and error messages display solution
68
110
  email:
69
111
  - pawel.goscicki@gmail.com