Giter Club home page Giter Club logo

globalize-accessors's Introduction

Globalize Accessors Build Status

Introduction

Generator of accessor methods for models using Globalize. Use globalize-accessors with a list of translated fields you want easily access to and extra locales array listing locales for which you want the accessors to be generated.

This way a single form can be used to edit given model fields with all anticipated translations.

globalize-accessors is compatible with both Rails 3.x and Rails 4.

Installation

gem install globalize-accessors

Example

Definition like this:

class Product
  translates :title, :description
  globalize_accessors :locales => [:en, :pl], :attributes => [:title]
end

Gives you access to methods: title_pl, title_en, title_pl=, title_en=. These work seamlessly with Globalize (not even touching the "core" title, title= methods used by Globalize itself).

The :locales and :attributes options are optional. Their default values are:

:locales => I18n.available_locales
:attributes => translated_attribute_names

Calling globalize_accessors with no options will therefore generate accessor methods for all translated fields and available languages.

You can also get the accessor locales for a class with the globalize_locales method:

Product.globalize_locales # => [:en, :pl]

You can also get modified attribute names -- ideal for use with strong parameters -- with the globalize_attribute_names method:

Product.globalize_attribute_names # => [:title_en, :title_pl]

Example with strong parameters:

params.require(:product).permit(*Product.globalize_attribute_names)

If you need to permit non-translatable attributes as well, you could include them with:

permitted = Product.globalize_attribute_names + [:position]
params.require(:product).permit(*permitted)

Always define accessors

If you wish to always define accessors and don't want to call the globalize_accessors method in every class, you can extend ActiveRecord::Base with a module:

module TranslatesWithAccessors

  def translates(*params)
    options = params.extract_options!
    options.reverse_merge!(:globalize_accessors => true)
    accessors = options.delete(:globalize_accessors)
    super
    globalize_accessors if accessors
  end

end

Licence

Copyright (c) 2009-2013 Tomek "Tomash" Stachewicz (http://tomash.wrug.eu), Robert Pankowecki (http://robert.pankowecki.pl), Chris Salzberg (http://dejimata.com) released under the MIT license

globalize-accessors's People

Contributors

shioyama avatar paneq avatar miks avatar parndt avatar eljojo avatar tomash avatar samotarnik avatar costis avatar dlepage avatar dougdroper avatar pwim avatar sergeykishenin avatar stephanvane avatar verlinden avatar zencocoon avatar tegon 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.