translatable_ar 1.0.5 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,7 +3,7 @@ class TranslatableAR
3
3
  extend ActiveSupport::Concern
4
4
 
5
5
  module ClassMethods
6
-
6
+
7
7
  attr_accessor :translatable_ar_attributes
8
8
 
9
9
  def translates(*attributes)
@@ -12,23 +12,22 @@ class TranslatableAR
12
12
  remember_translatable_ar_attribute(attribute)
13
13
  define_translatable_ar_methods(attribute)
14
14
  end
15
- end
16
-
15
+ end
16
+
17
17
  private
18
18
 
19
19
  def remember_translatable_ar_model
20
20
  TranslatableAR.models << self
21
21
  TranslatableAR.models.uniq!
22
22
  end
23
-
23
+
24
24
  def remember_translatable_ar_attribute(attribute)
25
- @translatable_ar_attributes ||= []
25
+ @translatable_ar_attributes ||= []
26
26
  @translatable_ar_attributes << attribute
27
27
  @translatable_ar_attributes.uniq!
28
28
  end
29
-
30
- def define_translatable_ar_methods(attribute)
31
- serialize "#{attribute}_i18n", TranslatableAR.coder_or_default_object
29
+
30
+ def define_translatable_ar_methods(attribute)
32
31
  class_eval do
33
32
  define_translatable_ar_writer(attribute)
34
33
  define_translatable_ar_reader(attribute)
@@ -36,55 +35,59 @@ class TranslatableAR
36
35
  define_translatable_ar_before_type_cast(attribute)
37
36
  end
38
37
  end
39
-
38
+
40
39
  def define_translatable_ar_reader(attribute)
41
40
  define_method("#{attribute}") do
42
41
  if respond_to?("#{attribute}_i18n")
43
- if send("#{attribute}_i18n").has_key?(I18n.locale.to_s)
44
- send("#{attribute}_i18n")[I18n.locale.to_s]
42
+ i18n = send("#{attribute}_i18n") || {}
43
+ if i18n.has_key?(I18n.locale.to_s)
44
+ i18n[I18n.locale.to_s]
45
45
  else
46
- send("#{attribute}_i18n")[TranslatableAR.fallback_locale.to_s]
46
+ i18n[TranslatableAR.fallback_locale.to_s]
47
47
  end
48
48
  else
49
49
  super()
50
50
  end
51
51
  end
52
52
  end
53
-
53
+
54
54
  def define_translatable_ar_writer(attribute)
55
55
  define_method("#{attribute}=") do |value|
56
56
  if respond_to?("#{attribute}_i18n")
57
57
  if value.is_a?(Hash)
58
58
  send("#{attribute}_i18n=", value)
59
59
  else
60
- send("#{attribute}_i18n")[I18n.locale.to_s] = value
60
+ i18n = send("#{attribute}_i18n") || {}
61
+ i18n[I18n.locale.to_s] = value
62
+ send("#{attribute}_i18n=", i18n)
61
63
  end
64
+ send("#{attribute}_i18n_will_change!")
62
65
  else
63
66
  super(value)
64
67
  end
65
68
  end
66
69
  end
67
-
70
+
68
71
  def define_translatable_ar_changed?(attribute)
69
- define_method("#{attribute}_changed?") do |value|
72
+ define_method("#{attribute}_changed?") do
70
73
  if respond_to?("#{attribute}_i18n")
71
74
  send("#{attribute}_i18n_changed?")
72
75
  else
73
- super(value)
76
+ super()
74
77
  end
75
78
  end
76
79
  end
77
-
80
+
78
81
  def define_translatable_ar_before_type_cast(attribute)
79
82
  define_method("#{attribute}_before_type_cast") do
80
83
  if respond_to?("#{attribute}_i18n")
81
- send("#{attribute}_i18n_before_type_cast")[I18n.locale.to_s]
84
+ send("#{attribute}_i18n")[I18n.locale.to_s]
82
85
  else
83
86
  super()
84
87
  end
85
88
  end
86
89
  end
87
-
90
+
88
91
  end
89
92
  end
90
93
  end
