├── .gitignore
├── .rspec
├── .travis.yml
├── Appraisals
├── CHANGELOG
├── Gemfile
├── Gemfile.lock
├── LICENSE
├── README.rdoc
├── Rakefile
├── gemfiles
├── 3.1.gemfile
├── 3.1.gemfile.lock
├── 3.2.gemfile
├── 3.2.gemfile.lock
├── 4.0.gemfile
└── 4.0.gemfile.lock
├── lib
├── message_block.rb
└── message_block
│ ├── engine.rb
│ ├── helpers.rb
│ └── version.rb
├── message_block.gemspec
├── spec
├── message_block
│ └── helpers_spec.rb
└── spec_helper.rb
└── vendor
└── assets
├── images
└── message_block
│ ├── alert.gif
│ ├── back.gif
│ ├── confirm.gif
│ ├── error.gif
│ ├── info.gif
│ ├── notice.gif
│ └── warn.gif
└── stylesheets
└── message_block.css.erb
/.gitignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | Manifest
3 | tmp
4 | .bundle
5 | *.gem
6 |
--------------------------------------------------------------------------------
/.rspec:
--------------------------------------------------------------------------------
1 | --colour
2 | --format nested
3 | --backtrace
4 |
--------------------------------------------------------------------------------
/.travis.yml:
--------------------------------------------------------------------------------
1 | language: ruby
2 | rvm:
3 | - 1.8.7
4 | - 1.9.2
5 | - 1.9.3
6 | - 2.0.0
7 | gemfile:
8 | - gemfiles/3.1.gemfile
9 | - gemfiles/3.2.gemfile
10 | - gemfiles/4.0.gemfile
11 | matrix:
12 | exclude:
13 | - rvm: 1.8.7
14 | gemfile: gemfiles/4.0.gemfile
15 | - rvm: 1.9.2
16 | gemfile: gemfiles/4.0.gemfile
17 | before_install:
18 | - "rm gemfiles/*.lock"
19 | - "gem install bundler -v=1.3.0"
20 | before_script:
21 | - "bundle install"
22 | script: "bundle exec rake clean test"
23 |
--------------------------------------------------------------------------------
/Appraisals:
--------------------------------------------------------------------------------
1 | appraise "3.1" do
2 | gem "rails", "~> 3.1.6"
3 | end
4 |
5 | appraise "3.2" do
6 | gem "rails", "~> 3.2.6"
7 | end
8 |
9 | appraise "4.0" do
10 | gem "rails", "~> 4.0.0"
11 | end
12 |
--------------------------------------------------------------------------------
/CHANGELOG:
--------------------------------------------------------------------------------
1 | == Version 2.0.3 (June 14, 2013)
2 |
3 | * Fixed path to confirm.gif
4 |
5 | == Version 2.0.2 (Feburary 17, 2013)
6 |
7 | * Fixed inclusion of vendor from gemspec
8 |
9 | == Version 2.0.1 (eburary 17, 2013)
10 |
11 | * Added 'alert' flash message type
12 |
13 | == Version 2.0.0 (Feburary 8, 2013)
14 |
15 | * Converted assets to use the asset pipeline (instead of a rake task to copy
16 | assets)
17 |
18 |
--------------------------------------------------------------------------------
/Gemfile:
--------------------------------------------------------------------------------
1 | source "http://rubygems.org"
2 |
3 | # Specify your gem's dependencies in testgem.gemspec
4 | gemspec
--------------------------------------------------------------------------------
/Gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: .
3 | specs:
4 | message_block (2.0.3)
5 | rails (>= 3.1.0)
6 |
7 | GEM
8 | remote: http://rubygems.org/
9 | specs:
10 | actionmailer (4.0.0)
11 | actionpack (= 4.0.0)
12 | mail (~> 2.5.3)
13 | actionpack (4.0.0)
14 | activesupport (= 4.0.0)
15 | builder (~> 3.1.0)
16 | erubis (~> 2.7.0)
17 | rack (~> 1.5.2)
18 | rack-test (~> 0.6.2)
19 | activemodel (4.0.0)
20 | activesupport (= 4.0.0)
21 | builder (~> 3.1.0)
22 | activerecord (4.0.0)
23 | activemodel (= 4.0.0)
24 | activerecord-deprecated_finders (~> 1.0.2)
25 | activesupport (= 4.0.0)
26 | arel (~> 4.0.0)
27 | activerecord-deprecated_finders (1.0.3)
28 | activesupport (4.0.0)
29 | i18n (~> 0.6, >= 0.6.4)
30 | minitest (~> 4.2)
31 | multi_json (~> 1.3)
32 | thread_safe (~> 0.1)
33 | tzinfo (~> 0.3.37)
34 | appraisal (0.5.1)
35 | bundler
36 | rake
37 | arel (4.0.0)
38 | atomic (1.1.13)
39 | builder (3.1.4)
40 | diff-lcs (1.2.1)
41 | erubis (2.7.0)
42 | hike (1.2.3)
43 | i18n (0.6.5)
44 | mail (2.5.4)
45 | mime-types (~> 1.16)
46 | treetop (~> 1.4.8)
47 | mime-types (1.24)
48 | minitest (4.7.5)
49 | multi_json (1.7.9)
50 | polyglot (0.3.3)
51 | rack (1.5.2)
52 | rack-test (0.6.2)
53 | rack (>= 1.0)
54 | rails (4.0.0)
55 | actionmailer (= 4.0.0)
56 | actionpack (= 4.0.0)
57 | activerecord (= 4.0.0)
58 | activesupport (= 4.0.0)
59 | bundler (>= 1.3.0, < 2.0)
60 | railties (= 4.0.0)
61 | sprockets-rails (~> 2.0.0)
62 | railties (4.0.0)
63 | actionpack (= 4.0.0)
64 | activesupport (= 4.0.0)
65 | rake (>= 0.8.7)
66 | thor (>= 0.18.1, < 2.0)
67 | rake (10.0.3)
68 | rspec (2.13.0)
69 | rspec-core (~> 2.13.0)
70 | rspec-expectations (~> 2.13.0)
71 | rspec-mocks (~> 2.13.0)
72 | rspec-core (2.13.0)
73 | rspec-expectations (2.13.0)
74 | diff-lcs (>= 1.1.3, < 2.0)
75 | rspec-mocks (2.13.0)
76 | sprockets (2.10.0)
77 | hike (~> 1.2)
78 | multi_json (~> 1.0)
79 | rack (~> 1.0)
80 | tilt (~> 1.1, != 1.3.0)
81 | sprockets-rails (2.0.0)
82 | actionpack (>= 3.0)
83 | activesupport (>= 3.0)
84 | sprockets (~> 2.8)
85 | sqlite3 (1.3.7)
86 | sqlite3-ruby (1.3.3)
87 | sqlite3 (>= 1.3.3)
88 | thor (0.18.1)
89 | thread_safe (0.1.2)
90 | atomic
91 | tilt (1.4.1)
92 | treetop (1.4.15)
93 | polyglot
94 | polyglot (>= 0.3.1)
95 | tzinfo (0.3.37)
96 |
97 | PLATFORMS
98 | ruby
99 |
100 | DEPENDENCIES
101 | appraisal (~> 0.5.1)
102 | message_block!
103 | rspec (~> 2.13)
104 | sqlite3-ruby (~> 1.3.1)
105 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Copyright (c) 2008 Ben Hughes
2 |
3 | Permission is hereby granted, free of charge, to any person obtaining
4 | a copy of this software and associated documentation files (the
5 | "Software"), to deal in the Software without restriction, including
6 | without limitation the rights to use, copy, modify, merge, publish,
7 | distribute, sublicense, and/or sell copies of the Software, and to
8 | permit persons to whom the Software is furnished to do so, subject to
9 | the following conditions:
10 |
11 | The above copyright notice and this permission notice shall be
12 | included in all copies or substantial portions of the Software.
13 |
14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 |
--------------------------------------------------------------------------------
/README.rdoc:
--------------------------------------------------------------------------------
1 | { }[http://travis-ci.org/rubiety/message_block]
2 | { }[https://gemnasium.com/rubiety/message_block]
3 | { }[https://codeclimate.com/github/rubiety/message_block]
4 |
5 | = Message Block
6 |
7 | Implements the common view pattern by which a list of messages are shown at the top,
8 | often a combination of flash messages and ActiveRecord validation issues on one or more models.
9 | This allows for a nice, stylized block of messages at the top of the page with icons
10 | indicating what type of message it is (error, confirmation, warning, etc.)
11 |
12 | This view helper acts as a replacement for error_messages_for by taking error messages
13 | from your models and combing them with flash messages (multiple types such as error, confirm, etc.)
14 | and outputting them to your view. This gem comes with an example stylesheet and images.
15 |
16 |
17 | == Installation: Rails 3.1+ Asset Pipeline (message_block 2.0)
18 |
19 | Include the gem using bundler in your Gemfile:
20 |
21 | gem "message_block", "~> 2.0"
22 |
23 | Include the stylesheet in your application.css:
24 |
25 | /*
26 | *= require message_block
27 | */
28 |
29 |
30 | == Installation: Rails 3.0 Without Asset Pipeline (message_block 1.1)
31 |
32 | Include the gem using bundler in your Gemfile:
33 |
34 | gem "message_block", "~> 1.1"
35 |
36 | Then run the rake task to install the static files:
37 |
38 | rake message_block:install
39 |
40 | Then be sure to include the CSS definitions:
41 |
42 | <%= stylesheet_include_tag "message_block" %>
43 |
44 |
45 | == Usage
46 |
47 | Once you install this, you should now have a set of images at public/images/message_block and
48 | a basic stylesheet installed at public/stylesheets/message_block.css. Then you can use
49 | the helper <%= message_block %> :
50 |
51 | The first argument specifies a hash options:
52 |
53 | * :on - 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.
54 | * :model_error_type - specifies the message type to use for validation errors; defaults to 'error'
55 | * :flash_types - specifies the keys to check in the flash hash. Messages will be grouped in ul
56 | lists according to this type. Defaults to: [:notice, :back, :confirm, :error, :alert, :info, :warn]
57 | * :html - Specifies HTML options for the containing div
58 | * :id - Specifies ID of the containing div; defaults to 'message_block'
59 | * :class - Specifies class name of the containing div; defaults to nothing.
60 | * :container - specifies which block-level element to contain the errors (defaults to :div).
61 |
62 | === Example
63 |
64 | Imagine you have a form for entering a user and a comment:
65 |
66 | <%= message_block :on => [:user, :comment] %>
67 |
68 | Imagine also you set these flash variables in the controller:
69 | class CommentsController
70 | def create
71 | flash.now[:error] = "Error A"
72 | flash.now[:confirm] = "Confirmation A" # Note you can use different types
73 | flash.now[:warn] = ["Warn A", "Warn B"] # Can set to an array for multiple messages
74 | end
75 | end
76 |
77 | And let's say that you want to show these messages but also show the validation issues
78 | given that both user and comment fail ActiveRecord validation:
79 |
80 |
81 |
82 | Error A
83 | User first name is required.
84 | Comment contents is required.
85 |
86 |
89 |
90 | Warn A
91 | Warn B
92 |
93 |
94 |
95 | Note that instead of manually specifying models you wish to report errors on, you can instead use the special :all value which will automatically use all view assign values that contain an ActiveModel::Errors "errors" method.
96 |
97 |
98 | == Running Tests
99 |
100 | This gem uses appraisal to test with different versions of the dependencies. See Appraisal first for which versions are tested, then run to test all appraisals:
101 |
102 | $ rake appraisal test
103 |
104 |
--------------------------------------------------------------------------------
/Rakefile:
--------------------------------------------------------------------------------
1 | require 'rubygems'
2 | require 'bundler/setup'
3 |
4 | require 'rake'
5 | require 'appraisal'
6 | require 'bundler/setup'
7 | require 'rspec/core/rake_task'
8 | require 'rdoc/task'
9 |
10 | Bundler::GemHelper.install_tasks
11 |
12 | desc 'Default: run unit tests.'
13 | task :default => [:clean, :test]
14 |
15 | desc "Run Specs"
16 | RSpec::Core::RakeTask.new(:spec) do |t|
17 | end
18 |
19 | task :test => :spec
20 |
21 | desc 'Default: run unit tests.'
22 | task :default => [:clean, :all]
23 |
24 | desc 'Test the paperclip plugin under all supported Rails versions.'
25 | task :all do |t|
26 | if ENV['BUNDLE_GEMFILE']
27 | exec('rake test')
28 | else
29 | Rake::Task["appraisal:install"].execute
30 | exec('rake appraisal test')
31 | end
32 | end
33 |
34 | desc "Clean up files."
35 | task :clean do |t|
36 | FileUtils.rm_rf "tmp"
37 | Dir.glob("message_block-*.gem").each {|f| FileUtils.rm f }
38 | end
39 |
40 | desc "Generate documentation for the plugin."
41 | Rake::RDocTask.new(:rdoc) do |rdoc|
42 | rdoc.rdoc_dir = "rdoc"
43 | rdoc.title = "message_block"
44 | rdoc.options << "--line-numbers" << "--inline-source"
45 | rdoc.rdoc_files.include('README')
46 | rdoc.rdoc_files.include('lib/**/*.rb')
47 | end
48 |
49 | Dir["#{File.dirname(__FILE__)}/lib/tasks/*.rake"].sort.each { |ext| load ext }
50 |
51 |
52 |
--------------------------------------------------------------------------------
/gemfiles/3.1.gemfile:
--------------------------------------------------------------------------------
1 | # This file was generated by Appraisal
2 |
3 | source "http://rubygems.org"
4 |
5 | gem "rails", "~> 3.1.6"
6 |
7 | gemspec :path=>"../"
--------------------------------------------------------------------------------
/gemfiles/3.1.gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: /Users/bhughes/Workspace/message_block
3 | specs:
4 | message_block (2.0.3)
5 | rails (>= 3.1.0)
6 |
7 | GEM
8 | remote: http://rubygems.org/
9 | specs:
10 | actionmailer (3.1.11)
11 | actionpack (= 3.1.11)
12 | mail (~> 2.3.3)
13 | actionpack (3.1.11)
14 | activemodel (= 3.1.11)
15 | activesupport (= 3.1.11)
16 | builder (~> 3.0.0)
17 | erubis (~> 2.7.0)
18 | i18n (~> 0.6)
19 | rack (~> 1.3.6)
20 | rack-cache (~> 1.2)
21 | rack-mount (~> 0.8.2)
22 | rack-test (~> 0.6.1)
23 | sprockets (~> 2.0.4)
24 | activemodel (3.1.11)
25 | activesupport (= 3.1.11)
26 | builder (~> 3.0.0)
27 | i18n (~> 0.6)
28 | activerecord (3.1.11)
29 | activemodel (= 3.1.11)
30 | activesupport (= 3.1.11)
31 | arel (~> 2.2.3)
32 | tzinfo (~> 0.3.29)
33 | activeresource (3.1.11)
34 | activemodel (= 3.1.11)
35 | activesupport (= 3.1.11)
36 | activesupport (3.1.11)
37 | multi_json (~> 1.0)
38 | appraisal (0.5.1)
39 | bundler
40 | rake
41 | arel (2.2.3)
42 | builder (3.0.4)
43 | diff-lcs (1.2.1)
44 | erubis (2.7.0)
45 | hike (1.2.1)
46 | i18n (0.6.4)
47 | json (1.7.7)
48 | mail (2.3.3)
49 | i18n (>= 0.4.0)
50 | mime-types (~> 1.16)
51 | treetop (~> 1.4.8)
52 | mime-types (1.21)
53 | multi_json (1.6.1)
54 | polyglot (0.3.3)
55 | rack (1.3.10)
56 | rack-cache (1.2)
57 | rack (>= 0.4)
58 | rack-mount (0.8.3)
59 | rack (>= 1.0.0)
60 | rack-ssl (1.3.3)
61 | rack
62 | rack-test (0.6.2)
63 | rack (>= 1.0)
64 | rails (3.1.11)
65 | actionmailer (= 3.1.11)
66 | actionpack (= 3.1.11)
67 | activerecord (= 3.1.11)
68 | activeresource (= 3.1.11)
69 | activesupport (= 3.1.11)
70 | bundler (~> 1.0)
71 | railties (= 3.1.11)
72 | railties (3.1.11)
73 | actionpack (= 3.1.11)
74 | activesupport (= 3.1.11)
75 | rack-ssl (~> 1.3.2)
76 | rake (>= 0.8.7)
77 | rdoc (~> 3.4)
78 | thor (~> 0.14.6)
79 | rake (10.0.3)
80 | rdoc (3.12.2)
81 | json (~> 1.4)
82 | rspec (2.13.0)
83 | rspec-core (~> 2.13.0)
84 | rspec-expectations (~> 2.13.0)
85 | rspec-mocks (~> 2.13.0)
86 | rspec-core (2.13.0)
87 | rspec-expectations (2.13.0)
88 | diff-lcs (>= 1.1.3, < 2.0)
89 | rspec-mocks (2.13.0)
90 | sprockets (2.0.4)
91 | hike (~> 1.2)
92 | rack (~> 1.0)
93 | tilt (~> 1.1, != 1.3.0)
94 | sqlite3 (1.3.7)
95 | sqlite3-ruby (1.3.3)
96 | sqlite3 (>= 1.3.3)
97 | thor (0.14.6)
98 | tilt (1.3.4)
99 | treetop (1.4.12)
100 | polyglot
101 | polyglot (>= 0.3.1)
102 | tzinfo (0.3.35)
103 |
104 | PLATFORMS
105 | ruby
106 |
107 | DEPENDENCIES
108 | appraisal (~> 0.5.1)
109 | message_block!
110 | rails (~> 3.1.6)
111 | rspec (~> 2.13)
112 | sqlite3-ruby (~> 1.3.1)
113 |
--------------------------------------------------------------------------------
/gemfiles/3.2.gemfile:
--------------------------------------------------------------------------------
1 | # This file was generated by Appraisal
2 |
3 | source "http://rubygems.org"
4 |
5 | gem "rails", "~> 3.2.6"
6 |
7 | gemspec :path=>"../"
--------------------------------------------------------------------------------
/gemfiles/3.2.gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: /Users/bhughes/Workspace/message_block
3 | specs:
4 | message_block (2.0.3)
5 | rails (>= 3.1.0)
6 |
7 | GEM
8 | remote: http://rubygems.org/
9 | specs:
10 | actionmailer (3.2.12)
11 | actionpack (= 3.2.12)
12 | mail (~> 2.4.4)
13 | actionpack (3.2.12)
14 | activemodel (= 3.2.12)
15 | activesupport (= 3.2.12)
16 | builder (~> 3.0.0)
17 | erubis (~> 2.7.0)
18 | journey (~> 1.0.4)
19 | rack (~> 1.4.5)
20 | rack-cache (~> 1.2)
21 | rack-test (~> 0.6.1)
22 | sprockets (~> 2.2.1)
23 | activemodel (3.2.12)
24 | activesupport (= 3.2.12)
25 | builder (~> 3.0.0)
26 | activerecord (3.2.12)
27 | activemodel (= 3.2.12)
28 | activesupport (= 3.2.12)
29 | arel (~> 3.0.2)
30 | tzinfo (~> 0.3.29)
31 | activeresource (3.2.12)
32 | activemodel (= 3.2.12)
33 | activesupport (= 3.2.12)
34 | activesupport (3.2.12)
35 | i18n (~> 0.6)
36 | multi_json (~> 1.0)
37 | appraisal (0.5.1)
38 | bundler
39 | rake
40 | arel (3.0.2)
41 | builder (3.0.4)
42 | diff-lcs (1.2.1)
43 | erubis (2.7.0)
44 | hike (1.2.1)
45 | i18n (0.6.4)
46 | journey (1.0.4)
47 | json (1.7.7)
48 | mail (2.4.4)
49 | i18n (>= 0.4.0)
50 | mime-types (~> 1.16)
51 | treetop (~> 1.4.8)
52 | mime-types (1.21)
53 | multi_json (1.6.1)
54 | polyglot (0.3.3)
55 | rack (1.4.5)
56 | rack-cache (1.2)
57 | rack (>= 0.4)
58 | rack-ssl (1.3.3)
59 | rack
60 | rack-test (0.6.2)
61 | rack (>= 1.0)
62 | rails (3.2.12)
63 | actionmailer (= 3.2.12)
64 | actionpack (= 3.2.12)
65 | activerecord (= 3.2.12)
66 | activeresource (= 3.2.12)
67 | activesupport (= 3.2.12)
68 | bundler (~> 1.0)
69 | railties (= 3.2.12)
70 | railties (3.2.12)
71 | actionpack (= 3.2.12)
72 | activesupport (= 3.2.12)
73 | rack-ssl (~> 1.3.2)
74 | rake (>= 0.8.7)
75 | rdoc (~> 3.4)
76 | thor (>= 0.14.6, < 2.0)
77 | rake (10.0.3)
78 | rdoc (3.12.2)
79 | json (~> 1.4)
80 | rspec (2.13.0)
81 | rspec-core (~> 2.13.0)
82 | rspec-expectations (~> 2.13.0)
83 | rspec-mocks (~> 2.13.0)
84 | rspec-core (2.13.0)
85 | rspec-expectations (2.13.0)
86 | diff-lcs (>= 1.1.3, < 2.0)
87 | rspec-mocks (2.13.0)
88 | sprockets (2.2.2)
89 | hike (~> 1.2)
90 | multi_json (~> 1.0)
91 | rack (~> 1.0)
92 | tilt (~> 1.1, != 1.3.0)
93 | sqlite3 (1.3.7)
94 | sqlite3-ruby (1.3.3)
95 | sqlite3 (>= 1.3.3)
96 | thor (0.17.0)
97 | tilt (1.3.4)
98 | treetop (1.4.12)
99 | polyglot
100 | polyglot (>= 0.3.1)
101 | tzinfo (0.3.35)
102 |
103 | PLATFORMS
104 | ruby
105 |
106 | DEPENDENCIES
107 | appraisal (~> 0.5.1)
108 | message_block!
109 | rails (~> 3.2.6)
110 | rspec (~> 2.13)
111 | sqlite3-ruby (~> 1.3.1)
112 |
--------------------------------------------------------------------------------
/gemfiles/4.0.gemfile:
--------------------------------------------------------------------------------
1 | # This file was generated by Appraisal
2 |
3 | source "http://rubygems.org"
4 |
5 | gem "rails", "~> 4.0.0"
6 |
7 | gemspec :path=>"../"
--------------------------------------------------------------------------------
/gemfiles/4.0.gemfile.lock:
--------------------------------------------------------------------------------
1 | PATH
2 | remote: /Users/bhughes/Workspace/message_block
3 | specs:
4 | message_block (2.0.3)
5 | rails (>= 3.1.0)
6 |
7 | GEM
8 | remote: http://rubygems.org/
9 | specs:
10 | actionmailer (4.0.0)
11 | actionpack (= 4.0.0)
12 | mail (~> 2.5.3)
13 | actionpack (4.0.0)
14 | activesupport (= 4.0.0)
15 | builder (~> 3.1.0)
16 | erubis (~> 2.7.0)
17 | rack (~> 1.5.2)
18 | rack-test (~> 0.6.2)
19 | activemodel (4.0.0)
20 | activesupport (= 4.0.0)
21 | builder (~> 3.1.0)
22 | activerecord (4.0.0)
23 | activemodel (= 4.0.0)
24 | activerecord-deprecated_finders (~> 1.0.2)
25 | activesupport (= 4.0.0)
26 | arel (~> 4.0.0)
27 | activerecord-deprecated_finders (1.0.3)
28 | activesupport (4.0.0)
29 | i18n (~> 0.6, >= 0.6.4)
30 | minitest (~> 4.2)
31 | multi_json (~> 1.3)
32 | thread_safe (~> 0.1)
33 | tzinfo (~> 0.3.37)
34 | appraisal (0.5.1)
35 | bundler
36 | rake
37 | arel (4.0.0)
38 | atomic (1.1.13)
39 | builder (3.1.4)
40 | diff-lcs (1.2.1)
41 | erubis (2.7.0)
42 | hike (1.2.3)
43 | i18n (0.6.5)
44 | mail (2.5.4)
45 | mime-types (~> 1.16)
46 | treetop (~> 1.4.8)
47 | mime-types (1.24)
48 | minitest (4.7.5)
49 | multi_json (1.7.9)
50 | polyglot (0.3.3)
51 | rack (1.5.2)
52 | rack-test (0.6.2)
53 | rack (>= 1.0)
54 | rails (4.0.0)
55 | actionmailer (= 4.0.0)
56 | actionpack (= 4.0.0)
57 | activerecord (= 4.0.0)
58 | activesupport (= 4.0.0)
59 | bundler (>= 1.3.0, < 2.0)
60 | railties (= 4.0.0)
61 | sprockets-rails (~> 2.0.0)
62 | railties (4.0.0)
63 | actionpack (= 4.0.0)
64 | activesupport (= 4.0.0)
65 | rake (>= 0.8.7)
66 | thor (>= 0.18.1, < 2.0)
67 | rake (10.0.3)
68 | rspec (2.13.0)
69 | rspec-core (~> 2.13.0)
70 | rspec-expectations (~> 2.13.0)
71 | rspec-mocks (~> 2.13.0)
72 | rspec-core (2.13.0)
73 | rspec-expectations (2.13.0)
74 | diff-lcs (>= 1.1.3, < 2.0)
75 | rspec-mocks (2.13.0)
76 | sprockets (2.10.0)
77 | hike (~> 1.2)
78 | multi_json (~> 1.0)
79 | rack (~> 1.0)
80 | tilt (~> 1.1, != 1.3.0)
81 | sprockets-rails (2.0.0)
82 | actionpack (>= 3.0)
83 | activesupport (>= 3.0)
84 | sprockets (~> 2.8)
85 | sqlite3 (1.3.7)
86 | sqlite3-ruby (1.3.3)
87 | sqlite3 (>= 1.3.3)
88 | thor (0.18.1)
89 | thread_safe (0.1.2)
90 | atomic
91 | tilt (1.4.1)
92 | treetop (1.4.15)
93 | polyglot
94 | polyglot (>= 0.3.1)
95 | tzinfo (0.3.37)
96 |
97 | PLATFORMS
98 | ruby
99 |
100 | DEPENDENCIES
101 | appraisal (~> 0.5.1)
102 | message_block!
103 | rails (~> 4.0.0)
104 | rspec (~> 2.13)
105 | sqlite3-ruby (~> 1.3.1)
106 |
--------------------------------------------------------------------------------
/lib/message_block.rb:
--------------------------------------------------------------------------------
1 | require "rubygems"
2 | require "rails"
3 | require "active_support"
4 | require "message_block/helpers"
5 | require "message_block/engine"
6 |
--------------------------------------------------------------------------------
/lib/message_block/engine.rb:
--------------------------------------------------------------------------------
1 | module MessageBlock
2 | class Engine < Rails::Engine
3 | initializer "message_block.insert_helpers" do
4 | ActiveSupport.on_load(:action_view) do
5 | ActionView::Base.send(:include, MessageBlock::Helpers)
6 | end
7 | end
8 | end
9 | end
10 |
--------------------------------------------------------------------------------
/lib/message_block/helpers.rb:
--------------------------------------------------------------------------------
1 | module MessageBlock
2 | module Helpers
3 |
4 | def message_block(options = {})
5 | options[:model_error_type] ||= :error
6 | options[:flash_types] ||= [:notice, :back, :confirm, :error, :alert, :info, :warn].sort_by(&:to_s)
7 | options[:on] ||= controller.controller_name.split('/').last.gsub(/\_controller$/, '').singularize.to_sym
8 | options[:html] ||= {:id => "message_block", :class => "message_block"}
9 | options[:html][:id] = options[:id] if options[:id]
10 | options[:html][:class] = options[:class] if options[:class]
11 | options[:container] = :div if options[:container].nil?
12 |
13 | flash_messages = {}
14 |
15 | options[:flash_types].each do |type|
16 | entries = flash[type.to_sym]
17 | next if entries.nil?
18 | entries = [entries] unless entries.is_a?(Array)
19 |
20 | flash_messages[type.to_sym] ||= []
21 | flash_messages[type.to_sym] += entries
22 | end
23 |
24 | options[:on] = [options[:on]] unless options[:on].is_a?(Array)
25 | model_objects = options[:on].map do |model_object|
26 | if model_object == :all
27 | assigns.values.select {|o| o.respond_to?(:errors) && o.errors.is_a?(ActiveModel::Errors) }
28 | elsif model_object.instance_of?(String) or model_object.instance_of?(Symbol)
29 | instance_variable_get("@#{model_object}")
30 | else
31 | model_object
32 | end
33 | end.flatten.select {|m| !m.nil? }
34 |
35 | model_errors = model_objects.inject([]) {|b, m| b += m.errors.full_messages }
36 |
37 | flash_messages[options[:model_error_type].to_sym] ||= []
38 | flash_messages[options[:model_error_type].to_sym] += model_errors
39 |
40 | contents = flash_messages.keys.sort_by(&:to_s).select {|type| !flash_messages[type.to_sym].empty? }.map do |type|
41 | "" + flash_messages[type.to_sym].map {|message| "#{message} " }.join + " "
42 | end.join
43 |
44 | unless contents.blank?
45 | if options[:container]
46 | content_tag(options[:container], contents, options[:html], false)
47 | else
48 | contents
49 | end
50 | end
51 | end
52 |
53 | end
54 | end
55 |
--------------------------------------------------------------------------------
/lib/message_block/version.rb:
--------------------------------------------------------------------------------
1 | module MessageBlock
2 | VERSION = "2.0.3"
3 | end
4 |
--------------------------------------------------------------------------------
/message_block.gemspec:
--------------------------------------------------------------------------------
1 | # -*- encoding: utf-8 -*-
2 | $:.push File.expand_path("../lib", __FILE__)
3 | require "message_block/version"
4 |
5 | Gem::Specification.new do |s|
6 | s.name = "message_block"
7 | s.version = MessageBlock::VERSION
8 | s.author = "Ben Hughes"
9 | s.email = "ben@railsgarden.com"
10 | s.homepage = "http://github.com/rubiety/message_block"
11 | s.summary = "Flash message and error_messages_for handling with a common interface via the asset pipeline"
12 | s.description = "Implements the common view pattern by which a list of messages are shown at the top, often a combination of flash messages and ActiveRecord validation issues on one or more models."
13 |
14 | s.files = Dir["{lib,spec,vendor}/**/*", "[A-Z]*", "init.rb"]
15 | s.require_path = "lib"
16 |
17 | s.rubyforge_project = s.name
18 | s.required_rubygems_version = ">= 1.3.4"
19 |
20 | s.add_dependency("rails", [">= 3.1.0"])
21 | s.add_development_dependency("rspec", ["~> 2.13"])
22 | s.add_development_dependency("appraisal", ["~> 0.5.1"])
23 | s.add_development_dependency("sqlite3-ruby", ["~> 1.3.1"])
24 | end
25 |
--------------------------------------------------------------------------------
/spec/message_block/helpers_spec.rb:
--------------------------------------------------------------------------------
1 | require "spec_helper"
2 |
3 | describe MessageBlock::Helpers do
4 | include ActionView::Helpers::TagHelper
5 | include MessageBlock::Helpers
6 |
7 | before do
8 | @post = Post.new
9 | @user = User.new
10 | stub!(:assigns).and_return(:user => @user, :post => @post)
11 | end
12 |
13 | it "should accept valid options" do
14 | lambda {
15 | message_block :on => :post,
16 | :model_error_type => "fail",
17 | :flash_types => %w(error warn),
18 | :html => {:style => 'height: 10em'},
19 | :id => "block",
20 | :class => "messages"
21 | }.should_not raise_error
22 | end
23 |
24 | it "should show nothing with no errors" do
25 | message_block.should be_nil
26 | end
27 |
28 | it "should automatically find post errors with posts controller" do
29 | @controller = posts_controller
30 | output = message_block
31 | output.should == %()
32 | end
33 |
34 | it "should give no error for post" do
35 | output = message_block(:on => :post)
36 | output.should == %()
37 | end
38 |
39 | it "should give error for user" do
40 | output = message_block(:on => :user)
41 | output.should == %()
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 == %(name can't be blank name can't be blank )
47 | end
48 |
49 | it "should give errors for both post and user" do
50 | output = message_block(:on => [:post, :user])
51 | output.should == %(name can't be blank name can't be blank )
52 | end
53 |
54 | it "should give errors for both post and user in the correct order" do
55 | output = message_block(:on => [:user, :post])
56 | output.should == %(name can't be blank name can't be blank )
57 | end
58 |
59 | it "should give error for user given direct instance variable" do
60 | output = message_block(:on => @user)
61 | output.should == %()
62 | end
63 |
64 | it "should respect model error type" do
65 | output = message_block(:on => :user, :model_error_type => "fail")
66 | output.should == %()
67 | end
68 |
69 | it "should be able to specify id for containing div" do
70 | @controller = posts_controller
71 | output = message_block(:id => "messages")
72 | output.should == %()
73 | end
74 |
75 | it "should be able to specify class for containing div" do
76 | @controller = posts_controller
77 | output = message_block(:class => "messages")
78 | output.should == %()
79 | end
80 |
81 | it "should be able to specify html options for containing div" do
82 | @controller = posts_controller
83 | output = message_block(:html => {:id => "block", :class => "messages"})
84 | output.should == %()
85 | end
86 |
87 | it "should be able to specify container option as false" do
88 | output = message_block(:on => :post, :container => false)
89 | output.should == %()
90 | end
91 |
92 | it "should be able to specify container option" do
93 | output = message_block(:on => :post, :container => :fieldset)
94 | output.should == %( )
95 | end
96 |
97 | it "should be able to see flash error string" do
98 | flash[:error] = "Error A"
99 | output = message_block
100 | output.should == %()
101 | end
102 |
103 | it "should be able to see flash error array" do
104 | flash[:error] = ["Error A", "Error B"]
105 | output = message_block
106 | output.should == %()
107 | end
108 |
109 | it "should be able to see default flash types" do
110 | default_types = [:notice, :back, :confirm, :error, :info, :warn].sort_by(&:to_s)
111 | default_types.each do |type|
112 | flash[type] = type.to_s
113 | end
114 |
115 | expected_contents = default_types.map do |type|
116 | content_tag(:ul, content_tag(:li, type.to_s), :class => type.to_s)
117 | end.join
118 |
119 | output = message_block
120 | output.should == %(#{expected_contents}
)
121 | end
122 |
123 | it "should be able to see flash error alongside model error" do
124 | flash[:error] = "Error A"
125 | output = message_block(:on => :post)
126 | output.should == %(Error A name can't be blank )
127 | end
128 |
129 | it "should be safe for html inside flash messages" do
130 | flash[:error] = ["Error A ", "Error B "]
131 | output = message_block
132 | output.should == %()
133 | end
134 |
135 | end
136 |
--------------------------------------------------------------------------------
/spec/spec_helper.rb:
--------------------------------------------------------------------------------
1 | require "rubygems"
2 | require "message_block"
3 | require "active_support"
4 | require "active_model"
5 | require "action_pack"
6 | require "action_view"
7 |
8 | class Post
9 | extend ActiveModel::Naming
10 |
11 | attr_accessor :name
12 | attr_reader :errors
13 |
14 | def initialize
15 | @errors = ActiveModel::Errors.new(self)
16 | @errors.add(:name, "can't be blank")
17 | end
18 |
19 | def read_attribute_for_validation(attr)
20 | send(attr)
21 | end
22 |
23 | def self.human_attribute_name(attr, options = {})
24 | attr
25 | end
26 |
27 | def self.lookup_ancestors
28 | [self]
29 | end
30 | end
31 |
32 | class User
33 | extend ActiveModel::Naming
34 |
35 | attr_accessor :name
36 | attr_reader :errors
37 |
38 | def initialize
39 | @errors = ActiveModel::Errors.new(self)
40 | @errors.add(:name, "can't be blank")
41 | end
42 |
43 | def read_attribute_for_validation(attr)
44 | send(attr)
45 | end
46 |
47 | def self.human_attribute_name(attr, options = {})
48 | attr
49 | end
50 |
51 | def self.lookup_ancestors
52 | [self]
53 | end
54 | end
55 |
56 |
57 | def controller
58 | @controller ||= Class.new {
59 | def controller_name
60 | "widgets_controller"
61 | end
62 | }.new
63 | end
64 |
65 | def posts_controller
66 | Class.new {
67 | def controller_name
68 | "posts_controller"
69 | end
70 | }.new
71 | end
72 |
73 | def flash
74 | @flash ||= {}
75 | end
76 |
--------------------------------------------------------------------------------
/vendor/assets/images/message_block/alert.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rubiety/message_block/9ec9f63f3d08b7cd6dea8a99d744dd1112726ad6/vendor/assets/images/message_block/alert.gif
--------------------------------------------------------------------------------
/vendor/assets/images/message_block/back.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rubiety/message_block/9ec9f63f3d08b7cd6dea8a99d744dd1112726ad6/vendor/assets/images/message_block/back.gif
--------------------------------------------------------------------------------
/vendor/assets/images/message_block/confirm.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rubiety/message_block/9ec9f63f3d08b7cd6dea8a99d744dd1112726ad6/vendor/assets/images/message_block/confirm.gif
--------------------------------------------------------------------------------
/vendor/assets/images/message_block/error.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rubiety/message_block/9ec9f63f3d08b7cd6dea8a99d744dd1112726ad6/vendor/assets/images/message_block/error.gif
--------------------------------------------------------------------------------
/vendor/assets/images/message_block/info.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rubiety/message_block/9ec9f63f3d08b7cd6dea8a99d744dd1112726ad6/vendor/assets/images/message_block/info.gif
--------------------------------------------------------------------------------
/vendor/assets/images/message_block/notice.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rubiety/message_block/9ec9f63f3d08b7cd6dea8a99d744dd1112726ad6/vendor/assets/images/message_block/notice.gif
--------------------------------------------------------------------------------
/vendor/assets/images/message_block/warn.gif:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/rubiety/message_block/9ec9f63f3d08b7cd6dea8a99d744dd1112726ad6/vendor/assets/images/message_block/warn.gif
--------------------------------------------------------------------------------
/vendor/assets/stylesheets/message_block.css.erb:
--------------------------------------------------------------------------------
1 | .message_block {
2 | clear: both;
3 | margin: 12px 0;
4 | }
5 |
6 | .message_block ul {
7 | border-bottom: 1px solid #ecd757;
8 | border-top: 1px solid #ecd757;
9 | list-style: none;
10 | padding: 10px;
11 | }
12 | .message_block ul li {
13 | margin-left: 3em;
14 | }
15 |
16 | .message_block ul.error {
17 | background: #fcf6d0 url(<%= asset_path("message_block/error.gif") %>) 16px 50% no-repeat;
18 | }
19 |
20 | .message_block ul.alert {
21 | background: #fcf6d0 url(<%= asset_path("message_block/alert.gif") %>) 16px 50% no-repeat;
22 | }
23 |
24 | .message_block ul.info {
25 | background: #fcf6d0 url(<%= asset_path("message_block/info.gif") %>) 16px 50% no-repeat;
26 | }
27 |
28 | .message_block ul.notice {
29 | background: #fcf6d0 url(<%= asset_path("message_block/notice.gif") %>) 16px 50% no-repeat;
30 | }
31 |
32 | .message_block ul.confirm {
33 | background: #fcf6d0 url(<%= asset_path("message_block/confirm.gif") %>) 16px 50% no-repeat;
34 | }
35 |
36 | .message_block ul.warn {
37 | background: #fcf6d0 url(<%= asset_path("message_block/warn.gif") %>) 1em 50% no-repeat;
38 | }
39 |
40 |
--------------------------------------------------------------------------------