buddy_translatable 0.2.0 → 1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7400068f6a44cecc838b666b11a3d04e2379bd0e10f3ef64e92b62bb70438d8d
4
- data.tar.gz: e41ee4fbe96d09530663597c37114f9dac7375e45008eeb4bafa3325b3790520
3
+ metadata.gz: 6b07b1d70b041c19890ca5130304dad96be154355506847b0819129bc44b6b9f
4
+ data.tar.gz: 9c47ee39424f448f4ec8ed4dad0d865af3df9e0bc4247d51af507d240e0af0d6
5
5
  SHA512:
6
- metadata.gz: 9efbcbc8f420ebd9011528e0d086d2836f8be5dfb6b0946c2911ba4c6089c6237ddb0e6b2f22542bdfecc95398df7a94483847a68430d9d6cd55e5a17adf3f7d
7
- data.tar.gz: 41c4c3a915c831689e52088cf6b44c3fa1e0274283b33bfe55bc4e61bf6324f8a9d06e88410c3b647340807a5f1c51e3814a8e7131e0fe35c70d3309b1f956eb
6
+ metadata.gz: e342c4fbecd70240b2f388a30048c6c1653aa325c37c0b431ac158add0987094e8427db1d653ce81703b284313193bd160750b1dd7b74eec5a4541e9701d96c9
7
+ data.tar.gz: e46117f2d65e8ece226134caa1f6d29c9a8f6a6c8e1a7ae165ab07395665f128b94607a61cd7b4923aba5e50f789d7c08c4b15c98f0ddd51e88c5bf92cac3c16
@@ -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
@@ -1,5 +1,5 @@
1
1
  sudo: false
2
2
  language: ruby
3
3
  rvm:
4
- - 2.3.1
4
+ - 2.5.5
5
5
  before_install: gem install bundler -v 1.16.2
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ # 1.0 (17-06-2021)
2
+ - feat: Support for text format attributes (non jsonb attributes)
3
+ - chore: Add container settings
4
+ - refactor: Change default data format from stringified keys into symbolized keys
5
+ - feat: support for locale specific setter
6
+
1
7
  # 0.2.0 (14-08-2020)
2
8
  - Removed sales_datable
3
9
 
data/Dockerfile ADDED
@@ -0,0 +1,7 @@
1
+ FROM ruby:2.5
2
+ RUN apt-get update -qq
3
+ RUN gem install bundler
4
+ WORKDIR /app
5
+ COPY . /app
6
+ RUN bundle install
7
+
data/Gemfile CHANGED
@@ -4,3 +4,6 @@ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
4
 
5
5
  # Specify your gem's dependencies in buddy_translatable.gemspec
6
6
  gemspec
7
+
8
+ gem 'pg'
9
+ gem 'rubocop'
data/Gemfile.lock CHANGED
@@ -2,32 +2,38 @@ PATH
2
2
  remote: .
3
3
  specs:
4
4
  buddy_translatable (0.2.0)
5
- activerecord (>= 5.0)
6
- activesupport (>= 5.0)
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 (6.0.3.2)
14
- activesupport (= 6.0.3.2)
15
- activerecord (6.0.3.2)
16
- activemodel (= 6.0.3.2)
17
- activesupport (= 6.0.3.2)
18
- activesupport (6.0.3.2)
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)
19
18
  concurrent-ruby (~> 1.0, >= 1.0.2)
20
- i18n (>= 0.7, < 2)
21
- minitest (~> 5.1)
22
- tzinfo (~> 1.1)
23
- zeitwerk (~> 2.2, >= 2.2.2)
24
- concurrent-ruby (1.1.7)
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)
25
25
  diff-lcs (1.3)
26
- i18n (1.8.5)
26
+ i18n (1.8.10)
27
27
  concurrent-ruby (~> 1.0)
28
- minitest (5.14.1)
28
+ minitest (5.14.4)
29
+ parallel (1.20.1)
30
+ parser (3.0.1.1)
31
+ ast (~> 2.4.1)
29
32
  pg (1.2.3)
33
+ rainbow (3.0.0)
30
34
  rake (10.5.0)
35
+ regexp_parser (2.1.1)
36
+ rexml (3.2.5)
31
37
  rspec (3.8.0)
32
38
  rspec-core (~> 3.8.0)
33
39
  rspec-expectations (~> 3.8.0)
@@ -41,10 +47,22 @@ GEM
41
47
  diff-lcs (>= 1.2.0, < 2.0)
42
48
  rspec-support (~> 3.8.0)
43
49
  rspec-support (3.8.2)
