active_record-translated 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d359bbfb6e7b356977ec10452c42131aa680559399023ffcffd0980b625135ae
4
- data.tar.gz: 947a046dcfedab49d567160b1cc9240e1c7a49b11fca0b3bc2bb06ccf78e000b
3
+ metadata.gz: fee7618494b14c5e229293ffcd43a7bb6d195ff8c2093cd8b4dc6609528436e2
4
+ data.tar.gz: '0586e332ac2995c5094ca4d1e8c16cbf61f9db72a72c607e0905bc7442115fd6'
5
5
  SHA512:
6
- metadata.gz: a58f249814f5390e80e2a6e1637134515767c2f8c635374e183051d6c6f5ef580f758b1f78be9c51e40c771d93129df844542b01537ce20b69dd1caea243cbe1
7
- data.tar.gz: 176e32b5c73a13ee06df807d10d96acecd4bc9df7ee5f329133833d9e9f80d0377bb5277af6a472d672a3bcb05b2305e8c43a20ddba1c846a6538135f07916d7
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
- The first and most obvious way is to just query a translation by its primary key. If you already now the ID, just do a normal #find:
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 unique primary key.
198
+ I18n.locale = :en # Ignored by #find when fetching a record by its primary key.
199
199
 
200
- post = Post.find(4) # English translation
201
- post_es = Post.find(5) # Spanish translation
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
- post_es.locale #=> :es
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, -> { global }, class_name: "Post", foreign_key: :record_id, primary_key: :record_id
15
- default_scope { where(locale: ActiveRecord::Translated.locale) }
16
- scope :global, -> { unscope(where: :locale) }
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
- super(*args)
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 = find_by(record_id: record_id)
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, locale: ActiveRecord::Translated.locale).exists?
47
+ translated.where(record_id: record_id).exists?
46
48
  end
47
49
  # rubocop:enable Rails/WhereExists
48
50
  end
@@ -3,6 +3,6 @@
3
3
  # :nocov:
4
4
  module ActiveRecord
5
5
  module Translated
6
- VERSION = "0.1.1"
6
+ VERSION = "0.2.0"
7
7
  end
8
8
  end
@@ -22,9 +22,7 @@ module ActiveRecord
22
22
  end
23
23
 
24
24
  def record_id?(string)
25
- return false unless string.is_a?(String)
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.1.1
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-28 00:00:00.000000000 Z
11
+ date: 2022-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: dry-configurable