Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/brightcommerce/attr_translate
Rails concern for ActiveRecord attribute translation using PostgreSQL's JSONB datatype.
https://github.com/brightcommerce/attr_translate
activerecord attributes concern i18n jsonb postgresql ruby-on-rails translation
Last synced: 4 days ago
JSON representation
Rails concern for ActiveRecord attribute translation using PostgreSQL's JSONB datatype.
- Host: GitHub
- URL: https://github.com/brightcommerce/attr_translate
- Owner: brightcommerce
- License: mit
- Created: 2018-12-24T01:00:08.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2018-12-24T01:36:56.000Z (almost 6 years ago)
- Last Synced: 2024-10-31T14:32:33.364Z (15 days ago)
- Topics: activerecord, attributes, concern, i18n, jsonb, postgresql, ruby-on-rails, translation
- Language: Ruby
- Size: 5.86 KB
- Stars: 6
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: MIT-LICENSE.md
Awesome Lists containing this project
README
# AttrTranslate
Rails concern for ActiveRecord attribute translation using PostgreSQL's JSONB datatype.
AttrTranslate is extracted from the Brightcommerce platform and is used in a number of other software projects.
## Installation
To install add the line to your Gemfile:
``` ruby
gem 'attr_translate'
```And `bundle install`.
## How To Use
To add AttrTranslate to a model, include the concern and class method:
``` ruby
class Post < ActiveRecord::Base
include AttrTranslateattr_translate :title, :body
end
```For convenience the `attr_translate` class method is aliased internally as `attr_translates`.
To autoload AttrTranslate for all models, add the following to an initializer:
``` ruby
require 'attr_translate/active_record'
```You then don't need to `include AttrTranslate` in any model, but you still need to add the `attr_translate` class method.
### Setup
Each attribute that will have translations will need to be setup appropriately in your model's migration. Append `_translations` to the column name, and set the column type to `:jsonb`. To aid in search, setup an index using the `gin` index type. Each translation will be stored using the locale as a key in a hash and converted to JSON.
``` ruby
class CreatePosts < ActiveRecord::Migration[5.2]
def change
create_table :posts do |table|
table.column :title_translations, :jsonb, default: {}, index: {using: 'gin'}
table.column :body_translations, :jsonb, default: {}, index: {using: 'gin'}
table.timestamps
end
end
end
```## Dependencies
AttrTranslate gem has the following runtime dependencies:
- activerecord >= 5.1.4
- activesupport >= 5.1.4## Compatibility
Tested with MRI 2.4.2 against Rails 5.2.2.
## Contributing
1. Fork it
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
5. Create new Pull Request## Credit
This gem was written and is maintained by [Jurgen Jocubeit](https://github.com/JurgenJocubeit), CEO and President Brightcommerce, Inc.
## License
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
## Copyright
Copyright 2018 Brightcommerce, Inc.