multilang-hstore 1.0.1 → 1.0.2
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.
- checksums.yaml +4 -4
- data/.travis.yml +4 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +36 -33
- data/README.md +6 -2
- data/lib/multilang-hstore.rb +1 -0
- data/lib/multilang-hstore/active_record_extensions.rb +11 -1
- data/lib/multilang-hstore/validators/translation_count_validator.rb +14 -0
- data/lib/multilang-hstore/version.rb +1 -1
- data/spec/multilang_spec.rb +43 -4
- data/spec/spec_helper.rb +18 -0
- metadata +5 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 30cb1cfbac7a16e14adcfb6d3acefb2ce39b0fa6
|
4
|
+
data.tar.gz: 1ec987774dfd5c0c2f3fd60bde0c78dcc518a28b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 482200164ad15c705742946ae4f3e6b906e03c240d566f4309de007a7495d0812f7ab4278d82fca0a2cfc56fb510717b8db38fcade54a1b348abc25a30c82082
|
7
|
+
data.tar.gz: aec1be154cc035e4aad3074e9c0b090c03205544d7fb98c207c99dcfc668cc2856c083fdae13e92e7261dbe183a507ee2c5c977c7bcbdb7275c570941deaa81a
|
data/.travis.yml
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,40 +1,42 @@
|
|
1
1
|
GEM
|
2
2
|
remote: http://rubygems.org/
|
3
3
|
specs:
|
4
|
-
activemodel (4.
|
5
|
-
activesupport (= 4.
|
6
|
-
builder (~> 3.1
|
7
|
-
activerecord (4.
|
8
|
-
activemodel (= 4.
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
4
|
+
activemodel (4.2.0)
|
5
|
+
activesupport (= 4.2.0)
|
6
|
+
builder (~> 3.1)
|
7
|
+
activerecord (4.2.0)
|
8
|
+
activemodel (= 4.2.0)
|
9
|
+
activesupport (= 4.2.0)
|
10
|
+
arel (~> 6.0)
|
11
|
+
activesupport (4.2.0)
|
12
|
+
i18n (~> 0.7)
|
13
|
+
json (~> 1.7, >= 1.7.7)
|
14
|
+
minitest (~> 5.1)
|
15
|
+
thread_safe (~> 0.3, >= 0.3.4)
|
16
|
+
tzinfo (~> 1.1)
|
17
|
+
arel (6.0.0)
|
18
|
+
builder (3.2.2)
|
19
|
+
diff-lcs (1.2.5)
|
20
|
+
i18n (0.7.0)
|
21
|
+
json (1.8.2)
|
22
|
+
minitest (5.5.1)
|
23
|
+
pg (0.18.1)
|
24
|
+
rake (10.4.2)
|
25
|
+
rspec (3.1.0)
|
26
|
+
rspec-core (~> 3.1.0)
|
27
|
+
rspec-expectations (~> 3.1.0)
|
28
|
+
rspec-mocks (~> 3.1.0)
|
29
|
+
rspec-core (3.1.7)
|
30
|
+
rspec-support (~> 3.1.0)
|
31
|
+
rspec-expectations (3.1.2)
|
32
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
33
|
+
rspec-support (~> 3.1.0)
|
34
|
+
rspec-mocks (3.1.3)
|
35
|
+
rspec-support (~> 3.1.0)
|
36
|
+
rspec-support (3.1.2)
|
37
|
+
thread_safe (0.3.4)
|
38
|
+
tzinfo (1.2.2)
|
17
39
|
thread_safe (~> 0.1)
|
18
|
-
tzinfo (~> 0.3.37)
|
19
|
-
arel (4.0.0)
|
20
|
-
atomic (1.1.9)
|
21
|
-
builder (3.1.4)
|
22
|
-
diff-lcs (1.1.3)
|
23
|
-
i18n (0.6.4)
|
24
|
-
minitest (4.7.5)
|
25
|
-
multi_json (1.7.7)
|
26
|
-
pg (0.14.1)
|
27
|
-
rspec (2.12.0)
|
28
|
-
rspec-core (~> 2.12.0)
|
29
|
-
rspec-expectations (~> 2.12.0)
|
30
|
-
rspec-mocks (~> 2.12.0)
|
31
|
-
rspec-core (2.12.2)
|
32
|
-
rspec-expectations (2.12.1)
|
33
|
-
diff-lcs (~> 1.1.3)
|
34
|
-
rspec-mocks (2.12.2)
|
35
|
-
thread_safe (0.1.0)
|
36
|
-
atomic
|
37
|
-
tzinfo (0.3.37)
|
38
40
|
|
39
41
|
PLATFORMS
|
40
42
|
ruby
|
@@ -42,4 +44,5 @@ PLATFORMS
|
|
42
44
|
DEPENDENCIES
|
43
45
|
activerecord (>= 4.0.0)
|
44
46
|
pg (>= 0.0.1)
|
47
|
+
rake
|
45
48
|
rspec
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Multilang-hstore
|
2
2
|
|
3
|
-
|
3
|
+
[](https://travis-ci.org/bithavoc/multilang-hstore)
|
4
|
+
|
5
|
+
> Multilang is a small translation library for translating database values for Active Support/Rails 4 using the [Hstore datatype](http://www.postgresql.org/docs/9.0/static/hstore.html).
|
4
6
|
|
5
7
|
This project is a fork of [artworklv/multilang](https://github.com/artworklv/multilang) with some remarkable differences:
|
6
8
|
|
@@ -115,7 +117,9 @@ The value from "any" method returns value for I18n.current_locale or, if value i
|
|
115
117
|
Multilang has some validation features:
|
116
118
|
|
117
119
|
multilang :title, :length => 100 #define maximal length validator
|
118
|
-
multilang :title, :required => true #define requirement validator
|
120
|
+
multilang :title, :required => true #define requirement validator for all available_locales
|
121
|
+
multilang :title, :required => 1 #define requirement validator for 1 locale
|
122
|
+
multilang :title, :required => [:en, :es] #define requirement validator for specific locales
|
119
123
|
multilang :title, :format => /regexp/ #define validates_format_of validator
|
120
124
|
|
121
125
|
## Tests
|
data/lib/multilang-hstore.rb
CHANGED
@@ -2,6 +2,7 @@ require 'multilang-hstore/version'
|
|
2
2
|
require 'multilang-hstore/exceptions'
|
3
3
|
require 'multilang-hstore/translation_proxy'
|
4
4
|
require 'multilang-hstore/translation_keeper'
|
5
|
+
require 'multilang-hstore/validators/translation_count_validator'
|
5
6
|
require 'multilang-hstore/active_record_extensions'
|
6
7
|
|
7
8
|
module Multilang
|
@@ -43,6 +43,12 @@ module Multilang
|
|
43
43
|
serialize "#{attribute}", ActiveRecord::Coders::Hstore
|
44
44
|
end if defined?(ActiveRecord::Coders::Hstore)
|
45
45
|
|
46
|
+
if options[:required].is_a? Numeric
|
47
|
+
module_eval do
|
48
|
+
validates "#{attribute}_before_type_cast", :'multilang/validators/translation_count' => {min: options[:required]}
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
46
52
|
I18n.available_locales.each do |locale|
|
47
53
|
|
48
54
|
define_method "#{attribute}_#{locale}" do
|
@@ -59,7 +65,7 @@ module Multilang
|
|
59
65
|
end
|
60
66
|
|
61
67
|
# attribute presence validator
|
62
|
-
if options[:required]
|
68
|
+
if options[:required] == true or locale_required?(options[:required], locale)
|
63
69
|
module_eval do
|
64
70
|
validates_presence_of "#{attribute}_#{locale}"
|
65
71
|
end
|
@@ -96,6 +102,10 @@ module Multilang
|
|
96
102
|
|
97
103
|
end
|
98
104
|
|
105
|
+
def locale_required? options_required, locale
|
106
|
+
options_required.is_a? Array and options_required.map(&:to_s).include?(locale.to_s)
|
107
|
+
end
|
108
|
+
|
99
109
|
end
|
100
110
|
|
101
111
|
end #module ActiveRecordExtensions
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module Multilang
|
2
|
+
module Validators
|
3
|
+
class TranslationCountValidator < ActiveModel::EachValidator
|
4
|
+
|
5
|
+
def validate_each(record, attribute, value)
|
6
|
+
count = record.send("#{attribute}").reject{|l,v| v.blank?}.size
|
7
|
+
if count < options[:min]
|
8
|
+
record.errors[:attribute] << (options[:message] || "has insufficient translations defined")
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
data/spec/multilang_spec.rb
CHANGED
@@ -123,7 +123,7 @@ describe Multilang do
|
|
123
123
|
|
124
124
|
rp.valid?
|
125
125
|
|
126
|
-
rp.
|
126
|
+
expect(rp.errors.size).to be >= 4
|
127
127
|
end
|
128
128
|
|
129
129
|
it "should mass assign attributes in RegularPost" do
|
@@ -146,7 +146,7 @@ describe Multilang do
|
|
146
146
|
multilang :title, :accessible => true
|
147
147
|
multilang :body, :accessible => true
|
148
148
|
end
|
149
|
-
}.to raise_error
|
149
|
+
}.to raise_error #Multilang::Exceptions::DeprecationError
|
150
150
|
end
|
151
151
|
|
152
152
|
it "should not raise an exception if the deprecated option :accessible is passed as false" do
|
@@ -156,7 +156,7 @@ describe Multilang do
|
|
156
156
|
multilang :title, :accessible => false
|
157
157
|
multilang :body, :accessible => false
|
158
158
|
end
|
159
|
-
}.to_not raise_error
|
159
|
+
}.to_not raise_error #Multilang::Exceptions::DeprecationError
|
160
160
|
end
|
161
161
|
|
162
162
|
it "should save/load attributes in RegularPost" do
|
@@ -242,7 +242,7 @@ describe Multilang do
|
|
242
242
|
I18n.locale = :ru
|
243
243
|
# test
|
244
244
|
rp.title_before_type_cast.actual_locale.should be :ru
|
245
|
-
rp.title_before_type_cast.locales.
|
245
|
+
expect(rp.title_before_type_cast.locales.size).to be >= 2
|
246
246
|
rp.title_before_type_cast.locales.should match_array [:es, :en]
|
247
247
|
rp.title_before_type_cast.should be_kind_of Hash
|
248
248
|
I18n.locale = :es
|
@@ -280,4 +280,43 @@ describe Multilang do
|
|
280
280
|
query_post.title.should == "English USA"
|
281
281
|
end
|
282
282
|
|
283
|
+
it "should be valid when required specified translations are present" do
|
284
|
+
post = TacoPost.new
|
285
|
+
post.title = {lv: "Lv", ru: "Ru"}
|
286
|
+
post.valid?
|
287
|
+
post.should be_valid
|
288
|
+
|
289
|
+
I18n.locale = :nl
|
290
|
+
post.title = "Nl"
|
291
|
+
post.valid?
|
292
|
+
post.should be_valid
|
293
|
+
end
|
294
|
+
|
295
|
+
it "should be invalid when required specified translations are not present" do
|
296
|
+
post = TacoPost.new
|
297
|
+
I18n.locale = :es
|
298
|
+
post.title = {lv: "Latvian"}
|
299
|
+
post.valid?
|
300
|
+
expect(post.errors.size).to be >= 1
|
301
|
+
end
|
302
|
+
|
303
|
+
it "should be valid when require number validation is met" do
|
304
|
+
post = SloppyPost.new
|
305
|
+
I18n.locale = :es
|
306
|
+
post.title = {en: "English"}
|
307
|
+
post.valid?
|
308
|
+
post.should be_valid
|
309
|
+
|
310
|
+
post.title = "In Spanish"
|
311
|
+
post.valid?
|
312
|
+
post.should be_valid
|
313
|
+
end
|
314
|
+
|
315
|
+
it "should be invalid when require number validation is not met" do
|
316
|
+
post = SloppyPost.new
|
317
|
+
I18n.locale = :es
|
318
|
+
post.valid?
|
319
|
+
expect(post.errors.size).to be >= 1
|
320
|
+
end
|
321
|
+
|
283
322
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -8,8 +8,16 @@ require 'multilang-hstore'
|
|
8
8
|
require 'logger'
|
9
9
|
|
10
10
|
ActiveRecord::Base.logger = Logger.new(nil)
|
11
|
+
ActiveRecord::Base.establish_connection(:adapter => "postgresql", :host=>'127.0.0.1', :user=>'postgres')
|
12
|
+
begin
|
13
|
+
ActiveRecord::Base.connection.execute('CREATE DATABASE "multilang-hstore-test" WITH OWNER postgres;')
|
14
|
+
rescue ActiveRecord::StatementInvalid
|
15
|
+
puts "Database already exists"
|
16
|
+
end
|
11
17
|
ActiveRecord::Base.establish_connection(:adapter => "postgresql", :database => "multilang-hstore-test", :host=>'127.0.0.1', :user=>'postgres')
|
18
|
+
ActiveRecord::Base.connection.execute('CREATE EXTENSION IF NOT EXISTS hstore;')
|
12
19
|
|
20
|
+
I18n.enforce_available_locales = false
|
13
21
|
I18n.available_locales = [:lv, :ru]
|
14
22
|
I18n.locale = I18n.default_locale = :lv
|
15
23
|
|
@@ -50,3 +58,13 @@ class NamedPost < ActiveRecord::Base
|
|
50
58
|
self.table_name = 'named_posts'
|
51
59
|
multilang :title
|
52
60
|
end
|
61
|
+
|
62
|
+
class TacoPost < ActiveRecord::Base
|
63
|
+
self.table_name = 'named_posts'
|
64
|
+
multilang :title, required: [:lv, :ru]
|
65
|
+
end
|
66
|
+
|
67
|
+
class SloppyPost < ActiveRecord::Base
|
68
|
+
self.table_name = 'named_posts'
|
69
|
+
multilang :title, required: 1
|
70
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: multilang-hstore
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Arthur Meinart
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date:
|
12
|
+
date: 2015-01-14 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: pg
|
@@ -46,6 +46,7 @@ extensions: []
|
|
46
46
|
extra_rdoc_files: []
|
47
47
|
files:
|
48
48
|
- ".gitignore"
|
49
|
+
- ".travis.yml"
|
49
50
|
- Gemfile
|
50
51
|
- Gemfile.lock
|
51
52
|
- LICENSE
|
@@ -56,6 +57,7 @@ files:
|
|
56
57
|
- lib/multilang-hstore/exceptions.rb
|
57
58
|
- lib/multilang-hstore/translation_keeper.rb
|
58
59
|
- lib/multilang-hstore/translation_proxy.rb
|
60
|
+
- lib/multilang-hstore/validators/translation_count_validator.rb
|
59
61
|
- lib/multilang-hstore/version.rb
|
60
62
|
- multilang-hstore.gemspec
|
61
63
|
- spec/multilang_spec.rb
|
@@ -81,7 +83,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
81
83
|
version: '0'
|
82
84
|
requirements: []
|
83
85
|
rubyforge_project:
|
84
|
-
rubygems_version: 2.
|
86
|
+
rubygems_version: 2.0.14
|
85
87
|
signing_key:
|
86
88
|
specification_version: 4
|
87
89
|
summary: Model translations for Rails 3 and Rails 4 backed by PostgreSQL and Hstore
|