awesome_hstore_translate 0.2.1 → 0.4.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA1:
3
- metadata.gz: 3b7a102f4de133d326d96a0b697f2c546409ce88
4
- data.tar.gz: 032d0828417f873bb3b4772fb4da311baa937b38
2
+ SHA256:
3
+ metadata.gz: 16b8c1361f264ebe2a857e13c237e02b4fd8a48f503a504ea57b1733b455b08f
4
+ data.tar.gz: 158506c34f32c0c5c071a53f6d2591a06b8858300648c3137a00aadc6a16832f
5
5
  SHA512:
6
- metadata.gz: 732b84d4d47e6ed0137746a7947834bf75884195db19700b0962337197e4e4e49f42571568f5778371fc73e34812e8789296e4fff914e05e09dd5936f05c004c
7
- data.tar.gz: f008e5892a26270fb68ae2773e90d1036d33bcfe8f5ad50a558403742cb8592833f7727ed71af701c3201263745d3d7d492ac4d599646bb481aa968e9033d97a
6
+ metadata.gz: 24bc81efa70e1275cc03d128110bc92073198d5296dd024b57e3e914968228ddbdfae77bc68e1e5758e8bf2ef2b69f45533a460490f4017489b028ec6d9446e8
7
+ data.tar.gz: 80fe0a1cc9e66f17c7c08d0fd4db5ddb6ee18757604b6ba19d1e23afbae85515da8d93ec01d1a490272e3e554f6e73eb8cd90e75217f4df2f5b04e534a0a1516
@@ -0,0 +1,14 @@
1
+ # [Choice] Ruby version (use -bullseye variants on local arm64/Apple Silicon): 3, 3.0, 2, 2.7, 2.6, 3-bullseye, 3.0-bullseye, 2-bullseye, 2.7-bullseye, 2.6-bullseye, 3-buster, 3.0-buster, 2-buster, 2.7-buster, 2.6-buster
2
+ ARG VARIANT=2-bullseye
3
+ FROM mcr.microsoft.com/vscode/devcontainers/ruby:0-${VARIANT}
4
+
5
+
6
+ # [Optional] Uncomment this section to install additional OS packages.
7
+ # RUN apt-get update && export DEBIAN_FRONTEND=noninteractive \
8
+ # && apt-get -y install --no-install-recommends <your-package-list-here>
9
+
10
+ # [Optional] Uncomment this line to install additional gems.
11
+ # RUN gem install <your-gem-names-here>
12
+
13
+ # [Optional] Uncomment this line to install global node packages.
14
+ # RUN su vscode -c "source /usr/local/share/nvm/nvm.sh && npm install -g <your-package-here>" 2>&1
@@ -0,0 +1,42 @@
1
+ // For format details, see https://aka.ms/devcontainer.json. For config options, see the README at:
2
+ // https://github.com/microsoft/vscode-dev-containers/tree/v0.202.5/containers/ruby-rails-postgres
3
+ // Update the VARIANT arg in docker-compose.yml to pick a Ruby version
4
+ {
5
+ "name": "Awesome Hstore Translate",
6
+ "dockerComposeFile": "docker-compose.yml",
7
+ "service": "app",
8
+ "workspaceFolder": "/workspace",
9
+
10
+ // Set *default* container specific settings.json values on container create.
11
+ "settings": {
12
+ "sqltools.connections": [
13
+ {
14
+ "name": "Test Database",
15
+ "driver": "PostgreSQL",
16
+ "previewLimit": 50,
17
+ "server": "localhost",
18
+ "port": 5432,
19
+
20
+ // update this to match config/database.yml
21
+ "database": "postgres",
22
+ "username": "postgres"
23
+ }
24
+ ]
25
+ },
26
+
27
+ // Add the IDs of extensions you want installed when the container is created.
28
+ "extensions": [
29
+ "rebornix.Ruby",
30
+ "mtxr.sqltools",
31
+ "mtxr.sqltools-driver-pg"
32
+ ],
33
+
34
+ // Use 'forwardPorts' to make a list of ports inside the container available locally.
35
+ // "forwardPorts": [3000, 5432],
36
+
37
+ // Use 'postCreateCommand' to run commands after the container is created.
38
+ // "postCreateCommand": "bundle install && rake db:setup",
39
+
40
+ // Comment out connect as root instead. More info: https://aka.ms/vscode-remote/containers/non-root.
41
+ //"remoteUser": "vscode"
42
+ }
@@ -0,0 +1,41 @@
1
+ version: '3'
2
+
3
+ services:
4
+ app:
5
+ build:
6
+ context: ..
7
+ dockerfile: .devcontainer/Dockerfile
8
+ args:
9
+ # Update 'VARIANT' to pick a version of Ruby: 3, 3.0, 2, 2.7, 2.6
10
+ # Append -bullseye or -buster to pin to an OS version.
11
+ # Use -bullseye variants on local arm64/Apple Silicon.
12
+ VARIANT: "3"
13
+
14
+ volumes:
15
+ - ..:/workspace:cached
16
+
17
+ # Overrides default command so things don't shut down after the process ends.
18
+ command: sleep infinity
19
+
20
+ # Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
21
+ network_mode: service:db
22
+ # Uncomment the next line to use a non-root user for all processes.
23
+ # user: vscode
24
+
25
+ # Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
26
+ # (Adding the "ports" property to this file will not forward from a Codespace.)
27
+
28
+ db:
29
+ image: postgres:latest
30
+ restart: unless-stopped
31
+ volumes:
32
+ - postgres-data:/var/lib/postgresql/data
33
+ environment:
34
+ POSTGRES_USER: postgres
35
+ POSTGRES_DB: postgres
36
+ POSTGRES_PASSWORD: postgres
37
+ # Add "forwardPorts": ["5432"] to **devcontainer.json** to forward PostgreSQL locally.
38
+ # (Adding the "ports" property to this file will not forward from a Codespace.)
39
+
40
+ volumes:
41
+ postgres-data: null
@@ -0,0 +1,32 @@
1
+ name: Check pull request
2
+
3
+ on:
4
+ - pull_request
5
+
6
+ jobs:
7
+ test:
8
+ runs-on: ubuntu-latest
9
+ services:
10
+ postgres:
11
+ image: postgres:14-alpine
12
+ env:
13
+ POSTGRES_USER: postgres
14
+ POSTGRES_PASSWORD: postgres
15
+ POSTGRES_DB: postgres
16
+ options: >-
17
+ --health-cmd pg_isready
18
+ --health-interval 10s
19
+ --health-timeout 5s
20
+ --health-retries 5
21
+ ports:
22
+ - 5432:5432
23
+ steps:
24
+ - uses: actions/checkout@v2
25
+ - uses: ruby/setup-ruby@v1
26
+ with:
27
+ ruby-version: 3.0
28
+ bundler-cache: true
29
+ - name: Install PostgreSQL client
30
+ run: sudo apt-get -yqq install libpq-dev
31
+ - name: Run tests
32
+ run: bundle exec rake
@@ -0,0 +1,26 @@
1
+ name: Release to RubyGems
2
+
3
+ on:
4
+ push:
5
+ branches:
6
+ - master
7
+
8
+ jobs:
9
+ release:
10
+ runs-on: ubuntu-latest
11
+ steps:
12
+ - uses: actions/checkout@v2
13
+ - uses: ruby/setup-ruby@v1
14
+ with:
15
+ ruby-version: 3.0 # Not needed with a .ruby-version file
16
+ bundler-cache: true # runs 'bundle install' and caches installed gems automatically
17
+ - name: publish gem
18
+ run: |
19
+ mkdir -p $HOME/.gem
20
+ touch $HOME/.gem/credentials
21
+ chmod 0600 $HOME/.gem/credentials
22
+ printf -- "---\n:rubygems_api_key: ${GEM_HOST_API_KEY}\n" > $HOME/.gem/credentials
23
+ gem build *.gemspec
24
+ gem push *.gem
25
+ env:
26
+ GEM_HOST_API_KEY: "${{secrets.RUBYGEMS_TOKEN}}"
data/.gitignore CHANGED
@@ -1,54 +1,148 @@
1
- /.bundle/
2
- /.yardoc
3
- /Gemfile.lock
4
- /_yardoc/
1
+
2
+ # Created by https://www.toptal.com/developers/gitignore/api/ruby,visualstudiocode,windows,linux,macos
3
+ # Edit at https://www.toptal.com/developers/gitignore?templates=ruby,visualstudiocode,windows,linux,macos
4
+
5
+ ### Linux ###
6
+ *~
7
+
8
+ # temporary files which can be created if a process still has a handle open of a deleted file
9
+ .fuse_hidden*
10
+
11
+ # KDE directory preferences
12
+ .directory
13
+
14
+ # Linux trash folder which might appear on any partition or disk
15
+ .Trash-*
16
+
17
+ # .nfs files are created when an open file is removed but is still being accessed
18
+ .nfs*
19
+
20
+ ### macOS ###
21
+ # General
22
+ .DS_Store
23
+ .AppleDouble
24
+ .LSOverride
25
+
26
+ # Icon must end with two \r
27
+ Icon
28
+
29
+
30
+ # Thumbnails
31
+ ._*
32
+
33
+ # Files that might appear in the root of a volume
34
+ .DocumentRevisions-V100
35
+ .fseventsd
36
+ .Spotlight-V100
37
+ .TemporaryItems
38
+ .Trashes
39
+ .VolumeIcon.icns
40
+ .com.apple.timemachine.donotpresent
41
+
42
+ # Directories potentially created on remote AFP share
43
+ .AppleDB
44
+ .AppleDesktop
45
+ Network Trash Folder
46
+ Temporary Items
47
+ .apdisk
48
+
49
+ ### Ruby ###
50
+ *.gem
51
+ *.rbc
52
+ /.config
5
53
  /coverage/
