the_notifications 0.6.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1333942861d042c9a0fcac2df8025c254c2183d7
4
+ data.tar.gz: 2b6f1c3b708b030cf93a2f16e84ecf68318bd552
5
+ SHA512:
6
+ metadata.gz: 4435039f259ff3189c06e7165b1687220ac687aec98efcf91ee4ef21761545e4d13752ff5d1c75982860c2e61e14790e6b9f2f737ea5cecd5ea353f8a0a1c0d9
7
+ data.tar.gz: 03255f1f2a509b295ee28a249cb08a62bc1cece92519f6f24b31bd3900224d80a1b6863e2c8a5db66d4b9753c22065c8a9365fa1f2c87fef8fc3e3560ba65eba
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.keep ADDED
File without changes
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in notifications.gemspec
4
+ gemspec
@@ -0,0 +1,9 @@
1
+ ### MIT License
2
+
3
+ Copyright (c) 2013-[Current Year] Ilya N. Zykin
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,134 @@
1
+ ```ruby
2
+ This gem is a part of TheOpenCMS project. https://github.com/TheOpenCMS
3
+ ```
4
+
5
+ ## Notifications
6
+
7
+ Just add JSON on HTML notifications in your Rails app
8
+
9
+ ```ruby
10
+ gem 'notifications'
11
+ ```
12
+
13
+ ## How it works
14
+
15
+ This gem helps you to get rid of this bunch of a code
16
+
17
+ ```ruby
18
+ <%= form_for(@resource) do |f| %>
19
+ <% if @resource.errors.any? %>
20
+ <div id="error_explanation">
21
+ <h2><%= pluralize(@resource.errors.count, "error") %> prohibited this resource from being saved:</h2>
22
+
23
+ <ul>
24
+ <% @resource.errors.full_messages.each do |msg| %>
25
+ <li><%= msg %></li>
26
+ <% end %>
27
+ </ul>
28
+ </div>
29
+ <% end %>
30
+
31
+ # ....
32
+
33
+ <% end %>
34
+ ```
35
+
36
+ and replace it with the following code:
37
+
38
+ ```slim
39
+ - form_for(@post) do |f|
40
+ = render partial: 'notifications/form', locals: { object: @post }
41
+ ```
42
+
43
+ and this code to render Flash messages:
44
+
45
+ ```slim
46
+ body
47
+ = render partial: 'notifications/flash'
48
+ = yield
49
+ ```
50
+
51
+ ## How to use
52
+
53
+ *Note:* `toastr` is an external dependency for `format: :json` case. Download and and install it yourself. `vendors/toastr` is just a demo path to assets
54
+
55
+ *application.css*
56
+
57
+ ```css
58
+ //= require vendors/toastr
59
+ ```
60
+
61
+ *application.js*
62
+
63
+ ```js
64
+ //= require vendors/toastr
65
+ //= require notifications
66
+
67
+ $ ->
68
+ Notifications.init()
69
+ Notifications.show_notifications()
70
+ ```
71
+
72
+ ### Format `html` or `json`
73
+
74
+ You can select a way how to render notifications. There are 2 options: `format: :html` and `format: :json`
75
+
76
+ ```slim
77
+ = render partial: 'notifications/flash', locals: { format: :html }
78
+ = render partial: 'notifications/form', locals: { object: @post, format: :json }
79
+ ```
80
+
81
+ ### Default notifications format
82
+
83
+ *initializers/notifications.rb*
84
+
85
+ ```ruby
86
+ Notifications.configure do |config|
87
+ config.default_type = :json # :html
88
+ end
89
+ ```
90
+
91
+ ### Errors' localization
92
+
93
+ To avoid this case
94
+
95
+ ```
96
+ Title: не может быть пустым
97
+ ```
98
+
99
+ Do the following steps:
100
+
101
+ *models/user.rb*
102
+
103
+ ```ruby
104
+ class Post < ActiveRecord::Base
105
+ include Notifications::LocalizedErrors
106
+ end
107
+ ```
108
+
109
+ *config/locales/ru.yml*
110
+
111
+ ```
112
+ activerecord:
113
+ models:
114
+ post: Публикация
115
+ attributes:
116
+ post:
117
+ title: "Загловок"
118
+ ```
119
+
120
+ Now errors messages will look so:
121
+
122
+ ```
123
+ Загловок: не может быть пустым
124
+ ```
125
+
126
+ ### MIT License
127
+
128
+ Copyright (c) 2013-[Current Year] Ilya N. Zykin
129
+
130
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
131
+
132
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
133
+
134
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,51 @@
1
+ NotificationTool = toastr
2
+
3
+ @Notifications = do ->
4
+ clear: -> @clean()
5
+ clean: -> NotificationTool.clear()
6
+
7
+ show_error: (error) ->
8
+ NotificationTool.error(error) if error
9
+
10
+ show_errors: (errors) ->
11
+ for field, errs of errors
12
+ for err in errs
13
+ NotificationTool.error "<b>#{ field }:</b> #{ err }"
14
+
15
+ show_flash: (flash) ->
16
+ fu =
17
+ info: 'info'
18
+ notice: 'info'
19
+ errors: 'error'
20
+ error: 'error'
21
+ warning: 'warning'
22
+ alert: 'warning'
23
+
24
+ for level, msg of flash
25
+ method = fu[level] || 'info'
26
+
27
+ if msg instanceof Array
28
+ for _msg in msg
29
+ NotificationTool[method] _msg
30
+ else
31
+ NotificationTool[method] msg
32
+
33
+ show_notifications: ->
34
+ data = window.notifications
35
+ return false unless data
36
+
37
+ @show_errors errors if errors = data?.errors
38
+ @show_flash flash if flash = data?.flash
39
+
40
+ click_for_close_init: ->
41
+ $(document).on 'click', '.flash_msgs, .error_explanation', (e) ->
42
+ $(e.currentTarget).slideUp(500)
43
+
44
+ turbolinks_init: ->
45
+ $(document).on 'page:before-unload', (e) ->
46
+ window.notifications = undefined
47
+
48
+ init: ->
49
+ @inited ||= do =>
50
+ @click_for_close_init()
51
+ @turbolinks_init() if Turbolinks?
@@ -0,0 +1,43 @@
1
+ .toast-message
2
+ font-size: 15px
3
+
4
+ .flash_msgs
5
+ padding: 10px 10px 0px 10px
6
+ margin-bottom: 15px
7
+ border-radius: 5px
8
+ cursor: pointer
9
+
10
+ &.alert-info, &.alert-warning
11
+ color: #3A87AD
12
+ border: 3px solid gray
13
+
14
+ p
15
+ font-size: 14px
16
+ line-height: 150%
17
+ margin: 10px 0 15px 0
18
+
19
+ &.alert-info border-color: #3A87AD
20
+ &.alert-warning border-color: orange
21
+
22
+ .error_explanation.alert-danger
23
+ border: 3px solid #B94A48
24
+ color: #B94A48
25
+
26
+ padding: 10px
27
+ margin-bottom: 15px
28
+ border-radius: 5px
29
+ cursor: pointer
30
+
31
+ h4
32
+ margin: 10px 0 20px 0
33
+
34
+
35
+ ul
36
+ margin-left: 15px
37
+ padding-bottom: 0.1px
38
+ list-style: disc inside none
39
+
40
+ li
41
+ font-size: 14px
42
+ line-height: 130%
43
+ margin-bottom: 15px
@@ -0,0 +1,11 @@
1
+ module NotificationsHelper
2
+ def flash_class(level)
3
+ case level.to_sym
4
+ when :notice then "alert-info"
5
+ when :errors then "alert-danger"
6
+ when :error then "alert-danger"
7
+ when :alert then "alert-warning"
8
+ else "alert-success"
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,41 @@
1
+ # include ::Notifications::LocalizedErrors
2
+ module Notifications
3
+ module LocalizedErrors
4
+ extend ActiveSupport::Concern
5
+
6
+ # @post.localized_errors(except: [:'comment.title'])
7
+ def localized_errors opts = {}
8
+ opts.symbolize_keys!
9
+ excepts = opts.delete(:except) || []
10
+
11
+ errors.inject({}) do |hash, (k, v)|
12
+ unless excepts.include?(k.to_sym)
13
+ k = self.class.human_attribute_name k
14
+ hash[k].blank? ? hash[k] = [v] : hash[k].push(v)
15
+ hash[k].uniq!
16
+ end
17
+
18
+ hash
19
+ end
20
+ end
21
+
22
+ # @post.inline_localized_errors(except: [:'comment.title'])
23
+ def inline_localized_errors opts = {}
24
+ opts.symbolize_keys!
25
+ excepts = opts.delete(:except) || []
26
+
27
+ errors.inject({}) do |hash, (k, v)|
28
+ unless excepts.include?(k.to_sym)
29
+ name = self.class.human_attribute_name k
30
+ key = "#{ self.class }/#{ k }".parameterize.underscore.camelcase
31
+
32
+ hash[key] = {} if hash[key].blank?
33
+ hash[key][name] = [] if hash[key][name].blank?
34
+ hash[key][name].push(v)
35
+ end
36
+
37
+ hash
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,21 @@
1
+ - flash = self.flash
2
+ - format = (format || Notifications.config.default_type).to_s
3
+ - flash_types = %w[ notice alert error errors ]
4
+
5
+ - if format == 'html'
6
+ - flash_types.each do |level|
7
+ - msg = flash.try(:[], level) || flash.try(:[], level.to_sym)
8
+ - if msg.present?
9
+ .alert.flash_msgs class=flash_class(level)
10
+ = content_tag :p, msg
11
+ - else
12
+ - if flash.present?
13
+ = javascript_tag defer: :defer, type: "text/javascript" do
14
+ | window.notifications = window.notifications || {};
15
+ | window.notifications['flash'] = window.notifications['flash'] || {};
16
+
17
+ - flash_types.each do |level|
18
+ - msg = flash.try(:[], level) || flash.try(:[], level.to_sym)
19
+ - if msg.present?
20
+ = javascript_tag defer: :defer, type: "text/javascript" do
21
+ | window.notifications['flash']['#{ level }'] = '#{ j msg }';
@@ -0,0 +1,28 @@
1
+ -# Error mesages
2
+ -# Post.model_name.human => activerecord.models.post
3
+ -# Post.human_attribute_name('title') => activerecord.attributes.post.title
4
+ - format = (format || Notifications.config.default_type).to_s
5
+
6
+ - if object.try(:errors).try(:any?)
7
+ - _errors = object.respond_to?(:localized_errors) ? object.localized_errors : object.errors
8
+ - if format == 'html'
9
+ .error_explanation.alert.alert-danger
10
+ h4= t "notifications.form_errors"
11
+ ul
12
+ - _errors.map do |name, msgs|
13
+ - if msgs.is_a? Array
14
+ - msgs.each do |msg|
15
+ li
16
+ strong= name
17
+ '
18
+ = msg
19
+ - else
20
+ li
21
+ strong= name
22
+ '
23
+ = msgs
24
+
25
+ - else
26
+ = javascript_tag defer: :defer, type: "text/javascript" do
27
+ | window.notifications = window.notifications || {};
28
+ | window.notifications['errors'] = JSON.parse('#{ raw j _errors.to_json }');
@@ -0,0 +1,3 @@
1
+ en:
2
+ notifications:
3
+ form_errors: "Some errors here:"
@@ -0,0 +1,3 @@
1
+ ru:
2
+ notifications:
3
+ form_errors: "При сохранении возникли ошибки:"
@@ -0,0 +1,12 @@
1
+ require 'slim'
2
+ require_relative 'notifications_config'
3
+ _root_ = File.expand_path('../../', __FILE__)
4
+
5
+ module Notifications
6
+ class Engine < Rails::Engine; end
7
+ end
8
+
9
+ # TODO: Rework it with `autoload`
10
+ %w[ localized_errors ].each do |concern|
11
+ require "#{ _root_ }/app/models/concerns/#{ concern }.rb"
12
+ end
@@ -0,0 +1,20 @@
1
+ module Notifications
2
+ def self.configure(&block)
3
+ yield @config ||= Notifications::Configuration.new
4
+ end
5
+
6
+ def self.config
7
+ @config
8
+ end
9
+
10
+ # Configuration class
11
+ class Configuration
12
+ include ActiveSupport::Configurable
13
+
14
+ config_accessor :default_type
15
+ end
16
+
17
+ configure do |config|
18
+ config.default_type = :html # :json
19
+ end
20
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ module Notifications
6
+ VERSION = "0.6.0"
7
+ end
8
+
9
+ Gem::Specification.new do |spec|
10
+ spec.name = "the_notifications"
11
+ spec.version = Notifications::VERSION
12
+ spec.authors = ["Ilya N. Zykin"]
13
+ spec.email = ["zykin-ilya@ya.ru"]
14
+ spec.description = %q{Notifications and Alerts for a Rails app}
15
+ spec.summary = %q{This gem helps to render flash messages in a Rails app}
16
+ spec.homepage = "https://github.com/TheOpenCMS"
17
+ spec.license = "MIT"
18
+
19
+ spec.files = `git ls-files`.split($/)
20
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
21
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ spec.add_dependency "slim"
27
+ end
metadata ADDED
@@ -0,0 +1,103 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: the_notifications
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.6.0
5
+ platform: ruby
6
+ authors:
7
+ - Ilya N. Zykin
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-03-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: slim
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Notifications and Alerts for a Rails app
56
+ email:
57
+ - zykin-ilya@ya.ru
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".keep"
64
+ - Gemfile
65
+ - LICENSE.txt
66
+ - README.md
67
+ - Rakefile
68
+ - app/assets/javascripts/notifications/base.js.coffee
69
+ - app/assets/stylesheets/notifications/notifications.sass
70
+ - app/helpers/notifications_helper.rb
71
+ - app/models/concerns/localized_errors.rb
72
+ - app/views/notifications/_flash.html.slim
73
+ - app/views/notifications/_form.html.slim
74
+ - config/locales/en.yml
75
+ - config/locales/ru.yml
76
+ - lib/notifications.rb
77
+ - lib/notifications_config.rb
78
+ - notifications.gemspec
79
+ homepage: https://github.com/TheOpenCMS
80
+ licenses:
81
+ - MIT
82
+ metadata: {}
83
+ post_install_message:
84
+ rdoc_options: []
85
+ require_paths:
86
+ - lib
87
+ required_ruby_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ required_rubygems_version: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ requirements: []
98
+ rubyforge_project:
99
+ rubygems_version: 2.5.2
100
+ signing_key:
101
+ specification_version: 4
102
+ summary: This gem helps to render flash messages in a Rails app
103
+ test_files: []