globalize-validations 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: fb9bb8432b00243438c0c1934768912b738d4fee
4
+ data.tar.gz: a585a262096b2042bad4f8c4b6f05ff8c4fef25b
5
+ SHA512:
6
+ metadata.gz: a59b6a5a2fd5ed0f5ecfc1d169b83c664ac487486745bada3c594c36a84a8c1b624a73947b0ae093e7d172c92317234f684ea2ba4f873d1a09555de68dd45ca4
7
+ data.tar.gz: 98899d92fbc90baee4a88556e4623bd9b46756f54fb84e8ad59dd79c99ba5946300ee1471ef72afa1667076c58bf07398a16508cf141878ae8b3533b3fa1eae4
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2014 BookingSync
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.
@@ -0,0 +1,76 @@
1
+ [![Code Climate](https://codeclimate.com/github/BookingSync/globalize-validations.png)](https://codeclimate.com/github/BookingSync/globalize-validations)
2
+ [![Build Status](https://travis-ci.org/BookingSync/globalize-validations.png?branch=master)](https://travis-ci.org/BookingSync/globalize-validations)
3
+
4
+ # Globalize-Validations
5
+
6
+ Globalize Validations validates your translated attributes for each given locales.
7
+ The errors are added to the globalize attributes accessor names (example `title_en`).
8
+
9
+ `globalize-validations` is compatible with Rails 3.x, Rails 4.0.x and Rails 4.1.x.
10
+
11
+ ## Requirements
12
+
13
+ Globalize `>= 3.0.0`, Globalize-Accessors `>= 0.1.3` and Ruby `>= 1.9.3`.
14
+
15
+ ## Documentation
16
+
17
+ [API documentation is available at rdoc.info](http://rdoc.info/github/BookingSync/globalize-validations/master/frames).
18
+
19
+ ## Installation
20
+
21
+ Add this line to your application's Gemfile:
22
+
23
+ ```ruby
24
+ gem 'globalize-validations'
25
+ ```
26
+
27
+ And then execute:
28
+
29
+ ```
30
+ $ bundle
31
+ ```
32
+
33
+ ## Usage
34
+
35
+ Default
36
+
37
+ ```ruby
38
+ class Page < ActiveRecord::Base
39
+ translates :title, :body
40
+ globalize_accessors locales: [:en, :es, :fr, :pl], attributes: [:title]
41
+ globalize_validations # Will use Model.globalize_locales by default
42
+
43
+ # Add all your validations before
44
+ validate :validates_globalized_attributes
45
+ end
46
+ ```
47
+
48
+ With custom locales
49
+
50
+ ```ruby
51
+ class Page < ActiveRecord::Base
52
+ translates :title, :body
53
+ globalize_accessors locales: [:en, :es, :fr, :pl], attributes: [:title]
54
+ globalize_validations locales: [:en, :es] # Validates only `:en` and `:es` locales
55
+
56
+ # Add all your validations before
57
+ validate :validates_globalized_attributes
58
+ end
59
+ ```
60
+
61
+ With custom locales as Proc
62
+
63
+ ```ruby
64
+ class Page < ActiveRecord::Base
65
+ translates :title, :body
66
+ globalize_accessors locales: [:en, :es, :fr, :pl], attributes: [:title]
67
+ globalize_validations locales: Proc.new { |page| page.available_locales }
68
+
69
+ # Add all your validations before
70
+ validate :validates_globalized_attributes
71
+ end
72
+ ```
73
+
74
+ ## Licence
75
+
76
+ Copyright (c) 2014 BookingSync released under the MIT license
@@ -0,0 +1,22 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ Bundler::GemHelper.install_tasks
9
+ require 'rdoc/task'
10
+ require 'rake/testtask'
11
+ require 'rake'
12
+
13
+ desc 'Default: run unit tests.'
14
+ task :default => :test
15
+
16
+ desc 'Test globalize-validations gem.'
17
+ Rake::TestTask.new(:test) do |t|
18
+ t.libs << 'lib'
19
+ t.libs << 'test'
20
+ t.pattern = 'test/**/*_test.rb'
21
+ t.verbose = true
22
+ end
@@ -0,0 +1,11 @@
1
+ require "globalize"
2
+ require "globalize-accessors"
3
+ require "globalize-validations/model"
4
+ require "globalize-validations/concern"
5
+
6
+ module Globalize
7
+ module Validations
8
+ end
9
+ end
10
+
11
+ ActiveRecord::Base.extend Globalize::Validations::Model
@@ -0,0 +1,68 @@
1
+ module Globalize
2
+ module Validations
3
+ module Concern
4
+ extend ActiveSupport::Concern
5
+
6
+ private
7
+
8
+ # This validation will perform a validation round against each globalized locales
9
+ # and add errors for globalized attributes names
10
+ def validates_globalized_attributes
11
+ # Only validates globalized attributes from the admin locale
12
+ return unless Globalize.locale == I18n.locale
13
+
14
+ # Define which locales to validate against
15
+ locales = if self.globalize_validations_locales.respond_to?(:call)
16
+ self.globalize_validations_locales.call(self)
17
+ else
18
+ self.globalize_validations_locales
19
+ end
20
+
21
+ globalized_errors = globalized_errors_for_locales(translated_attribute_names, locales)
22
+
23
+ # Add translated attributes errors back to the object
24
+ globalized_errors.each do |attribute, messages|
25
+ messages.each do |message|
26
+ errors.add(attribute, message)
27
+ end
28
+ end
29
+ end
30
+
31
+ # Return all translated attributes with errors for the given locales,
32
+ # including their error messages
33
+ def globalized_errors_for_locales(attribute_names, locales)
34
+ locales.map!(&:to_s)
35
+ additional_locales = locales - [I18n.locale.to_s]
36
+
37
+ {}.tap do |globalized_errors|
38
+ if locales.include? I18n.locale.to_s
39
+ # Track errors for current locale
40
+ globalized_errors.merge! globalized_errors_for_locale(attribute_names, I18n.locale)
41
+ end
42
+
43
+ # Validates the given object against each locale except the current one
44
+ # and track their errors
45
+ additional_locales.each do |locale|
46
+ Globalize.with_locale(locale) do
47
+ if invalid?
48
+ globalized_errors.merge! globalized_errors_for_locale(attribute_names, locale)
49
+ end
50
+ end
51
+ end
52
+ end
53
+ end
54
+
55
+ # Return all translated attributes with errors for the given locale,
56
+ # including their error messages
57
+ def globalized_errors_for_locale(translated_attribute_names, locale)
58
+ {}.tap do |globalized_errors|
59
+ translated_attribute_names.each do |attribute|
60
+ if (error = errors.messages.delete(attribute.to_sym)).present?
61
+ globalized_errors["#{attribute}_#{locale}".to_sym] = error
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,20 @@
1
+ module Globalize
2
+ module Validations
3
+ module Model
4
+ # Enables validations for all available locales
5
+ #
6
+ # # @param options [Hash] Configuration options for globalize-validations.
7
+ # They are inherited by subclasses, but can be overwritten in the subclass.
8
+ # @option options [Symbol] locales: locales against which the object will be validated,
9
+ # default is the Model's globalize_locales.
10
+ def globalize_validations(options = {})
11
+ options.reverse_merge!(locales: self.globalize_locales)
12
+ class_attribute :globalize_validations_locales
13
+
14
+ self.globalize_validations_locales = options[:locales]
15
+
16
+ include Globalize::Validations::Concern
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,5 @@
1
+ module Globalize
2
+ module Validations
3
+ VERSION = '0.0.1'
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: globalize-validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Sebastien Grosjean
8
+ - Braulio Martinez LM
9
+ - Adrian Mugnolo
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2014-07-27 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: globalize
17
+ requirement: !ruby/object:Gem::Requirement
18
+ requirements:
19
+ - - "~>"
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ requirements:
26
+ - - "~>"
27
+ - !ruby/object:Gem::Version
28
+ version: '3'
29
+ - !ruby/object:Gem::Dependency
30
+ name: globalize-accessors
31
+ requirement: !ruby/object:Gem::Requirement
32
+ requirements:
33
+ - - "~>"
34
+ - !ruby/object:Gem::Version
35
+ version: '0.1'
36
+ type: :runtime
37
+ prerelease: false
38
+ version_requirements: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - "~>"
41
+ - !ruby/object:Gem::Version
42
+ version: '0.1'
43
+ - !ruby/object:Gem::Dependency
44
+ name: bundler
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '1.3'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
55
+ - !ruby/object:Gem::Version
56
+ version: '1.3'
57
+ - !ruby/object:Gem::Dependency
58
+ name: rake
59
+ requirement: !ruby/object:Gem::Requirement
60
+ requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '0.9'
64
+ type: :development
65
+ prerelease: false
66
+ version_requirements: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - "~>"
69
+ - !ruby/object:Gem::Version
70
+ version: '0.9'
71
+ - !ruby/object:Gem::Dependency
72
+ name: sqlite3
73
+ requirement: !ruby/object:Gem::Requirement
74
+ requirements:
75
+ - - "~>"
76
+ - !ruby/object:Gem::Version
77
+ version: '1.3'
78
+ type: :development
79
+ prerelease: false
80
+ version_requirements: !ruby/object:Gem::Requirement
81
+ requirements:
82
+ - - "~>"
83
+ - !ruby/object:Gem::Version
84
+ version: '1.3'
85
+ - !ruby/object:Gem::Dependency
86
+ name: minitest
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - "~>"
90
+ - !ruby/object:Gem::Version
91
+ version: '4.2'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - "~>"
97
+ - !ruby/object:Gem::Version
98
+ version: '4.2'
99
+ description: Validates translated attributes accessed with globalized accessors
100
+ email:
101
+ - dev@bookingsync.com
102
+ - brauliomartinezlm@gmail.com
103
+ - adrian@mugnolo.com
104
+ executables: []
105
+ extensions: []
106
+ extra_rdoc_files: []
107
+ files:
108
+ - MIT-LICENSE
109
+ - README.md
110
+ - Rakefile
111
+ - lib/globalize-validations.rb
112
+ - lib/globalize-validations/concern.rb
113
+ - lib/globalize-validations/model.rb
114
+ - lib/globalize-validations/version.rb
115
+ homepage: https://github.com/BookingSync/globalize-validations
116
+ licenses:
117
+ - MIT
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.2.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Validates translated attributes accessed with globalized accessors
139
+ test_files: []
140
+ has_rdoc: