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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 23ee19926b80f1ecbc9da7b7bfc9b12406f49391
4
- data.tar.gz: ce9bd778a659b5450cd5132c55d487b705cee9fa
3
+ metadata.gz: 30cb1cfbac7a16e14adcfb6d3acefb2ce39b0fa6
4
+ data.tar.gz: 1ec987774dfd5c0c2f3fd60bde0c78dcc518a28b
5
5
  SHA512:
6
- metadata.gz: e7b3f71b1adc7beee8a9d48583144c43cb90fa53aaf0e5815e870a5409f99d3db69ae6a0cae2c99516889167b9b21f29c588ba39807e59f1887f0cf96eca64c0
7
- data.tar.gz: 94f088a74d57d3df9706693f16eac9082dee46c59ae8de0b90179e97c673d11a9f757e38f1e6300e3f68026270d9f50fd5f2ee3f9a15d49f74ad21d0c9bc1bc1
6
+ metadata.gz: 482200164ad15c705742946ae4f3e6b906e03c240d566f4309de007a7495d0812f7ab4278d82fca0a2cfc56fb510717b8db38fcade54a1b348abc25a30c82082
7
+ data.tar.gz: aec1be154cc035e4aad3074e9c0b090c03205544d7fb98c207c99dcfc668cc2856c083fdae13e92e7261dbe183a507ee2c5c977c7bcbdb7275c570941deaa81a
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - ruby-head
data/Gemfile CHANGED
@@ -3,6 +3,8 @@ source "http://rubygems.org"
3
3
  gem 'pg', '>= 0.0.1'
4
4
  gem 'activerecord', '>= 4.0.0'
5
5
 
6
+ gem 'rake'
7
+
6
8
  group :test, :development do
7
9
  gem 'rspec'
8
10
  end
@@ -1,40 +1,42 @@
1
1
  GEM
2
2
  remote: http://rubygems.org/
3
3
  specs:
4
- activemodel (4.0.0)
5
- activesupport (= 4.0.0)
6
- builder (~> 3.1.0)
7
- activerecord (4.0.0)
8
- activemodel (= 4.0.0)
9
- activerecord-deprecated_finders (~> 1.0.2)
10
- activesupport (= 4.0.0)
11
- arel (~> 4.0.0)
12
- activerecord-deprecated_finders (1.0.3)
13
- activesupport (4.0.0)
14
- i18n (~> 0.6, >= 0.6.4)
15
- minitest (~> 4.2)
16
- multi_json (~> 1.3)
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
- 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).
3
+ [![Build Status](https://travis-ci.org/bithavoc/multilang-hstore.svg)](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
@@ -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
@@ -1,3 +1,3 @@
1
1
  module Multilang
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
@@ -123,7 +123,7 @@ describe Multilang do
123
123
 
124
124
  rp.valid?
125
125
 
126
- rp.should have(4).errors
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(Multilang::Exceptions::DeprecationError)
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(Multilang::Exceptions::DeprecationError)
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.should have(2).items
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
@@ -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.1
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: 2014-09-17 00:00:00.000000000 Z
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.2.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