traco 0.2.6 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -36,7 +36,7 @@ Declare these columns in the model:
36
36
 
37
37
  You can still use your accessors like `title_sv` and `title_sv=` in forms, validations and other code, but you also get:
38
38
 
39
- `#title`: Shows the title in the current locale. If blank, falls back to default locale, then to any locale.
39
+ `#title`: Shows the title in the current locale. If blank, falls back to default locale. Otherwise nil.
40
40
 
41
41
  `#title=`: Assigns the title to the column for the current locale, if present. Raises if the column doesn't exist.
42
42
 
@@ -56,6 +56,18 @@ You can still use your accessors like `title_sv` and `title_sv=` in forms, valid
56
56
  And the equivalent methods for `body`, of course.
57
57
 
58
58
 
59
+ ### Disable fallback
60
+
61
+ if you specify
62
+
63
+ class Post < ActiveRecord::Base
64
+ translates :title, :body,
65
+ fallback: false
66
+ end
67
+
68
+ then `#title` will return `nil` if there is no translation in the current locale, instead of falling back to the default locale.
69
+
70
+
59
71
  ## Installation
60
72
 
61
73
  Add this to your `Gemfile` if you use Bundler 1.1+:
@@ -1,12 +1,13 @@
1
1
  module Traco
2
2
  class LocalizedReader
3
- def initialize(record, column)
3
+ def initialize(record, column, options)
4
4
  @record = record
5
5
  @column = column
6
+ @fallback = options[:fallback]
6
7
  end
7
8
 
8
9
  def value
9
- locales.each do |locale|
10
+ locales_to_try.each do |locale|
10
11
  value = @record.send("#{@column}_#{locale}")
11
12
  return value if value.present?
12
13
  end
@@ -16,24 +17,19 @@ module Traco
16
17
 
17
18
  private
18
19
 
19
- def locales
20
- @record.class.locales_for_column(@column).sort_by { |locale|
21
- locale_sort_value(locale)
22
- }
20
+ def locales_to_try
21
+ @locales_to_try ||= locale_chain & locales_for_column
23
22
  end
24
23
 
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
24
+ def locale_chain
25
+ chain = []
26
+ chain << I18n.locale
27
+ chain << I18n.default_locale if @fallback
28
+ chain
29
+ end
30
+
31
+ def locales_for_column
32
+ @record.class.locales_for_column(@column)
37
33
  end
38
34
  end
39
35
  end
@@ -1,8 +1,9 @@
1
1
  module Traco
2
2
  module Translates
3
3
  def translates(*columns)
4
+ options = columns.extract_options!
4
5
  set_up_once
5
- store_as_translatable_columns columns.map(&:to_sym)
6
+ store_as_translatable_columns columns.map(&:to_sym), options
6
7
  end
7
8
 
8
9
  private
@@ -18,19 +19,27 @@ module Traco
18
19
  self.translatable_columns = []
19
20
  end
20
21
 
21
- def store_as_translatable_columns(columns)
22
+ def store_as_translatable_columns(columns, options)
23
+ fallback = options.fetch(:fallback, true)
24
+
22
25
  self.translatable_columns |= columns
23
26
 
24
27
  columns.each do |column|
25
- define_localized_reader column
28
+ define_localized_reader column, :fallback => fallback
26
29
  define_localized_writer column
27
30
  end
28
31
  end
29
32
 
30
- def define_localized_reader(column)
33
+ def define_localized_reader(column, options)
34
+ fallback = options[:fallback]
35
+
31
36
  define_method(column) do
32
37
  @localized_readers ||= {}
33
- @localized_readers[column] ||= Traco::LocalizedReader.new(self, column)
38
+ @localized_readers[column] ||= Traco::LocalizedReader.new(
39
+ self,
40
+ column,
41
+ :fallback => fallback
42
+ )
34
43
  @localized_readers[column].value
35
44
  end
36
45
  end
@@ -1,3 +1,3 @@
1
1
  module Traco
2
- VERSION = "0.2.6"
2
+ VERSION = "0.3.1"
3
3
  end
