Giter Site home page Giter Site logo

ar_translate's Introduction

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

ar_translate's People

Contributors

dziemba avatar

Watchers

Harald Wartig avatar Helge Rausch avatar Martin Held avatar Nick Reynolds avatar James Cloos avatar Mouaz Heron avatar Dustin Mierig avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.