translateable 0.1.2 → 0.1.3

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
  SHA1:
3
- metadata.gz: 1568846c469cec338d192c62938cbe251ca177b7
4
- data.tar.gz: 4c6e9f4d15edbdd175ad0fb3b8fa303c9e07db0d
3
+ metadata.gz: 74e5653f87bacd831d822678ab07d712e528312a
4
+ data.tar.gz: a89a68ddbbf4fc1477aab71edd138b10a5d8448b
5
5
  SHA512:
6
- metadata.gz: 30508916028a562c23edd5a58d48b0a3faa3bff3859fc8769ceb5ee14ec0c59f7418cdeae2142fff895d00b66505ca14cfa2de066e496e7a387721082b5fdf25
7
- data.tar.gz: e98d9b39fc4655a43b071282ee8c02e2bd7f39e6eb9bd5c048e2bc4e3ef091d80b5044ce9c8db845857fed11a0993f1edda1017d021c141e503f112e7a48ada5
6
+ metadata.gz: 546f7856b6fb230c8dbc606efee2aa077e1fcdbd193413c4c48921ad67411f71cb9e5cc95ae148d0cd6fd90d7b7bd136eaa4df745368e99e59d299e3758e31a2
7
+ data.tar.gz: 52c05cf36d6e1610c4964483ff90f21a7e0fe3c5163b857b87d54ba8f9253d73515171034a628936c13e1a248cc89bc4244d4cb144efb33387fd8acb8d895c62
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  # Translateable
2
2
 
