traco 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
data/lib/traco.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  require "traco/version"
2
2
  require "traco/translates"
3
3
  require "traco/class_methods"
4
- require "traco/instance_methods"
4
+ require "traco/localized_reader"
@@ -3,13 +3,7 @@ module Traco
3
3
  def locales_for_column(column)
4
4
  column_names.grep(/\A#{column}_([a-z]{2})\z/) {
5
5
  $1.to_sym
6
- }.sort_by { |locale|
7
- if locale == I18n.default_locale
8
- "0"
9
- else
10
- locale.to_s
11
- end
12
- }
6
+ }.sort_by { |locale| locale_sort_value(locale) }
13
7
  end
14
8
 
15
9
  def human_attribute_name(attribute, options = {})
@@ -25,6 +19,16 @@ module Traco
25
19
 
26
20
  private
27
21
 
22
+ def locale_sort_value(locale)
23
+ if locale == I18n.default_locale
24
+ # Sort the default locale first.
25
+ "0"
26
+ else
27
+ # Sort the rest alphabetically.
28
+ locale.to_s
29
+ end
30
+ end
31
+
28
32
  def translates?(column)
29
33
  translatable_columns.include?(column.to_sym)
30
34
  end
@@ -0,0 +1,39 @@
1
+ module Traco
2
+ class LocalizedReader
3
+ def initialize(record, column)
4
+ @record = record
5
+ @column = column
6
+ end
7
+
8
+ def value
9
+ locales.each do |locale|
10
+ value = @record.send("#{@column}_#{locale}")
11
+ return value if value.present?
12
+ end
13
+
14
+ nil
15
+ end
16
+
17
+ private
18
+
19
+ def locales
20
+ @record.class.locales_for_column(@column).sort_by { |locale|
21
+ locale_sort_value(locale)
22
+ }
23
+ end
24
+
25
+ def locale_sort_value(locale)
26
+ case locale
27
+ when I18n.locale
28
+ # Sort the current locale first.
29
+ "0"
30
+ when I18n.default_locale
31
+ # Sort the default locale second.
32
+ "1"
33
+ else
34
+ # Sort the rest alphabetically.
35
+ locale.to_s
36
+ end
37
+ end
38
+ end
39
+ end
@@ -18,7 +18,6 @@ module Traco
18
18
  return if respond_to?(:translatable_columns)
19
19
 
20
20
  extend Traco::ClassMethods
21
- include Traco::InstanceMethods
22
21
 
23
22
  class_attribute :translatable_columns
24
23
  self.translatable_columns = []
@@ -30,13 +29,14 @@ module Traco
30
29
 
31
30
  def define_localized_reader(column)
32
31
  define_method(column) do
33
- read_localized_value(column)
32
+ @localized_reader ||= Traco::LocalizedReader.new(self, column)
33
+ @localized_reader.value
34
34
  end
35
35
  end
36
36
 
37
37
  def define_localized_writer(column)
38
38
  define_method("#{column}=") do |value|
39
- write_localized_value(column, value)
39
+ send("#{column}_#{I18n.locale}=", value)
40
40
  end
41
41
  end
42
42
  end
data/lib/traco/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Traco
2
- VERSION = "0.2.3"
2
+ VERSION = "0.2.4"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: traco
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -13,7 +13,7 @@ date: 2012-11-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70300030903820 !ruby/object:Gem::Requirement
16
+ requirement: &70337754840160 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ~>
@@ -21,10 +21,10 @@ dependencies:
21
21
  version: '3.0'
22
22
  type: :runtime
23
23
  prerelease: false
24
- version_requirements: *70300030903820
24
+ version_requirements: *70337754840160
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &70300030903360 !ruby/object:Gem::Requirement
27
+ requirement: &70337754839700 !ruby/object:Gem::Requirement
28
28
  none: false
29
29
  requirements:
30
30
  - - ! '>='
@@ -32,10 +32,10 @@ dependencies:
32
32
  version: '0'
33
33
  type: :development
34
34
  prerelease: false
35
- version_requirements: *70300030903360
35
+ version_requirements: *70337754839700
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70300030902900 !ruby/object:Gem::Requirement
38
+ requirement: &70337754839240 !ruby/object:Gem::Requirement
39
39
  none: false
40
40
  requirements:
41
41
  - - ! '>='
@@ -43,10 +43,10 @@ dependencies:
43
43
  version: '0'
44
44
  type: :development
45
45
  prerelease: false
46
- version_requirements: *70300030902900
46
+ version_requirements: *70337754839240
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70300030902460 !ruby/object:Gem::Requirement
49
+ requirement: &70337754838800 !ruby/object:Gem::Requirement
50
50
  none: false
51
51
  requirements:
52
52
  - - ! '>='
@@ -54,10 +54,10 @@ dependencies:
54
54
  version: '0'
55
55
  type: :development
56
56
  prerelease: false
57
- version_requirements: *70300030902460
57
+ version_requirements: *70337754838800
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard
60
- requirement: &70300030901980 !ruby/object:Gem::Requirement
60
+ requirement: &70337754838320 !ruby/object:Gem::Requirement
61
61
  none: false
62
62
  requirements:
63
63
  - - ! '>='
@@ -65,10 +65,10 @@ dependencies:
65
65
  version: '0'
66
66
  type: :development
67
67
  prerelease: false
68
- version_requirements: *70300030901980
68
+ version_requirements: *70337754838320
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
- requirement: &70300030901500 !ruby/object:Gem::Requirement
71
+ requirement: &70337754837840 !ruby/object:Gem::Requirement
72
72
  none: false
73
73
  requirements:
74
74
  - - ! '>='
@@ -76,7 +76,7 @@ dependencies:
76
76
  version: '0'
77
77
  type: :development
78
78
  prerelease: false
79
- version_requirements: *70300030901500
79
+ version_requirements: *70337754837840
80
80
  description:
81
81
  email:
82
82
  - henrik@barsoom.se
@@ -93,7 +93,7 @@ files:
93
93
  - Rakefile
94
94
  - lib/traco.rb
95
95
  - lib/traco/class_methods.rb
96
- - lib/traco/instance_methods.rb
96
+ - lib/traco/localized_reader.rb
97
97
  - lib/traco/translates.rb
98
98
  - lib/traco/version.rb
99
99
  - spec/app/post.rb
@@ -1,28 +0,0 @@
1
- module Traco
2
- module InstanceMethods
3
-
4
- private
5
-
6
- def read_localized_value(column)
7
- locales_for_reading_column(column).each do |locale|
8
- value = send("#{column}_#{locale}")
9
- return value if value.present?
10
- end
11
- nil
12
- end
13
-
14
- def write_localized_value(column, value)
15
- send("#{column}_#{I18n.locale}=", value)
16
- end
17
-
18
- def locales_for_reading_column(column)
19
- self.class.locales_for_column(column).sort_by { |locale|
20
- case locale
21
- when I18n.locale then "0"
22
- when I18n.default_locale then "1"
23
- else locale.to_s
24
- end
25
- }
26
- end
27
- end
28
- end