ar_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 +9 -0
- data/.rspec +2 -0
- data/.ruby-version +1 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README.md +77 -0
- data/Rakefile +2 -0
- data/ar_translate.gemspec +27 -0
- data/circle.yml +9 -0
- data/lib/ar_translate.rb +11 -0
- data/lib/ar_translate/error.rb +4 -0
- data/lib/ar_translate/translated_column.rb +93 -0
- data/lib/ar_translate/version.rb +3 -0
- metadata +141 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: e8666d7846ba61edb998c0d30aef7dd695b5c10e
|
4
|
+
data.tar.gz: 7647b7eef464793712146ecb9d4f231d97797b69
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fc7bf2b4429dca5dd301cb3d4c6a39d6b8ca2e4c01efb0acf707ec0ce186678483cd2176da308ea0e0cc1c8e296d2a2553497665ee6852a3c9b04ddaff269b9d
|
7
|
+
data.tar.gz: 9739ca357a7cac1db0b61b54fc4729e37d9b284597d65597a24fe8408eb5736e48469efc254c6ba0fe644906ee4348b97b7fd852d3303e6be537755fcc1c939e
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.ruby-version
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
2.2.2
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2015 ad2games GmbH
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,77 @@
|
|
1
|
+
# ArTranslate
|
2
|
+
|
3
|
+
[](https://circleci.com/gh/ad2games/ar_translate)
|
4
|
+
[](https://codeclimate.com/github/ad2games/ar_translate)
|
5
|
+
[](https://codeclimate.com/github/ad2games/ar_translate/coverage)
|
6
|
+
|
7
|
+
Store values for multiple languages in an ActiveRecord attribute using PostgreSQL's hstore.
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add the gem to your application's Gemfile:
|
12
|
+
|
13
|
+
```ruby
|
14
|
+
gem 'ar_translate'
|
15
|
+
```
|
16
|
+
|
17
|
+
## Usage
|
18
|
+
|
19
|
+
Create a hstore column of the pluralized attribute name:
|
20
|
+
```ruby
|
21
|
+
add_column :posts, :descriptions, :hstore
|
22
|
+
```
|
23
|
+
|
24
|
+
Add the following to your model:
|
25
|
+
```ruby
|
26
|
+
class Post < ActiveRecord::Base
|
27
|
+
extend ArTranslate
|
28
|
+
|
29
|
+
translates :descriptions, languages: %w(de en)
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
Now you have access to the following methods:
|
34
|
+
|
35
|
+
```ruby
|
36
|
+
post = Post.new
|
37
|
+
|
38
|
+
post.description_de = 'Hallo wie geht es dir?'
|
39
|
+
post.description_en = 'Whats up?'
|
40
|
+
|
41
|
+
post.descriptions
|
42
|
+
# => { 'de' => 'Hallo wie geht es dir?', 'en' => 'Whats up?' }
|
43
|
+
|
44
|
+
post.description_de
|
45
|
+
# => 'Hallo wie geht es dir?'
|
46
|
+
|
47
|
+
post.description_languages
|
48
|
+
# => ['de', 'en']
|
49
|
+
|
50
|
+
post.description_attributes
|
51
|
+
# => [:description_de, :description_en]
|
52
|
+
```
|
53
|
+
|
54
|
+
This makes it really easy to use forms with translated attributes:
|
55
|
+
```ruby
|
56
|
+
= form_for @post do |f|
|
57
|
+
= f.text_field :name
|
58
|
+
- @post.description_attributes.each do |attr|
|
59
|
+
= f.text_field attr
|
60
|
+
```
|
61
|
+
|
62
|
+
## License
|
63
|
+
|
64
|
+
MIT, see LICENSE.txt
|
65
|
+
|
66
|
+
## Contributing
|
67
|
+
|
68
|
+
Feel free to fork and submit pull requests!
|
69
|
+
|
70
|
+
You need to set the `DATABASE_URL` environment variable
|
71
|
+
to a valid PostgreSQL database for testing.
|
72
|
+
|
73
|
+
```
|
74
|
+
$ createdb ar_translate_test
|
75
|
+
$ export DATABASE_URL="postgres://localhost/ar_translate_test"
|
76
|
+
$ bundle exec rspec
|
77
|
+
```
|
data/Rakefile
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'ar_translate/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'ar_translate'
|
8
|
+
spec.version = ArTranslate::VERSION
|
9
|
+
spec.authors = ['Niko Dziemba']
|
10
|
+
spec.email = ['niko@dziemba.com']
|
11
|
+
|
12
|
+
spec.summary = 'Store translations in AR models with hstore'
|
13
|
+
spec.description =
|
14
|
+
'Store values for multiple languages in an ActiveRecord attribute using PostgreSQL\'s hstore.'
|
15
|
+
spec.homepage = 'https://github.com/ad2games/ar_translate'
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.require_paths = ['lib']
|
19
|
+
|
20
|
+
spec.add_dependency 'activerecord', '>= 4.0.0'
|
21
|
+
spec.add_dependency 'activesupport', '>= 4.0.0'
|
22
|
+
spec.add_dependency 'pg'
|
23
|
+
|
24
|
+
spec.add_development_dependency 'bundler'
|
25
|
+
spec.add_development_dependency 'rake'
|
26
|
+
spec.add_development_dependency 'rspec'
|
27
|
+
end
|
data/circle.yml
ADDED
data/lib/ar_translate.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'active_support/inflector'
|
3
|
+
require 'ar_translate/version'
|
4
|
+
require 'ar_translate/error'
|
5
|
+
require 'ar_translate/translated_column'
|
6
|
+
|
7
|
+
module ArTranslate
|
8
|
+
def translates(column, opts = {})
|
9
|
+
TranslatedColumn.new(self, column, opts).inject
|
10
|
+
end
|
11
|
+
end
|
@@ -0,0 +1,93 @@
|
|
1
|
+
module ArTranslate
|
2
|
+
class TranslatedColumn
|
3
|
+
KEYWORDS = %w(attributes languages)
|
4
|
+
|
5
|
+
attr_reader :model, :column, :langs
|
6
|
+
|
7
|
+
def initialize(model, column, opts)
|
8
|
+
@model = model
|
9
|
+
@column = column.to_s
|
10
|
+
@langs = opts.delete(:languages)
|
11
|
+
|
12
|
+
check_plural_name!
|
13
|
+
check_languages!
|
14
|
+
end
|
15
|
+
|
16
|
+
def inject
|
17
|
+
define_attributes
|
18
|
+
define_languages
|
19
|
+
|
20
|
+
langs.each do |lang|
|
21
|
+
define_reader(lang)
|
22
|
+
define_writer(lang)
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def prefix
|
29
|
+
column.singularize
|
30
|
+
end
|
31
|
+
|
32
|
+
def attributes
|
33
|
+
langs.map { |lang| :"#{prefix}_#{lang}" }
|
34
|
+
end
|
35
|
+
|
36
|
+
def check_plural_name!
|
37
|
+
return if column.pluralize == column
|
38
|
+
return if column.singularize != column
|
39
|
+
|
40
|
+
fail Error, "Column name #{column} is not pluralized"
|
41
|
+
end
|
42
|
+
|
43
|
+
def check_languages!
|
44
|
+
if !langs.is_a?(Array) || langs.empty?
|
45
|
+
fail Error, "No languages specified for column #{column}"
|
46
|
+
end
|
47
|
+
|
48
|
+
langs.each do |lang|
|
49
|
+
next if lang =~ /^[a-z]+$/ && !KEYWORDS.include?(lang)
|
50
|
+
fail Error, "Invalid language '#{lang}' (should be lowercase only)"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def model_method(name, &block)
|
55
|
+
method_name = "#{prefix}_#{name}"
|
56
|
+
c_column = column
|
57
|
+
|
58
|
+
model.instance_eval do
|
59
|
+
define_method(method_name) do |*args|
|
60
|
+
column_info = self.class.columns_hash[c_column]
|
61
|
+
fail Error, "Invalid column #{c_column}" if column_info.nil?
|
62
|
+
fail Error, "Column type for #{c_column} is not hstore" unless column_info.type == :hstore
|
63
|
+
|
64
|
+
instance_exec(*args, &block)
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
def define_reader(lang)
|
70
|
+
c_column = column
|
71
|
+
model_method(lang) do
|
72
|
+
self[c_column][lang]
|
73
|
+
end
|
74
|
+
end
|
75
|
+
|
76
|
+
def define_writer(lang)
|
77
|
+
c_column = column
|
78
|
+
model_method("#{lang}=") do |val|
|
79
|
+
self[c_column][lang] = val
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def define_attributes
|
84
|
+
c_attributes = attributes
|
85
|
+
model_method(:attributes) { c_attributes }
|
86
|
+
end
|
87
|
+
|
88
|
+
def define_languages
|
89
|
+
c_langs = langs
|
90
|
+
model_method(:languages) { c_langs }
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
metadata
ADDED
@@ -0,0 +1,141 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ar_translate
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Niko Dziemba
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-05-19 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activerecord
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.0.0
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: 4.0.0
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activesupport
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 4.0.0
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: 4.0.0
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pg
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
description: Store values for multiple languages in an ActiveRecord attribute using
|
98
|
+
PostgreSQL's hstore.
|
99
|
+
email:
|
100
|
+
- niko@dziemba.com
|
101
|
+
executables: []
|
102
|
+
extensions: []
|
103
|
+
extra_rdoc_files: []
|
104
|
+
files:
|
105
|
+
- ".gitignore"
|
106
|
+
- ".rspec"
|
107
|
+
- ".ruby-version"
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- ar_translate.gemspec
|
113
|
+
- circle.yml
|
114
|
+
- lib/ar_translate.rb
|
115
|
+
- lib/ar_translate/error.rb
|
116
|
+
- lib/ar_translate/translated_column.rb
|
117
|
+
- lib/ar_translate/version.rb
|
118
|
+
homepage: https://github.com/ad2games/ar_translate
|
119
|
+
licenses: []
|
120
|
+
metadata: {}
|
121
|
+
post_install_message:
|
122
|
+
rdoc_options: []
|
123
|
+
require_paths:
|
124
|
+
- lib
|
125
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
126
|
+
requirements:
|
127
|
+
- - ">="
|
128
|
+
- !ruby/object:Gem::Version
|
129
|
+
version: '0'
|
130
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
131
|
+
requirements:
|
132
|
+
- - ">="
|
133
|
+
- !ruby/object:Gem::Version
|
134
|
+
version: '0'
|
135
|
+
requirements: []
|
136
|
+
rubyforge_project:
|
137
|
+
rubygems_version: 2.4.5
|
138
|
+
signing_key:
|
139
|
+
specification_version: 4
|
140
|
+
summary: Store translations in AR models with hstore
|
141
|
+
test_files: []
|