multilang-hstore 0.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.
@@ -30,6 +30,10 @@ module Multilang
30
30
  multilang_translation_keeper(attribute).update(value)
31
31
  end
32
32
 
33
+ define_method "#{attribute}_before_type_cast" do
34
+ multilang_translation_keeper(attribute).translations
35
+ end
36
+
33
37
  #attribute accessibility for mass assignment
34
38
  if options[:accessible]
35
39
  matr = multilang_accessible_translations + [attribute.to_sym]
@@ -13,22 +13,11 @@ module Multilang
13
13
  end
14
14
 
15
15
  def value(locale = nil)
16
- locale ||= actual_locale
17
- read(locale)
16
+ @translations.value(locale)
18
17
  end
19
18
 
20
19
  def current_or_any_value
21
- v = value
22
- if v.empty?
23
- reduced_locales = locales - [actual_locale]
24
- reduced_locales.each do |locale|
25
- v = value(locale)
26
- return v unless v.empty?
27
- end
28
- else
29
- return v
30
- end
31
- return ''
20
+ @translations.current_or_any_value
32
21
  end
33
22
 
34
23
  def to_s
@@ -60,7 +49,7 @@ module Multilang
60
49
  end
61
50
 
62
51
  def locales
63
- @translations.keys.map(&:to_sym)
52
+ @translations.locales
64
53
  end
65
54
 
66
55
  def empty?
@@ -70,7 +59,7 @@ module Multilang
70
59
  private
71
60
 
72
61
  def actual_locale
73
- I18n.locale
62
+ @translations.actual_locale
74
63
  end
75
64
 
76
65
  def write(locale, value)
@@ -78,7 +67,7 @@ module Multilang
78
67
  end
79
68
 
80
69
  def read(locale)
81
- MultilangTranslationProxy.new(self, locale)
70
+ @translations.read(locale)
82
71
  end
83
72
 
84
73
  def raw_read(locale)
@@ -93,6 +82,45 @@ module Multilang
93
82
  data = @model[@attribute]
94
83
  data = data.blank? ? nil : data
95
84
  @translations = data.is_a?(Hash) ? data : {}
85
+
86
+ class << translations
87
+ attr_accessor :multilang_keeper
88
+ def to_s
89
+ "#{current_or_any_value}"
90
+ end
91
+
92
+ def locales
93
+ self.keys.map(&:to_sym)
94
+ end
95
+
96
+ def read(locale)
97
+ MultilangTranslationProxy.new(self.multilang_keeper, locale)
98
+ end
99
+
100
+ def current_or_any_value
101
+ v = value
102
+ if v.empty?
103
+ reduced_locales = locales - [actual_locale]
104
+ reduced_locales.each do |locale|
105
+ v = value(locale)
106
+ return v unless v.empty?
107
+ end
108
+ else
109
+ return v
110
+ end
111
+ return ''
112
+ end
113
+
114
+ def value(locale = nil)
115
+ locale ||= actual_locale
116
+ read(locale)
117
+ end
118
+
119
+ def actual_locale
120
+ I18n.locale
121
+ end
122
+ end
123
+ @translations.public_send("multilang_keeper=", self)
96
124
  end
97
125
 
98
126
  def flush!
@@ -1,3 +1,3 @@
1
1
  module Multilang
2
- VERSION = "0.1"
2
+ VERSION = "0.2"
3
3
  end
@@ -1,5 +1,4 @@
1
1
  # -*- coding: utf-8 -*-
2
- $KCODE = "U"
3
2
 
4
3
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
5
4
 
@@ -255,4 +254,30 @@ describe Multilang do
255
254
  rp.body.any.should == "Latviešu apraksts"
256
255
 
257
256
  end
257
+
258
+ it "should set/get attributes directly from HStore underlying type returned by before_type_cast" do
259
+ rp = RegularPost.new
260
+
261
+ # set
262
+ I18n.locale = :es
263
+ rp.title = "Hola"
264
+ rp.body = "Hola Mundo"
265
+
266
+ I18n.locale = :en
267
+ rp.title = "Hello"
268
+ rp.body = "Hello World"
269
+
270
+ I18n.locale = :ru
271
+ # test
272
+ rp.title_before_type_cast.actual_locale.should be :ru
273
+ rp.title_before_type_cast.locales.should have(2).items
274
+ rp.title_before_type_cast.locales.should match_array [:es, :en]
275
+ rp.title_before_type_cast.should be_kind_of Hash
276
+ I18n.locale = :es
277
+ rp.title_before_type_cast.value("en").should eq("Hello")
278
+ rp.title_before_type_cast.value("es").should eq("Hola")
279
+ rp.title_before_type_cast.value.should eq("Hola")
280
+
281
+ end
282
+
258
283
  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: '0.1'
4
+ version: '0.2'
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-10-29 00:00:00.000000000 Z
13
+ date: 2012-11-12 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Model translations for Rails 3 backed by PostgreSQL and Hstore
16
16
  email: hello@firebase.co