awesome_hstore_translate 0.1.0 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +27 -6
- data/lib/awesome_hstore_translate/active_record.rb +2 -0
- data/lib/awesome_hstore_translate/active_record/act_as_translatable.rb +1 -0
- data/lib/awesome_hstore_translate/active_record/class_methods.rb +7 -0
- data/lib/awesome_hstore_translate/active_record/core.rb +22 -0
- data/lib/awesome_hstore_translate/active_record/instance_methods.rb +18 -6
- data/lib/awesome_hstore_translate/active_record/query_methods.rb +37 -0
- data/lib/awesome_hstore_translate/version.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 776affdef8d15b23476763a402444c5cd9d77f52
|
4
|
+
data.tar.gz: 119148870bf3a17cda07f86c017652c742345aae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f1c7928bd63ef520c96de9183905e2008a7140fd43644cb7499b1eec466c464555104dfba7b041a9e2fb4fdf77a59e31638dcbcb400ee68a730d092b8898c8e6
|
7
|
+
data.tar.gz: 6123dae82312fa213b1b4cc7160d02276846c281d48755537a3acdc579d83cdc8b923f3af462464ced517995a79b8b3ebf23b2873c452b29e34fac5bf90235ae
|
data/README.md
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# Awesome Hstore Translate
|
2
|
+
[![Gem Version](https://badge.fury.io/rb/awesome_hstore_translate.svg)](https://badge.fury.io/rb/awesome_hstore_translate)
|
3
|
+
|
2
4
|
This gem uses PostgreSQLs hstore datatype and ActiveRecord models to translate model data. It is based on the gem
|
3
|
-
[hstore_translate](https://github.com/Leadformance/hstore_translate) by Rob Worely.
|
5
|
+
[`hstore_translate`](https://github.com/Leadformance/hstore_translate) by Rob Worely.
|
4
6
|
|
5
7
|
- It's ready for Rails 5
|
6
8
|
- No extra columns or tables needed to operate
|
@@ -8,14 +10,18 @@ This gem uses PostgreSQLs hstore datatype and ActiveRecord models to translate m
|
|
8
10
|
- Everything is well tested
|
9
11
|
|
10
12
|
## Features
|
11
|
-
- [x] Attributes override / Raw attributes
|
12
|
-
- [x] Fallbacks
|
13
|
-
- [x] Language specific accessors
|
14
|
-
- [
|
15
|
-
|
13
|
+
- [x] `v0.1.0` Attributes override / Raw attributes
|
14
|
+
- [x] `v0.1.0` Fallbacks
|
15
|
+
- [x] `v0.1.0` Language specific accessors
|
16
|
+
- [x] `v0.2.0` Awesome Hstore Translate as drop in replace for [`hstore_translate`](https://github.com/Leadformance/hstore_translate)
|
17
|
+
- `with_[attr]_translation(str)` is not supported
|
18
|
+
- [x] `v0.2.0` Support record selection via ActiveRecord (e. g. `where`, `find_by`)
|
19
|
+
- maybe it's a bad idea to implement this
|
20
|
+
- [ ] `backlog` Support `friendly_id` (see `friendly_id-awesome_hstore` gem)
|
16
21
|
|
17
22
|
## Requirements
|
18
23
|
- ActiveRecord `>= 5`
|
24
|
+
- Please use [`hstore_translate`](https://github.com/Leadformance/hstore_translate), if you are on an older version.
|
19
25
|
- I18n
|
20
26
|
|
21
27
|
## Installation
|
@@ -129,6 +135,21 @@ p.title_en # => English title
|
|
129
135
|
p.title_de # => Deutscher Titel
|
130
136
|
```
|
131
137
|
|
138
|
+
### Find
|
139
|
+
`awesome_hstore_translate` patches ActiveRecord, so you can conviniently use `where` and `find_by` as you like.
|
140
|
+
```ruby
|
141
|
+
Page.create!(:title_en => 'English title', :title_de => 'Deutscher Titel')
|
142
|
+
Page.create!(:title_en => 'Another English title', :title_de => 'Noch ein Deutscher Titel')
|
143
|
+
|
144
|
+
Page.where(title: 'Another English title') # => Page
|
145
|
+
```
|
146
|
+
|
147
|
+
|
148
|
+
### Upgrade from [`hstore_translate`](https://github.com/Leadformance/hstore_translate)
|
149
|
+
1. Replace the [`hstore_translate`](https://github.com/Leadformance/hstore_translate) with `awesome_hstore_translate` in your Gemfile
|
150
|
+
1. Activate accessors, if you used the [`hstore_translate`](https://github.com/Leadformance/hstore_translate) accessors
|
151
|
+
1. Replace `with_[attr]_translation(str)` with equivalents (see "Support record selection via ActiveRecord" feature)
|
152
|
+
|
132
153
|
## Development
|
133
154
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
134
155
|
|
@@ -4,6 +4,8 @@ module AwesomeHstoreTranslate
|
|
4
4
|
autoload :ActAsTranslatable, 'awesome_hstore_translate/active_record/act_as_translatable'
|
5
5
|
autoload :Attributes, 'awesome_hstore_translate/active_record/attributes'
|
6
6
|
autoload :ClassMethods, 'awesome_hstore_translate/active_record/class_methods'
|
7
|
+
autoload :Core, 'awesome_hstore_translate/active_record/core'
|
7
8
|
autoload :InstanceMethods, 'awesome_hstore_translate/active_record/instance_methods'
|
9
|
+
autoload :QueryMethods, 'awesome_hstore_translate/active_record/query_methods'
|
8
10
|
end
|
9
11
|
end
|
@@ -24,6 +24,13 @@ module AwesomeHstoreTranslate
|
|
24
24
|
def toggle_fallback
|
25
25
|
translation_options[:fallbacks] = !translation_options[:fallbacks]
|
26
26
|
end
|
27
|
+
|
28
|
+
private
|
29
|
+
|
30
|
+
# Override the default relation methods in order to inject custom finder methods for hstore translations.
|
31
|
+
def relation
|
32
|
+
super.extending!(QueryMethods)
|
33
|
+
end
|
27
34
|
end
|
28
35
|
end
|
29
36
|
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
module Core
|
4
|
+
module ClassMethods
|
5
|
+
def find_by(*args)
|
6
|
+
attrs = args.first
|
7
|
+
if attrs.is_a?(Hash) && contains_translated_attributes(attrs)
|
8
|
+
where(attrs).limit(1).first
|
9
|
+
else
|
10
|
+
super(args)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
16
|
+
def contains_translated_attributes(attrs)
|
17
|
+
!(self.translated_attribute_names & attrs.keys).empty?
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -8,11 +8,13 @@ module AwesomeHstoreTranslate
|
|
8
8
|
locales << locale
|
9
9
|
locales += get_fallback_for_locale(locale) || [] if translation_options[:fallbacks]
|
10
10
|
|
11
|
-
translations =
|
11
|
+
translations = read_raw_attribute(attr)
|
12
12
|
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
if translations
|
14
|
+
locales.uniq.each do |cur|
|
15
|
+
if translations.has_key?(cur.to_s) && !translations[cur.to_s].empty?
|
16
|
+
return translations[cur.to_s]
|
17
|
+
end
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
@@ -20,7 +22,7 @@ module AwesomeHstoreTranslate
|
|
20
22
|
end
|
21
23
|
|
22
24
|
def read_raw_attribute(attr)
|
23
|
-
read_attribute(attr)
|
25
|
+
read_attribute(get_column_name(attr))
|
24
26
|
end
|
25
27
|
|
26
28
|
def write_translated_attribute(attr, value, locale= I18n.locale)
|
@@ -30,12 +32,22 @@ module AwesomeHstoreTranslate
|
|
30
32
|
end
|
31
33
|
|
32
34
|
def write_raw_attribute(attr, value)
|
33
|
-
write_attribute(attr, value)
|
35
|
+
write_attribute(get_column_name(attr), value)
|
34
36
|
end
|
35
37
|
|
36
38
|
def get_fallback_for_locale(locale)
|
37
39
|
I18n.fallbacks[locale] if I18n.respond_to?(:fallbacks)
|
38
40
|
end
|
41
|
+
|
42
|
+
private
|
43
|
+
|
44
|
+
def get_column_name(attr)
|
45
|
+
column_name = attr.to_s
|
46
|
+
# detect column from original hstore_translate
|
47
|
+
column_name << '_translations' if !has_attribute?(column_name) && has_attribute?("#{column_name}_translations")
|
48
|
+
|
49
|
+
column_name
|
50
|
+
end
|
39
51
|
end
|
40
52
|
end
|
41
53
|
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
module QueryMethods
|
4
|
+
class WhereChain < ::ActiveRecord::QueryMethods::WhereChain
|
5
|
+
end
|
6
|
+
|
7
|
+
def where(opts = :chain, *rest)
|
8
|
+
if opts == :chain
|
9
|
+
WhereChain.new(spawn)
|
10
|
+
elsif opts.blank?
|
11
|
+
return self
|
12
|
+
elsif opts.is_a?(Hash)
|
13
|
+
translated_attrs = translated_attributes(opts)
|
14
|
+
normal_attrs = opts.reject{ |key, _| translated_attrs.include? key}
|
15
|
+
query = spawn
|
16
|
+
query.where!(normal_attrs) unless normal_attrs.empty?
|
17
|
+
|
18
|
+
translated_attrs.each do |attribute|
|
19
|
+
if opts[attribute].is_a?(String)
|
20
|
+
query.where!("'#{opts[attribute]}' = any(avals(#{attribute}))")
|
21
|
+
else
|
22
|
+
super(opts, rest)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
return query
|
26
|
+
end
|
27
|
+
super(opts, rest)
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def translated_attributes(opts)
|
33
|
+
self.translated_attribute_names & opts.keys
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_hstore_translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Bühler
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-07-
|
12
|
+
date: 2016-07-30 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -161,7 +161,9 @@ files:
|
|
161
161
|
- lib/awesome_hstore_translate/active_record/act_as_translatable.rb
|
162
162
|
- lib/awesome_hstore_translate/active_record/attributes.rb
|
163
163
|
- lib/awesome_hstore_translate/active_record/class_methods.rb
|
164
|
+
- lib/awesome_hstore_translate/active_record/core.rb
|
164
165
|
- lib/awesome_hstore_translate/active_record/instance_methods.rb
|
166
|
+
- lib/awesome_hstore_translate/active_record/query_methods.rb
|
165
167
|
- lib/awesome_hstore_translate/version.rb
|
166
168
|
homepage: https://github.com/openscript/awesome_hstore_translate
|
167
169
|
licenses:
|