globalize-validations-for-nested-form 0.0.4

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: 6e5d63f4587c869b8562baf7fd17feebd9b574f0
4
+ data.tar.gz: 41d72aac31c574f225ad5cc4238736f13e3289a8
5
+ SHA512:
6
+ metadata.gz: 23c91ae70275c9120031dc321cf77723a12ed21c9ef436d1e22fb34bc2e4cdbba836c94dbf31dc6dae40996106d8740ceb773e41d9b3d97a337e9dce49e0e924
7
+ data.tar.gz: 02bd3a51e40c1a16957785b15a3d79ff012768e3dbff1a54843ff6986422c14d1edb6e1bceb4489e0b9b4d6b138fabfada52b44ccdb5b4f7b0460f5678efb4b1
@@ -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,58 @@
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 globalize_validations_locales.respond_to?(:call)
16
+ globalize_validations_locales.call(self)
17
+ else
18
+ globalize_validations_locales
19
+ end
20
+ locales ||= []
21
+
22
+ globalized_errors_for_locales(translated_attribute_names, locales)
23
+ end
24
+
25
+ # Return all translated attributes with errors for the given locales,
26
+ # including their error messages
27
+ def globalized_errors_for_locales(attribute_names, locales)
28
+ locales.map!(&:to_s)
29
+ additional_locales = locales - [I18n.locale.to_s]
30
+
31
+ if locales.include? I18n.locale.to_s
32
+ # Track errors for current locale
33
+ globalized_errors_for_locale(attribute_names, I18n.locale)
34
+ end
35
+
36
+ # Validates the given object against each locale except the current one
37
+ # and track their errors
38
+ additional_locales.each do |locale|
39
+ Globalize.with_locale(locale) do
40
+ globalized_errors_for_locale(attribute_names, locale) if invalid?
41
+ end
42
+ end
43
+ end
44
+
45
+ # Return all translated attributes with errors for the given locale,
46
+ # including their error messages
47
+ def globalized_errors_for_locale(translated_attribute_names, locale)
48
+ errors.add(:base, :invalid)
49
+
50
+ translated_attribute_names.each do |attribute|
51
+ errors.messages.delete(attribute.to_sym).to_a.each do |error|
52
+ translation.errors.add(attribute, error)
53
+ end
54
+ end
55
+ end
56
+ end
57
+ end
58
+ 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.4"
4
+ end
5
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: globalize-validations-for-nested-form
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.4
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: 2017-01-30 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.5.1
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: Validates translated attributes accessed with globalized accessors
139
+ test_files: []