armot 0.3.4 → 0.4.0
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.
- data/README.md +18 -0
- data/lib/armot/active_record_extensions.rb +26 -1
- data/lib/armot/version.rb +1 -1
- data/test/armot_test.rb +53 -3
- data/test/test_helper.rb +0 -1
- metadata +3 -3
data/README.md
CHANGED
@@ -207,6 +207,24 @@ defined, using this syntax:
|
|
207
207
|
define_localized_accessors_for :all, :locales => [:klingon, :pt]
|
208
208
|
end
|
209
209
|
|
210
|
+
Presence validators
|
211
|
+
----------
|
212
|
+
|
213
|
+
If you use the standard presence validator from Rails, as in
|
214
|
+
|
215
|
+
validates_presence_of :some_attribute
|
216
|
+
|
217
|
+
Only the value for the current locale will be checked. However, you
|
218
|
+
might want to check the presence of the value for a specific set of
|
219
|
+
locales. If this is your purpose, you can use:
|
220
|
+
|
221
|
+
validates_armotized_presence_of :some_attribute, %w{ en ca es }
|
222
|
+
|
223
|
+
where the first parameter is the attribute you want to check presence
|
224
|
+
of, and the second is the set of locales for which you want to check the
|
225
|
+
presence of the attribute. Note that the locales can either be a string
|
226
|
+
or symbol array (as in the example above), or a single string or symbol
|
227
|
+
in case you only want to do the check for one locale.
|
210
228
|
|
211
229
|
Development with armot
|
212
230
|
----------------------
|
@@ -105,8 +105,12 @@ module Armot
|
|
105
105
|
armot_attributes[I18n.locale]["#{attribute}"].present?
|
106
106
|
end
|
107
107
|
|
108
|
+
define_method :"#{attribute}_raw" do
|
109
|
+
return armot_attributes[I18n.locale]["#{attribute}"]
|
110
|
+
end
|
111
|
+
|
108
112
|
define_method :"#{attribute}" do
|
109
|
-
return
|
113
|
+
return send("#{attribute}_raw") if armot_attributes[I18n.locale]["#{attribute}"]
|
110
114
|
|
111
115
|
if armot_attributes.any? && I18n.respond_to?(:fallbacks)
|
112
116
|
I18n.fallbacks[I18n.locale].each do |fallback|
|
@@ -130,6 +134,10 @@ module Armot
|
|
130
134
|
extend self.const_get("ArmotClassMethods") unless self.singleton_class.included_modules.map(&:to_s).include?("#{self}::ArmotClassMethods")
|
131
135
|
end
|
132
136
|
|
137
|
+
def validates_armotized_presence_of(attr_name, locales)
|
138
|
+
validates_with Armot::ActiveRecordExtensions::Validations::PresenceValidator, :attr => attr_name, :locales => locales
|
139
|
+
end
|
140
|
+
|
133
141
|
private
|
134
142
|
|
135
143
|
# configure model
|
@@ -175,6 +183,23 @@ module Armot
|
|
175
183
|
res
|
176
184
|
end
|
177
185
|
end
|
186
|
+
|
187
|
+
module Validations
|
188
|
+
class PresenceValidator < ActiveModel::Validator
|
189
|
+
def validate(record)
|
190
|
+
attr_name = options[:attr]
|
191
|
+
locales = options[:locales]
|
192
|
+
locales = [locales] if [ String, Symbol ].include?(locales.class)
|
193
|
+
|
194
|
+
valid = locales.map do |locale|
|
195
|
+
I18n.locale = locale.to_sym
|
196
|
+
record.send("#{attr_name}_raw").present?
|
197
|
+
end.inject(:&)
|
198
|
+
|
199
|
+
record.errors.add(attr_name, "has to be present for locales #{locales.to_sentence}") unless valid
|
200
|
+
end
|
201
|
+
end
|
202
|
+
end
|
178
203
|
end
|
179
204
|
end
|
180
205
|
|
data/lib/armot/version.rb
CHANGED
data/test/armot_test.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
1
|
+
# encoding: UTF-8
|
2
2
|
|
3
|
+
require 'test_helper'
|
3
4
|
|
4
5
|
def to_method_name(name)
|
5
6
|
if Gem::Version.new(RUBY_VERSION.dup) >= Gem::Version.new("1.9")
|
@@ -45,14 +46,63 @@ class ArmotTest < ActiveSupport::TestCase
|
|
45
46
|
assert_equal 'English title', post.title
|
46
47
|
end
|
47
48
|
|
48
|
-
test '
|
49
|
-
|
49
|
+
test 'validates_armotized_presence_of should work' do
|
50
|
+
class ValidatedPost < Post
|
51
|
+
validates_armotized_presence_of :title, %w{ en ca es }
|
52
|
+
end
|
53
|
+
|
54
|
+
post = ValidatedPost.new
|
50
55
|
assert_equal false, post.valid?
|
51
56
|
|
57
|
+
I18n.locale = :en
|
52
58
|
post.title = 'English title'
|
59
|
+
assert_equal false, post.valid?
|
60
|
+
|
61
|
+
{ :ca => 'Títol català', :es => 'Título castellano' }.each do |locale, title|
|
62
|
+
I18n.locale = locale
|
63
|
+
post.title = title
|
64
|
+
end
|
65
|
+
|
66
|
+
assert_equal true, post.valid?
|
67
|
+
end
|
68
|
+
|
69
|
+
test 'validates_armotized_presence_of should work with one locale' do
|
70
|
+
class AnotherValidatedPost < Post
|
71
|
+
validates_armotized_presence_of :title, :ca
|
72
|
+
end
|
73
|
+
|
74
|
+
post = AnotherValidatedPost.new
|
75
|
+
I18n.locale = :ca
|
76
|
+
post.title = 'soc una cucota'
|
53
77
|
assert_equal true, post.valid?
|
54
78
|
end
|
55
79
|
|
80
|
+
test 'validates_armotized_presence_of fails when no valid locales provided' do
|
81
|
+
class WrongValidatedPost < Post
|
82
|
+
validates_armotized_presence_of :title, 1234
|
83
|
+
end
|
84
|
+
|
85
|
+
post = WrongValidatedPost.new
|
86
|
+
begin
|
87
|
+
post.valid?
|
88
|
+
fail
|
89
|
+
rescue
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
test 'validates_armotized_presence_of fails when no valid attr provided' do
|
94
|
+
class AnotherWrongValidatedPost < Post
|
95
|
+
validates_armotized_presence_of :invented_title, %w{ en ca es }
|
96
|
+
end
|
97
|
+
|
98
|
+
post = AnotherWrongValidatedPost.new
|
99
|
+
begin
|
100
|
+
post.valid?
|
101
|
+
fail
|
102
|
+
rescue
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
56
106
|
test 'temporary locale switch should not clear changes' do
|
57
107
|
I18n.locale = :de
|
58
108
|
post = Post.first
|
data/test/test_helper.rb
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: armot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.4.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2012-
|
12
|
+
date: 2012-12-23 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: i18n-active_record
|
@@ -134,7 +134,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
134
134
|
version: '0'
|
135
135
|
requirements: []
|
136
136
|
rubyforge_project: armot
|
137
|
-
rubygems_version: 1.8.
|
137
|
+
rubygems_version: 1.8.24
|
138
138
|
signing_key:
|
139
139
|
specification_version: 3
|
140
140
|
summary: translation support for your models with an I18n active-record backend
|