simply_messages 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,5 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+ *.swp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in simply_messages.gemspec
4
+ gemspec
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009-2011 Paweł Gościcki
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.
data/README.rdoc ADDED
@@ -0,0 +1,87 @@
1
+ = simply_messages
2
+
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).
5
+
6
+
7
+ == Requirements
8
+
9
+ Rails version 3.0+
10
+
11
+
12
+ == Installation
13
+
14
+ Put this line in your Gemfile:
15
+
16
+ gem 'simply_messages'
17
+
18
+ Then bundle:
19
+
20
+ bundle
21
+
22
+
23
+ In `app/views/layouts/application.html.erb`:
24
+
25
+ <%= messages_block %>
26
+
27
+
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).
29
+
30
+ Will also display appropriate messages for :success and :error flash keys.
31
+
32
+
33
+ We can also specify the model we would like to display errors for, like this:
34
+
35
+ <%= messages_block :for => :comment %>
36
+
37
+ We can also display errors for more than one model:
38
+
39
+ <%= messages_block :for => [:post, :comment] %>
40
+
41
+
42
+ === EXAMPLE 1
43
+
44
+ controller:
45
+
46
+ flash.now[:notice] = 'Post saved'
47
+
48
+ view:
49
+
50
+ <%= messages_block %>
51
+
52
+ will result in:
53
+
54
+ <div class="notice">
55
+ <p>Post saved.</p>
56
+ </div>
57
+
58
+
59
+ === EXAMPLE 2
60
+
61
+ users_controller:
62
+
63
+ @user.errors.add('name', 'name should not be empty')
64
+ flash.now[:alert] = 'Unable to add user'
65
+
66
+ view:
67
+
68
+ <%= messages_block %>
69
+
70
+ will result in:
71
+
72
+ <div class="alert">
73
+ <p>Unable to add user:</p>
74
+ <ul>
75
+ <li>name should not be empty</li>
76
+ </ul>
77
+ </div>
78
+
79
+
80
+ == Contributing to simply_messages
81
+
82
+ * Fork, fix, then send me a pull request.
83
+
84
+
85
+ == Copyright
86
+
87
+ Copyright (c) 2009-2011 Paweł Gościcki, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ # encoding: utf-8
2
+
3
+ require 'bundler'
4
+ Bundler::GemHelper.install_tasks
5
+
6
+ require 'rake/rdoctask'
7
+ Rake::RDocTask.new do |rdoc|
8
+ require 'simply_messages/version'
9
+
10
+ rdoc.rdoc_dir = 'rdoc'
11
+ rdoc.title = "simply_messages #{SimplyMessages::VERSION}"
12
+ rdoc.rdoc_files.include('README*')
13
+ rdoc.rdoc_files.include('lib/**/*.rb')
14
+ end
@@ -0,0 +1 @@
1
+ require File.join(File.dirname(__FILE__), 'simply_messages/railtie')
@@ -0,0 +1,72 @@
1
+ module SimplyMessages
2
+
3
+ module ActionViewExtension
4
+
5
+ extend ::ActiveSupport::Concern
6
+
7
+ module InstanceMethods
8
+ def messages_block(options = {})
9
+
10
+ # make options[:for] an array
11
+ options[:for] ||= []
12
+ options[:for] = [options[:for]] unless options[:for].is_a?(Array)
13
+
14
+ # add model object associated with current controller
15
+ options[:for] << controller.controller_name.split('/').last.gsub(/\_controller$/, '').singularize.to_sym
16
+
17
+ # fetch all model objects
18
+ model_objects = options[:for].map do |model_object|
19
+ if model_object.instance_of?(String) or model_object.instance_of?(Symbol)
20
+ instance_variable_get("@#{model_object}")
21
+ else
22
+ model_object
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
32
+
33
+ # messages_block
34
+ messages_block = ''
35
+
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)
43
+ end
44
+
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
+
50
+ if flash[key].present?
51
+ msg += content_tag(:p, flash[key] + (models_errors.any? ? ':' : '.'))
52
+ end
53
+
54
+ if models_errors.any?
55
+ errors = []
56
+ models_errors.each do |e|
57
+ errors << content_tag(:li, e)
58
+ end
59
+ msg += content_tag(:ul, errors.join)
60
+ end
61
+
62
+ messages_block += content_tag(:div, msg, :class => key.to_s)
63
+ end
64
+
65
+ messages_block
66
+ end
67
+
68
+ end
69
+
70
+ end
71
+
72
+ end
@@ -0,0 +1,14 @@
1
+ require 'rails'
2
+
3
+ require File.join(File.dirname(__FILE__), 'action_view_extension')
4
+
5
+ module SimplyMessages
6
+ class Railtie < ::Rails::Railtie #:nodoc:
7
+ initializer 'simply_messages' do |app|
8
+ ActiveSupport.on_load(:action_view) do
9
+ ::ActionView::Base.send :include, SimplyMessages::ActionViewExtension
10
+ end
11
+ end
12
+ end
13
+ end
14
+
@@ -0,0 +1,3 @@
1
+ module SimplyMessages
2
+ VERSION = '0.0.1'
3
+ end
@@ -0,0 +1,28 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+
4
+ require "simply_messages/version"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = 'simply_messages'
8
+ s.version = SimplyMessages::VERSION
9
+ s.platform = Gem::Platform::RUBY
10
+ s.authors = ['Paweł Gościcki']
11
+ s.email = ['pawel.goscicki@gmail.com']
12
+ s.homepage = 'https://github.com/pjg/simply_messages'
13
+ s.summary = 'Unified flash notices and model error messages display solution'
14
+ s.description = 'simply_messages is a unified flash and error messages display solution'
15
+
16
+ s.rubyforge_project = 'simply_messages'
17
+
18
+ s.files = `git ls-files`.split("\n")
19
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
20
+ s.extra_rdoc_files = ['README.rdoc']
21
+ s.require_paths = ['lib']
22
+
23
+ s.licenses = ['MIT']
24
+
25
+ s.add_dependency 'rails', ['>= 3.0.0']
26
+ s.add_development_dependency 'bundler', ['>= 1.0.0']
27
+ s.add_development_dependency 'sqlite3', ['>= 0']
28
+ end
@@ -0,0 +1,81 @@
1
+ require File.dirname(__FILE__) + '/../test_helper'
2
+ require 'action_controller'
3
+ require 'action_controller/test_process'
4
+
5
+ class Mock
6
+ attr_accessor :errors
7
+
8
+ def initialize(opts = {})
9
+ # create Errors object so I could add errors to this mock model
10
+ @errors = ActiveRecord::Errors.new(self)
11
+ end
12
+ end
13
+
14
+ class MocksController < ApplicationController
15
+ def success
16
+ flash.now[:success] = 'We have a success'
17
+ render :inline => '<%= messages_block %>'
18
+ end
19
+
20
+ def notice
21
+ flash.now[:notice] = 'We have a notice'
22
+ render :inline => '<%= messages_block %>'
23
+ end
24
+
25
+ def error
26
+ flash.now[:error] = 'Error: no luck this time'
27
+ render :inline => '<%= messages_block %>'
28
+ end
29
+
30
+ def alert
31
+ flash.now[:alert] = 'Alert: no luck this time'
32
+ render :inline => '<%= messages_block %>'
33
+ end
34
+
35
+ def model_error
36
+ flash.now[:error] = 'Model error'
37
+ @mock = Mock.new
38
+ @mock.errors.add('name', 'just having some troubles')
39
+ render :inline => '<%= messages_block %>'
40
+ end
41
+ end
42
+
43
+ class MocksControllerTest < ActionController::TestCase
44
+
45
+ def setup
46
+ @controller = MocksController.new
47
+ @request = ActionController::TestRequest.new
48
+ @response = ActionController::TestResponse.new
49
+
50
+ ActionController::Routing::Routes.draw do |map|
51
+ map.connect ':controller/:action/:id'
52
+ end
53
+ end
54
+
55
+ def test_success
56
+ get :success
57
+ assert_select 'div.success p', :text => /We have a success./
58
+ end
59
+
60
+ def test_notice
61
+ get :notice
62
+ assert_select 'div.notice p', :text => /We have a notice./
63
+ end
64
+
65
+ def test_error
66
+ get :error
67
+ assert_select 'div.error p', :text => /Error: no luck this time./
68
+ end
69
+
70
+ def test_alert
71
+ get :alert
72
+ assert_select 'div.alert p', :text => /Alert: no luck this time./
73
+ end
74
+
75
+ def test_model_error
76
+ get :model_error
77
+ assert_select 'div.error p', :text => /Model error:/
78
+ assert_select 'div.error ul li', :text => /just having some troubles/
79
+ end
80
+
81
+ end
@@ -0,0 +1,11 @@
1
+ ENV['RAILS_ENV'] = 'test'
2
+ ENV['RAILS_ROOT'] ||= File.dirname(__FILE__) + '/../../../..'
3
+
4
+ # Optional gems
5
+ begin
6
+ require 'redgreen'
7
+ rescue LoadError
8
+ end
9
+
10
+ # Load Rails
11
+ require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simply_messages
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - "Pawe\xC5\x82 Go\xC5\x9Bcicki"
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2011-05-14 00:00:00 +02:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: rails
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 7
30
+ segments:
31
+ - 3
32
+ - 0
33
+ - 0
34
+ version: 3.0.0
35
+ type: :runtime
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: bundler
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 23
46
+ segments:
47
+ - 1
48
+ - 0
49
+ - 0
50
+ version: 1.0.0
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: sqlite3
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ hash: 3
62
+ segments:
63
+ - 0
64
+ version: "0"
65
+ type: :development
66
+ version_requirements: *id003
67
+ description: simply_messages is a unified flash and error messages display solution
68
+ email:
69
+ - pawel.goscicki@gmail.com
70
+ executables: []
71
+
72
+ extensions: []
73
+
74
+ extra_rdoc_files:
75
+ - README.rdoc
76
+ files:
77
+ - .gitignore
78
+ - Gemfile
79
+ - MIT-LICENSE
80
+ - README.rdoc
81
+ - Rakefile
82
+ - lib/simply_messages.rb
83
+ - lib/simply_messages/action_view_extension.rb
84
+ - lib/simply_messages/railtie.rb
85
+ - lib/simply_messages/version.rb
86
+ - simply_messages.gemspec
87
+ - test/functional/helpers_test.rb
88
+ - test/test_helper.rb
89
+ has_rdoc: true
90
+ homepage: https://github.com/pjg/simply_messages
91
+ licenses:
92
+ - MIT
93
+ post_install_message:
94
+ rdoc_options: []
95
+
96
+ require_paths:
97
+ - lib
98
+ required_ruby_version: !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
+ required_rubygems_version: !ruby/object:Gem::Requirement
108
+ none: false
109
+ requirements:
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ hash: 3
113
+ segments:
114
+ - 0
115
+ version: "0"
116
+ requirements: []
117
+
118
+ rubyforge_project: simply_messages
119
+ rubygems_version: 1.6.2
120
+ signing_key:
121
+ specification_version: 3
122
+ summary: Unified flash notices and model error messages display solution
123
+ test_files: []
124
+