buddy_translatable 0.1.0 → 1.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.github/workflows/release.yml +43 -0
- data/.github/workflows/tests.yml +31 -0
- data/.rubocop.yml +18 -0
- data/.travis.yml +1 -1
- data/CHANGELOG.md +14 -0
- data/Dockerfile +7 -0
- data/Gemfile +3 -0
- data/Gemfile.lock +45 -25
- data/README.md +20 -47
- data/Rakefile +5 -3
- data/buddy_translatable.gemspec +6 -7
- data/docker-compose.yaml +25 -0
- data/lib/buddy_translatable.rb +0 -5
- data/lib/buddy_translatable/core.rb +49 -45
- data/lib/buddy_translatable/version.rb +3 -1
- metadata +25 -35
- data/lib/buddy_translatable/configuration.rb +0 -15
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 137d4c2784bfe06dbd6969ccb23c08230752a379d1c16cae7c86f2bd3a26c77c
|
4
|
+
data.tar.gz: 41137ae24aa4c5ce86f6e418824e44fcd6d7754041d0c4129c02587153895bdc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9428062d7b98175ab808858b9598e85bfbd19686862c72f7f1fc23e4d87e3b0323ff7b7606066cbee56b6c0a01e9432e2effc61598d5733fc8d1630e8c26ba8a
|
7
|
+
data.tar.gz: 5de3c7ecc7ce1f3c0aebaa0579952584552d9a446b0a9e4c8d1a4b8e37927d3cb170181a66be3092504aac8bcb9fedc0074d924b5a0c4253346e398d02fe1090
|
@@ -0,0 +1,43 @@
|
|
1
|
+
on:
|
2
|
+
push:
|
3
|
+
tags: # triggered once a git tag is published
|
4
|
+
- '*'
|
5
|
+
|
6
|
+
name: Create Release
|
7
|
+
|
8
|
+
jobs:
|
9
|
+
build:
|
10
|
+
name: Create Release
|
11
|
+
runs-on: ubuntu-latest
|
12
|
+
steps:
|
13
|
+
- name: Checkout code
|
14
|
+
uses: actions/checkout@v2
|
15
|
+
with:
|
16
|
+
fetch-depth: 0
|
17
|
+
|
18
|
+
# Changelog action adaptations
|
19
|
+
- name: Create required package.json
|
20
|
+
run: test -f package.json || echo '{}' >package.json
|
21
|
+
- name: Detect Previous Tag (action not detecting very well)
|
22
|
+
run: echo "::set-output name=previous_tag::$(git describe --abbrev=0 --tags `git rev-list --tags --skip=1 --max-count=1`)"
|
23
|
+
id: tag_checker
|
24
|
+
|
25
|
+
- name: Generate Changelog
|
26
|
+
uses: scottbrenner/generate-changelog-action@master
|
27
|
+
id: Changelog
|
28
|
+
with:
|
29
|
+
from-tag: ${{steps.tag_checker.outputs.previous_tag}}
|
30
|
+
to-tag: HEAD
|
31
|
+
|
32
|
+
- name: Create Release
|
33
|
+
id: create_release
|
34
|
+
uses: actions/create-release@latest
|
35
|
+
env:
|
36
|
+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # This token is provided by Actions, you do not need to create your own token
|
37
|
+
with:
|
38
|
+
tag_name: ${{ github.ref }}
|
39
|
+
release_name: Release ${{ github.ref }}
|
40
|
+
body: |
|
41
|
+
${{ steps.Changelog.outputs.changelog }}
|
42
|
+
draft: false
|
43
|
+
prerelease: false
|
@@ -0,0 +1,31 @@
|
|
1
|
+
name: App tests
|
2
|
+
|
3
|
+
on:
|
4
|
+
push:
|
5
|
+
branches:
|
6
|
+
- master
|
7
|
+
- staging
|
8
|
+
pull_request:
|
9
|
+
|
10
|
+
jobs:
|
11
|
+
build:
|
12
|
+
runs-on: ubuntu-latest
|
13
|
+
|
14
|
+
steps:
|
15
|
+
- name: Checkout repository
|
16
|
+
uses: actions/checkout@v2
|
17
|
+
- name: Cancel previous Workflow Actions
|
18
|
+
uses: styfle/cancel-workflow-action@0.6.0
|
19
|
+
with:
|
20
|
+
access_token: ${{ github.token }}
|
21
|
+
- name: download docker-compose cache
|
22
|
+
run: docker-compose pull
|
23
|
+
- uses: satackey/action-docker-layer-caching@v0.0.11 # Cache or restore docker cache
|
24
|
+
continue-on-error: true # Ignore the failure of a step and avoid terminating the job.
|
25
|
+
with:
|
26
|
+
key: app_docker
|
27
|
+
|
28
|
+
- name: Run rspec test
|
29
|
+
run: docker-compose run test /bin/sh -c "rspec"
|
30
|
+
- name: Check codestyle
|
31
|
+
run: docker-compose run test /bin/sh -c "rubocop"
|
data/.rubocop.yml
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# This is the configuration used to check the rubocop source code.
|
2
|
+
|
3
|
+
AllCops:
|
4
|
+
Exclude:
|
5
|
+
- 'bin/**/*'
|
6
|
+
- 'spec/**/*'
|
7
|
+
- 'spec/spec_helper.rb'
|
8
|
+
- 'Gemfile'
|
9
|
+
- 'buddy_translatable.gemspec'
|
10
|
+
|
11
|
+
Style/BlockComments:
|
12
|
+
Enabled: false
|
13
|
+
|
14
|
+
Style/Documentation:
|
15
|
+
Enabled: false
|
16
|
+
|
17
|
+
Style/RescueStandardError:
|
18
|
+
EnforcedStyle: implicit
|
data/.travis.yml
CHANGED
data/CHANGELOG.md
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
# 1.0.1 (18-06-2021)
|
2
|
+
- Fix: fix error when running rails db:migrate
|
3
|
+
|
4
|
+
# 1.0 (17-06-2021)
|
5
|
+
- feat: Support for text format attributes (non jsonb attributes)
|
6
|
+
- chore: Add container settings
|
7
|
+
- refactor: Change default data format from stringified keys into symbolized keys
|
8
|
+
- feat: support for locale specific setter
|
9
|
+
|
10
|
+
# 0.2.0 (14-08-2020)
|
11
|
+
- Removed sales_datable
|
12
|
+
|
13
|
+
# 0.1.2
|
14
|
+
- First public release
|
data/Dockerfile
ADDED
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -1,34 +1,39 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
buddy_translatable (0.1
|
5
|
-
activerecord
|
6
|
-
activesupport
|
4
|
+
buddy_translatable (1.0.1)
|
5
|
+
activerecord
|
6
|
+
activesupport
|
7
7
|
i18n
|
8
|
-
pg
|
9
8
|
|
10
9
|
GEM
|
11
10
|
remote: https://rubygems.org/
|
12
11
|
specs:
|
13
|
-
activemodel (
|
14
|
-
activesupport (=
|
15
|
-
activerecord (
|
16
|
-
activemodel (=
|
17
|
-
activesupport (=
|
18
|
-
|
19
|
-
activesupport (5.2.3)
|
12
|
+
activemodel (6.1.3.2)
|
13
|
+
activesupport (= 6.1.3.2)
|
14
|
+
activerecord (6.1.3.2)
|
15
|
+
activemodel (= 6.1.3.2)
|
16
|
+
activesupport (= 6.1.3.2)
|
17
|
+
activesupport (6.1.3.2)
|
20
18
|
concurrent-ruby (~> 1.0, >= 1.0.2)
|
21
|
-
i18n (>=
|
22
|
-
minitest (
|
23
|
-
tzinfo (~>
|
24
|
-
|
25
|
-
|
19
|
+
i18n (>= 1.6, < 2)
|
20
|
+
minitest (>= 5.1)
|
21
|
+
tzinfo (~> 2.0)
|
22
|
+
zeitwerk (~> 2.3)
|
23
|
+
ast (2.4.2)
|
24
|
+
concurrent-ruby (1.1.9)
|
26
25
|
diff-lcs (1.3)
|
27
|
-
i18n (1.
|
26
|
+
i18n (1.8.10)
|
28
27
|
concurrent-ruby (~> 1.0)
|
29
|
-
minitest (5.
|
30
|
-
|
28
|
+
minitest (5.14.4)
|
29
|
+
parallel (1.20.1)
|
30
|
+
parser (3.0.1.1)
|
31
|
+
ast (~> 2.4.1)
|
32
|
+
pg (1.2.3)
|
33
|
+
rainbow (3.0.0)
|
31
34
|
rake (10.5.0)
|
35
|
+
regexp_parser (2.1.1)
|
36
|
+
rexml (3.2.5)
|
32
37
|
rspec (3.8.0)
|
33
38
|
rspec-core (~> 3.8.0)
|
34
39
|
rspec-expectations (~> 3.8.0)
|
@@ -42,9 +47,22 @@ GEM
|
|
42
47
|
diff-lcs (>= 1.2.0, < 2.0)
|
43
48
|
rspec-support (~> 3.8.0)
|
44
49
|
rspec-support (3.8.2)
|
45
|
-
|
46
|
-
|
47
|
-
|
50
|
+
rubocop (1.17.0)
|
51
|
+
parallel (~> 1.10)
|
52
|
+
parser (>= 3.0.0.0)
|
53
|
+
rainbow (>= 2.2.2, < 4.0)
|
54
|
+
regexp_parser (>= 1.8, < 3.0)
|
55
|
+
rexml
|
56
|
+
rubocop-ast (>= 1.7.0, < 2.0)
|
57
|
+
ruby-progressbar (~> 1.7)
|
58
|
+
unicode-display_width (>= 1.4.0, < 3.0)
|
59
|
+
rubocop-ast (1.7.0)
|
60
|
+
parser (>= 3.0.1.1)
|
61
|
+
ruby-progressbar (1.11.0)
|
62
|
+
tzinfo (2.0.4)
|
63
|
+
concurrent-ruby (~> 1.0)
|
64
|
+
unicode-display_width (2.0.0)
|
65
|
+
zeitwerk (2.4.2)
|
48
66
|
|
49
67
|
PLATFORMS
|
50
68
|
ruby
|
@@ -52,8 +70,10 @@ PLATFORMS
|
|
52
70
|
DEPENDENCIES
|
53
71
|
buddy_translatable!
|
54
72
|
bundler
|
55
|
-
|
56
|
-
|
73
|
+
pg
|
74
|
+
rake
|
75
|
+
rspec
|
76
|
+
rubocop
|
57
77
|
|
58
78
|
BUNDLED WITH
|
59
|
-
1.
|
79
|
+
1.17.3
|
data/README.md
CHANGED
@@ -1,9 +1,11 @@
|
|
1
1
|
# JSON Buddy Translatable
|
2
2
|
|
3
|
-
Allows you to store text data in multiple languages
|
3
|
+
Allows you to store text data in multiple languages. Based on [translateable](https://github.com/olegantonyan/translateable), but with a few differences:
|
4
4
|
|
5
5
|
1. Support for key methods
|
6
|
-
2.
|
6
|
+
2. No extra table dependency
|
7
|
+
3. Single attribute
|
8
|
+
4. Support for `jsonb` and `text` format attributes
|
7
9
|
|
8
10
|
### Translations
|
9
11
|
```ruby
|
@@ -23,6 +25,7 @@ post.title #=> Hallo
|
|
23
25
|
# Getter using methods
|
24
26
|
post.title_en #=> Hello
|
25
27
|
post.title_de #=> Hallo
|
28
|
+
post.title_for(:de) #=> Hallo
|
26
29
|
post.title_data_for(:en) #=> Hello
|
27
30
|
post.title_data # => return all data (Hash)
|
28
31
|
|
@@ -32,48 +35,16 @@ post.update(title: 'Hello changed')
|
|
32
35
|
post.title_en # => Hello changed
|
33
36
|
post.title_de # => Hallo
|
34
37
|
post.title_es # => Hallo (return default key if not found)
|
38
|
+
post.title_es = 'Hola' # => Set value for a specific locale
|
39
|
+
post.title_es # => Hola (return defined value)
|
35
40
|
|
36
41
|
# Replace all data
|
37
42
|
post.update(title: { en: 'En val', de: 'De val' })
|
38
43
|
post.title_en # => En val
|
39
|
-
|
40
|
-
|
41
|
-
### Sales Channels
|
42
|
-
```ruby
|
43
|
-
class Post < ActiveRecord::Base
|
44
|
-
include BuddyTranslatable
|
45
|
-
sales_datable :key, default_key: :vev
|
46
|
-
end
|
47
|
-
post = Post.create(key: { vev: 'vev', ebay: 'ebay' })
|
48
|
-
|
49
|
-
# getter
|
50
|
-
post.key #=> vev # return default key
|
51
|
-
BuddyTranslatable.config.current_sales_key = :ebay
|
52
|
-
post.key #=> ebay
|
53
|
-
|
54
|
-
# you can reset current sales channel by
|
55
|
-
BuddyTranslatable.config.reset_current_sales_key
|
56
|
-
|
57
|
-
# Getter using methods
|
58
|
-
post.key_vev #=> vev
|
59
|
-
post.key_ebay #=> ebay
|
60
|
-
post.key_data_for(:vev) #=> vev
|
61
|
-
post.key_data # => return all data (Hash)
|
62
|
-
|
63
|
-
# Update current sales key (if empty will use default_key) and maintain others
|
64
|
-
BuddyTranslatable.config.current_sales_key = :ebay
|
65
|
-
post.update(key: 'Ebay changed')
|
66
|
-
post.key_ebay # => Ebay changed
|
67
|
-
post.key_vev # => vev
|
68
|
-
|
69
|
-
# Replace all data
|
70
|
-
post.update(key: { vev: 'vev val', ebay: 'ebay val' })
|
71
|
-
post.key_vev # => vev val
|
44
|
+
post.title_es # => De val # default_key value is used when not defined
|
72
45
|
```
|
73
46
|
|
74
47
|
## Requirements
|
75
|
-
|
76
|
-
- PostgreSQL >= 9.4
|
77
48
|
- ActiveRecord >= 4.2
|
78
49
|
- I18n
|
79
50
|
|
@@ -82,15 +53,11 @@ post.key_vev # => vev val
|
|
82
53
|
Add this line to your application's Gemfile:
|
83
54
|
|
84
55
|
```ruby
|
85
|
-
gem 'buddy_translatable'
|
56
|
+
gem 'buddy_translatable', '>= 1.0'
|
86
57
|
```
|
87
58
|
|
88
|
-
|
89
|
-
```
|
90
|
-
# config/initializers/translatable.rb
|
91
|
-
BuddyTranslatable.config.available_sales_keys = %i[de ebay]
|
92
|
-
I18n.available_locales = %i[de en es]
|
93
|
-
```
|
59
|
+
Configure your available locales in your rails app, like:
|
60
|
+
```I18n.available_locales = %i[de en es]```
|
94
61
|
|
95
62
|
And then execute:
|
96
63
|
``` bundle install ```
|
@@ -102,7 +69,6 @@ class TestModel < ActiveRecord::Base
|
|
102
69
|
include BuddyTranslatable
|
103
70
|
|
104
71
|
translatable :title, default_key: :de
|
105
|
-
sales_datable :key, default_key: :vev #, available_keys: [:ebay, :amazon, :vev]
|
106
72
|
end
|
107
73
|
```
|
108
74
|
|
@@ -114,13 +80,16 @@ class AddTitleToPosts < ActiveRecord::Migration
|
|
114
80
|
def change
|
115
81
|
add_column :posts, :title, :jsonb, null: false, default: {}
|
116
82
|
add_column :posts, :key, :jsonb, null: false, default: {}
|
83
|
+
|
84
|
+
# for databases that does not support jsonb format
|
85
|
+
# add_column :posts, :title, :text, null: false, default: '{}'
|
117
86
|
end
|
118
87
|
end
|
119
88
|
```
|
120
89
|
|
121
90
|
### Queries
|
122
91
|
|
123
|
-
For searching you can use the following concern:
|
92
|
+
For searching you can use the following concern (Only `jsonb` format attributes):
|
124
93
|
```ruby
|
125
94
|
module JsonbQuerable
|
126
95
|
extend ActiveSupport::Concern
|
@@ -156,8 +125,11 @@ Post.where("title->>'en' = ?", 'hello')
|
|
156
125
|
```
|
157
126
|
|
158
127
|
## Development
|
128
|
+
Run tests with:
|
129
|
+
`docker-compose run test bash -c "rspec"`
|
130
|
+
Check code style:
|
131
|
+
`docker-compose run test bash -c "rubocop"`
|
159
132
|
|
160
|
-
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
161
133
|
|
162
134
|
## Contributing
|
163
135
|
|
@@ -165,3 +137,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/owen23
|
|
165
137
|
|
166
138
|
## License
|
167
139
|
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
140
|
+
|
data/Rakefile
CHANGED
data/buddy_translatable.gemspec
CHANGED
@@ -11,8 +11,8 @@ Gem::Specification.new do |spec|
|
|
11
11
|
spec.authors = ['Owen']
|
12
12
|
spec.email = ['owenperedo@gmail.com']
|
13
13
|
|
14
|
-
spec.summary = 'Manages translations
|
15
|
-
spec.description = 'Manages translations
|
14
|
+
spec.summary = 'Manages attribute translations translations'
|
15
|
+
spec.description = 'Manages attribute translations translations'
|
16
16
|
spec.homepage = 'https://github.com/owen2345/buddy_translatable'
|
17
17
|
spec.license = 'MIT'
|
18
18
|
|
@@ -29,11 +29,10 @@ Gem::Specification.new do |spec|
|
|
29
29
|
spec.require_paths = %w[lib]
|
30
30
|
|
31
31
|
spec.add_development_dependency 'bundler'
|
32
|
-
spec.add_development_dependency 'rake'
|
33
|
-
spec.add_development_dependency 'rspec'
|
32
|
+
spec.add_development_dependency 'rake'
|
33
|
+
spec.add_development_dependency 'rspec'
|
34
34
|
|
35
|
-
spec.add_dependency 'activerecord'
|
36
|
-
spec.add_dependency 'activesupport'
|
35
|
+
spec.add_dependency 'activerecord'
|
36
|
+
spec.add_dependency 'activesupport'
|
37
37
|
spec.add_dependency 'i18n'
|
38
|
-
spec.add_dependency 'pg'
|
39
38
|
end
|
data/docker-compose.yaml
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
version: '2'
|
2
|
+
|
3
|
+
services:
|
4
|
+
postgres:
|
5
|
+
image: postgres:10.6-alpine
|
6
|
+
volumes:
|
7
|
+
- postgres_data:/var/lib/postgresql/data
|
8
|
+
ports:
|
9
|
+
- 5438:5432
|
10
|
+
environment:
|
11
|
+
POSTGRES_USER: ${POSTGRES_USER:-root}
|
12
|
+
POSTGRES_PASSWORD: ${POSTGRES_PASSWORD:-password}
|
13
|
+
|
14
|
+
test:
|
15
|
+
build: .
|
16
|
+
command: sh -c 'bundle exec rspec'
|
17
|
+
volumes:
|
18
|
+
- .:/app
|
19
|
+
- bundler_gems:/usr/local/bundle/
|
20
|
+
depends_on:
|
21
|
+
- postgres
|
22
|
+
|
23
|
+
volumes:
|
24
|
+
bundler_gems:
|
25
|
+
postgres_data:
|
data/lib/buddy_translatable.rb
CHANGED
@@ -1,13 +1,8 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
require 'buddy_translatable/version'
|
4
|
-
require 'buddy_translatable/configuration'
|
5
4
|
require 'buddy_translatable/core'
|
6
5
|
module BuddyTranslatable
|
7
|
-
def self.config
|
8
|
-
@config ||= Configuration.new
|
9
|
-
end
|
10
|
-
|
11
6
|
def self.included(base)
|
12
7
|
base.extend BuddyTranslatable::Core
|
13
8
|
end
|
@@ -2,47 +2,32 @@
|
|
2
2
|
|
3
3
|
module BuddyTranslatable
|
4
4
|
module Core
|
5
|
-
def translatable(*attrs, default_key: :
|
6
|
-
|
5
|
+
def translatable(*attrs, default_key: :en, available_locales: nil)
|
6
|
+
available_locales ||= I18n.available_locales
|
7
7
|
attrs.each do |attr|
|
8
|
-
|
9
|
-
|
10
|
-
define_translatable_key_methods(attr, available_keys)
|
8
|
+
define_translatable_methods(attr, default_key.to_sym)
|
9
|
+
define_translatable_key_methods(attr, available_locales.map(&:to_sym))
|
11
10
|
end
|
12
11
|
end
|
13
12
|
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
define_translatable_methods(attr, default_key, is_locale: false)
|
21
|
-
define_translatable_key_methods(attr, available_keys)
|
22
|
-
end
|
23
|
-
end
|
24
|
-
|
25
|
-
def translatable_sanity_check(attr)
|
26
|
-
msg = "no such column '#{attr}' in '#{name}' model"
|
27
|
-
raise ArgumentError, msg unless column_names.include?(attr.to_s)
|
28
|
-
end
|
29
|
-
|
30
|
-
def define_translatable_methods(attr, default_key, is_locale: true)
|
31
|
-
current_key = lambda do
|
32
|
-
res = I18n.locale
|
33
|
-
current_sales_key = BuddyTranslatable.config.current_sales_key
|
34
|
-
res = current_sales_key || default_key unless is_locale
|
35
|
-
res.to_sym
|
36
|
-
end
|
37
|
-
define_translatable_setters(attr, current_key)
|
38
|
-
define_translatable_key_getters(attr, default_key)
|
39
|
-
define_translatable_getters(attr, current_key)
|
13
|
+
# rubocop:disable Style/RescueModifier
|
14
|
+
def define_translatable_methods(attr, fallback_locale)
|
15
|
+
format = (try(:column_types) || try(:attribute_types))[attr.to_s].type rescue :text
|
16
|
+
define_translatable_setters(attr, format)
|
17
|
+
define_translatable_key_getters(attr, fallback_locale)
|
18
|
+
define_translatable_getters(attr)
|
40
19
|
end
|
20
|
+
# rubocop:enable Style/RescueModifier
|
41
21
|
|
42
|
-
|
22
|
+
# Sample:
|
23
|
+
# model.title_data = { de: 'de val', en: 'en val' } # ==> replace value data
|
24
|
+
# model.title = { de: 'de val', en: 'en val' } # replace value data
|
25
|
+
# model.title = 'custom value' # sets new value for current locale
|
26
|
+
def define_translatable_setters(attr, format)
|
43
27
|
define_method("#{attr}_data=") do |arg|
|
44
28
|
data = send("#{attr}_data")
|
45
|
-
|
29
|
+
data = arg.is_a?(Hash) ? arg.symbolize_keys : data.merge(I18n.locale => arg)
|
30
|
+
self[attr] = %i[string text].include?(format) ? data.to_json : data
|
46
31
|
end
|
47
32
|
|
48
33
|
define_method("#{attr}=") do |arg|
|
@@ -50,33 +35,52 @@ module BuddyTranslatable
|
|
50
35
|
end
|
51
36
|
end
|
52
37
|
|
53
|
-
|
38
|
+
# Sample:
|
39
|
+
# model.title_for(:de) # ==> print value for provided locale
|
40
|
+
# model.title_data_for(:de) # print value for provided locale
|
41
|
+
def define_translatable_key_getters(attr, fallback_locale)
|
54
42
|
define_method("#{attr}_data_for") do |key|
|
55
43
|
value = send("#{attr}_data")
|
56
44
|
value[key] ||
|
57
|
-
value[
|
45
|
+
value[fallback_locale].presence ||
|
58
46
|
value.values.find(&:present?)
|
59
47
|
end
|
48
|
+
|
49
|
+
define_method("#{attr}_for") do |key|
|
50
|
+
send("#{attr}_data_for", key)
|
51
|
+
end
|
60
52
|
end
|
61
53
|
|
62
|
-
|
54
|
+
# Sample:
|
55
|
+
# model.title_data # ==> print values data
|
56
|
+
# model.title # print value for current locale
|
57
|
+
def define_translatable_getters(attr)
|
63
58
|
define_method("#{attr}_data") do
|
64
|
-
res = self[attr]
|
65
|
-
res = new_record? ? {
|
66
|
-
res.
|
59
|
+
res = self[attr] || {}
|
60
|
+
res = new_record? ? { I18n.locale => '' } : {} unless res.present?
|
61
|
+
res = JSON.parse(res) if res.is_a?(String)
|
62
|
+
res.symbolize_keys
|
67
63
|
end
|
68
64
|
|
69
65
|
define_method(attr) do |**_args|
|
70
|
-
send("#{attr}_data_for",
|
66
|
+
send("#{attr}_data_for", I18n.locale)
|
71
67
|
end
|
72
68
|
end
|
73
69
|
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
70
|
+
# Sample:
|
71
|
+
# model.title_de # ==> "de title"
|
72
|
+
# model.title_de = "new title" # assign value for specific locale
|
73
|
+
def define_translatable_key_methods(attr, locales)
|
74
|
+
locales.each do |locale|
|
75
|
+
define_method("#{attr}_#{locale}") do
|
76
|
+
send("#{attr}_data_for", locale)
|
77
|
+
end
|
78
|
+
|
79
|
+
define_method("#{attr}_#{locale}=") do |value|
|
80
|
+
data = send("#{attr}_data").merge(locale => value)
|
81
|
+
send("#{attr}_data=", data)
|
78
82
|
end
|
79
83
|
end
|
80
84
|
end
|
81
85
|
end
|
82
|
-
end
|
86
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: buddy_translatable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1
|
4
|
+
version: 1.0.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Owen
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-06-18 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,60 +28,46 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '0'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - ">="
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '0'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - ">="
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '0'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - ">="
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '0'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: activerecord
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
59
|
- - ">="
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '0'
|
62
62
|
type: :runtime
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
66
|
- - ">="
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '0'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: activesupport
|
71
|
-
requirement: !ruby/object:Gem::Requirement
|
72
|
-
requirements:
|
73
|
-
- - ">="
|
74
|
-
- !ruby/object:Gem::Version
|
75
|
-
version: '5.0'
|
76
|
-
type: :runtime
|
77
|
-
prerelease: false
|
78
|
-
version_requirements: !ruby/object:Gem::Requirement
|
79
|
-
requirements:
|
80
|
-
- - ">="
|
81
|
-
- !ruby/object:Gem::Version
|
82
|
-
version: '5.0'
|
83
|
-
- !ruby/object:Gem::Dependency
|
84
|
-
name: i18n
|
85
71
|
requirement: !ruby/object:Gem::Requirement
|
86
72
|
requirements:
|
87
73
|
- - ">="
|
@@ -95,7 +81,7 @@ dependencies:
|
|
95
81
|
- !ruby/object:Gem::Version
|
96
82
|
version: '0'
|
97
83
|
- !ruby/object:Gem::Dependency
|
98
|
-
name:
|
84
|
+
name: i18n
|
99
85
|
requirement: !ruby/object:Gem::Requirement
|
100
86
|
requirements:
|
101
87
|
- - ">="
|
@@ -108,17 +94,22 @@ dependencies:
|
|
108
94
|
- - ">="
|
109
95
|
- !ruby/object:Gem::Version
|
110
96
|
version: '0'
|
111
|
-
description: Manages translations
|
97
|
+
description: Manages attribute translations translations
|
112
98
|
email:
|
113
99
|
- owenperedo@gmail.com
|
114
100
|
executables: []
|
115
101
|
extensions: []
|
116
102
|
extra_rdoc_files: []
|
117
103
|
files:
|
104
|
+
- ".github/workflows/release.yml"
|
105
|
+
- ".github/workflows/tests.yml"
|
118
106
|
- ".gitignore"
|
119
107
|
- ".rspec"
|
108
|
+
- ".rubocop.yml"
|
120
109
|
- ".travis.yml"
|
110
|
+
- CHANGELOG.md
|
121
111
|
- CODE_OF_CONDUCT.md
|
112
|
+
- Dockerfile
|
122
113
|
- Gemfile
|
123
114
|
- Gemfile.lock
|
124
115
|
- LICENSE.txt
|
@@ -127,15 +118,15 @@ files:
|
|
127
118
|
- bin/console
|
128
119
|
- bin/setup
|
129
120
|
- buddy_translatable.gemspec
|
121
|
+
- docker-compose.yaml
|
130
122
|
- lib/buddy_translatable.rb
|
131
|
-
- lib/buddy_translatable/configuration.rb
|
132
123
|
- lib/buddy_translatable/core.rb
|
133
124
|
- lib/buddy_translatable/version.rb
|
134
125
|
homepage: https://github.com/owen2345/buddy_translatable
|
135
126
|
licenses:
|
136
127
|
- MIT
|
137
128
|
metadata: {}
|
138
|
-
post_install_message:
|
129
|
+
post_install_message:
|
139
130
|
rdoc_options: []
|
140
131
|
require_paths:
|
141
132
|
- lib
|
@@ -150,9 +141,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
150
141
|
- !ruby/object:Gem::Version
|
151
142
|
version: '0'
|
152
143
|
requirements: []
|
153
|
-
|
154
|
-
|
155
|
-
signing_key:
|
144
|
+
rubygems_version: 3.1.2
|
145
|
+
signing_key:
|
156
146
|
specification_version: 4
|
157
|
-
summary: Manages translations
|
147
|
+
summary: Manages attribute translations translations
|
158
148
|
test_files: []
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
module BuddyTranslatable
|
4
|
-
class Configuration
|
5
|
-
attr_accessor :available_sales_keys, :current_sales_key
|
6
|
-
|
7
|
-
def initialize
|
8
|
-
@available_sales_keys = %i[vev ebay]
|
9
|
-
end
|
10
|
-
|
11
|
-
def reset_current_sales_key
|
12
|
-
@current_sales_key = nil
|
13
|
-
end
|
14
|
-
end
|
15
|
-
end
|