@@ -76,10 +76,10 @@ describe Post, "#title" do
76
76
  post.title.should == "Halloa"
77
77
  end
78
78
 
79
- it "should fall back to any other locale if default locale is blank" do
79
+ it "should not fall back to any other locale if default locale is blank" do
80
80
  post.title_sv = " "
81
81
  post.title_en = ""
82
- post.title.should == "Moi moi"
82
+ post.title.should be_nil
83
83
  end
84
84
 
85
85
  it "should return nil if all are blank" do
@@ -97,6 +97,28 @@ describe Post, "#title" do
97
97
  post.title.should == "title"
98
98
  post.body.should == "body"
99
99
  end
100
+
101
+ context "with :fallback => false" do
102
+ let(:post) {
103
+ Post.new(:title_sv => "Hej", :title_en => "Halloa")
104
+ }
105
+
106
+ before do
107
+ Post.translates :title, fallback: false
108
+ I18n.default_locale = :en
109
+ end
110
+
111
+ it "should not fall back to the default locale if locale has no column" do
112
+ I18n.locale = :ru
113
+ post.title.should be_nil
114
+ end
115
+
116
+ it "should not fall back to the default locale if blank" do
117
+ I18n.locale = :sv
118
+ post.title_sv = " "
119
+ post.title.should be_nil
120
+ end
121
+ end
100
122
  end
101
123
 
102
124
  describe Post, "#title=" do
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.6
4
+ version: 0.3.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-11-19 00:00:00.000000000 Z
12
+ date: 2012-11-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activerecord
16
- requirement: &70236680064560 !ruby/object:Gem::Requirement
16
+ requirement: &70244124230460 !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: *70236680064560
24
+ version_requirements: *70244124230460
25
25
  - !ruby/object:Gem::Dependency
26
26
  name: sqlite3
27
- requirement: &70236680064100 !ruby/object:Gem::Requirement
27
+ requirement: &70244124230040 !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: *70236680064100
35
+ version_requirements: *70244124230040
36
36
  - !ruby/object:Gem::Dependency
37
37
  name: rake
38
- requirement: &70236680063600 !ruby/object:Gem::Requirement
38
+ requirement: &70244124213140 !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: *70236680063600
46
+ version_requirements: *70244124213140
47
47
  - !ruby/object:Gem::Dependency
48
48
  name: rspec
49
- requirement: &70236680063180 !ruby/object:Gem::Requirement
49
+ requirement: &70244124212720 !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: *70236680063180
57
+ version_requirements: *70244124212720
58
58
  - !ruby/object:Gem::Dependency
59
59
  name: guard
60
- requirement: &70236680062740 !ruby/object:Gem::Requirement
60
+ requirement: &70244124212300 !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: *70236680062740
68
+ version_requirements: *70244124212300
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: guard-rspec
71
- requirement: &70236680062280 !ruby/object:Gem::Requirement
71
+ requirement: &70244124211880 !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: *70236680062280
79
+ version_requirements: *70244124211880
80
80
  description:
81
81
  email:
82
82
  - henrik@barsoom.se
@@ -113,12 +113,18 @@ required_ruby_version: !ruby/object:Gem::Requirement
113
113
  - - ! '>='
114
114
  - !ruby/object:Gem::Version
115
115
  version: '0'
116
+ segments:
117
+ - 0
118
+ hash: 2678658822643103053
116
119
  required_rubygems_version: !ruby/object:Gem::Requirement
117
120
  none: false
118
121
  requirements:
119
122
  - - ! '>='
120
123
  - !ruby/object:Gem::Version
121
124
  version: '0'
125
+ segments:
126
+ - 0
127
+ hash: 2678658822643103053
122
128
  requirements: []
123
129
  rubyforge_project:
124
130
  rubygems_version: 1.8.5
@@ -130,4 +136,3 @@ test_files:
130
136
  - spec/app/sv.yml
131
137
  - spec/spec_helper.rb
132
138
  - spec/traco_spec.rb
133
- has_rdoc: