0.0
No commit activity in last 3 years
No release in over 3 years
Store values for multiple languages in an ActiveRecord attribute using PostgreSQL's hstore.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

>= 0
>= 0

Runtime

>= 4.0.0
>= 0
 Project Readme

ArTranslate

Circle CI Code Climate Test Coverage

Store values for multiple languages in an ActiveRecord attribute using PostgreSQL's hstore.

Installation

Add the gem to your application's Gemfile:

gem 'ar_translate'

Usage

Create a hstore column of the pluralized attribute name:

add_column :posts, :descriptions, :hstore

Add the following to your model:

class Post < ActiveRecord::Base
  extend ArTranslate

  translates :descriptions, languages: %w(de en es)
end

Now you have access to the following methods:

post = Post.new

post.description_de = 'Hallo wie geht es dir?'
post.description_en = 'Whats up?'

post.descriptions
# => { 'de' => 'Hallo wie geht es dir?', 'en' => 'Whats up?' }

post.description_de
# => 'Hallo wie geht es dir?'

post.description_languages
# => ['de', 'en', 'es']

post.description_attributes
# => [:description_de, :description_en, :description_es]

# The two methods above also work at class level:
Post.description_languages # => [...]
Post.description_attributes # => [...]

This makes it really easy to use forms with translated attributes:

= form_for @post do |f|
  = f.text_field :name
  - @post.description_attributes.each do |attr|
    = f.text_field attr

Or you can add validations to your model:

class Post < ActiveRecord::Base
  extend ArTranslate

  translates :descriptions, languages: %w(de en es)

  description_attributes.each do |attr|
    validates attr, length: { in: 20..200 }
  end
end

License

MIT, see LICENSE.txt

Contributing

Feel free to fork and submit pull requests!

You need to set the DATABASE_URL environment variable to a valid PostgreSQL database for testing.

$ createdb ar_translate_test
$ export DATABASE_URL="postgres://localhost/ar_translate_test"
$ bundle exec rspec