message_block 1.0.3 → 1.0.4

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- message_block (1.0.1)
4
+ message_block (1.0.3)
5
5
  rails (>= 3.0.0)
6
6
 
7
7
  GEM
data/README.rdoc CHANGED
@@ -33,7 +33,7 @@ the helper <tt><%= message_block %></tt>:
33
33
 
34
34
  The first argument specifies a hash options:
35
35
 
36
- * <tt>:on</tt> - specifies one or many model names for which to check error messages.
36
+ * <tt>:on</tt> - specifies one or many model names for which to check error messages. You can specify the special value of :all which will grab all view assignments that contain an ActiveModel "errors" method.
37
37
  * <tt>:model_error_type</tt> - specifies the message type to use for validation errors; defaults to 'error'
38
38
  * <tt>:flash_types</tt> - specifies the keys to check in the flash hash. Messages will be grouped in ul
39
39
  lists according to this type. Defaults to: %w(back confirm error info warn)
@@ -43,6 +43,7 @@ The first argument specifies a hash options:
43
43
  * <tt>:container</tt> - specifies which block-level element to contain the errors (defaults to :div).
44
44
 
45
45
  === Example
46
+
46
47
  Imagine you have a form for entering a user and a comment:
47
48
 
48
49
  <%= message_block :on => [:user, :comment] %>
@@ -74,6 +75,8 @@ given that both user and comment fail ActiveRecord validation:
74
75
  </ul>
75
76
  </div>
76
77
 
78
+ Note that instead of manually specifying models you wish to report errors on, you can instead use the special <tt>:all</tt> value which will automatically use all view assign values that contain an ActiveModel::Errors "errors" method.
79
+
77
80
  === Ajax & JavaScript Integration
78
81
 
79
82
  Sometimes you'll want to use the message block pattern within JavaScript. Wouldn't it be nice to just populate
@@ -128,4 +131,4 @@ This could be useful if you're interacting with stuff via Ajax, for example:
128
131
 
129
132
  // Do something...
130
133
  }
131
- });
134
+ });
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file
@@ -1,11 +1,11 @@
1
1
  .message_block {
2
2
  clear: both;
3
- margin-top: 0.2em;
4
- margin-bottom: 0.3em;
3
+ margin: 12px 0;
5
4
  }
6
5
 
7
6
  .message_block ul {
8
- margin-bottom: 0;
7
+ border-bottom: 1px solid #ecd757;
8
+ border-top: 1px solid #ecd757;
9
9
  list-style: none;
10
10
  padding: 10px;
11
11
  }
@@ -14,31 +14,21 @@
14
14
  }
15
15
 
16
16
  .message_block ul.error {
17
- border-top: 1px solid #ecd757;
18
- border-bottom: 1px solid #ecd757;
19
- background: #fcf6d0 url(../../images/message_block/error_m.gif) 1em 50% no-repeat;
17
+ background: #fcf6d0 url(../../images/message_block/error.gif) 16px 50% no-repeat;
20
18
  }
21
19
 
22
20
  .message_block ul.info {
23
- border-top: 1px solid #ecd757;
24
- border-bottom: 1px solid #ecd757;
25
- background: #fcf6d0 url(../../images/message_block/info_m.gif) 1em 50% no-repeat;
21
+ background: #fcf6d0 url(../../images/message_block/info.gif) 16px 50% no-repeat;
26
22
  }
27
23
 
28
24
  .message_block ul.notice {
29
- border-top: 1px solid #ecd757;
30
- border-bottom: 1px solid #ecd757;
31
- background: #fcf6d0 url(../../images/message_block/notice_m.gif) 1em 50% no-repeat;
25
+ background: #fcf6d0 url(../../images/message_block/notice.gif) 16px 50% no-repeat;
32
26
  }
33
27
 
34
28
  .message_block ul.confirm {
35
- border-top: 1px solid #ecd757;
36
- border-bottom: 1px solid #ecd757;
37
- background: #fcf6d0 url(../../images/message_block/confirmation_m.gif) 1em 50% no-repeat;
29
+ background: #fcf6d0 url(../../images/message_block/confirmation.gif) 16px 50% no-repeat;
38
30
  }
39
31
 
40
32
  .message_block ul.warn {
41
- border-top: 1px solid #ecd757;
42
- border-bottom: 1px solid #ecd757;
43
- background: #fcf6d0 url(../../images/message_block/warn_m.gif) 1em 50% no-repeat;
44
- }
33
+ background: #fcf6d0 url(../../images/message_block/warn.gif) 1em 50% no-repeat;
34
+ }
@@ -2,8 +2,6 @@ module MessageBlock
2
2
  module Helpers
3
3
 
4
4
  def message_block(options = {})
5
- options.assert_valid_keys(:on, :model_error_type, :flash_types, :html, :id, :class, :container)
6
-
7
5
  options[:model_error_type] ||= :error
8
6
  options[:flash_types] ||= [:notice, :back, :confirm, :error, :info, :warn].sort_by(&:to_s)
9
7
  options[:on] ||= controller.controller_name.split('/').last.gsub(/\_controller$/, '').singularize.to_sym
@@ -27,12 +25,14 @@ module MessageBlock
27
25
 
28
26
  options[:on] = [options[:on]] unless options[:on].is_a?(Array)
29
27
  model_objects = options[:on].map do |model_object|
30
- if model_object.instance_of?(String) or model_object.instance_of?(Symbol)
28
+ if model_object == :all
29
+ assigns.values.select {|o| o.respond_to?(:errors) && o.errors.is_a?(ActiveModel::Errors) }
30
+ elsif model_object.instance_of?(String) or model_object.instance_of?(Symbol)
31
31
  instance_variable_get("@#{model_object}")
32
32
  else
33
33
  model_object
34
34
  end
35
- end.select {|m| !m.nil? }
35
+ end.flatten.select {|m| !m.nil? }
36
36
 
37
37
  model_errors = model_objects.inject([]) {|b, m| b += m.errors.full_messages }
38
38
 
@@ -1,3 +1,3 @@
1
1
  module MessageBlock
2
- VERSION = "1.0.3"
3
- end
2
+ VERSION = "1.0.4"
3
+ end
@@ -5,12 +5,9 @@ describe MessageBlock::Helpers do
5
5
  include MessageBlock::Helpers
6
6
 
7
7
  before do
8
- setup_post
9
- setup_user
10
- end
11
-
12
- it "should not accept invalid options" do
13
- lambda { message_block :invalid => "option" }.should raise_error(ArgumentError)
8
+ @post = Post.new
9
+ @user = User.new
10
+ stub!(:assigns).and_return(:user => @user, :post => @post)
14
11
  end
15
12
 
16
13
  it "should accept valid options" do
@@ -28,40 +25,45 @@ describe MessageBlock::Helpers do
28
25
  message_block.should == %(<div class="message_block" id="message_block"></div>)
29
26
  end
30
27
 
31
- it "should automatically find post errorts with posts controller" do
28
+ it "should automatically find post errors with posts controller" do
32
29
  @controller = posts_controller
33
30
  output = message_block
34
- output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>Author name can't be empty</li></ul></div>)
31
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li></ul></div>)
35
32
  end
36
33
 
37
34
  it "should give no error for post" do
38
35
  output = message_block(:on => :post)
39
- output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>Author name can't be empty</li></ul></div>)
36
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li></ul></div>)
40
37
  end
41
38
 
42
39
  it "should give error for user" do
43
40
  output = message_block(:on => :user)
44
- output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>User email can't be empty</li></ul></div>)
41
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li></ul></div>)
42
+ end
43
+
44
+ it "should give error for both user and post when using :all" do
45
+ output = message_block(:on => :all)
46
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li><li>name can't be blank</li></ul></div>)
45
47
  end
46
48
 
47
49
  it "should give errors for both post and user" do
48
50
  output = message_block(:on => [:post, :user])
49
- output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>Author name can't be empty</li><li>User email can't be empty</li></ul></div>)
51
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li><li>name can't be blank</li></ul></div>)
50
52
  end
51
53
 
52
54
  it "should give errors for both post and user in the correct order" do
53
55
  output = message_block(:on => [:user, :post])
54
- output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>User email can't be empty</li><li>Author name can't be empty</li></ul></div>)
56
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li><li>name can't be blank</li></ul></div>)
55
57
  end
56
58
 
57
59
  it "should give error for user given direct instance variable" do
58
60
  output = message_block(:on => @user)
59
- output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>User email can't be empty</li></ul></div>)
61
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li></ul></div>)
60
62
  end
61
63
 
62
64
  it "should respect model error type" do
63
65
  output = message_block(:on => :user, :model_error_type => "fail")
64
- output.should == %(<div class="message_block" id="message_block"><ul class="fail"><li>User email can't be empty</li></ul></div>)
66
+ output.should == %(<div class="message_block" id="message_block"><ul class="fail"><li>name can't be blank</li></ul></div>)
65
67
  end
66
68
 
67
69
  it "should be able to specify id for containing div" do
@@ -81,12 +83,12 @@ describe MessageBlock::Helpers do
81
83
 
82
84
  it "should be able to specify container option as false" do
83
85
  output = message_block(:on => :post, :container => false)
84
- output.should == %(<ul class="error"><li>Author name can't be empty</li></ul>)
86
+ output.should == %(<ul class="error"><li>name can't be blank</li></ul>)
85
87
  end
86
88
 
87
89
  it "should be able to specify container option" do
88
90
  output = message_block(:on => :post, :container => :fieldset)
89
- output.should == %(<fieldset class="message_block" id="message_block"><ul class="error"><li>Author name can't be empty</li></ul></fieldset>)
91
+ output.should == %(<fieldset class="message_block" id="message_block"><ul class="error"><li>name can't be blank</li></ul></fieldset>)
90
92
  end
91
93
 
92
94
  it "should be able to see flash error string" do
@@ -118,7 +120,7 @@ describe MessageBlock::Helpers do
118
120
  it "should be able to see flash error alongside model error" do
119
121
  flash[:error] = "Error A"
120
122
  output = message_block(:on => :post)
121
- output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>Error A</li><li>Author name can't be empty</li></ul></div>)
123
+ output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>Error A</li><li>name can't be blank</li></ul></div>)
122
124
  end
123
125
 
124
126
  it "should be safe for html inside flash messages" do
@@ -127,4 +129,4 @@ describe MessageBlock::Helpers do
127
129
  output.should == %(<div class="message_block" id="message_block"><ul class="error"><li>Error <strong>A</strong></li><li>Error <strong>B</strong></li></ul></div>)
128
130
  end
129
131
 
130
- end
132
+ end
data/spec/spec_helper.rb CHANGED
@@ -1,54 +1,61 @@
1
1
  require "rubygems"
2
2
  require "message_block"
3
3
  require "active_support"
4
+ require "active_model"
4
5
  require "action_pack"
5
6
  require "action_view"
6
7
 
7
8
  require File.dirname(__FILE__) + '/../init'
8
9
 
9
- # Borrowed model stubs from Rails active_record_helper_test.rb
10
- # TODO: Re-implement using mocha
11
- Post = Struct.new("Post", :title, :author_name)
12
- User = Struct.new("User", :email)
10
+ class Post
11
+ extend ActiveModel::Naming
13
12
 
14
- def setup_post
15
- @post = Post.new
16
- def @post.errors
17
- Class.new do
18
- def on(field)
19
- case field.to_s
20
- when "author_name"
21
- "can't be empty"
22
- when "body"
23
- true
24
- else
25
- false
26
- end
27
- end
28
- def empty?() false end
29
- def count() 1 end
30
- def full_messages() [ "Author name can't be empty" ] end
31
- end.new
13
+ attr_accessor :name
14
+ attr_reader :errors
15
+
16
+ def initialize
17
+ @errors = ActiveModel::Errors.new(self)
18
+ @errors.add(:name, "can't be blank")
19
+ end
20
+
21
+ def read_attribute_for_validation(attr)
22
+ send(attr)
23
+ end
24
+
25
+ def self.human_attribute_name(attr, options = {})
26
+ attr
32
27
  end
33
28
 
34
- @post.title = "Hello World"
35
- @post.author_name = ""
29
+ def self.lookup_ancestors
30
+ [self]
31
+ end
36
32
  end
37
33
 
38
- def setup_user
39
- @user = User.new
40
- def @user.errors
41
- Class.new {
42
- def on(field) field == "email" end
43
- def empty?() false end
44
- def count() 1 end
45
- def full_messages() [ "User email can't be empty" ] end
46
- }.new
34
+ class User
35
+ extend ActiveModel::Naming
36
+
37
+ attr_accessor :name
38
+ attr_reader :errors
39
+
40
+ def initialize
41
+ @errors = ActiveModel::Errors.new(self)
42
+ @errors.add(:name, "can't be blank")
47
43
  end
48
44
 
49
- @user.email = ""
45
+ def read_attribute_for_validation(attr)
46
+ send(attr)
47
+ end
48
+
49
+ def self.human_attribute_name(attr, options = {})
50
+ attr
51
+ end
52
+
53
+ def self.lookup_ancestors
54
+ [self]
55
+ end
50
56
  end
51
57
 
58
+
52
59
  def controller
53
60
  @controller ||= Class.new {
54
61
  def controller_name
@@ -67,4 +74,4 @@ end
67
74
 
68
75
  def flash
69
76
  @flash ||= {}
70
- end
77
+ end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: message_block
3
3
  version: !ruby/object:Gem::Version
4
- hash: 17
4
+ hash: 31
5
5
  prerelease: false
6
6
  segments:
7
7
  - 1
8
8
  - 0
9
- - 3
10
- version: 1.0.3
9
+ - 4
10
+ version: 1.0.4
11
11
  platform: ruby
12
12
  authors:
13
13
  - Ben Hughes
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-10-27 00:00:00 +03:00
18
+ date: 2011-04-15 00:00:00 -04:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -114,21 +114,14 @@ files:
114
114
  - spec/message_block/helpers_spec.rb
115
115
  - spec/spec_helper.rb
116
116
  - assets/images/back.gif
117
- - assets/images/back_m.gif
118
117
  - assets/images/confirmation.gif
119
- - assets/images/confirmation_m.gif
120
118
  - assets/images/error.gif
121
- - assets/images/error_m.gif
122
119
  - assets/images/info.gif
123
- - assets/images/info_m.gif
124
120
  - assets/images/notice.gif
125
- - assets/images/notice_m.gif
126
121
  - assets/images/warn.gif
127
- - assets/images/warn_m.gif
128
122
  - assets/javascripts/message_block.js
129
123
  - assets/stylesheets/message_block.css
130
124
  - rails/init.rb
131
- - CHANGELOG.rdoc
132
125
  - Gemfile
133
126
  - Gemfile.lock
134
127
  - LICENSE
data/CHANGELOG.rdoc DELETED
@@ -1,15 +0,0 @@
1
- == [2010-10-13] 1.0.1 Removed Rails 2.3 Compatibility, Cucumber Overhaul
2
-
3
- * Issues with loading rake tasks in Rails 2.3; removing compatibility
4
- * Revamped Cucumber invocation to use Rails 3 with 'gem "message_block", :path => "..."'
5
- * Upgraded RSpec to 2.0
6
-
7
- == [2010-10-13] 1.0.0 Major Updates
8
-
9
- * Finally updated to be a Gem
10
- * Internal refactoring
11
- * Converted tests to RSpec
12
- * New cucumber features
13
- * Added support for Rails 3.0
14
-
15
- == 0.1.0 Initial Beta Version
Binary file
Binary file
Binary file
Binary file
Binary file
Binary file