44
- thread_safe (0.3.6)
45
- tzinfo (1.2.7)
46
- thread_safe (~> 0.1)
47
- zeitwerk (2.4.0)
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
- rake (~> 10.0)
56
- rspec (~> 3.0)
73
+ pg
74
+ rake
75
+ rspec
76
+ rubocop
57
77
 
58
78
  BUNDLED WITH
59
79
  1.17.3
data/README.md CHANGED
@@ -5,6 +5,7 @@ Allows you to store text data in multiple languages. Based on [translateable](ht
5
5
  1. Support for key methods
6
6
  2. No extra table dependency
7
7
  3. Single attribute
8
+ 4. Support for `jsonb` and `text` format attributes
8
9
 
9
10
  ### Translations
10
11
  ```ruby
@@ -34,6 +35,8 @@ post.update(title: 'Hello changed')
34
35
  post.title_en # => Hello changed
35
36
  post.title_de # => Hallo
36
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)
37
40
 
38
41
  # Replace all data
39
42
  post.update(title: { en: 'En val', de: 'De val' })
@@ -42,8 +45,6 @@ post.title_es # => De val # default_key value is used when not defined
42
45
  ```
43
46
 
44
47
  ## Requirements
45
-
46
- - PostgreSQL >= 9.4
47
48
  - ActiveRecord >= 4.2
48
49
  - I18n
49
50
 
@@ -52,7 +53,7 @@ post.title_es # => De val # default_key value is used when not defined
52
53
  Add this line to your application's Gemfile:
53
54
 
54
55
  ```ruby
55
- gem 'buddy_translatable'
56
+ gem 'buddy_translatable', '>= 1.0'
56
57
  ```
57
58
 
58
59
  Configure your available locales in your rails app, like:
@@ -79,13 +80,16 @@ class AddTitleToPosts < ActiveRecord::Migration
79
80
  def change
80
81
  add_column :posts, :title, :jsonb, null: false, default: {}
81
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: '{}'
82
86
  end
83
87
  end
84
88
  ```
85
89
 
86
90
  ### Queries
87
91
 
88
- For searching you can use the following concern:
92
+ For searching you can use the following concern (Only `jsonb` format attributes):
89
93
  ```ruby
90
94
  module JsonbQuerable
91
95
  extend ActiveSupport::Concern
@@ -121,8 +125,11 @@ Post.where("title->>'en' = ?", 'hello')
121
125
  ```
122
126
 
123
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"`
124
132
 
125
- 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.
126
133
 
127
134
  ## Contributing
128
135
 
@@ -130,3 +137,4 @@ Bug reports and pull requests are welcome on GitHub at https://github.com/owen23
130
137
 
131
138
  ## License
132
139
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
140
+
data/Rakefile CHANGED
@@ -1,6 +1,8 @@
1
- require "bundler/gem_tasks"
2
- require "rspec/core/rake_task"
1
+ # frozen_string_literal: true
2
+
3
+ require 'bundler/gem_tasks'
4
+ require 'rspec/core/rake_task'
3
5
 
4
6
  RSpec::Core::RakeTask.new(:spec)
5
7
 
6
- task :default => :spec
8
+ task default: :spec
@@ -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', '~> 10.0'
33
- spec.add_development_dependency 'rspec', '~> 3.0'
32
+ spec.add_development_dependency 'rake'
33
+ spec.add_development_dependency 'rspec'
34
34
 
35
- spec.add_dependency 'activerecord', '>= 5.0'
36
- spec.add_dependency 'activesupport', '>= 5.0'
35
+ spec.add_dependency 'activerecord'
36
+ spec.add_dependency 'activesupport'
37
37
  spec.add_dependency 'i18n'
38
- spec.add_dependency 'pg'
39
38
  end
@@ -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
+ - ${PGPORT:-5432}: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:
@@ -2,24 +2,30 @@
2
2
 
3
3
  module BuddyTranslatable
4
4
  module Core
5
- def translatable(*attrs, default_key: :en, available_keys: nil)
6
- available_keys ||= I18n.available_locales
5
+ def translatable(*attrs, default_key: :en, available_locales: nil)
6
+ available_locales ||= I18n.available_locales
7
7
  attrs.each do |attr|
8
- define_translatable_methods(attr, default_key)
9
- 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))
10
10
  end
11
11
  end
12
12
 
13
- def define_translatable_methods(attr, default_key)
14
- define_translatable_setters(attr)
15
- define_translatable_key_getters(attr, default_key)
13
+ def define_translatable_methods(attr, fallback_locale)
14
+ format = respond_to?(:column_types) ? column_types[attr.to_s].type : attribute_types[attr.to_s].type
15
+ define_translatable_setters(attr, format)
16
+ define_translatable_key_getters(attr, fallback_locale)
16
17
  define_translatable_getters(attr)
17
18
  end
