multilang-hstore 0.4 → 1.0.0.rc1

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.
@@ -1,8 +1,8 @@
1
1
  require 'multilang-hstore/version'
2
+ require 'multilang-hstore/exceptions'
2
3
  require 'multilang-hstore/translation_proxy'
3
4
  require 'multilang-hstore/translation_keeper'
4
5
  require 'multilang-hstore/active_record_extensions'
5
- require 'activerecord-postgres-hstore'
6
6
 
7
7
  module Multilang
8
8
  end
@@ -1,11 +1,11 @@
1
1
  module Multilang
2
2
  module ActiveRecordExtensions
3
3
 
4
- module ClassMethods
4
+ def self.raise_mass_asignment_deprecation_error!
5
+ raise Multilang::Exceptions::DeprecationError.new(":accessible was deprecated starting multilang-hstore >= 1.0.0 which is intended for Rails >= 4.0.0. Check more info about the deprecation of mass-asignment in Rails 4")
6
+ end
5
7
 
6
- def multilang_accessible_translations
7
- class_variable_get(:@@multilang_accessible_translations)
8
- end
8
+ module ClassMethods
9
9
 
10
10
  def multilang(*args)
11
11
 
@@ -18,7 +18,7 @@ module Multilang
18
18
 
19
19
  options.assert_valid_keys([:required, :length, :accessible, :format, :nil])
20
20
 
21
- define_translation_base! unless included_modules.include?(InstanceMethods)
21
+ define_translation_base!
22
22
 
23
23
  args.each do |attribute|
24
24
 
@@ -36,13 +36,12 @@ module Multilang
36
36
 
37
37
  #attribute accessibility for mass assignment
38
38
  if options[:accessible]
39
- matr = multilang_accessible_translations + [attribute.to_sym]
40
- class_variable_set(:@@multilang_accessible_translations, matr)
39
+ Multilang::ActiveRecordExtensions.raise_mass_asignment_deprecation_error!
41
40
  end
42
41
 
43
42
  module_eval do
44
43
  serialize "#{attribute}", ActiveRecord::Coders::Hstore
45
- end
44
+ end if defined?(ActiveRecord::Coders::Hstore)
46
45
 
47
46
  I18n.available_locales.each do |locale|
48
47
 
@@ -56,8 +55,7 @@ module Multilang
56
55
 
57
56
  # locale based attribute accessibility for mass assignment
58
57
  if options[:accessible]
59
- matr = multilang_accessible_translations + ["#{attribute}_#{locale}".to_sym]
60
- class_variable_set(:@@multilang_accessible_translations, matr)
58
+ Multilang::ActiveRecordExtensions.raise_mass_asignment_deprecation_error!
61
59
  end
62
60
 
63
61
  # attribute presence validator
@@ -88,29 +86,18 @@ module Multilang
88
86
  private
89
87
 
90
88
  def define_translation_base!
91
- include InstanceMethods
92
89
 
93
90
  define_method "multilang_translation_keeper" do |attribute|
94
91
  unless instance_variable_defined?("@multilang_attribute_#{attribute}")
95
92
  instance_variable_set("@multilang_attribute_#{attribute}", MultilangTranslationKeeper.new(self, attribute))
96
93
  end
97
94
  instance_variable_get "@multilang_attribute_#{attribute}"
98
- end
99
-
100
- unless class_variable_defined?(:@@multilang_accessible_translations)
101
- class_variable_set(:@@multilang_accessible_translations, [])
102
- end
95
+ end unless method_defined? :multilang_translation_keeper
103
96
 
104
97
  end
105
98
 
106
99
  end
107
100
 
108
- module InstanceMethods
109
- def mass_assignment_authorizer(role = :default)
110
- super(role) + (self.class.multilang_accessible_translations || [])
111
- end
112
- end #module InstanceMethods
113
-
114
101
  end #module ActiveRecordExtensions
115
102
  end ##module Multilang
116
103
 
@@ -124,6 +124,7 @@ module Multilang
124
124
  end
125
125
 
126
126
  def flush!
127
+ @model.send("#{@attribute}_will_change!")
127
128
  @model[@attribute] = @translations
128
129
  end
129
130
 
@@ -1,3 +1,3 @@
1
1
  module Multilang
2
- VERSION = "0.4"
2
+ VERSION = "1.0.0.rc1"
3
3
  end
