pg_translatable 0.0.1 → 0.0.2
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
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 317b37f935e62b8f70c47ec8f3af4f51ab446738
|
4
|
+
data.tar.gz: c311d1fd8384550f9be3d2eea0789416af0692ea
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: a6b0f1475a56e965d101c77fb7880ef4bb8083f5ca96e556714d75224d029436b20841de4b3d1396154f69700334ab5752cb821e23b68fbf87a51643c22042ea
|
7
|
+
data.tar.gz: 6a7d99f280a8c0b3a0993065aa8ebb44000f1a2b56a9f9ada82000b807e7af24e1a87b5e1c1f531632a09d2fb18019a28fb838b7c7075b0982a9ef5635ffbf9f
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,3 +1,4 @@
|
|
1
|
+
[](http://badge.fury.io/rb/pg_translatable)
|
1
2
|
[](https://travis-ci.org/Stankec/pg_translatable)
|
2
3
|
[](https://codeclimate.com/github/Stankec/pg_translatable)
|
3
4
|
[](https://codeclimate.com/github/Stankec/pg_translatable/coverage)
|
@@ -14,6 +15,118 @@ To use this gem in your rails app simply add the following to your `Gemfile`:
|
|
14
15
|
gem 'pg_translatable'
|
15
16
|
```
|
16
17
|
|
18
|
+
# Usage
|
19
|
+
|
20
|
+
To translate your model's fields you must call the `translate` method:
|
21
|
+
|
22
|
+
```Ruby
|
23
|
+
# app/models/post.rb
|
24
|
+
|
25
|
+
class Post < ActiveRecord::Base
|
26
|
+
translate :title, :content
|
27
|
+
|
28
|
+
...
|
29
|
+
|
30
|
+
end
|
31
|
+
```
|
32
|
+
|
33
|
+
These fields will be translated to all locales that you have defined in
|
34
|
+
`config.i18n.available_locales`.
|
35
|
+
|
36
|
+
E.g. the following configuration would store translations for english, german,
|
37
|
+
french and spanish:
|
38
|
+
|
39
|
+
```Ruby
|
40
|
+
# config/application.rb
|
41
|
+
|
42
|
+
module TestApp
|
43
|
+
class Application < Rails::Application
|
44
|
+
config.i18n.default_locale = :en
|
45
|
+
config.i18n.available_locales = [:en, :de, :fr, :es]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
```
|
49
|
+
|
50
|
+
## Getters and setters
|
51
|
+
|
52
|
+
When you call the field's name in singular it will return the value for the
|
53
|
+
current locale, that is the locale defined in `I18n.locale`. So for the above
|
54
|
+
example, if my current locale was set to `:fr` calling `title` would return the
|
55
|
+
title in french.
|
56
|
+
|
57
|
+
When you call the field's name in plural it will return a hash containing all
|
58
|
+
translations. For the above example, calling `titles` would return a hash
|
59
|
+
containing all translations.
|
60
|
+
|
61
|
+
Additionally a per locale getter and setter will be defined for each field.
|
62
|
+
For the above example the following getters and setters would be generated:
|
63
|
+
`title_en`, `title_de`, `title_fr`, `title_es`,
|
64
|
+
`content_en`, `content_de`, `content_fr`, `content_es`
|
65
|
+
|
66
|
+
__NOTE:__ If it happens that the singular and plural of a given field are the
|
67
|
+
same then the plural will get suffixed with `_translations`.
|
68
|
+
|
69
|
+
E.g. If there was a field called `news` it's getters and setters would be
|
70
|
+
`news` for the current translation and `news_translations` for the
|
71
|
+
translations hash.
|
72
|
+
|
73
|
+
## Formatters
|
74
|
+
|
75
|
+
If you want to format the output of a getter method then you can just redefine
|
76
|
+
the formatter method for any field.
|
77
|
+
|
78
|
+
E.g. if you wanted your content's translations to be returned in reverse:
|
79
|
+
|
80
|
+
```Ruby
|
81
|
+
# app/models/post.rb
|
82
|
+
|
83
|
+
class Post < ActiveRecord::Base
|
84
|
+
translate :title, :content, :price
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def content_formatter(value)
|
89
|
+
value.to_s.reverse
|
90
|
+
end
|
91
|
+
end
|
92
|
+
```
|
93
|
+
|
94
|
+
__NOTE:__ Only getters pass data through formatters, setters save data given to
|
95
|
+
them as is.
|
96
|
+
|
97
|
+
## Strong params
|
98
|
+
|
99
|
+
To better integrate translations with strong parameters a convenience method
|
100
|
+
will be defined for each field, it will just be suffixed with `_fields`.
|
101
|
+
|
102
|
+
For the above example you could do the following:
|
103
|
+
|
104
|
+
```Ruby
|
105
|
+
# app/controllers/posts_controller.rb
|
106
|
+
|
107
|
+
class PostsController < ApplicationController
|
108
|
+
|
109
|
+
...
|
110
|
+
|
111
|
+
private
|
112
|
+
|
113
|
+
def post_params
|
114
|
+
params.require(:post).permit(
|
115
|
+
*Post.title_fields,
|
116
|
+
*Post.content_fields
|
117
|
+
)
|
118
|
+
end
|
119
|
+
|
120
|
+
...
|
121
|
+
|
122
|
+
end
|
123
|
+
```
|
124
|
+
|
125
|
+
## Validation
|
126
|
+
|
127
|
+
There is no practical solution for validating this kind of data.
|
128
|
+
My suggestion is to write custom validator classes.
|
129
|
+
|
17
130
|
# Development
|
18
131
|
|
19
132
|
After checking out the repo, make sure you have a Postgres database server
|
@@ -27,6 +140,9 @@ rake db:migrate
|
|
27
140
|
|
28
141
|
# Contributing
|
29
142
|
|
143
|
+
Open an issue [here](https://github.com/Stankec/pg_translatable/issues) or do
|
144
|
+
the following:
|
145
|
+
|
30
146
|
1. [Fork it]( https://github.com/Stankec/pg_translatable/fork )
|
31
147
|
2. Create your feature branch (`git checkout -b my-new-feature`)
|
32
148
|
3. Commit your changes (`git commit -am 'Add some feature'`)
|
@@ -11,7 +11,7 @@ module PgTranslatable
|
|
11
11
|
def define_strong_params_fields
|
12
12
|
languages_list = @languages.map { |locale| ":#{@column}_#{locale}" }
|
13
13
|
@object.class_eval <<-RUBY
|
14
|
-
define_singleton_method("#{@
|
14
|
+
define_singleton_method("#{@column_name}_fields") do
|
15
15
|
[#{languages_list.join(', ')}]
|
16
16
|
end
|
17
17
|
RUBY
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: pg_translatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Stanko Krtalić Rusendić
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-08-
|
11
|
+
date: 2015-08-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|