data/lib/railtie.rb CHANGED
@@ -31,7 +31,7 @@ class TranslatableAr < Rails::Railtie
31
31
 
32
32
  migration_template 'setup.rb', "db/migrate/setup_translatable_ar_#{time}.rb"
33
33
  end
34
-
34
+
35
35
  def models
36
36
  TranslatableAR.models.select do |model|
37
37
  model.translatable_ar_attributes.any? do |attribute|
@@ -39,7 +39,7 @@ class TranslatableAr < Rails::Railtie
39
39
  end
40
40
  end
41
41
  end
42
-
42
+
43
43
  def attributes
44
44
  attributes = []
45
45
  attribute_struct = Struct.new(:name, :model)
@@ -52,14 +52,6 @@ class TranslatableAr < Rails::Railtie
52
52
  end
53
53
  attributes
54
54
  end
55
-
56
- def column_type
57
- uses_hstore? ? :hstore : :text
58
- end
59
-
60
- def uses_hstore?
61
- TranslatableAR.uses_hstore?
62
- end
63
55
 
64
56
  end
65
57
  end
@@ -1,20 +1,16 @@
1
1
  class <%= migration_class_name %> < ActiveRecord::Migration
2
2
 
3
3
  def up
4
-
5
- <% if uses_hstore? -%>
4
+
6
5
  execute "CREATE EXTENSION IF NOT EXISTS hstore"
7
- <% end -%>
8
-
6
+
9
7
  <% attributes.each do |attribute| -%>
10
- add_column :<%= attribute.model.table_name %>, :<%= attribute.name %>_i18n, :<%= column_type %>
8
+ add_column :<%= attribute.model.table_name %>, :<%= attribute.name %>_i18n, :hstore
11
9
  <% end -%>
12
10
 
13
- <% if uses_hstore?
14
11
  attributes.each do |attribute| -%>
15
12
  add_hstore_index :<%= attribute.model.table_name %>, :<%= attribute.name %>_i18n
16
- <% end
17
- end-%>
13
+ <% end -%>
18
14
 
19
15
  <% models.each do |model| -%>
20
16
  puts "Preparing existing <%= model.to_s %> objects ..."
@@ -27,17 +23,15 @@ end-%>
27
23
  end
28
24
  <% end -%>
29
25
  end
30
-
26
+
31
27
  def down
32
28
  <% attributes.each do |attribute| -%>
33
29
  remove_column :<%= attribute.model.table_name %>, :<%= attribute.name %>_i18n
34
30
  <% end -%>
35
31
 
36
- <% if uses_hstore?
37
- attributes.each do |attribute| -%>
32
+ <% attributes.each do |attribute| -%>
38
33
  remove_hstore_index :<%= attribute.model.table_name %>, :<%= attribute.name %>_i18n
39
- <% end
40
- end-%>
34
+ <% end -%>
41
35
  end
42
36
 
43
37
  end
@@ -1,18 +1,9 @@
1
1
  class TranslatableAR
2
2
 
3
3
  class << self
4
- attr_accessor :models, :coder_or_default_object, :fallback_locale
5
- def coder_or_default_object
6
- @coder_or_default_object ||= Hash
7
- end
4
+ attr_accessor :models, :fallback_locale
8
5
  def models
9
6
  @models ||= []
10
- end
11
- def uses_hstore?
12
- @coder_or_default_object == ActiveRecord::Coders::Hstore rescue false
13
- end
14
- def use_hstore!
15
- @coder_or_default_object = ActiveRecord::Coders::Hstore
16
7
  end
17
8
  def fallback_locale=(locale)
18
9
  @fallback_locale = locale
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translatable_ar
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.5
4
+ version: 1.1.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: 2013-05-08 00:00:00.000000000 Z
12
+ date: 2014-10-16 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
@@ -62,5 +62,5 @@ rubyforge_project:
62
62
  rubygems_version: 1.8.25
63
63
  signing_key:
64
64
  specification_version: 3
65
- summary: i18n for your active record models with HStore / serialized attribute
65
+ summary: i18n for your active record models with HStore attribute
66
66
  test_files: []