traco 0.2.1 → 0.2.3
Sign up to get free protection for your applications and to get access to all the features.
- data/.rvmrc +1 -1
- data/.travis.yml +4 -0
- data/README.markdown +4 -0
- data/lib/traco/class_methods.rb +5 -7
- data/lib/traco/instance_methods.rb +5 -6
- data/lib/traco/translates.rb +29 -16
- data/lib/traco/version.rb +1 -1
- data/spec/spec_helper.rb +3 -3
- data/spec/traco_spec.rb +4 -4
- data/traco.gemspec +1 -0
- metadata +25 -12
data/.rvmrc
CHANGED
@@ -1 +1 @@
|
|
1
|
-
rvm ruby-1.9.3-
|
1
|
+
rvm ruby-1.9.3-p194-falcon@traco --create
|
data/.travis.yml
ADDED
data/README.markdown
CHANGED
@@ -1,5 +1,7 @@
|
|
1
1
|
# Traco
|
2
2
|
|
3
|
+
[![Build Status](https://secure.travis-ci.org/barsoom/traco.png)](http://travis-ci.org/barsoom/traco)
|
4
|
+
|
3
5
|
Translatable columns for Rails 3, stored in the model table itself.
|
4
6
|
|
5
7
|
Inspired by Iain Hecker's [translatable_columns](https://github.com/iain/translatable_columns/).
|
@@ -76,6 +78,7 @@ to install it.
|
|
76
78
|
bundle
|
77
79
|
rake # or guard
|
78
80
|
|
81
|
+
|
79
82
|
<!-- Keeping this a hidden brain dump for now.
|
80
83
|
|
81
84
|
## TODO
|
@@ -93,6 +96,7 @@ Possible improvements to make:
|
|
93
96
|
|
94
97
|
-->
|
95
98
|
|
99
|
+
|
96
100
|
## Credits and license
|
97
101
|
|
98
102
|
By [Barsoom](http://barsoom.se) under the MIT license:
|
data/lib/traco/class_methods.rb
CHANGED
@@ -1,20 +1,19 @@
|
|
1
1
|
module Traco
|
2
2
|
module ClassMethods
|
3
|
-
|
4
3
|
def locales_for_column(column)
|
5
4
|
column_names.grep(/\A#{column}_([a-z]{2})\z/) {
|
6
5
|
$1.to_sym
|
7
6
|
}.sort_by { |locale|
|
8
7
|
if locale == I18n.default_locale
|
9
|
-
|
8
|
+
"0"
|
10
9
|
else
|
11
|
-
locale
|
10
|
+
locale.to_s
|
12
11
|
end
|
13
12
|
}
|
14
13
|
end
|
15
14
|
|
16
|
-
def human_attribute_name(attribute, options={})
|
17
|
-
default = super(attribute, options.merge(default
|
15
|
+
def human_attribute_name(attribute, options = {})
|
16
|
+
default = super(attribute, options.merge(:default => ""))
|
18
17
|
if default.blank? && attribute.to_s.match(/\A(\w+)_([a-z]{2})\z/)
|
19
18
|
column, locale = $1, $2.to_sym
|
20
19
|
if translates?(column)
|
@@ -31,8 +30,7 @@ module Traco
|
|
31
30
|
end
|
32
31
|
|
33
32
|
def locale_name(locale)
|
34
|
-
I18n.t(locale, scope
|
33
|
+
I18n.t(locale, :scope => :"i18n.languages", :default => locale.to_s.upcase)
|
35
34
|
end
|
36
|
-
|
37
35
|
end
|
38
36
|
end
|
@@ -5,25 +5,24 @@ module Traco
|
|
5
5
|
|
6
6
|
def read_localized_value(column)
|
7
7
|
locales_for_reading_column(column).each do |locale|
|
8
|
-
value =
|
8
|
+
value = send("#{column}_#{locale}")
|
9
9
|
return value if value.present?
|
10
10
|
end
|
11
11
|
nil
|
12
12
|
end
|
13
13
|
|
14
14
|
def write_localized_value(column, value)
|
15
|
-
|
15
|
+
send("#{column}_#{I18n.locale}=", value)
|
16
16
|
end
|
17
17
|
|
18
18
|
def locales_for_reading_column(column)
|
19
19
|
self.class.locales_for_column(column).sort_by { |locale|
|
20
20
|
case locale
|
21
|
-
when I18n.locale then
|
22
|
-
when I18n.default_locale then
|
23
|
-
else locale
|
21
|
+
when I18n.locale then "0"
|
22
|
+
when I18n.default_locale then "1"
|
23
|
+
else locale.to_s
|
24
24
|
end
|
25
25
|
}
|
26
26
|
end
|
27
|
-
|
28
27
|
end
|
29
28
|
end
|
data/lib/traco/translates.rb
CHANGED
@@ -1,29 +1,42 @@
|
|
1
1
|
module Traco
|
2
2
|
module Translates
|
3
3
|
def translates(*columns)
|
4
|
+
set_up_once
|
5
|
+
store_as_translatable_columns columns.map(&:to_sym)
|
4
6
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
# Don't overwrite values if running multiple times in the same class
|
9
|
-
# or in different classes of an inheritance chain.
|
10
|
-
unless respond_to?(:translatable_columns)
|
11
|
-
class_attribute :translatable_columns
|
12
|
-
self.translatable_columns = []
|
7
|
+
columns.each do |column|
|
8
|
+
define_localized_reader column
|
9
|
+
define_localized_writer column
|
13
10
|
end
|
11
|
+
end
|
14
12
|
|
15
|
-
|
13
|
+
private
|
16
14
|
|
17
|
-
|
15
|
+
# Don't overwrite values if running multiple times in the same
|
16
|
+
# class or in different classes of an inheritance chain.
|
17
|
+
def set_up_once
|
18
|
+
return if respond_to?(:translatable_columns)
|
19
|
+
|
20
|
+
extend Traco::ClassMethods
|
21
|
+
include Traco::InstanceMethods
|
18
22
|
|
19
|
-
|
20
|
-
|
21
|
-
|
23
|
+
class_attribute :translatable_columns
|
24
|
+
self.translatable_columns = []
|
25
|
+
end
|
22
26
|
|
23
|
-
|
24
|
-
|
25
|
-
|
27
|
+
def store_as_translatable_columns(columns)
|
28
|
+
self.translatable_columns |= columns
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_localized_reader(column)
|
32
|
+
define_method(column) do
|
33
|
+
read_localized_value(column)
|
34
|
+
end
|
35
|
+
end
|
26
36
|
|
37
|
+
def define_localized_writer(column)
|
38
|
+
define_method("#{column}=") do |value|
|
39
|
+
write_localized_value(column, value)
|
27
40
|
end
|
28
41
|
end
|
29
42
|
end
|
data/lib/traco/version.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -18,11 +18,11 @@ end
|
|
18
18
|
require "active_record"
|
19
19
|
require "app/post.rb"
|
20
20
|
|
21
|
-
ActiveRecord::Base.establish_connection adapter
|
21
|
+
ActiveRecord::Base.establish_connection :adapter => "sqlite3", :database => ":memory:"
|
22
22
|
|
23
23
|
silence_stream(STDOUT) do
|
24
|
-
ActiveRecord::Schema.define(version
|
25
|
-
create_table :posts, force
|
24
|
+
ActiveRecord::Schema.define(:version => 0) do
|
25
|
+
create_table :posts, :force => true do |t|
|
26
26
|
t.string :title_sv, :title_en, :title_fi
|
27
27
|
t.string :body_sv, :body_en, :body_fi
|
28
28
|
end
|
data/spec/traco_spec.rb
CHANGED
@@ -59,7 +59,7 @@ end
|
|
59
59
|
|
60
60
|
describe Post, "#title" do
|
61
61
|
|
62
|
-
let(:post) { Post.new(title_sv
|
62
|
+
let(:post) { Post.new(:title_sv => "Hej", :title_en => "Halloa", :title_fi => "Moi moi") }
|
63
63
|
|
64
64
|
before do
|
65
65
|
Post.translates :title
|
@@ -113,7 +113,7 @@ describe Post, "#title=" do
|
|
113
113
|
|
114
114
|
it "should raise if locale has no column" do
|
115
115
|
I18n.locale = :ru
|
116
|
-
|
116
|
+
lambda {
|
117
117
|
post.title = "Privet"
|
118
118
|
}.should raise_error(NoMethodError, /title_ru/)
|
119
119
|
end
|
@@ -150,11 +150,11 @@ describe Post, ".human_attribute_name" do
|
|
150
150
|
# ActiveModel::Errors#full_messages passes in an ugly default.
|
151
151
|
|
152
152
|
it "should not yield to passed-in defaults" do
|
153
|
-
Post.human_attribute_name(:title_en, default
|
153
|
+
Post.human_attribute_name(:title_en, :default => "Title en").should == "Titel (engelska)"
|
154
154
|
end
|
155
155
|
|
156
156
|
it "should pass through defaults" do
|
157
|
-
Post.human_attribute_name(:body_sv, default
|
157
|
+
Post.human_attribute_name(:body_sv, :default => "Boday").should == "Boday"
|
158
158
|
end
|
159
159
|
|
160
160
|
end
|
data/traco.gemspec
CHANGED
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.
|
4
|
+
version: 0.2.3
|
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-
|
12
|
+
date: 2012-11-18 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
16
|
-
requirement: &
|
16
|
+
requirement: &70300030903820 !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: *
|
24
|
+
version_requirements: *70300030903820
|
25
25
|
- !ruby/object:Gem::Dependency
|
26
26
|
name: sqlite3
|
27
|
-
requirement: &
|
27
|
+
requirement: &70300030903360 !ruby/object:Gem::Requirement
|
28
28
|
none: false
|
29
29
|
requirements:
|
30
30
|
- - ! '>='
|
@@ -32,10 +32,21 @@ dependencies:
|
|
32
32
|
version: '0'
|
33
33
|
type: :development
|
34
34
|
prerelease: false
|
35
|
-
version_requirements: *
|
35
|
+
version_requirements: *70300030903360
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rake
|
38
|
+
requirement: &70300030902900 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70300030902900
|
36
47
|
- !ruby/object:Gem::Dependency
|
37
48
|
name: rspec
|
38
|
-
requirement: &
|
49
|
+
requirement: &70300030902460 !ruby/object:Gem::Requirement
|
39
50
|
none: false
|
40
51
|
requirements:
|
41
52
|
- - ! '>='
|
@@ -43,10 +54,10 @@ dependencies:
|
|
43
54
|
version: '0'
|
44
55
|
type: :development
|
45
56
|
prerelease: false
|
46
|
-
version_requirements: *
|
57
|
+
version_requirements: *70300030902460
|
47
58
|
- !ruby/object:Gem::Dependency
|
48
59
|
name: guard
|
49
|
-
requirement: &
|
60
|
+
requirement: &70300030901980 !ruby/object:Gem::Requirement
|
50
61
|
none: false
|
51
62
|
requirements:
|
52
63
|
- - ! '>='
|
@@ -54,10 +65,10 @@ dependencies:
|
|
54
65
|
version: '0'
|
55
66
|
type: :development
|
56
67
|
prerelease: false
|
57
|
-
version_requirements: *
|
68
|
+
version_requirements: *70300030901980
|
58
69
|
- !ruby/object:Gem::Dependency
|
59
70
|
name: guard-rspec
|
60
|
-
requirement: &
|
71
|
+
requirement: &70300030901500 !ruby/object:Gem::Requirement
|
61
72
|
none: false
|
62
73
|
requirements:
|
63
74
|
- - ! '>='
|
@@ -65,7 +76,7 @@ dependencies:
|
|
65
76
|
version: '0'
|
66
77
|
type: :development
|
67
78
|
prerelease: false
|
68
|
-
version_requirements: *
|
79
|
+
version_requirements: *70300030901500
|
69
80
|
description:
|
70
81
|
email:
|
71
82
|
- henrik@barsoom.se
|
@@ -75,6 +86,7 @@ extra_rdoc_files: []
|
|
75
86
|
files:
|
76
87
|
- .gitignore
|
77
88
|
- .rvmrc
|
89
|
+
- .travis.yml
|
78
90
|
- Gemfile
|
79
91
|
- Guardfile
|
80
92
|
- README.markdown
|
@@ -118,3 +130,4 @@ test_files:
|
|
118
130
|
- spec/app/sv.yml
|
119
131
|
- spec/spec_helper.rb
|
120
132
|
- spec/traco_spec.rb
|
133
|
+
has_rdoc:
|