awesome_hstore_translate 0.1.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 +7 -0
- data/.gitignore +54 -0
- data/.travis.yml +5 -0
- data/CODE_OF_CONDUCT.md +49 -0
- data/Gemfile +2 -0
- data/LICENSE.txt +21 -0
- data/README.md +143 -0
- data/Rakefile +10 -0
- data/awesome_hstore_translate.gemspec +31 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/awesome_hstore_translate/active_record/accessors.rb +30 -0
- data/lib/awesome_hstore_translate/active_record/act_as_translatable.rb +54 -0
- data/lib/awesome_hstore_translate/active_record/attributes.rb +38 -0
- data/lib/awesome_hstore_translate/active_record/class_methods.rb +29 -0
- data/lib/awesome_hstore_translate/active_record/instance_methods.rb +41 -0
- data/lib/awesome_hstore_translate/active_record.rb +9 -0
- data/lib/awesome_hstore_translate/version.rb +3 -0
- data/lib/awesome_hstore_translate.rb +8 -0
- metadata +190 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: fe3efb120f94ec493d1060c558f02ab06cc623af
|
4
|
+
data.tar.gz: cf33e049416e9b841095eca3826f599f25618989
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: af5faab9487a7aa3c4597a601b94d5056a04c6bca14e488d90e8cf6c12fa80bdaa3262f61ed75f34d659402376b35f073e2822d609f568f2ed8303157a84e1a4
|
7
|
+
data.tar.gz: e39ba04a78f235157946d7794c53e4326977cca208154195d5924ab8be59e289212a15d8a93a03f85c7ee2886345c2fe44788ee240b8dbc3ac2ec90ae25ced9d
|
data/.gitignore
ADDED
@@ -0,0 +1,54 @@
|
|
1
|
+
/.bundle/
|
2
|
+
/.yardoc
|
3
|
+
/Gemfile.lock
|
4
|
+
/_yardoc/
|
5
|
+
/coverage/
|
6
|
+
/doc/
|
7
|
+
/pkg/
|
8
|
+
/spec/reports/
|
9
|
+
/tmp/
|
10
|
+
|
11
|
+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm
|
12
|
+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
|
13
|
+
|
14
|
+
# User-specific stuff:
|
15
|
+
.idea/workspace.xml
|
16
|
+
.idea/tasks.xml
|
17
|
+
.idea/dictionaries
|
18
|
+
.idea/vcs.xml
|
19
|
+
.idea/jsLibraryMappings.xml
|
20
|
+
|
21
|
+
# Sensitive or high-churn files:
|
22
|
+
.idea/dataSources.ids
|
23
|
+
.idea/dataSources.xml
|
24
|
+
.idea/dataSources.local.xml
|
25
|
+
.idea/sqlDataSources.xml
|
26
|
+
.idea/dynamic.xml
|
27
|
+
.idea/uiDesigner.xml
|
28
|
+
|
29
|
+
# Gradle:
|
30
|
+
.idea/gradle.xml
|
31
|
+
.idea/libraries
|
32
|
+
|
33
|
+
# Mongo Explorer plugin:
|
34
|
+
.idea/mongoSettings.xml
|
35
|
+
|
36
|
+
## File-based project format:
|
37
|
+
*.iws
|
38
|
+
|
39
|
+
## Plugin-specific files:
|
40
|
+
|
41
|
+
# IntelliJ
|
42
|
+
/out/
|
43
|
+
|
44
|
+
# mpeltonen/sbt-idea plugin
|
45
|
+
.idea_modules/
|
46
|
+
|
47
|
+
# JIRA plugin
|
48
|
+
atlassian-ide-plugin.xml
|
49
|
+
|
50
|
+
# Crashlytics plugin (for Android Studio and IntelliJ)
|
51
|
+
com_crashlytics_export_strings.xml
|
52
|
+
crashlytics.properties
|
53
|
+
crashlytics-build.properties
|
54
|
+
fabric.properties
|
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, and in the interest of
|
4
|
+
fostering an open and welcoming community, we pledge to respect all people who
|
5
|
+
contribute through reporting issues, posting feature requests, updating
|
6
|
+
documentation, submitting pull requests or patches, and other activities.
|
7
|
+
|
8
|
+
We are committed to making participation in this project a harassment-free
|
9
|
+
experience for everyone, regardless of level of experience, gender, gender
|
10
|
+
identity and expression, sexual orientation, disability, personal appearance,
|
11
|
+
body size, race, ethnicity, age, religion, or nationality.
|
12
|
+
|
13
|
+
Examples of unacceptable behavior by participants include:
|
14
|
+
|
15
|
+
* The use of sexualized language or imagery
|
16
|
+
* Personal attacks
|
17
|
+
* Trolling or insulting/derogatory comments
|
18
|
+
* Public or private harassment
|
19
|
+
* Publishing other's private information, such as physical or electronic
|
20
|
+
addresses, without explicit permission
|
21
|
+
* Other unethical or unprofessional conduct
|
22
|
+
|
23
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
24
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
25
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
26
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
27
|
+
threatening, offensive, or harmful.
|
28
|
+
|
29
|
+
By adopting this Code of Conduct, project maintainers commit themselves to
|
30
|
+
fairly and consistently applying these principles to every aspect of managing
|
31
|
+
this project. Project maintainers who do not follow or enforce the Code of
|
32
|
+
Conduct may be permanently removed from the project team.
|
33
|
+
|
34
|
+
This code of conduct applies both within project spaces and in public spaces
|
35
|
+
when an individual is representing the project or its community.
|
36
|
+
|
37
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
38
|
+
reported by contacting a project maintainer at nospam@example.com. All
|
39
|
+
complaints will be reviewed and investigated and will result in a response that
|
40
|
+
is deemed necessary and appropriate to the circumstances. Maintainers are
|
41
|
+
obligated to maintain confidentiality with regard to the reporter of an
|
42
|
+
incident.
|
43
|
+
|
44
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage],
|
45
|
+
version 1.3.0, available at
|
46
|
+
[http://contributor-covenant.org/version/1/3/0/][version]
|
47
|
+
|
48
|
+
[homepage]: http://contributor-covenant.org
|
49
|
+
[version]: http://contributor-covenant.org/version/1/3/0/
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2016 Robin Bühler
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,143 @@
|
|
1
|
+
# Awesome Hstore Translate
|
2
|
+
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.
|
4
|
+
|
5
|
+
- It's ready for Rails 5
|
6
|
+
- No extra columns or tables needed to operate
|
7
|
+
- Clean naming in the database model
|
8
|
+
- Everything is well tested
|
9
|
+
|
10
|
+
## Features
|
11
|
+
- [x] Attributes override / Raw attributes
|
12
|
+
- [x] Fallbacks
|
13
|
+
- [x] Language specific accessors
|
14
|
+
- [ ] Support instance selection (e. g. `where`, `find_by`)
|
15
|
+
- [ ] Support `friendly_id`
|
16
|
+
|
17
|
+
## Requirements
|
18
|
+
- ActiveRecord `>= 5`
|
19
|
+
- I18n
|
20
|
+
|
21
|
+
## Installation
|
22
|
+
Add this line to your application's Gemfile:
|
23
|
+
|
24
|
+
```ruby
|
25
|
+
gem 'awesome_hstore_translate'
|
26
|
+
```
|
27
|
+
|
28
|
+
And then execute:
|
29
|
+
|
30
|
+
$ bundle
|
31
|
+
|
32
|
+
Or install it yourself as:
|
33
|
+
|
34
|
+
$ gem install awesome_hstore_translate
|
35
|
+
|
36
|
+
## Usage
|
37
|
+
Use `translates` in your models, to define the attributes, which should be translateable:
|
38
|
+
```ruby
|
39
|
+
class Page < ActiveRecord::Base
|
40
|
+
translates :title, :content
|
41
|
+
end
|
42
|
+
```
|
43
|
+
Make sure that the datatype of this columns is `hstore`:
|
44
|
+
```ruby
|
45
|
+
class CreatePages < ActiveRecord::Migration
|
46
|
+
def change
|
47
|
+
create_table :pages do |t|
|
48
|
+
t.column :title, :hstore
|
49
|
+
t.column :content, :hstore
|
50
|
+
t.timestamps
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
```
|
55
|
+
|
56
|
+
Use the model attributes per locale:
|
57
|
+
```ruby
|
58
|
+
p = Page.first
|
59
|
+
|
60
|
+
I18n.locale = :en
|
61
|
+
p.title # => English title
|
62
|
+
|
63
|
+
I18n.locale = :de
|
64
|
+
p.title # => Deutscher Titel
|
65
|
+
|
66
|
+
I18n.with_locale :en do
|
67
|
+
p.title # => English title
|
68
|
+
end
|
69
|
+
```
|
70
|
+
|
71
|
+
The raw data is available via the suffix `_raw`:
|
72
|
+
```ruby
|
73
|
+
p = Page.new(:title_raw => {'en' => 'English title', 'de' => 'Deutscher Titel'})
|
74
|
+
|
75
|
+
p.title_raw # => {'en' => 'English title', 'de' => 'Deutscher Titel'}
|
76
|
+
```
|
77
|
+
|
78
|
+
|
79
|
+
### Fallbacks
|
80
|
+
To enable fallbacks you can set `I18n.fallbacks` to `true` or enable it manually in the model:
|
81
|
+
```ruby
|
82
|
+
class Page < ActiveRecord::Base
|
83
|
+
translates :title, :content, fallbacks: true
|
84
|
+
end
|
85
|
+
```
|
86
|
+
|
87
|
+
Set `I18n.default_locale` or `I18n.fallbacks` to define the fallback:
|
88
|
+
```ruby
|
89
|
+
I18n.fallbacks.map(:en => :de) # => if :en is nil or empty, it will use :de
|
90
|
+
|
91
|
+
p = Page.new(:title_raw => {'de' => 'Deutscher Titel'})
|
92
|
+
|
93
|
+
I18n.with_locale :en do
|
94
|
+
p.title # => Deutscher Titel
|
95
|
+
end
|
96
|
+
```
|
97
|
+
|
98
|
+
It's possible to activate (`with_fallbacks`) or deactivate (`without_fallbacks`) fallbacks for a block execution:
|
99
|
+
```ruby
|
100
|
+
p = PageWithoutFallbacks.new(:title_raw => {'de' => 'Deutscher Titel'})
|
101
|
+
|
102
|
+
I18n.with_locale(:en) do
|
103
|
+
PageWithoutFallbacks.with_fallbacks do
|
104
|
+
assert_equal('Deutscher Titel', p.title)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
```
|
108
|
+
|
109
|
+
### Accessors
|
110
|
+
Convenience accessors can be enabled via the model descriptor:
|
111
|
+
```ruby
|
112
|
+
class Page < ActiveRecord::Base
|
113
|
+
translates :title, :content, accessors: [:de, :en]
|
114
|
+
end
|
115
|
+
```
|
116
|
+
|
117
|
+
It's also make sense to activate the accessors for all available locales:
|
118
|
+
```ruby
|
119
|
+
class Page < ActiveRecord::Base
|
120
|
+
translates :title, :content, accessors: I18n.available_locales
|
121
|
+
end
|
122
|
+
```
|
123
|
+
|
124
|
+
Now locale-suffixed accessors can be used:
|
125
|
+
```ruby
|
126
|
+
p = Page.create!(:title_en => 'English title', :title_de => 'Deutscher Titel')
|
127
|
+
|
128
|
+
p.title_en # => English title
|
129
|
+
p.title_de # => Deutscher Titel
|
130
|
+
```
|
131
|
+
|
132
|
+
## Development
|
133
|
+
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
|
+
|
135
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
136
|
+
|
137
|
+
## Contributing
|
138
|
+
Bug reports and pull requests are welcome on [GitHub](https://github.com/openscript/awesome_hstore_translate). This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
139
|
+
|
140
|
+
|
141
|
+
## License
|
142
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
143
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'awesome_hstore_translate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'awesome_hstore_translate'
|
8
|
+
spec.version = AwesomeHstoreTranslate::VERSION
|
9
|
+
spec.authors = ['Robin Bühler', 'Rob Worley']
|
10
|
+
spec.email = ['public@openscript.ch']
|
11
|
+
|
12
|
+
spec.summary = 'Using PostgreSQLs hstore datatype to provide ActiveRecord models data translation.'
|
13
|
+
spec.description = 'This gem uses PostgreSQLs hstore datatype and ActiveRecord models to translate model data. It is based on the gem hstore_translate by Rob Worely.'
|
14
|
+
spec.homepage = 'https://github.com/openscript/awesome_hstore_translate'
|
15
|
+
spec.license = 'MIT'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features|\.idea)/}) }
|
18
|
+
spec.bindir = 'bin'
|
19
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ['lib']
|
21
|
+
|
22
|
+
spec.add_dependency 'activerecord', '>= 5.0', '< 6'
|
23
|
+
spec.add_dependency 'activemodel', '>= 5.0', '< 6'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.12'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'minitest', '~> 5.0'
|
28
|
+
spec.add_development_dependency 'database_cleaner', '~> 1.5'
|
29
|
+
spec.add_development_dependency 'pg', '~> 0.18'
|
30
|
+
spec.add_development_dependency 'simplecov', '~> 0.12'
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "awesome_hstore_translate"
|
5
|
+
|
6
|
+
# You can add fixtures and/or initialization code here to make experimenting
|
7
|
+
# with your gem easier. You can also use a different console, if you like.
|
8
|
+
|
9
|
+
# (If you use this, don't forget to add pry to your Gemfile!)
|
10
|
+
# require "pry"
|
11
|
+
# Pry.start
|
12
|
+
|
13
|
+
require "irb"
|
14
|
+
IRB.start
|
data/bin/setup
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
module Accessors
|
4
|
+
protected
|
5
|
+
|
6
|
+
def define_accessors(attr)
|
7
|
+
translation_options[:accessors].each do |locale|
|
8
|
+
define_reader_accessor(attr, locale)
|
9
|
+
define_writer_accessor(attr, locale)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def define_reader_accessor(attr, locale)
|
14
|
+
define_method get_accessor_name(attr, locale) do
|
15
|
+
read_translated_attribute(attr, locale)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def define_writer_accessor(attr, locale)
|
20
|
+
define_method "#{get_accessor_name(attr, locale)}=" do |value|
|
21
|
+
write_translated_attribute(attr, value, locale)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def get_accessor_name(attr, locale)
|
26
|
+
"#{attr}_#{locale.to_s.underscore}"
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
module ActAsTranslatable
|
4
|
+
def translates(*attr_names)
|
5
|
+
options = attr_names.extract_options!
|
6
|
+
|
7
|
+
bootstrap(options, attr_names)
|
8
|
+
|
9
|
+
if attr_names.present?
|
10
|
+
enable_attributes(attr_names)
|
11
|
+
enable_accessors(attr_names) if options[:accessors]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
protected
|
16
|
+
|
17
|
+
def enable_attributes(attr_names)
|
18
|
+
extend Attributes
|
19
|
+
attr_names.each do |attr_name|
|
20
|
+
define_attributes(attr_name)
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def enable_accessors(attr_names)
|
25
|
+
extend Accessors
|
26
|
+
attr_names.each do |attr_name|
|
27
|
+
define_accessors(attr_name)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def apply_options(options)
|
32
|
+
fallbacks = I18n.respond_to?(:fallbacks) ? I18n.fallbacks : true
|
33
|
+
options[:fallbacks] = fallbacks unless options.include?(:fallbacks)
|
34
|
+
options[:accessors] = false unless options.include?(:accessors)
|
35
|
+
|
36
|
+
class_attribute :translation_options
|
37
|
+
self.translation_options = options
|
38
|
+
end
|
39
|
+
|
40
|
+
def expose_translated_attrs(attr_names)
|
41
|
+
class_attribute :translated_attribute_names
|
42
|
+
self.translated_attribute_names = attr_names
|
43
|
+
end
|
44
|
+
|
45
|
+
def bootstrap(options, attr_names)
|
46
|
+
apply_options(options)
|
47
|
+
expose_translated_attrs(attr_names)
|
48
|
+
|
49
|
+
include InstanceMethods
|
50
|
+
extend ClassMethods
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
module Attributes
|
4
|
+
protected
|
5
|
+
|
6
|
+
def define_attributes(attr)
|
7
|
+
define_reader_attribute(attr)
|
8
|
+
define_writer_attribute(attr)
|
9
|
+
define_raw_reader_attribute(attr)
|
10
|
+
define_raw_writer_attribute(attr)
|
11
|
+
end
|
12
|
+
|
13
|
+
def define_reader_attribute(attr)
|
14
|
+
define_method(attr) do
|
15
|
+
read_translated_attribute(attr)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
def define_raw_reader_attribute(attr)
|
20
|
+
define_method(:"#{attr}_raw") do
|
21
|
+
read_raw_attribute(attr)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def define_writer_attribute(attr)
|
26
|
+
define_method(:"#{attr}=") do |value|
|
27
|
+
write_translated_attribute(attr, value)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def define_raw_writer_attribute(attr)
|
32
|
+
define_method(:"#{attr}_raw=") do |value|
|
33
|
+
write_raw_attribute(attr, value)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
module ClassMethods
|
4
|
+
def translates?
|
5
|
+
included_modules.include?(InstanceMethods)
|
6
|
+
end
|
7
|
+
|
8
|
+
def without_fallbacks(&block)
|
9
|
+
before_state = translation_options[:fallbacks]
|
10
|
+
toggle_fallback if translation_options[:fallbacks]
|
11
|
+
yield block
|
12
|
+
translation_options[:fallbacks] = before_state
|
13
|
+
end
|
14
|
+
|
15
|
+
def with_fallbacks(&block)
|
16
|
+
before_state = translation_options[:fallbacks]
|
17
|
+
toggle_fallback unless translation_options[:fallbacks]
|
18
|
+
yield block
|
19
|
+
translation_options[:fallbacks] = before_state
|
20
|
+
end
|
21
|
+
|
22
|
+
protected
|
23
|
+
|
24
|
+
def toggle_fallback
|
25
|
+
translation_options[:fallbacks] = !translation_options[:fallbacks]
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
module InstanceMethods
|
4
|
+
protected
|
5
|
+
|
6
|
+
def read_translated_attribute(attr, locale = I18n.locale)
|
7
|
+
locales = []
|
8
|
+
locales << locale
|
9
|
+
locales += get_fallback_for_locale(locale) || [] if translation_options[:fallbacks]
|
10
|
+
|
11
|
+
translations = read_attribute(attr)
|
12
|
+
|
13
|
+
locales.uniq.each do |cur|
|
14
|
+
if translations.has_key?(cur.to_s) && !translations[cur.to_s].empty?
|
15
|
+
return translations[cur.to_s]
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
nil
|
20
|
+
end
|
21
|
+
|
22
|
+
def read_raw_attribute(attr)
|
23
|
+
read_attribute(attr)
|
24
|
+
end
|
25
|
+
|
26
|
+
def write_translated_attribute(attr, value, locale= I18n.locale)
|
27
|
+
translations = read_raw_attribute(attr) || {}
|
28
|
+
translations[locale] = value
|
29
|
+
write_raw_attribute(attr, translations)
|
30
|
+
end
|
31
|
+
|
32
|
+
def write_raw_attribute(attr, value)
|
33
|
+
write_attribute(attr, value)
|
34
|
+
end
|
35
|
+
|
36
|
+
def get_fallback_for_locale(locale)
|
37
|
+
I18n.fallbacks[locale] if I18n.respond_to?(:fallbacks)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
module AwesomeHstoreTranslate
|
2
|
+
module ActiveRecord
|
3
|
+
autoload :Accessors, 'awesome_hstore_translate/active_record/accessors'
|
4
|
+
autoload :ActAsTranslatable, 'awesome_hstore_translate/active_record/act_as_translatable'
|
5
|
+
autoload :Attributes, 'awesome_hstore_translate/active_record/attributes'
|
6
|
+
autoload :ClassMethods, 'awesome_hstore_translate/active_record/class_methods'
|
7
|
+
autoload :InstanceMethods, 'awesome_hstore_translate/active_record/instance_methods'
|
8
|
+
end
|
9
|
+
end
|
metadata
ADDED
@@ -0,0 +1,190 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: awesome_hstore_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Robin Bühler
|
8
|
+
- Rob Worley
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-07-20 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activerecord
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
requirements:
|
18
|
+
- - ">="
|
19
|
+
- !ruby/object:Gem::Version
|
20
|
+
version: '5.0'
|
21
|
+
- - "<"
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: '6'
|
24
|
+
type: :runtime
|
25
|
+
prerelease: false
|
26
|
+
version_requirements: !ruby/object:Gem::Requirement
|
27
|
+
requirements:
|
28
|
+
- - ">="
|
29
|
+
- !ruby/object:Gem::Version
|
30
|
+
version: '5.0'
|
31
|
+
- - "<"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '6'
|
34
|
+
- !ruby/object:Gem::Dependency
|
35
|
+
name: activemodel
|
36
|
+
requirement: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5.0'
|
41
|
+
- - "<"
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '6'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: !ruby/object:Gem::Requirement
|
47
|
+
requirements:
|
48
|
+
- - ">="
|
49
|
+
- !ruby/object:Gem::Version
|
50
|
+
version: '5.0'
|
51
|
+
- - "<"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '6'
|
54
|
+
- !ruby/object:Gem::Dependency
|
55
|
+
name: bundler
|
56
|
+
requirement: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.12'
|
61
|
+
type: :development
|
62
|
+
prerelease: false
|
63
|
+
version_requirements: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.12'
|
68
|
+
- !ruby/object:Gem::Dependency
|
69
|
+
name: rake
|
70
|
+
requirement: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '10.0'
|
75
|
+
type: :development
|
76
|
+
prerelease: false
|
77
|
+
version_requirements: !ruby/object:Gem::Requirement
|
78
|
+
requirements:
|
79
|
+
- - "~>"
|
80
|
+
- !ruby/object:Gem::Version
|
81
|
+
version: '10.0'
|
82
|
+
- !ruby/object:Gem::Dependency
|
83
|
+
name: minitest
|
84
|
+
requirement: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - "~>"
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '5.0'
|
89
|
+
type: :development
|
90
|
+
prerelease: false
|
91
|
+
version_requirements: !ruby/object:Gem::Requirement
|
92
|
+
requirements:
|
93
|
+
- - "~>"
|
94
|
+
- !ruby/object:Gem::Version
|
95
|
+
version: '5.0'
|
96
|
+
- !ruby/object:Gem::Dependency
|
97
|
+
name: database_cleaner
|
98
|
+
requirement: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - "~>"
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '1.5'
|
103
|
+
type: :development
|
104
|
+
prerelease: false
|
105
|
+
version_requirements: !ruby/object:Gem::Requirement
|
106
|
+
requirements:
|
107
|
+
- - "~>"
|
108
|
+
- !ruby/object:Gem::Version
|
109
|
+
version: '1.5'
|
110
|
+
- !ruby/object:Gem::Dependency
|
111
|
+
name: pg
|
112
|
+
requirement: !ruby/object:Gem::Requirement
|
113
|
+
requirements:
|
114
|
+
- - "~>"
|
115
|
+
- !ruby/object:Gem::Version
|
116
|
+
version: '0.18'
|
117
|
+
type: :development
|
118
|
+
prerelease: false
|
119
|
+
version_requirements: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - "~>"
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0.18'
|
124
|
+
- !ruby/object:Gem::Dependency
|
125
|
+
name: simplecov
|
126
|
+
requirement: !ruby/object:Gem::Requirement
|
127
|
+
requirements:
|
128
|
+
- - "~>"
|
129
|
+
- !ruby/object:Gem::Version
|
130
|
+
version: '0.12'
|
131
|
+
type: :development
|
132
|
+
prerelease: false
|
133
|
+
version_requirements: !ruby/object:Gem::Requirement
|
134
|
+
requirements:
|
135
|
+
- - "~>"
|
136
|
+
- !ruby/object:Gem::Version
|
137
|
+
version: '0.12'
|
138
|
+
description: This gem uses PostgreSQLs hstore datatype and ActiveRecord models to
|
139
|
+
translate model data. It is based on the gem hstore_translate by Rob Worely.
|
140
|
+
email:
|
141
|
+
- public@openscript.ch
|
142
|
+
executables:
|
143
|
+
- console
|
144
|
+
- setup
|
145
|
+
extensions: []
|
146
|
+
extra_rdoc_files: []
|
147
|
+
files:
|
148
|
+
- ".gitignore"
|
149
|
+
- ".travis.yml"
|
150
|
+
- CODE_OF_CONDUCT.md
|
151
|
+
- Gemfile
|
152
|
+
- LICENSE.txt
|
153
|
+
- README.md
|
154
|
+
- Rakefile
|
155
|
+
- awesome_hstore_translate.gemspec
|
156
|
+
- bin/console
|
157
|
+
- bin/setup
|
158
|
+
- lib/awesome_hstore_translate.rb
|
159
|
+
- lib/awesome_hstore_translate/active_record.rb
|
160
|
+
- lib/awesome_hstore_translate/active_record/accessors.rb
|
161
|
+
- lib/awesome_hstore_translate/active_record/act_as_translatable.rb
|
162
|
+
- lib/awesome_hstore_translate/active_record/attributes.rb
|
163
|
+
- lib/awesome_hstore_translate/active_record/class_methods.rb
|
164
|
+
- lib/awesome_hstore_translate/active_record/instance_methods.rb
|
165
|
+
- lib/awesome_hstore_translate/version.rb
|
166
|
+
homepage: https://github.com/openscript/awesome_hstore_translate
|
167
|
+
licenses:
|
168
|
+
- MIT
|
169
|
+
metadata: {}
|
170
|
+
post_install_message:
|
171
|
+
rdoc_options: []
|
172
|
+
require_paths:
|
173
|
+
- lib
|
174
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
175
|
+
requirements:
|
176
|
+
- - ">="
|
177
|
+
- !ruby/object:Gem::Version
|
178
|
+
version: '0'
|
179
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
180
|
+
requirements:
|
181
|
+
- - ">="
|
182
|
+
- !ruby/object:Gem::Version
|
183
|
+
version: '0'
|
184
|
+
requirements: []
|
185
|
+
rubyforge_project:
|
186
|
+
rubygems_version: 2.5.1
|
187
|
+
signing_key:
|
188
|
+
specification_version: 4
|
189
|
+
summary: Using PostgreSQLs hstore datatype to provide ActiveRecord models data translation.
|
190
|
+
test_files: []
|