@@ -139,52 +139,24 @@ describe Multilang do
139
139
  rp.body_ru.should == "Русский текст"
140
140
  end
141
141
 
142
- it "should not mass assign attributes in ProtectedPost" do
143
- rp = ProtectedPost.new( :title_lv => "Latviešu nosaukums",
144
- :title_ru => "Русский заголовок",
145
- :body_lv => "Latviešu apraksts",
146
- :body_ru => "Русский текст" )
147
-
148
- # test
149
- rp.title_lv.should_not == "Latviešu nosaukums"
150
- rp.body_lv.should_not == "Latviešu apraksts"
151
- rp.title_ru.should_not == "Русский заголовок"
152
- rp.body_ru.should_not == "Русский текст"
153
- end
154
-
155
-
156
- it "should mass assign attributes in RegulularPost, v2" do
157
- rp = RegularPost.new( { :title => {
158
- :lv => "Latviešu nosaukums",
159
- :ru => "Русский заголовок"
160
- },
161
- :body => {:lv => "Latviešu apraksts",
162
- :ru => "Русский текст"
163
- }
164
- } )
165
-
166
- # test
167
- rp.title_lv.should == "Latviešu nosaukums"
168
- rp.body_lv.should == "Latviešu apraksts"
169
- rp.title_ru.should == "Русский заголовок"
170
- rp.body_ru.should == "Русский текст"
142
+ it "should raise an exception if the deprecated option :accessible is passed as true" do
143
+ expect {
144
+ class ProtectedPost < ActiveRecord::Base
145
+ self.table_name = 'protected_posts'
146
+ multilang :title, :accessible => true
147
+ multilang :body, :accessible => true
148
+ end
149
+ }.to raise_error(Multilang::Exceptions::DeprecationError)
171
150
  end
172
151
 
173
- it "should not not mass assign attributes in ProtectedPost, v2" do
174
- rp = ProtectedPost.new( { :title => {
175
- :lv => "Latviešu nosaukums",
176
- :ru => "Русский заголовок"
177
- },
178
- :body => {:lv => "Latviešu apraksts",
179
- :ru => "Русский текст"
180
- }
181
- } )
182
-
183
- # test
184
- rp.title_lv.should_not == "Latviešu nosaukums"
185
- rp.body_lv.should_not == "Latviešu apraksts"
186
- rp.title_ru.should_not == "Русский заголовок"
187
- rp.body_ru.should_not == "Русский текст"
152
+ it "should not raise an exception if the deprecated option :accessible is passed as false" do
153
+ expect {
154
+ class ProtectedPost < ActiveRecord::Base
155
+ self.table_name = 'protected_posts'
156
+ multilang :title, :accessible => false
157
+ multilang :body, :accessible => false
158
+ end
159
+ }.to_not raise_error(Multilang::Exceptions::DeprecationError)
188
160
  end
189
161
 
190
162
  it "should save/load attributes in RegularPost" do
@@ -280,4 +252,32 @@ describe Multilang do
280
252
 
281
253
  end
282
254
 
255
+ it "should load the updated translations" do
256
+ # create
257
+ post = NamedPost.new
258
+ post.title = {:en=>"English", :es=>"Spanish"}
259
+ post.name = "First"
260
+ post.save!
261
+
262
+ # query
263
+ query_post = NamedPost.where(:name=>"First").first
264
+ query_post.should_not be_nil
265
+ I18n.locale = :es
266
+ query_post.title.should == "Spanish"
267
+ I18n.locale = :en
268
+ query_post.title.should == "English"
269
+
270
+ # update
271
+ query_post.title = {:en=>"English USA", :es=>"Spanish LAT"}
272
+ query_post.save!
273
+
274
+ #reload
275
+ query_post = NamedPost.where(:name=>"First").first
276
+ query_post.should_not be_nil
277
+ I18n.locale = :es
278
+ query_post.title.should == "Spanish LAT"
279
+ I18n.locale = :en
280
+ query_post.title.should == "English USA"
281
+ end
282
+
283
283
  end
data/spec/schema.rb CHANGED
@@ -1,19 +1,15 @@
1
1
  ActiveRecord::Schema.define(:version => 1) do
2
+
2
3
  create_table :abstract_posts do |t|
3
4
  t.hstore :title
4
5
  t.hstore :body