6
- /doc/
54
+ /InstalledFiles
7
55
  /pkg/
8
56
  /spec/reports/
57
+ /spec/examples.txt
58
+ /test/tmp/
59
+ /test/version_tmp/
9
60
  /tmp/
10
61
 
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
62
+ # Used by dotenv library to load environment variables.
63
+ # .env
64
+
65
+ # Ignore Byebug command history file.
66
+ .byebug_history
67
+
68
+ ## Specific to RubyMotion:
69
+ .dat*
70
+ .repl_history
71
+ build/
72
+ *.bridgesupport
73
+ build-iPhoneOS/
74
+ build-iPhoneSimulator/
75
+
76
+ ## Specific to RubyMotion (use of CocoaPods):
77
+ #
78
+ # We recommend against adding the Pods directory to your .gitignore. However
79
+ # you should judge for yourself, the pros and cons are mentioned at:
80
+ # https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control
81
+ # vendor/Pods/
82
+
83
+ ## Documentation cache and generated files:
84
+ /.yardoc/
85
+ /_yardoc/
86
+ /doc/
87
+ /rdoc/
88
+
89
+ ## Environment normalization:
90
+ /.bundle/
91
+ /vendor/bundle
92
+ /lib/bundler/man/
93
+
94
+ # for a library or gem, you might want to ignore these files since the code is
95
+ # intended to run in multiple environments; otherwise, check them in:
96
+ # Gemfile.lock
97
+ # .ruby-version
98
+ # .ruby-gemset
99
+
100
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
101
+ .rvmrc
102
+
103
+ # Used by RuboCop. Remote config files pulled in from inherit_from directive.
104
+ # .rubocop-https?--*
13
105
 