3
3
  [![Build Status](https://travis-ci.org/olegantonyan/translateable.svg)](https://travis-ci.org/olegantonyan/translateable)
4
+ [![Gem Version](https://badge.fury.io/rb/translateable.svg)](https://badge.fury.io/rb/translateable)
4
5
 
5
- Allows you to store text data in multiple languages with your ActiveRecord models. Similar to [globalize](https://github.com/globalize/globalize), but have a few differences:
6
+ Allows you to store text data in multiple languages with your ActiveRecord models. Similar to [globalize](https://github.com/globalize/globalize), but with a few differences:
6
7
 
7
8
  1. Works with Rails 5
8
9
  2. Uses single field in the table. No additional tables required to store translated data. PostgreSQL 9.4 is required to do this (JSONB)
9
- 3. Provides easy integration with forms using nested attributes so you can create records with multiple translations in one form. Together with [nested_form_fields](https://github.com/ncri/nested_form_fields) you can dynamically add/delete/update tranlations without a single line of JavaScript.
10
+ 3. Provides easy integration with forms using nested attributes so you can create records with multiple translations in one form. Together with [nested_form_fields](https://github.com/ncri/nested_form_fields) you can dynamically add/delete/update translations without a single line of JavaScript.
10
11
 
11
12
  ```ruby
12
13
  I18n.locale = :en
@@ -21,7 +22,7 @@ I18n.locale = :en
21
22
  post.title #=> hello
22
23
  ```
23
24
 
24
- It adds very thin abstraction layer on top of JSONB field. All data is stored in a simple JSON structure: `{ "locale_name": "data" }`. JSONB can be indexed (this is a main reason why use it instead of just JSON, which is available in previous postgres versions).
25
+ It adds very thin abstraction layer on top of JSONB field. All data is stored in a simple JSON structure: `{ "locale_name": "data" }`. JSONB can be indexed (this is the main reason to use it instead of just JSON, available in earlier Postgres versions).
25
26
 
26
27
  ## Requirements
27
28
 
@@ -80,7 +81,16 @@ You can pass multiple attributes:
80
81
  translateable :title, :body
81
82
  ```
82
83
 
83
- If there is no translation for a selected locale, than `I18n.default_locale` will be used. If there is no translation for `I18n.default_locale`, than first available will be used.
84
+ If there is no translation for a selected locale, than `I18n.default_locale` will be used. If there is no translation for `I18n.default_locale`, than the first available ​one will be used. You can override this behavior with `strict` option, in this case you'll get `nil` if there is no translation for the selected locale:
85
+ ```ruby
86
+ I18n.locale = :en
87
+ post = Post.create(title: 'hello')
88
+ post.title #=> hello
89
+
90
+ I18n.locale = :ru
91
+ post.title #=> hello
92
+ post.title(strict: true) #=> nil
93
+ ```
84
94
 
85
95
  You can assign all locales data as a hash at once:
86
96
  ```ruby
@@ -129,11 +139,11 @@ end
129
139
  # `translateable_permitted_attributes` method provides strong_params for all translateable attributes
130
140
  # for example with `title` attribute those will be: `title_translateable_attributes: [:locale, :data, :_destroy]`
131
141
  ```
132
- Now you can add/delete/update `title` attribute value in different languages via single form.
142
+ Now you can add/delete/update `title` attribute value in different languages via a single form.
133
143
 
134
144
  ### Migration
135
145
 
136
- Attributes must exist with `JSONB` type in database, so create a migration:
146
+ Attributes must exist with `JSONB` type in a database, so create a migration:
137
147
  ```ruby
138
148
  class AddTitleToPosts < ActiveRecord::Migration
139
149
  def change
@@ -142,11 +152,11 @@ class AddTitleToPosts < ActiveRecord::Migration
142
152
  end
143
153
  ```
144
154
 
145
- If you already have a data and want to migrate it to new translateable structure, use provided generator:
155
+ If you already have data and you want to migrate it to a new translateable structure, use ​a generator provided:
146
156
  ```
147
157
  bin/rails generate translateable:migration posts title
148
158
  ```
149
- This will create a reversible migration for data in `title` field of the `posts` table. By default, existent data will be moved into `I18n.default_locale`. If you want to use another locale, provide it as third argument:
159
+ This will create a reversible migration for data in `title` field of the `posts` table. By default, the existent data will be moved into `I18n.default_locale`. If you want to use another locale, provide it as a third argument:
150
160
  ```
151
161
  bin/rails generate translateable:migration posts title ru
152
162
  ```
@@ -204,20 +214,19 @@ module JsonbQuerable
204
214
  if case_sens
205
215
  where("EXISTS (SELECT 1 FROM jsonb_each_text(#{ta}) j WHERE j.value LIKE ?)", "%#{value}%")
206
216
  else
207
- q = "%#{Unicode.downcase(value.to_s)}%" # Unicode came from 'unicode' gem https://github.com/blackwinter/unicode
208
- where("EXISTS (SELECT 1 FROM jsonb_each_text(#{ta}) j WHERE lower(j.value) LIKE ?)", q)
217
+ where("EXISTS (SELECT 1 FROM jsonb_each_text(#{ta}) j WHERE lower(j.value) LIKE lower(?))", "%#{value}%")
209
218
  end
210
219
  }
211
220
  end
212
221
  end
213
222
  ```
214
223
 
215
- Refer to [postgres documentation](http://www.postgresql.org/docs/9.4/static/functions-json.html).
224
+ Refer to the [Postgres documentation](http://www.postgresql.org/docs/9.4/static/functions-json.html).
216
225
 
217
226
  ## TODO
218
227
 
219
228
  - Add options (fallback locales lookup behavior maybe?)
220
- - More clever database management for testing (temp schema or so)
229
+ - More clever database management for testing (temp schema or similar)
221
230
 
222
231
  ## Development
223
232
 
data/lib/translateable.rb CHANGED
@@ -21,7 +21,7 @@ module Translateable
21
21
  def translateable_sanity_check(attr)
22
22
  attr = attr.to_s
23
23
  raise ArgumentError, "no such column '#{attr}' in '#{name}' model" unless column_names.include?(attr)
24
- raise ArgumentError, "'#{attr}' column must be of JSONB type" unless columns_hash[attr].type.to_s.casecmp('jsonb') == 0
24
+ raise ArgumentError, "'#{attr}' column must be of JSONB type" unless columns_hash[attr].type.to_s.casecmp('jsonb').zero?
25
25
  end
26
26
 
27
27
  def define_translateable_strong_params(*attrs)
@@ -51,9 +51,9 @@ module Translateable
51
51
  end
52
52
  end
53
53
 
54
- define_method(attr) do
54
+ define_method(attr) do |**args|
55
55
  value = send("#{attr}_fetch_translateable")
56
- value[I18n.locale] || value[I18n.default_locale] || value.values.first
56
+ value[I18n.locale] || (value[I18n.default_locale] unless args[:strict]) || (value.values.first unless args[:strict])
57
57
  end
58
58
 
59
59
  define_method("#{attr}=") do |arg|
@@ -1,3 +1,3 @@
1
1
  module Translateable
2
- VERSION = '0.1.2'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: translateable
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oleg Antonyan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-04-01 00:00:00.000000000 Z
11
+ date: 2016-09-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler