armot 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -5,9 +5,31 @@ module Armot
5
5
  make_it_armot! unless included_modules.include?(InstanceMethods)
6
6
 
7
7
  attributes.each do |attribute|
8
+ self.class.instance_eval do
9
+ define_method "find_by_#{attribute}" do |value|
10
+ trans = I18n::Backend::ActiveRecord::Translation.find_by_locale_and_value(I18n.locale, value.to_yaml)
11
+ return send("where", {:"#{attribute}" => value}).first if trans.nil?
12
+
13
+ find trans.key.split("_").last
14
+ end
15
+
16
+ define_method "find_by_#{attribute}!" do |value|
17
+ trans = I18n::Backend::ActiveRecord::Translation.find_by_locale_and_value(I18n.locale, value.to_yaml)
18
+
19
+ if trans.nil?
20
+ original = send("where", {:"#{attribute}" => value}).first
21
+ raise ActiveRecord::RecordNotFound if original.nil?
22
+ original
23
+ else
24
+ find trans.key.split("_").last
25
+ end
26
+ end
27
+ end
28
+
8
29
  # attribute setter
9
30
  define_method "#{attribute}=" do |value|
10
31
  armot_attributes[I18n.locale][attribute] = value
32
+ I18n.backend.reload!
11
33
  end
12
34
 
13
35
  # attribute getter
@@ -1,3 +1,3 @@
1
1
  module Armot
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
@@ -99,4 +99,78 @@ class ArmotTest < ActiveSupport::TestCase
99
99
 
100
100
  assert_equal "original title", Post.first.title
101
101
  end
102
+
103
+ test "should find by translated title in database as a translation" do
104
+ post = Post.first
105
+ I18n.locale = :ca
106
+ post.title = "Catalan title"
107
+
108
+ I18n.locale = :en
109
+ post.title = "English title"
110
+ post.save!
111
+
112
+ I18n.locale = :ca
113
+ foo = Post.find_by_title "Catalan title"
114
+ assert_not_equal nil, foo
115
+ assert_equal "Catalan title", foo.title
116
+
117
+ foo = Post.find_by_title! "Catalan title"
118
+ assert_equal "Catalan title", foo.title
119
+ end
120
+
121
+ test "should not find a translation in database that does not match the current locale" do
122
+ post = Post.first
123
+ I18n.locale = :ca
124
+ post.title = "Catalan title"
125
+
126
+ I18n.locale = :en
127
+ post.title = "English title"
128
+ post.save!
129
+
130
+ foo = Post.find_by_title "Catalan title"
131
+ assert_equal nil, foo
132
+ end
133
+
134
+ test "should raise exception with bang version" do
135
+ assert_raise(ActiveRecord::RecordNotFound) do
136
+ Post.find_by_title! "Non existant"
137
+ end
138
+ end
139
+
140
+ test "should find by translated title without translations" do
141
+ post = Post.first
142
+ post[:title] = "Eng title"
143
+ post.save!
144
+
145
+ I18n::Backend::ActiveRecord::Translation.delete_all
146
+
147
+ foo = Post.find_by_title "Eng title"
148
+ assert_not_equal nil, foo
149
+ assert_nothing_raised do
150
+ foo = Post.find_by_title! "Eng title"
151
+ end
152
+ end
153
+
154
+ test "should return nil when no translations and no match" do
155
+ post = Post.first
156
+ post[:title] = "Eng title"
157
+ post.save!
158
+
159
+ I18n::Backend::ActiveRecord::Translation.delete_all
160
+
161
+ foo = Post.find_by_title "Wrong title"
162
+ assert_equal nil, foo
163
+ end
164
+
165
+ test "should raise an exception when no translationts and no match, with a bang" do
166
+ post = Post.first
167
+ post[:title] = "Eng title"
168
+ post.save!
169
+
170
+ I18n::Backend::ActiveRecord::Translation.delete_all
171
+
172
+ assert_raise(ActiveRecord::RecordNotFound) do
173
+ Post.find_by_title! "Non existant"
174
+ end
175
+ end
102
176
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: armot
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Roger Campos
@@ -15,8 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2011-03-14 00:00:00 +01:00
19
- default_executable:
18
+ date: 2011-07-19 00:00:00 Z
20
19
  dependencies:
21
20
  - !ruby/object:Gem::Dependency
22
21
  name: i18n-active_record
@@ -119,7 +118,6 @@ files:
119
118
  - test/puret_migration_test.rb
120
119
  - test/schema.rb
121
120
  - test/test_helper.rb
122
- has_rdoc: true
123
121
  homepage: https://github.com/rogercampos/armot
124
122
  licenses: []
125
123
 
@@ -149,7 +147,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
149
147
  requirements: []
150
148
 
151
149
  rubyforge_project: armot
152
- rubygems_version: 1.4.2
150
+ rubygems_version: 1.8.5
153
151
  signing_key:
154
152
  specification_version: 3
155
153
  summary: translation support for your models with an I18n active-record backend