14
- # User-specific stuff:
15
- .idea/workspace.xml
16
- .idea/tasks.xml
17
- .idea/dictionaries
18
- .idea/vcs.xml
19
- .idea/jsLibraryMappings.xml
106
+ ### VisualStudioCode ###
107
+ .vscode/*
108
+ !.vscode/settings.json
109
+ !.vscode/tasks.json
110
+ !.vscode/launch.json
111
+ !.vscode/extensions.json
112
+ *.code-workspace
20
113
 
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
114
+ # Local History for Visual Studio Code
115
+ .history/
28
116
 
29
- # Gradle:
30
- .idea/gradle.xml
31
- .idea/libraries
117
+ ### VisualStudioCode Patch ###
118
+ # Ignore all local history of files
119
+ .history
120
+ .ionide
32
121
 
33
- # Mongo Explorer plugin:
34
- .idea/mongoSettings.xml
122
+ ### Windows ###
123
+ # Windows thumbnail cache files
124
+ Thumbs.db
125
+ Thumbs.db:encryptable
126
+ ehthumbs.db
127
+ ehthumbs_vista.db
35
128
 
36
- ## File-based project format:
37
- *.iws
129
+ # Dump file
130
+ *.stackdump
38
131
 
39
- ## Plugin-specific files:
132
+ # Folder config file
133
+ [Dd]esktop.ini
40
134
 
41
- # IntelliJ
42
- /out/
135
+ # Recycle Bin used on file shares
136
+ $RECYCLE.BIN/
43
137
 
44
- # mpeltonen/sbt-idea plugin
45
- .idea_modules/
138
+ # Windows Installer files
139
+ *.cab
140
+ *.msi
141
+ *.msix
142
+ *.msm
143
+ *.msp
46
144
 
47
- # JIRA plugin
48
- atlassian-ide-plugin.xml
145
+ # Windows shortcuts
146
+ *.lnk
49
147
 
50
- # Crashlytics plugin (for Android Studio and IntelliJ)
51
- com_crashlytics_export_strings.xml
52
- crashlytics.properties
53
- crashlytics-build.properties
54
- fabric.properties
148
+ # End of https://www.toptal.com/developers/gitignore/api/ruby,visualstudiocode,windows,linux,macos
data/CHANGELOG.md ADDED
@@ -0,0 +1,13 @@
1
+ # Changelog
2
+
3
+ ## [0.3.0](https://www.github.com/openscript/awesome_hstore_translate/compare/v0.2.2...v0.3.0) (2021-10-25)
4
+
5
+
6
+ ### ⚠ BREAKING CHANGES
7
+
8
+ * Adds compatibility with Rails 6
9
+
10
+ ### Features
11
+
12
+ * Adds compatibility with Rails 6 ([3fdd9bc](https://www.github.com/openscript/awesome_hstore_translate/commit/3fdd9bc51e770fae74c867b6d28c182623394ab0))
13
+ * Updates ([5c7b7e7](https://www.github.com/openscript/awesome_hstore_translate/commit/5c7b7e7883bb87452ef60a8c629fb45146381a20))
data/Gemfile.lock ADDED
@@ -0,0 +1,60 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ awesome_hstore_translate (0.4.0)
5
+ activemodel (>= 5.0)
6
+ activerecord (>= 5.0)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ activemodel (6.1.4.1)
12
+ activesupport (= 6.1.4.1)
13
+ activerecord (6.1.4.1)
14
+ activemodel (= 6.1.4.1)
15
+ activesupport (= 6.1.4.1)
16
+ activesupport (6.1.4.1)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 1.6, < 2)
19
+ minitest (>= 5.1)
20
+ tzinfo (~> 2.0)
21
+ zeitwerk (~> 2.3)
22
+ byebug (11.1.3)
23
+ concurrent-ruby (1.1.9)
24
+ database_cleaner (2.0.1)
25
+ database_cleaner-active_record (~> 2.0.0)
26
+ database_cleaner-active_record (2.0.1)
27
+ activerecord (>= 5.a)
28
+ database_cleaner-core (~> 2.0.0)
29
+ database_cleaner-core (2.0.1)
30
+ docile (1.4.0)
31
+ i18n (1.8.10)
32
+ concurrent-ruby (~> 1.0)
33
+ minitest (5.14.4)
34
+ pg (1.2.3)
35
+ rake (13.0.6)
36
+ simplecov (0.21.2)
37
+ docile (~> 1.1)
38
+ simplecov-html (~> 0.11)
39
+ simplecov_json_formatter (~> 0.1)
40
+ simplecov-html (0.12.3)
41
+ simplecov_json_formatter (0.1.3)
42
+ tzinfo (2.0.4)
43
+ concurrent-ruby (~> 1.0)
44
+ zeitwerk (2.5.1)
45
+
46
+ PLATFORMS
47
+ x86_64-linux
48
+
49
+ DEPENDENCIES
50
+ awesome_hstore_translate!
51
+ bundler (~> 2.2)
52
+ byebug (~> 11.1)
53
+ database_cleaner (~> 2.0)
54
+ minitest (~> 5.0)
55
+ pg (~> 1.2)
56
+ rake (~> 13.0)
57
+ simplecov (~> 0.21)
58
+
59
+ BUNDLED WITH
60
+ 2.2.22
data/README.md CHANGED
@@ -1,29 +1,34 @@
1
1
  # Awesome Hstore Translate
2
+
2
3
  [![Gem Version](https://badge.fury.io/rb/awesome_hstore_translate.svg)](https://badge.fury.io/rb/awesome_hstore_translate)
3
4
 
4
5
  This gem uses PostgreSQLs hstore datatype and ActiveRecord models to translate model data. It is based on the gem
5
- [`hstore_translate`](https://github.com/Leadformance/hstore_translate) by Rob Worely.
6
+ [`hstore_translate`](https://github.com/Leadformance/hstore_translate) by Rob Worely. An alternative is [`json_translate`](https://github.com/cfabianski/json_translate).
6
7
 
7
- - It's ready for Rails 5
8
- - No extra columns or tables needed to operate
9
- - Clean naming in the database model
10
- - Everything is well tested
8
+ - Works with Rails 5 and 6
9
+ - No extra columns or tables needed to operate
10
+ - Clean naming in the database model
11
+ - Everything is well tested
11
12
 
12
13
  ## Features
13
- - [x] `v0.1.0` Attributes override / Raw attributes
14
- - [x] `v0.1.0` Fallbacks
15
- - [x] `v0.1.0` Language specific accessors
16
- - [x] `v0.2.0` Awesome Hstore Translate as drop in replace for [`hstore_translate`](https://github.com/Leadformance/hstore_translate)
17
- - `with_[attr]_translation(str)` is not supported
18
- - [x] `v0.2.0` Support record selection via ActiveRecord (e. g. `where`, `find_by`, ..)
19
- - [ ] `backlog` Support `friendly_id` (see `friendly_id-awesome_hstore` gem)
14
+
15
+ - [x] `v0.1.0` Attributes override / Raw attributes
16
+ - [x] `v0.1.0` Fallbacks
17
+ - [x] `v0.1.0` Language specific accessors
18
+ - [x] `v0.2.0` Awesome Hstore Translate as drop in replace for [`hstore_translate`](https://github.com/Leadformance/hstore_translate)
19
+ - `with_[attr]_translation(str)` is not supported
20
+ - [x] `v0.2.2` Support record selection via ActiveRecord (e. g. `where`, `find_by`, ..)
21
+ - [x] `v0.3.0` Support record ordering via ActiveRecord `order`
22
+ - [ ] `backlog` Support `friendly_id` (see `friendly_id-awesome_hstore` gem)
20
23
 
21
24
  ## Requirements
22
- - ActiveRecord `>= 5`
23
- - Please use [`hstore_translate`](https://github.com/Leadformance/hstore_translate), if you are on an older version.
24
- - I18n
25
+
26
+ - ActiveRecord `>= 5`
27
+ - Please use [`hstore_translate`](https://github.com/Leadformance/hstore_translate), if you are on an older version.
28
+ - I18n
25
29
 
26
30
  ## Installation
31
+
27
32
  Add this line to your application's Gemfile:
28
33
 
29
34
  ```ruby
@@ -39,16 +44,23 @@ Or install it yourself as:
39
44
  $ gem install awesome_hstore_translate
40
45
 
41
46
  ## Usage
47
+
42
48
  Use `translates` in your models, to define the attributes, which should be translateable:
49
+
43
50
  ```ruby
44
51
  class Page < ActiveRecord::Base
45
52
  translates :title, :content
46
53
  end
47
54
  ```
55
+
48
56
  Make sure that the datatype of this columns is `hstore`:
57
+
49
58
  ```ruby
50
59
  class CreatePages < ActiveRecord::Migration
51
60
  def change
61
+ # Make sure you enable the hstore extenion
62
+ enable_extension 'hstore' unless extension_enabled?('hstore')
63
+
52
64
  create_table :pages do |t|
53
65
  t.column :title, :hstore
54
66
  t.column :content, :hstore
@@ -59,6 +71,7 @@ end
59
71
  ```
60
72
 
61
73
  Use the model attributes per locale:
74
+
62
75
  ```ruby
63
76
  p = Page.first
64
77
 
@@ -74,15 +87,23 @@ end
74
87
  ```
75
88
 
76
89
  The raw data is available via the suffix `_raw`:
90
+
77
91
  ```ruby
78
92
  p = Page.new(:title_raw => {'en' => 'English title', 'de' => 'Deutscher Titel'})
79
93
 
80
94
  p.title_raw # => {'en' => 'English title', 'de' => 'Deutscher Titel'}
81
95
  ```
82
96
 
97
+ Translated attributes:
98
+
99
+ ```ruby
100
+ Page.translated_attribute_names # [:title]
101
+ ```
83
102
 
84
103
  ### Fallbacks
104
+
85
105
  It's possible to fall back to another language, if there is no or an empty value for the primary language. To enable fallbacks you can set `I18n.fallbacks` to `true` or enable it manually in the model:
106
+
86
107
  ```ruby
87
108
  class Page < ActiveRecord::Base
88
109
  translates :title, :content, fallbacks: true
@@ -90,6 +111,7 @@ end
90
111
  ```
91
112
 
92
113
  Set `I18n.default_locale` or `I18n.fallbacks` to define the fallback:
114
+
93
115
  ```ruby
94
116
  I18n.fallbacks.map(:en => :de) # => if :en is nil or empty, it will use :de
95
117
 
@@ -101,6 +123,7 @@ end
101
123
  ```
102
124
 
103
125
  It's possible to activate (`with_fallbacks`) or deactivate (`without_fallbacks`) fallbacks for a block execution:
126
+
104
127
  ```ruby
105
128
  p = PageWithoutFallbacks.new(:title_raw => {'de' => 'Deutscher Titel'})
106
129
 
@@ -112,7 +135,9 @@ end
112
135
  ```
113
136
 
114
137
  ### Accessors
138
+
115
139
  Convenience accessors can be enabled via the model descriptor:
140
+
116
141
  ```ruby
117
142
  class Page < ActiveRecord::Base
118
143
  translates :title, :content, accessors: [:de, :en]
@@ -120,6 +145,7 @@ end
120
145
  ```
121
146
 
122
147
  It's also make sense to activate the accessors for all available locales:
148
+
123
149
  ```ruby
124
150
  class Page < ActiveRecord::Base
125
151
  translates :title, :content, accessors: I18n.available_locales
@@ -127,6 +153,7 @@ end
127
153
  ```
128
154
 
129
155
  Now locale-suffixed accessors can be used:
156
+
130
157
  ```ruby
131
158
  p = Page.create!(:title_en => 'English title', :title_de => 'Deutscher Titel')
132
159
 
@@ -134,30 +161,67 @@ p.title_en # => English title
134
161
  p.title_de # => Deutscher Titel
135
162
  ```
136
163
 
164
+ Translated accessor attributes:
165
+
166
+ ```ruby
167
+ Page.translated_accessor_names # [:title_en, :title_de]
168
+ ```
169
+
137
170
  ### Find
171
+
138
172
  `awesome_hstore_translate` patches ActiveRecord, so you can conviniently use `where` and `find_by` as you like.
173
+
139
174
  ```ruby
140
175
  Page.create!(:title_en => 'English title', :title_de => 'Deutscher Titel')
141
176
  Page.create!(:title_en => 'Another English title', :title_de => 'Noch ein Deutscher Titel')
142
177
 
143
- Page.where(title: 'Another English title') # => Page
178
+ Page.where(title: 'Another English title') # => Page with title 'Another English title'
144
179
  ```
145
180
 
181
+ ### Order
182
+
183
+ `awesome_hstore_translate` patches ActiveRecord, so you can conviniently use `order` as you like.
184
+
185
+ ```ruby
186
+ Page.create!(:title_en => 'English title', :title_de => 'Deutscher Titel')
187
+ Page.create!(:title_en => 'Another English title', :title_de => 'Noch ein Deutscher Titel')
188
+
189
+ Page.all.order(title: :desc) # => Page with title 'English title'
190
+ ```
191
+
192
+ ### Limitations
193
+
194
+ `awesome_hstore_translate` patches ActiveRecord, which create the limitation, that a with `where` chained `first_or_create` and `first_or_create!` **doesn't work** as expected.
195
+ Here is an example, which **won't** work:
196
+
197
+ ```ruby
198
+ Page.where(title: 'Titre français').first_or_create!
199
+ ```
200
+
201
+ A workaround is:
202
+
203
+ ```ruby
204
+ Page.where(title: 'Titre français').first_or_create!(title: 'Titre français')
205
+ ```
206
+
207
+ The where clause is internally rewritten to `WHERE 'Titre français' = any(avals(title))`, so the `title: 'Titre français'` is not bound to the scope.
146
208
 
147
209
  ### Upgrade from [`hstore_translate`](https://github.com/Leadformance/hstore_translate)
210
+
148
211
  1. Replace the [`hstore_translate`](https://github.com/Leadformance/hstore_translate) with `awesome_hstore_translate` in your Gemfile
149
212
  1. Activate accessors, if you used the [`hstore_translate`](https://github.com/Leadformance/hstore_translate) accessors
150
213
  1. Replace `with_[attr]_translation(str)` with equivalents (see "Support record selection via ActiveRecord" feature)
151
214
 
152
215
  ## Development
216
+
153
217
  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.
154
218
 
155
219
  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).
156
220
 
157
221
  ## Contributing
158
- 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.
159
222
 
223
+ 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.
160
224
 
161
225
  ## License
162
- The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
163
226
 
227
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
@@ -6,8 +6,8 @@ require 'awesome_hstore_translate/version'
6
6
  Gem::Specification.new do |spec|
7
7
  spec.name = 'awesome_hstore_translate'
8
8
  spec.version = AwesomeHstoreTranslate::VERSION
9
- spec.authors = ['Robin Bühler', 'Rob Worley']
10
- spec.email = ['public@openscript.ch']
9
+ spec.authors = ['Robin Bühler', 'Rob Worley', 'Edouard Piron']
10
+ spec.email = ['r+rubygems@obin.ch']
11
11
 
12
12
  spec.summary = 'Using PostgreSQLs hstore datatype to provide ActiveRecord models data translation.'
13
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.'
@@ -19,13 +19,14 @@ Gem::Specification.new do |spec|
19
19
  spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
20
20
  spec.require_paths = ['lib']
21
21
 
22
- spec.add_dependency 'activerecord', '>= 5.0', '< 6'
23
- spec.add_dependency 'activemodel', '>= 5.0', '< 6'
22
+ spec.add_dependency 'activerecord', '>= 5.0'
23
+ spec.add_dependency 'activemodel', '>= 5.0'
24
24
 
25
- spec.add_development_dependency 'bundler', '~> 1.12'
26
- spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'byebug', '~> 11.1'
26
+ spec.add_development_dependency 'bundler', '~> 2.2'
27
+ spec.add_development_dependency 'rake', '~> 13.0'
27
28
  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'
29
+ spec.add_development_dependency 'database_cleaner', '~> 2.0'
30
+ spec.add_development_dependency 'pg', '~> 1.2'
31
+ spec.add_development_dependency 'simplecov', '~> 0.21'
31
32
  end
@@ -7,6 +7,7 @@ module AwesomeHstoreTranslate
7
7
  translation_options[:accessors].each do |locale|
8
8
  define_reader_accessor(attr, locale)
9
9
  define_writer_accessor(attr, locale)
10
+ self.translated_accessor_names << :"#{attr}_#{locale}"
10
11
  end
11
12
  end
12
13
 
@@ -27,4 +28,4 @@ module AwesomeHstoreTranslate
27
28
  end
28
29
  end
29
30
  end
30
- end
31
+ end
@@ -40,6 +40,9 @@ module AwesomeHstoreTranslate
40
40
  def expose_translated_attrs(attr_names)
41
41
  class_attribute :translated_attribute_names
42
42
  self.translated_attribute_names = attr_names
43
+
44
+ class_attribute :translated_accessor_names
45
+ self.translated_accessor_names = []
43
46
  end
44
47
 
45
48
  def bootstrap(options, attr_names)
@@ -52,4 +55,4 @@ module AwesomeHstoreTranslate
52
55
  end
53
56
  end
54
57
  end
55
- end
58
+ end
@@ -7,7 +7,7 @@ module AwesomeHstoreTranslate
7
7
  if attrs.is_a?(Hash) && contains_translated_attributes(attrs)
8
8
  where(attrs).limit(1).first
9
9
  else
10
- super(args)
10
+ super
11
11
  end
12
12
  end
13
13
 
@@ -4,16 +4,15 @@ module AwesomeHstoreTranslate
4
4
  protected
5
5
 
6
6
  def read_translated_attribute(attr, locale = I18n.locale)
7
- locales = []
8
- locales << locale
7
+ locales = Array(locale)
9
8
  locales += get_fallback_for_locale(locale) || [] if translation_options[:fallbacks]
10
9
 
11
10
  translations = read_raw_attribute(attr)
12
11
 
13
12
  if translations
14
- locales.uniq.each do |cur|
15
- if translations.has_key?(cur.to_s) && !translations[cur.to_s].empty?
16
- return translations[cur.to_s]
13
+ locales.map(&:to_s).uniq.each do |cur|
14
+ if translations.has_key?(cur) && !translations[cur].blank?
15
+ return translations[cur]
17
16
  end
18
17
  end
19
18
  end
@@ -50,4 +49,4 @@ module AwesomeHstoreTranslate
50
49
  end
51
50
  end
52
51
  end
53
- end
52
+ end
@@ -3,18 +3,52 @@ module AwesomeHstoreTranslate
3
3
  module QueryMethods
4
4
  def where(opts = :chain, *rest)
5
5
  if opts.is_a?(Hash)
6
- translated_attrs = translated_attributes(opts)
7
- normal_attrs = opts.reject{ |key, _| translated_attrs.include? key}
8
6
  query = spawn
9
- query.where!(normal_attrs) unless normal_attrs.empty?
7
+ translated_attrs = translated_attributes(opts)
8
+ untranslated_attrs = untranslated_attributes(opts)
9
+
10
+ unless untranslated_attrs.empty?
11
+ query.where!(untranslated_attrs, *rest)
12
+ end
10
13
 
11
- translated_attrs.each do |attribute|
12
- if opts[attribute].is_a?(String)
13
- query.where!("'#{opts[attribute]}' = any(avals(#{attribute}))")
14
+ translated_attrs.each do |key, value|
15
+ if value.is_a?(String)
16
+ query.where!(":value = any(avals(#{key}))", value: value)
14
17
  else
15
18
  super
16
19
  end
17
20
  end
21
+
22
+ query
23
+ else
24
+ super
25
+ end
26
+ end
27
+
28
+ def order(*args)
29
+ if args.is_a?(Array)
30
+ check_if_method_has_arguments!(:order, args)
31
+ query = spawn
32
+ attrs = args
33
+
34
+ # TODO Remove this ugly hack
35
+ if args[0].is_a?(Hash)
36
+ attrs = args[0]
37
+ elsif args[0].is_a?(Symbol)
38
+ attrs = Hash[args.map {|attr| [attr, :asc]}]
39
+ end
40
+
41
+ translated_attrs = translated_attributes(attrs)
42
+ untranslated_attrs = untranslated_attributes(attrs)
43
+
44
+ unless untranslated_attrs.empty?
45
+ query.order!(untranslated_attrs)
46
+ end
47
+
48
+ translated_attrs.each do |key, value|
49
+ query.order!(Arel.sql("#{key} -> '#{I18n.locale.to_s}' #{value}"))
50
+ end
51
+
18
52
  query
19
53
  else
20
54
  super
@@ -24,7 +58,19 @@ module AwesomeHstoreTranslate
24
58
  private
25
59
 
26
60
  def translated_attributes(opts)
27
- self.translated_attribute_names & opts.keys
61
+ opts.select{ |key, _| self.translated_attribute_names.include?(key) }
62
+ end
63
+
64
+ def untranslated_attributes(opts)
65
+ return safe_untranslated_attributes(opts) if opts.is_a?(Array)
66
+
67
+ opts.reject{ |key, _| self.translated_attribute_names.include?(key) }
68
+ end
69
+
70
+ def safe_untranslated_attributes(opts)
71
+ opts
72
+ .reject { |opt| opt.is_a?(Arel::Nodes::Ordering) }
73
+ .map! { |opt| Arel.sql(opt.to_s) }
28
74
  end
29
75
  end
30
76
  end
@@ -1,3 +1,3 @@
1
1
  module AwesomeHstoreTranslate
2
- VERSION = '0.2.1'
2
+ VERSION = '0.4.0'
3
3
  end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: awesome_hstore_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robin Bühler
8
8
  - Rob Worley
9
+ - Edouard Piron
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2016-07-30 00:00:00.000000000 Z
13
+ date: 2021-10-25 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: activerecord
@@ -18,9 +19,6 @@ dependencies:
18
19
  - - ">="
19
20
  - !ruby/object:Gem::Version
20
21
  version: '5.0'
21
- - - "<"
22
- - !ruby/object:Gem::Version
23
- version: '6'
24
22
  type: :runtime
25
23
  prerelease: false
26
24
  version_requirements: !ruby/object:Gem::Requirement
@@ -28,9 +26,6 @@ dependencies:
28
26
  - - ">="
29
27
  - !ruby/object:Gem::Version
30
28
  version: '5.0'
31
- - - "<"
32
- - !ruby/object:Gem::Version
33
- version: '6'
34
29
  - !ruby/object:Gem::Dependency
35
30
  name: activemodel
36
31
  requirement: !ruby/object:Gem::Requirement
@@ -38,9 +33,6 @@ dependencies:
38
33
  - - ">="
39
34
  - !ruby/object:Gem::Version
40
35
  version: '5.0'
41
- - - "<"
42
- - !ruby/object:Gem::Version
43
- version: '6'
44
36
  type: :runtime
45
37
  prerelease: false
46
38
  version_requirements: !ruby/object:Gem::Requirement
@@ -48,37 +40,48 @@ dependencies:
48
40
  - - ">="
49
41
  - !ruby/object:Gem::Version
50
42
  version: '5.0'
51
- - - "<"
43
+ - !ruby/object:Gem::Dependency
44
+ name: byebug
45
+ requirement: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - "~>"
48
+ - !ruby/object:Gem::Version
49
+ version: '11.1'
50
+ type: :development
51
+ prerelease: false
52
+ version_requirements: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - "~>"
52
55
  - !ruby/object:Gem::Version
53
- version: '6'
56
+ version: '11.1'
54
57
  - !ruby/object:Gem::Dependency
55
58
  name: bundler
56
59
  requirement: !ruby/object:Gem::Requirement
57
60
  requirements:
58
61
  - - "~>"
59
62
  - !ruby/object:Gem::Version
60
- version: '1.12'
63
+ version: '2.2'
61
64
  type: :development
62
65
  prerelease: false
63
66
  version_requirements: !ruby/object:Gem::Requirement
64
67
  requirements:
65
68
  - - "~>"
66
69
  - !ruby/object:Gem::Version
67
- version: '1.12'
70
+ version: '2.2'
68
71
  - !ruby/object:Gem::Dependency
69
72
  name: rake
70
73
  requirement: !ruby/object:Gem::Requirement
71
74
  requirements:
72
75
  - - "~>"
73
76
  - !ruby/object:Gem::Version
74
- version: '10.0'
77
+ version: '13.0'
75
78
  type: :development
76
79
  prerelease: false
77
80
  version_requirements: !ruby/object:Gem::Requirement
78
81
  requirements:
79
82
  - - "~>"
80
83
  - !ruby/object:Gem::Version
81
- version: '10.0'
84
+ version: '13.0'
82
85
  - !ruby/object:Gem::Dependency
83
86
  name: minitest
84
87
  requirement: !ruby/object:Gem::Requirement
@@ -99,56 +102,62 @@ dependencies:
99
102
  requirements:
100
103
  - - "~>"
101
104
  - !ruby/object:Gem::Version
102
- version: '1.5'
105
+ version: '2.0'
103
106
  type: :development
104
107
  prerelease: false
105
108
  version_requirements: !ruby/object:Gem::Requirement
106
109
  requirements:
107
110
  - - "~>"
108
111
  - !ruby/object:Gem::Version
109
- version: '1.5'
112
+ version: '2.0'
110
113
  - !ruby/object:Gem::Dependency
111
114
  name: pg
112
115
  requirement: !ruby/object:Gem::Requirement
113
116
  requirements:
114
117
  - - "~>"
115
118
  - !ruby/object:Gem::Version
116
- version: '0.18'
119
+ version: '1.2'
117
120
  type: :development
118
121
  prerelease: false
119
122
  version_requirements: !ruby/object:Gem::Requirement
120
123
  requirements:
121
124
  - - "~>"
122
125
  - !ruby/object:Gem::Version
123
- version: '0.18'
126
+ version: '1.2'
124
127
  - !ruby/object:Gem::Dependency
125
128
  name: simplecov
126
129
  requirement: !ruby/object:Gem::Requirement
127
130
  requirements:
128
131
  - - "~>"
129
132
  - !ruby/object:Gem::Version
130
- version: '0.12'
133
+ version: '0.21'
131
134
  type: :development
132
135
  prerelease: false
133
136
  version_requirements: !ruby/object:Gem::Requirement
134
137
  requirements:
135
138
  - - "~>"
136
139
  - !ruby/object:Gem::Version
137
- version: '0.12'
140
+ version: '0.21'
138
141
  description: This gem uses PostgreSQLs hstore datatype and ActiveRecord models to
139
142
  translate model data. It is based on the gem hstore_translate by Rob Worely.
140
143
  email:
141
- - public@openscript.ch
144
+ - r+rubygems@obin.ch
142
145
  executables:
143
146
  - console
144
147
  - setup
145
148
  extensions: []
146
149
  extra_rdoc_files: []
147
150
  files:
151
+ - ".devcontainer/Dockerfile"
152
+ - ".devcontainer/devcontainer.json"
153
+ - ".devcontainer/docker-compose.yml"
154
+ - ".github/workflows/pull.yml"
155
+ - ".github/workflows/release.yml"
148
156
  - ".gitignore"
149
- - ".travis.yml"
157
+ - CHANGELOG.md
150
158
  - CODE_OF_CONDUCT.md
151
159
  - Gemfile
160
+ - Gemfile.lock
152
161
  - LICENSE.txt
153
162
  - README.md
154
163
  - Rakefile
@@ -184,8 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
184
193
  - !ruby/object:Gem::Version
185
194
  version: '0'
186
195
  requirements: []
187
- rubyforge_project:
188
- rubygems_version: 2.5.1
196
+ rubygems_version: 3.2.22
189
197
  signing_key:
190
198
  specification_version: 4
191
199
  summary: Using PostgreSQLs hstore datatype to provide ActiveRecord models data translation.
data/.travis.yml DELETED
@@ -1,5 +0,0 @@
1
- sudo: false
2
- language: ruby
3
- rvm:
4
- - 2.3.1
5
- before_install: gem install bundler -v 1.12.5