attr-translatable 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -32,7 +32,41 @@ end
32
32
  )
33
33
  @post.update_translation(:title, :ja, "新しいブログポスト")
34
34
  @post.translate(:title, :ja)
35
- #=> "新しいブログポスト"
35
+ # => "新しいブログポスト"
36
+ @post.t(:title, :ja)
37
+ # => "新しいブログポスト"
38
+ ```
39
+
40
+ ## Define Translatable Attributes
41
+
42
+ You can define translatable as shown below. All translatable attributes must be a string.
43
+
44
+ ```ruby
45
+ attr_translatable :attr_name1, :attr_name2, ...
46
+ ```
47
+
48
+ ## Add/Update Translation
49
+
50
+ ```ruby
51
+ update_translation(attr_name, lang, translation)
52
+ ```
53
+
54
+ ## Translate Attributes
55
+
56
+ ```ruby
57
+ translate(attr_name, lang)
58
+ # or you can use shorten version:
59
+ t(attr_name, lang)
60
+ ```
61
+
62
+ ## Access Translatable Attributes Name
63
+
64
+ ```ruby
65
+ ActiveRecord::Base.translatable_attrs
66
+ # => ["attr1", "attr2", ...]
67
+ # For example,
68
+ Post.translatable_attrs
69
+ # => ["title", "content"]
36
70
  ```
37
71
  ## Authors
38
72
 
@@ -10,14 +10,14 @@ module AttrTranslatable
10
10
  def attr_translatable(*args)
11
11
  attrs = validate_attrs(args)
12
12
  unless ancestors.include?(AttrTranslatable::Translation)
13
- @valid_translatable_attrs = attrs
14
- has_many :attr_translatables, as: :model, dependent: :destroy
13
+ @translatable_attrs = attrs
14
+ has_many :attr_translatables, :as => :model, :dependent => :destroy
15
15
  include AttrTranslatable::Translation
16
16
  end
17
17
  end
18
18
 
19
- def valid_translatable_attrs
20
- @valid_translatable_attrs || []
19
+ def translatable_attrs
20
+ @translatable_attrs || []
21
21
  end
22
22
 
23
23
  private
@@ -28,7 +28,7 @@ module AttrTranslatable
28
28
  a = a.to_s
29
29
  raise ArgumentError, "#{a} is not an attribute of #{self.name}." unless valid_attrs.include?(a)
30
30
  type = columns.find { |c| c.name == a }.type
31
- raise ArgumentError, "Only a string type can be translatable." unless type == :string
31
+ raise ArgumentError, "#{a} has a #{type} type. Only a string type can be translatable." unless [:string, :text].include? type
32
32
  a
33
33
  end
34
34
  end
@@ -28,12 +28,12 @@ module AttrTranslatable
28
28
  def internal_translate(attr, lang)
29
29
  attr, lang = [attr.to_s, lang.to_s]
30
30
  validate_attr(attr)
31
- at = AttrTranslation.select(:translation).where(attr: attr, lang: lang, model_id: self.id, model_type: self.class.name).first
31
+ at = AttrTranslation.select(:translation).where(:attr => attr, :lang => lang, :model_id => self.id, :model_type => self.class.name).first
32
32
  at ? at.translation : self.send(attr)
33
33
  end
34
34
 
35
35
  def validate_attr(attr)
36
- raise ArgumentError, "#{attr} is not translatable attribute." unless self.class.valid_translatable_attrs.include?(attr)
36
+ raise ArgumentError, "#{attr} is not translatable attribute." unless self.class.translatable_attrs.include?(attr)
37
37
  end
38
38
  end
39
39
  end
@@ -1,3 +1,3 @@
1
1
  module AttrTranslatable
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.1"
3
3
  end
@@ -5,5 +5,5 @@ class AttrTranslation < ActiveRecord::Base
5
5
 
6
6
  attr_accessible :translation, :lang, :attr, :model_id, :model_type
7
7
 
8
- validates :attr, uniqueness: { :scope => [:lang, :model_id, :model_type] }
8
+ validates :attr, :uniqueness => { :scope => [:lang, :model_id, :model_type] }
9
9
  end
@@ -24,12 +24,33 @@ describe ActiveRecord::Base do
24
24
  Post.attr_translatable :title, :content
25
25
  end
26
26
 
27
+ describe "#translatable_attrs" do
28
+ it "returns translatable attributes" do
29
+ Post.translatable_attrs.should == ["title", "content"]
30
+ end
31
+ end
32
+
27
33
  describe "#update_translation" do
28
34
  context "when non translatable attribute is given" do
29
35
  it "raises an error" do
30
36
  lambda { @post.update_translation(:url, :ja, "アドレス") }.should raise_error ArgumentError
31
37
  end
32
38
  end
39
+ context "when translatable attribute is given" do
40
+ it "adds new translation" do
41
+ translation = "ブログのタイトル"
42
+ @post.update_translation(:title, :ja, translation)
43
+ @post.t(:title, :ja).should == translation
44
+ end
45
+ it "updates existing translation" do
46
+ translation = "ブログのタイトル"
47
+ @post.update_translation(:title, :ja, translation)
48
+ @post.t(:title, :ja).should == translation
49
+ translation = "新しいブログのタイトル"
50
+ @post.update_translation(:title, :ja, translation)
51
+ @post.t(:title, :ja).should == translation
52
+ end
53
+ end
33
54
  end
34
55
 
35
56
  describe "#translate" do
data/spec/spec_helper.rb CHANGED
@@ -6,8 +6,8 @@ require 'sqlite3'
6
6
  require 'attr_translatable'
7
7
 
8
8
  ActiveRecord::Base.establish_connection(
9
- adapter: "sqlite3",
10
- database: ":memory:"
9
+ :adapter => "sqlite3",
10
+ :database => ":memory:"
11
11
  )
12
12
 
13
13
  RSpec.configure do |config|
@@ -26,11 +26,11 @@ ActiveRecord::Schema.define do
26
26
  t.string :translation
27
27
  t.string :lang
28
28
  t.string :attr
29
- t.references :model, polymorphic: true
29
+ t.references :model, :polymorphic => true
30
30
  t.timestamps
31
31
  end
32
32
 
33
- add_index :attr_translations, [:lang, :attr, :model_id, :model_type], unique: true, name: "index_attr_translations_on_attr_and_lang_and_model"
33
+ add_index :attr_translations, [:lang, :attr, :model_id, :model_type], :unique => true, :name => "index_attr_translations_on_attr_and_lang_and_model"
34
34
 
35
35
  create_table :posts do |t|
36
36
  t.string :title
metadata CHANGED
@@ -1,119 +1,111 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: attr-translatable
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
5
- prerelease:
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 1
9
+ version: 0.1.1
6
10
  platform: ruby
7
- authors:
11
+ authors:
8
12
  - Katsuya Noguchi
9
13
  autorequire:
10
14
  bindir: bin
11
15
  cert_chain: []
12
- date: 2012-08-03 00:00:00.000000000 Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
16
+
17
+ date: 2012-08-06 00:00:00 +09:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
15
21
  name: activerecord
16
- requirement: !ruby/object:Gem::Requirement
17
- none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :development
23
22
  prerelease: false
24
- version_requirements: !ruby/object:Gem::Requirement
25
- none: false
26
- requirements:
27
- - - ! '>='
28
- - !ruby/object:Gem::Version
29
- version: '0'
30
- - !ruby/object:Gem::Dependency
31
- name: rake
32
- requirement: !ruby/object:Gem::Requirement
33
- none: false
34
- requirements:
35
- - - ! '>='
36
- - !ruby/object:Gem::Version
37
- version: 0.8.7
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ requirements:
25
+ - - ">="
26
+ - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ version: "0"
38
30
  type: :development
31
+ version_requirements: *id001
32
+ - !ruby/object:Gem::Dependency
33
+ name: rake
39
34
  prerelease: false
40
- version_requirements: !ruby/object:Gem::Requirement
41
- none: false
42
- requirements:
43
- - - ! '>='
44
- - !ruby/object:Gem::Version
35
+ requirement: &id002 !ruby/object:Gem::Requirement
36
+ requirements:
37
+ - - ">="
38
+ - !ruby/object:Gem::Version
39
+ segments:
40
+ - 0
41
+ - 8
42
+ - 7
45
43
  version: 0.8.7
46
- - !ruby/object:Gem::Dependency
47
- name: rspec
48
- requirement: !ruby/object:Gem::Requirement
49
- none: false
50
- requirements:
51
- - - ~>
52
- - !ruby/object:Gem::Version
53
- version: '2.8'
54
44
  type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: rspec
55
48
  prerelease: false
56
- version_requirements: !ruby/object:Gem::Requirement
57
- none: false
58
- requirements:
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ requirements:
59
51
  - - ~>
60
- - !ruby/object:Gem::Version
61
- version: '2.8'
62
- - !ruby/object:Gem::Dependency
63
- name: rdoc
64
- requirement: !ruby/object:Gem::Requirement
65
- none: false
66
- requirements:
67
- - - ! '>='
68
- - !ruby/object:Gem::Version
69
- version: '0'
52
+ - !ruby/object:Gem::Version
53
+ segments:
54
+ - 2
55
+ - 8
56
+ version: "2.8"
70
57
  type: :development
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: rdoc
71
61
  prerelease: false
72
- version_requirements: !ruby/object:Gem::Requirement
73
- none: false
74
- requirements:
75
- - - ! '>='
76
- - !ruby/object:Gem::Version
77
- version: '0'
78
- - !ruby/object:Gem::Dependency
79
- name: database_cleaner
80
- requirement: !ruby/object:Gem::Requirement
81
- none: false
82
- requirements:
83
- - - ~>
84
- - !ruby/object:Gem::Version
85
- version: 0.7.1
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ requirements:
64
+ - - ">="
65
+ - !ruby/object:Gem::Version
66
+ segments:
67
+ - 0
68
+ version: "0"
86
69
  type: :development
70
+ version_requirements: *id004
71
+ - !ruby/object:Gem::Dependency
72
+ name: database_cleaner
87
73
  prerelease: false
88
- version_requirements: !ruby/object:Gem::Requirement
89
- none: false
90
- requirements:
74
+ requirement: &id005 !ruby/object:Gem::Requirement
75
+ requirements:
91
76
  - - ~>
92
- - !ruby/object:Gem::Version
77
+ - !ruby/object:Gem::Version
78
+ segments:
79
+ - 0
80
+ - 7
81
+ - 1
93
82
  version: 0.7.1
94
- - !ruby/object:Gem::Dependency
95
- name: sqlite3
96
- requirement: !ruby/object:Gem::Requirement
97
- none: false
98
- requirements:
99
- - - ~>
100
- - !ruby/object:Gem::Version
101
- version: 1.3.5
102
83
  type: :development
84
+ version_requirements: *id005
85
+ - !ruby/object:Gem::Dependency
86
+ name: sqlite3
103
87
  prerelease: false
104
- version_requirements: !ruby/object:Gem::Requirement
105
- none: false
106
- requirements:
88
+ requirement: &id006 !ruby/object:Gem::Requirement
89
+ requirements:
107
90
  - - ~>
108
- - !ruby/object:Gem::Version
91
+ - !ruby/object:Gem::Version
92
+ segments:
93
+ - 1
94
+ - 3
95
+ - 5
109
96
  version: 1.3.5
97
+ type: :development
98
+ version_requirements: *id006
110
99
  description: attr-translatable gem allows attributes of ActiveRecord to be translated.
111
- email:
100
+ email:
112
101
  - katsuya7s@gmail.com
113
102
  executables: []
103
+
114
104
  extensions: []
105
+
115
106
  extra_rdoc_files: []
116
- files:
107
+
108
+ files:
117
109
  - LICENSE
118
110
  - README.md
119
111
  - Rakefile
@@ -126,34 +118,35 @@ files:
126
118
  - lib/models/attr_translation.rb
127
119
  - spec/attr_translatable_spec.rb
128
120
  - spec/spec_helper.rb
121
+ has_rdoc: true
129
122
  homepage: https://github.com/katsuyan/attr-translatable
130
123
  licenses: []
124
+
131
125
  post_install_message:
132
126
  rdoc_options: []
133
- require_paths:
127
+
128
+ require_paths:
134
129
  - lib
135
- required_ruby_version: !ruby/object:Gem::Requirement
136
- none: false
137
- requirements:
138
- - - ! '>='
139
- - !ruby/object:Gem::Version
140
- version: '0'
141
- segments:
130
+ required_ruby_version: !ruby/object:Gem::Requirement
131
+ requirements:
132
+ - - ">="
133
+ - !ruby/object:Gem::Version
134
+ segments:
142
135
  - 0
143
- hash: -1703276739540596886
144
- required_rubygems_version: !ruby/object:Gem::Requirement
145
- none: false
146
- requirements:
147
- - - ! '>='
148
- - !ruby/object:Gem::Version
149
- version: '0'
150
- segments:
136
+ version: "0"
137
+ required_rubygems_version: !ruby/object:Gem::Requirement
138
+ requirements:
139
+ - - ">="
140
+ - !ruby/object:Gem::Version
141
+ segments:
151
142
  - 0
152
- hash: -1703276739540596886
143
+ version: "0"
153
144
  requirements: []
145
+
154
146
  rubyforge_project:
155
- rubygems_version: 1.8.24
147
+ rubygems_version: 1.3.6
156
148
  signing_key:
157
149
  specification_version: 3
158
150
  summary: attr-translatable gem allows attributes of ActiveRecord to be translated.
159
151
  test_files: []
152
+