active_record-translated 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.
- checksums.yaml +4 -4
- data/README.md +25 -8
- data/lib/active_record/translated/model.rb +8 -6
- data/lib/active_record/translated/version.rb +1 -1
- data/lib/active_record/translated.rb +1 -3
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: fee7618494b14c5e229293ffcd43a7bb6d195ff8c2093cd8b4dc6609528436e2
|
4
|
+
data.tar.gz: '0586e332ac2995c5094ca4d1e8c16cbf61f9db72a72c607e0905bc7442115fd6'
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: '089abbfe8e6a705d4d0812579f43aeb667aa9690d14f4b70313dc16f085052bea9f930a468d7fc00250f4f5e37b32048b20ac5826422ac1871f15925b88e24b2'
|
7
|
+
data.tar.gz: cbdc85d8ed2c6c011325ad34d7e6bf76dfd9f9651a06fc1dbeab1f008c921c1aa486ef6fe0a809a3a19391aa40e4d9a7a35c4a42e925c0a9ab1549039c5f5c81
|
data/README.md
CHANGED
@@ -192,16 +192,13 @@ Your translated model will automatically scope query results by the current loca
|
|
192
192
|
|
193
193
|
#### Find by primary key
|
194
194
|
|
195
|
-
|
195
|
+
This gem wont break the default #find method. So if you already now the ID, just fetch it with a normal #find:
|
196
196
|
|
197
197
|
```ruby
|
198
|
-
I18n.locale = :en # Ignored by #find when fetching a record by its
|
198
|
+
I18n.locale = :en # Ignored by #find when fetching a record by its primary key.
|
199
199
|
|
200
|
-
post = Post.find(
|
201
|
-
|
202
|
-
|
203
|
-
post.locale # => :en
|
204
|
-
post_es.locale #=> :es
|
200
|
+
post = Post.find(5) # Spanish translation
|
201
|
+
post.locale #=> :es
|
205
202
|
```
|
206
203
|
|
207
204
|
#### Find by record_id
|
@@ -212,7 +209,27 @@ You can also find a translated record by using a record_id as the argument. This
|
|
212
209
|
I18n.locale = :es
|
213
210
|
|
214
211
|
post = Post.find("0e048f11-0ad9-48f1-b493-36e1f01d7994")
|
215
|
-
|
212
|
+
post.locale #=> :es
|
213
|
+
```
|
214
|
+
|
215
|
+
#### Fetching collection of records
|
216
|
+
|
217
|
+
All query results are scoped by the current locale. Use `#unscoped_locale` to disable the locale scope.
|
218
|
+
|
219
|
+
```ruby
|
220
|
+
|
221
|
+
Post.create(title: "Foo bar", ..., locale: :es) # es
|
222
|
+
Post.create(title: "Foo bar", ..., locale: :es) # es
|
223
|
+
Post.create(title: "Foo bar", ..., locale: :pt) # pt
|
224
|
+
|
225
|
+
I18n.locale = :es
|
226
|
+
Post.count #=> :2
|
227
|
+
|
228
|
+
I18n.locale = :pt
|
229
|
+
Post.count #=> :1
|
230
|
+
|
231
|
+
# Returns all posts
|
232
|
+
Post.unscoped_locale.count #=> :3
|
216
233
|
```
|
217
234
|
|
218
235
|
License
|
@@ -11,9 +11,11 @@ module ActiveRecord
|
|
11
11
|
before_save :set_record_id
|
12
12
|
before_save :set_locale
|
13
13
|
|
14
|
-
has_many :translations,
|
15
|
-
|
16
|
-
|
14
|
+
has_many :translations, lambda {
|
15
|
+
unscoped_locale
|
16
|
+
}, class_name: "Post", foreign_key: :record_id, primary_key: :record_id
|
17
|
+
default_scope -> { where(locale: ActiveRecord::Translated.locale) }
|
18
|
+
scope :unscoped_locale, -> { unscope(where: :locale) }
|
17
19
|
|
18
20
|
class << self
|
19
21
|
def find(*args)
|
@@ -21,7 +23,7 @@ module ActiveRecord
|
|
21
23
|
|
22
24
|
return find_translated(id) if ActiveRecord::Translated.record_id?(id)
|
23
25
|
|
24
|
-
|
26
|
+
unscoped_locale.find(*args)
|
25
27
|
end
|
26
28
|
|
27
29
|
def exists?(id)
|
@@ -34,7 +36,7 @@ module ActiveRecord
|
|
34
36
|
private
|
35
37
|
|
36
38
|
def find_translated(record_id, allow_nil: false)
|
37
|
-
result =
|
39
|
+
result = where(record_id: record_id).first
|
38
40
|
return result if result
|
39
41
|
|
40
42
|
raise ActiveRecord::RecordNotFound unless allow_nil
|
@@ -42,7 +44,7 @@ module ActiveRecord
|
|
42
44
|
|
43
45
|
# rubocop:disable Rails/WhereExists
|
44
46
|
def exists_translated?(record_id)
|
45
|
-
where(record_id: record_id
|
47
|
+
translated.where(record_id: record_id).exists?
|
46
48
|
end
|
47
49
|
# rubocop:enable Rails/WhereExists
|
48
50
|
end
|
@@ -22,9 +22,7 @@ module ActiveRecord
|
|
22
22
|
end
|
23
23
|
|
24
24
|
def record_id?(string)
|
25
|
-
|
26
|
-
|
27
|
-
string.match(/^[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}$\z/)
|
25
|
+
string.is_a?(String) && string.match(/^[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}$\z/) ? true : false
|
28
26
|
end
|
29
27
|
|
30
28
|
def locale
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_record-translated
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Rasmus Kjellberg
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2022-11-
|
11
|
+
date: 2022-11-29 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: dry-configurable
|