18
19
 
19
- def define_translatable_setters(attr)
20
+ # Sample:
21
+ # model.title_data = { de: 'de val', en: 'en val' } # ==> replace value data
22
+ # model.title = { de: 'de val', en: 'en val' } # replace value data
23
+ # model.title = 'custom value' # sets new value for current locale
24
+ def define_translatable_setters(attr, format)
20
25
  define_method("#{attr}_data=") do |arg|
21
26
  data = send("#{attr}_data")
22
- self[attr] = arg.is_a?(Hash) ? arg : data.merge(I18n.locale => arg)
27
+ data = arg.is_a?(Hash) ? arg.symbolize_keys : data.merge(I18n.locale => arg)
28
+ self[attr] = %i[string text].include?(format) ? data.to_json : data
23
29
  end
24
30
 
25
31
  define_method("#{attr}=") do |arg|
@@ -27,11 +33,14 @@ module BuddyTranslatable
27
33
  end
28
34
  end
29
35
 
30
- def define_translatable_key_getters(attr, default_key)
36
+ # Sample:
37
+ # model.title_for(:de) # ==> print value for provided locale
38
+ # model.title_data_for(:de) # print value for provided locale
39
+ def define_translatable_key_getters(attr, fallback_locale)
31
40
  define_method("#{attr}_data_for") do |key|
32
41
  value = send("#{attr}_data")
33
42
  value[key] ||
34
- value[default_key].presence ||
43
+ value[fallback_locale].presence ||
35
44
  value.values.find(&:present?)
36
45
  end
37
46
 
@@ -40,11 +49,15 @@ module BuddyTranslatable
40
49
  end
41
50
  end
42
51
 
52
+ # Sample:
53
+ # model.title_data # ==> print values data
54
+ # model.title # print value for current locale
43
55
  def define_translatable_getters(attr)
44
56
  define_method("#{attr}_data") do
45
- res = self[attr]
57
+ res = self[attr] || {}
46
58
  res = new_record? ? { I18n.locale => '' } : {} unless res.present?
47
- res.symbolize_keys.with_indifferent_access
59
+ res = JSON.parse(res) if res.is_a?(String)
60
+ res.symbolize_keys
48
61
  end
49
62
 
50
63
  define_method(attr) do |**_args|
@@ -52,12 +65,20 @@ module BuddyTranslatable
52
65
  end
53
66
  end
54
67
 
55
- def define_translatable_key_methods(attr, keys)
56
- keys.each do |key|
57
- define_method("#{attr}_#{key}") do
58
- send("#{attr}_data_for", key)
68
+ # Sample:
69
+ # model.title_de # ==> "de title"
70
+ # model.title_de = "new title" # assign value for specific locale
71
+ def define_translatable_key_methods(attr, locales)
72
+ locales.each do |locale|
73
+ define_method("#{attr}_#{locale}") do
74
+ send("#{attr}_data_for", locale)
75
+ end
76
+
77
+ define_method("#{attr}_#{locale}=") do |value|
78
+ data = send("#{attr}_data").merge(locale => value)
79
+ send("#{attr}_data=", data)
59
80
  end
60
81
  end
61
82
  end
62
83
  end
63
- end
84
+ end
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module BuddyTranslatable
2
- VERSION = '0.2.0'.freeze
4
+ VERSION = '1.0'
3
5
  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.2.0
4
+ version: '1.0'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Owen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-08-14 00:00:00.000000000 Z
11
+ date: 2021-06-17 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: '10.0'
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: '10.0'
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: '3.0'
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: '3.0'
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: '5.0'
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: '5.0'
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: pg
84
+ name: i18n
99
85
  requirement: !ruby/object:Gem::Requirement
100
86
  requirements:
101
87
  - - ">="
@@ -115,11 +101,15 @@ 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"
121
110
  - CHANGELOG.md
122
111
  - CODE_OF_CONDUCT.md
112
+ - Dockerfile
123
113
  - Gemfile
124
114
  - Gemfile.lock
125
115
  - LICENSE.txt
@@ -128,6 +118,7 @@ files:
128
118
  - bin/console
129
119
  - bin/setup
130
120
  - buddy_translatable.gemspec
121
+ - docker-compose.yaml
131
122
  - lib/buddy_translatable.rb
132
123
  - lib/buddy_translatable/core.rb
133
124
  - lib/buddy_translatable/version.rb
@@ -150,7 +141,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
150
141
  - !ruby/object:Gem::Version
151
142
  version: '0'
152
143
  requirements: []
153
- rubygems_version: 3.0.8
144
+ rubygems_version: 3.1.2
154
145
  signing_key:
155
146
  specification_version: 4
156
147
  summary: Manages attribute translations translations