validation-messages-for-hanami 0.1.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: 778aab5e99b47bfd45eec10616c2e21b9ef34df5
4
+ data.tar.gz: c2d495c08841a73a2ad34450347dad68ad20c96f
5
+ SHA512:
6
+ metadata.gz: fcc8c220e7f4f669a57c89cdb84d14162b5acd0a2a1c523dc02c9b95e43c2ecc00aa1a48090decc4bace33b66bbe0a57597ab0d7c0035c8fcaa5b30e9e254576
7
+ data.tar.gz: ca02a4a9ea105c691acaf1e27ec6c1c943d9d46ec56891a1ed8b66ef803e4e9e47320b5cfc4111d163798adeb93f95a44799311e26af4db612e422b89ae9d846
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec-helper
2
+ --color
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+
3
+ rvm:
4
+ - "2.2"
5
+
6
+ before_install: gem install bundler
7
+
8
+ script: bundle exec rspec
9
+
10
+ git:
11
+ submodules: false
data/Gemfile ADDED
@@ -0,0 +1,8 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'rspec', require: false
6
+ gem 'simplecov', require: false
7
+ gem 'coveralls', require: false
8
+ gem 'hanami-validations', require: false
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 mrubi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,267 @@
1
+ # Validation messages for Hanami
2
+
3
+ A helper to customize the validation messages in [Hanami](http://hanamirb.org/) application views.
4
+
5
+ Sometimes you need to customize a validation message based not only on the validation type and value, but also on the rendering context. That is, you may want the same validation on the same attribute with the same value to show different messages depending on the view you are rendering it. This helper allows you to do that nicely.
6
+
7
+ Actually this helper does not depend on [Hanami::View](https://github.com/hanami/view) at all (only on [Hanami::Validations](https://github.com/hanami/validations), and even that could be easily tweaked using a different [DisplayableError](lib/validation-messages/displayable-error.rb) adaptor), so it also could be used with other frameworks different than [Hanami](http://hanamirb.org/), but since [Hanami](http://hanamirb.org/) is the framework that makes me feel good, the one that I love and choose, I don't give a shit about that.
8
+
9
+ ## Status
10
+
11
+ [![Gem Version](https://badge.fury.io/rb/validation-messages-for-hanami.svg)](https://badge.fury.io/rb/validation-messages-for-hanami)
12
+ [![Build Status](https://travis-ci.org/cabeza-de-termo/validation-messages-for-hanami.svg?branch=master)](https://travis-ci.org/cabeza-de-termo/validation-messages-for-hanami)
13
+ [![Coverage Status](https://coveralls.io/repos/github/cabeza-de-termo/validation-messages-for-hanami/badge.svg?branch=master)](https://coveralls.io/github/cabeza-de-termo/validation-messages-for-hanami?branch=master)
14
+
15
+
16
+ ## Installation
17
+
18
+ Add this line to your application's Gemfile:
19
+
20
+ ```ruby
21
+ gem 'validation-messages-for-hanami', '~> 0.1'
22
+ ```
23
+
24
+ And then execute:
25
+
26
+ $ bundle install
27
+
28
+
29
+ ## Usage
30
+
31
+ ### Configuration
32
+
33
+ If you want to customize the messages only in a particular view, do
34
+
35
+ ```ruby
36
+ require 'validation-messages-for-hanami'
37
+
38
+ module Web::Views::Person
39
+ class Create
40
+ include Web::View
41
+ include CabezaDeTermo::ValidationsMessages::Behaviour
42
+
43
+ validation_messages do
44
+ at :presence, on: 'person.address.street' do |error|
45
+ "Please, fill in some contact address street."
46
+ end
47
+
48
+ at :presence, on: 'person.address.country' do |error|
49
+ "Please, choose a country from the list."
50
+ end
51
+ end
52
+ end
53
+ end
54
+ ```
55
+
56
+ If you want to include the helper in all the views in the application, in your `application.rb` include
57
+
58
+ ```ruby
59
+ require 'validation-messages-for-hanami'
60
+ ```
61
+
62
+ and then
63
+
64
+ ```ruby
65
+ # Configure the code that will yield each time Web::View is included
66
+ # This is useful for sharing common functionality
67
+ #
68
+ # See: http://www.rubydoc.info/gems/hanami-view#Configuration
69
+ view.prepare do
70
+ include Hanami::Helpers
71
+ include Web::Assets::Helpers
72
+
73
+ include CabezaDeTermo::ValidationsMessages::Behaviour
74
+ # If you want to customize the default messages for this application
75
+ # uncomment the next line and set your own validation messages library
76
+ # use_validation_messages_library WebValidationMessages.library
77
+ ```
78
+
79
+ and now you can just use it in your views like
80
+
81
+ ```ruby
82
+ module Web::Views::Person
83
+ class Create
84
+ include Web::View
85
+
86
+ validation_messages do
87
+ at :presence, on: 'person.address.street' do |error|
88
+ "Please, fill in some contact address."
89
+ end
90
+
91
+ at :presence, on: 'person.address.country' do |error|
92
+ "Please, choose a country from the list."
93
+ end
94
+ end
95
+ end
96
+ end
97
+ ```
98
+
99
+ ### Customizing the validation messages
100
+
101
+ To customize a validation message based on the validation type do
102
+
103
+ ```ruby
104
+ module Web::Views::Person
105
+ class Create
106
+ include Web::View
107
+
108
+ validation_messages do
109
+ at :presence do |error|
110
+ "#{error.attribute_name} must be present"
111
+ end
112
+ end
113
+ end
114
+ end
115
+ ```
116
+
117
+ To customize a validation message for a particular attribute do
118
+
119
+ ```ruby
120
+ module Web::Views::Person
121
+ class Create
122
+ include Web::View
123
+
124
+ validation_messages do
125
+ at :presence, on: 'person.address.country' do |error|
126
+ "Please, choose a country from the list."
127
+ end
128
+ end
129
+ end
130
+ end
131
+ ```
132
+
133
+ To customize the displayable name of an attribute in all the validation messages do
134
+
135
+ ```ruby
136
+ module Web::Views::Person
137
+ class Create
138
+ include Web::View
139
+
140
+ validation_messages do
141
+ display :street, as: 'contact street address'
142
+
143
+ at :presence do |error|
144
+ "#{error.attribute_name} must be present"
145
+ end
146
+ end
147
+ end
148
+ end
149
+ ```
150
+
151
+ and now `error.attribute_name` for `:street` attribute instead of displaying `'street'` will display `'contact street address'` in all the validation occurrences.
152
+
153
+ ### Customizing the default validations messages globally
154
+
155
+ If you want to change the default validation messages globally in all your applications do
156
+
157
+ ```ruby
158
+ CabezaDeTermo::ValidationMessages::Library.default.configure do
159
+ message_for :presence do |error|
160
+ "can not be left blank"
161
+ end
162
+
163
+ message_for :acceptance do |error|
164
+ "must be accepted"
165
+ end
166
+
167
+ message_for :confirmation do |error|
168
+ "doesn't match"
169
+ end
170
+
171
+ message_for :inclusion do |error|
172
+ "isn't included"
173
+ end
174
+
175
+ message_for :exclusion do |error|
176
+ "shouldn't belong to #{ Array(error.expected).join(', ') }"
177
+ end
178
+
179
+ message_for :format do |error|
180
+ "doesn't match expected format"
181
+ end
182
+
183
+ message_for :size do |error|
184
+ "doesn't match expected size"
185
+ end
186
+ end
187
+ ```
188
+
189
+ ### Customizing the default validation messages for a single application
190
+
191
+ If you want to customize the validation messages only for an application, in the `application.rb` file do
192
+
193
+ ```ruby
194
+ # Configure the code that will yield each time Web::View is included
195
+ # This is useful for sharing common functionality
196
+ #
197
+ # See: http://www.rubydoc.info/gems/hanami-view#Configuration
198
+ view.prepare do
199
+ include Hanami::Helpers
200
+ include Web::Assets::Helpers
201
+
202
+ include CabezaDeTermo::ValidationsMessages::Behaviour
203
+ use_validation_messages_library WebValidationMessages.library
204
+ ```
205
+
206
+ and define
207
+
208
+ ```ruby
209
+ class WebValidationMessages
210
+ def self.library
211
+ CabezaDeTermo::Validations::Messages::Library.new do
212
+ message_for :presence do |error|
213
+ "can not be left blank"
214
+ end
215
+
216
+ message_for :acceptance do |error|
217
+ "must be accepted"
218
+ end
219
+
220
+ message_for :confirmation do |error|
221
+ "doesn't match"
222
+ end
223
+
224
+ message_for :inclusion do |error|
225
+ "isn't included"
226
+ end
227
+
228
+ message_for :exclusion do |error|
229
+ "shouldn't belong to #{ Array(error.expected).join(', ') }"
230
+ end
231
+
232
+ message_for :format do |error|
233
+ "doesn't match expected format"
234
+ end
235
+
236
+ message_for :size do |error|
237
+ "doesn't match expected size"
238
+ end
239
+ end
240
+ end
241
+ end
242
+ ```
243
+
244
+ ### Customizing the default validation messages for a single view
245
+
246
+ If you want to customize the validation messages only for a view, in the view do
247
+
248
+ ```ruby
249
+ module Web::Views::Person
250
+ class Create
251
+ include Web::View
252
+
253
+ validation_messages do
254
+ use_validation_messages_library SomeValidationMessages.library
255
+ end
256
+ end
257
+ end
258
+ ```
259
+
260
+ ## Contributing
261
+
262
+ Bug reports and pull requests are welcome on GitHub at https://github.com/cabeza-de-termo/validation-messages-for-hanami.
263
+
264
+ ## License
265
+
266
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
267
+
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ require 'cdt/utilities'
2
+ require 'validation-messages/version'
3
+ require 'validation-messages/dictionary'
4
+ require 'validation-messages/displayable-error'
5
+ require 'validation-messages/library-configuration-dsl'
6
+ require 'validation-messages/library'
7
+ require 'validation-messages/behaviour'
8
+ require 'validation-messages/default-validation-messages'
9
+
@@ -0,0 +1,42 @@
1
+ module CabezaDeTermo::ValidationMessages
2
+ module Behaviour
3
+ def self.included(base)
4
+ base.class_eval do
5
+ extend ClassMethods
6
+
7
+ use_validation_messages_library CabezaDeTermo::ValidationMessages::Library.default
8
+ end
9
+ end
10
+
11
+ module ClassMethods
12
+ def use_validation_messages_library(validation_messages_library)
13
+ @validation_messages_library = validation_messages_library
14
+ end
15
+
16
+ def validation_messages_library()
17
+ @validation_messages_library
18
+ end
19
+
20
+ def validation_messages_dictionary()
21
+ @validation_messages_dictionary ||= new_validation_messages_dictionary
22
+ end
23
+
24
+ def validation_messages(&block)
25
+ @validation_messages_dictionary = new_validation_messages_dictionary
26
+ CdT.bind_block_evaluation_to(@validation_messages_dictionary, &block)
27
+ end
28
+
29
+ def new_validation_messages_dictionary()
30
+ Dictionary.new(validation_messages_library)
31
+ end
32
+ end
33
+
34
+ def validation_messages_dictionary(&block)
35
+ self.class.validation_messages_dictionary(&block)
36
+ end
37
+
38
+ def validation_message_for(error)
39
+ validation_messages_dictionary.message_for error
40
+ end
41
+ end
42
+ end
@@ -0,0 +1,29 @@
1
+ CabezaDeTermo::ValidationMessages::Library.default.configure do
2
+ message_for :presence do |error|
3
+ "can not be left blank"
4
+ end
5
+
6
+ message_for :acceptance do |error|
7
+ "must be accepted"
8
+ end
9
+
10
+ message_for :confirmation do |error|
11
+ "doesn't match"
12
+ end
13
+
14
+ message_for :inclusion do |error|
15
+ "isn't included"
16
+ end
17
+
18
+ message_for :exclusion do |error|
19
+ "shouldn't belong to #{ Array(error.expected).join(', ') }"
20
+ end
21
+
22
+ message_for :format do |error|
23
+ "doesn't match expected format"
24
+ end
25
+
26
+ message_for :size do |error|
27
+ "doesn't match expected size"
28
+ end
29
+ end
@@ -0,0 +1,54 @@
1
+ module CabezaDeTermo::ValidationMessages
2
+ class Dictionary
3
+ attr_reader :library
4
+
5
+ def initialize(library)
6
+ @messages = Hash.new { |hash, key| hash[key] = Hash[] }
7
+ @parameters_display_strings = Hash[]
8
+ @library = library
9
+ end
10
+
11
+ def use_validation_messages_library(library)
12
+ @library = library
13
+ end
14
+
15
+ def at(validation_name, on: nil, &block)
16
+ attribute = on.nil? ? CdT.undefined : on.to_sym
17
+ @messages[validation_name][attribute] = block
18
+ end
19
+
20
+ def display(parameter, as:)
21
+ @parameters_display_strings[parameter.to_sym] = as
22
+ end
23
+
24
+ def message_for(error)
25
+ CdT.object message_block_for(error),
26
+ if_not_nil: proc { |message_block| message_block.call(displayable_error_on(error)) },
27
+ if_nil: proc { library.message_for(displayable_error_on(error)) }
28
+ end
29
+
30
+ def display_string_for(error)
31
+ @parameters_display_strings[error.attribute_name.to_sym]
32
+ end
33
+
34
+ def displayable_error_on(error)
35
+ DisplayableError.on error, display_string: display_string_for(error)
36
+ end
37
+
38
+ protected
39
+
40
+ def message_block_for(error)
41
+ message_block_for_validation(error.validation, attribute: error.attribute)
42
+ end
43
+
44
+ def message_block_for_validation(validation, attribute:)
45
+ @messages
46
+ .fetch(validation, Hash[])
47
+ .fetch(attribute.to_sym, default_message_for_validation(validation))
48
+ end
49
+
50
+ def default_message_for_validation(validation)
51
+ @messages.fetch(validation, Hash[])[CdT.undefined]
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,24 @@
1
+ module CabezaDeTermo::ValidationMessages
2
+ class DisplayableError
3
+ def self.on(validation_error, display_string: nil)
4
+ self.new(validation_error, display_string: display_string)
5
+ end
6
+
7
+ def initialize(validation_error, display_string: nil)
8
+ @validation_error = validation_error
9
+ @attribute_display_string = display_string
10
+ end
11
+
12
+ def attribute_name
13
+ no_attribute_display_string? ? @validation_error.attribute_name : @attribute_display_string
14
+ end
15
+
16
+ def method_missing(method_name, *args, &block)
17
+ @validation_error.send(method_name, *args, &block)
18
+ end
19
+
20
+ def no_attribute_display_string?()
21
+ @attribute_display_string.nil?
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,15 @@
1
+ require 'lunfardo'
2
+
3
+ module CabezaDeTermo::ValidationMessages
4
+ class LibraryConfigurationDSL
5
+ include CabezaDeTermo::Lunfardo::Behaviour
6
+
7
+ dsl do
8
+ on :message_for do |validation_message_name, &message_block|
9
+ perform do |validation_message_name, &message_block|
10
+ context.message_at(validation_message_name, &message_block)
11
+ end
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,36 @@
1
+ module CabezaDeTermo::ValidationMessages
2
+ class Library
3
+ def self.default(library = nil)
4
+ @default_library = library unless library.nil?
5
+ @default_library
6
+ end
7
+
8
+ default self.new
9
+
10
+ def initialize(&block)
11
+ configure(&block) unless block.nil?
12
+ end
13
+
14
+ def configure(&block)
15
+ LibraryConfigurationDSL.evaluate_on(self, &block)
16
+ end
17
+
18
+ def message_at(validation_name, &block)
19
+ messages[validation_name] = block
20
+ end
21
+
22
+ def message_block_for(validation_name)
23
+ messages.fetch(validation_name)
24
+ end
25
+
26
+ def message_for(error)
27
+ message_block_for(error.validation).call(error)
28
+ end
29
+
30
+ protected
31
+
32
+ def messages
33
+ @messages ||= Hash[]
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,5 @@
1
+ module CabezaDeTermo
2
+ module ValidationMessages
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'validation-messages/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "validation-messages-for-hanami"
8
+ spec.version = CabezaDeTermo::ValidationMessages::VERSION
9
+ spec.authors = ["Martin Rubi"]
10
+ spec.email = ["martinrubi@gmail.com"]
11
+
12
+ spec.summary = %q{Helper to customize the validation messages in Hanami application views.}
13
+ spec.homepage = "https://github.com/cabeza-de-termo/validation-messages-for-hanami"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "cdt-utilities", "~> 0.4"
22
+ spec.add_dependency "lunfardo", "~> 0.1"
23
+ spec.add_development_dependency "bundler", "~> 1.11"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ end
metadata ADDED
@@ -0,0 +1,116 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: validation-messages-for-hanami
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Martin Rubi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: cdt-utilities
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: lunfardo
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.11'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.11'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '10.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '10.0'
69
+ description:
70
+ email:
71
+ - martinrubi@gmail.com
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - ".gitignore"
77
+ - ".rspec"
78
+ - ".travis.yml"
79
+ - Gemfile
80
+ - LICENSE.txt
81
+ - README.md
82
+ - Rakefile
83
+ - lib/validation-messages-for-hanami.rb
84
+ - lib/validation-messages/behaviour.rb
85
+ - lib/validation-messages/default-validation-messages.rb
86
+ - lib/validation-messages/dictionary.rb
87
+ - lib/validation-messages/displayable-error.rb
88
+ - lib/validation-messages/library-configuration-dsl.rb
89
+ - lib/validation-messages/library.rb
90
+ - lib/validation-messages/version.rb
91
+ - validation-messages-for-hanami.gemspec
92
+ homepage: https://github.com/cabeza-de-termo/validation-messages-for-hanami
93
+ licenses:
94
+ - MIT
95
+ metadata: {}
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ requirements:
102
+ - - ">="
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ requirements: []
111
+ rubyforge_project:
112
+ rubygems_version: 2.6.3
113
+ signing_key:
114
+ specification_version: 4
115
+ summary: Helper to customize the validation messages in Hanami application views.
116
+ test_files: []