6
+ t.string :name
5
7
  t.integer :void, :default => 1
6
8
  end
7
9
 
8
- #create_table :posts do |t|
9
- # t.text :title
10
- # t.text :body
11
- # t.integer :void, :default => 1
12
- #end
10
+ create_table :named_posts do |t|
11
+ t.hstore :title
12
+ t.string :name
13
+ end
13
14
 
14
- #create_table :pages do |t|
15
- # t.text :title
16
- # t.text :body
17
- # t.integer :void, :default => 1
18
- #end
19
15
  end
data/spec/spec_helper.rb CHANGED
@@ -4,8 +4,6 @@ $:.unshift File.expand_path(File.dirname(__FILE__))
4
4
  require 'rubygems'
5
5
  require 'active_support'
6
6
  require 'active_record'
7
- require 'activerecord-postgres-hstore/activerecord'
8
- require 'activerecord-postgres-hstore/coder'
9
7
  require 'multilang-hstore'
10
8
  require 'logger'
11
9
 
@@ -28,34 +26,27 @@ end
28
26
 
29
27
  #testable models
30
28
  class AbstractPost < ActiveRecord::Base
31
- serialize :title, ActiveRecord::Coders::Hstore
32
- serialize :body, ActiveRecord::Coders::Hstore
33
29
  end
34
30
 
35
31
  class MinimalPost < ActiveRecord::Base
36
32
  self.table_name = 'abstract_posts'
37
- multilang :title, :required => false, :accessible => true
38
- multilang :body, :required => false, :accessible => true
39
- attr_accessible :void
33
+ multilang :title, :required => false
34
+ multilang :body, :required => false
40
35
  end
41
36
 
42
37
  class RegularPost < ActiveRecord::Base
43
38
  self.table_name = 'abstract_posts'
44
- multilang :title, :required => true, :length => 25, :accessible => true
45
- multilang :body, :required => true, :length => 1000, :accessible => true
46
- attr_accessible :void
47
- end
48
-
49
- class ProtectedPost < ActiveRecord::Base
50
- self.table_name = 'abstract_posts'
51
- multilang :title, :accessible => false
52
- multilang :body, :accessible => false
53
- attr_accessible :void
39
+ multilang :title, :required => true, :length => 25
40
+ multilang :body, :required => true, :length => 1000
54
41
  end
55
42
 
56
43
  class PartialPost < ActiveRecord::Base
57
44
  self.table_name = 'abstract_posts'
58
45
  multilang :title
59
46
  multilang :body
60
- attr_accessible :void
47
+ end
48
+
49
+ class NamedPost < ActiveRecord::Base
50
+ self.table_name = 'named_posts'
51
+ multilang :title
61
52
  end
metadata CHANGED
@@ -1,8 +1,8 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: multilang-hstore
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.4'
5
- prerelease:
4
+ version: 1.0.0.rc1
5
+ prerelease: 6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Arthur Meinart
@@ -29,13 +29,13 @@ dependencies:
29
29
  - !ruby/object:Gem::Version
30
30
  version: 0.0.1
31
31
  - !ruby/object:Gem::Dependency
32
- name: activerecord-postgres-hstore
32
+ name: activerecord
33
33
  requirement: !ruby/object:Gem::Requirement
34
34
  none: false
35
35
  requirements:
36
36
  - - ! '>='
37
37
  - !ruby/object:Gem::Version
38
- version: 0.7.5
38
+ version: 4.0.0
39
39
  type: :runtime
40
40
  prerelease: false
41
41
  version_requirements: !ruby/object:Gem::Requirement
@@ -43,7 +43,7 @@ dependencies:
43
43
  requirements:
44
44
  - - ! '>='
45
45
  - !ruby/object:Gem::Version
46
- version: 0.7.5
46
+ version: 4.0.0
47
47
  description: Model translations for Rails 3 backed by PostgreSQL and Hstore
48
48
  email: hello@heapsource.com
49
49
  executables: []
@@ -73,9 +73,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
73
73
  required_rubygems_version: !ruby/object:Gem::Requirement
74
74
  none: false
75
75
  requirements:
76
- - - ! '>='
76
+ - - ! '>'
77
77
  - !ruby/object:Gem::Version
78
- version: '0'
78
+ version: 1.3.1
79
79
  requirements: []
80
80
  rubyforge_project:
81
81
  rubygems_version: 1.8.25