Skip to content

mattbeedle/has_constant

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

130 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

HasConstant is a gem to limit the values that a certain field may contain. I find it very useful for multilingual sites to translate the values in forms and in the corresponding models. It’s also useful for pretty much any field in a model which is filled in from a select field or radio button.

Because HasConstant takes a list, and stores the select index in the model, it can be used to translate the values. Two important points to acheive this though; First of all, the two lists must be the same length, so, if you are storing salutations ‘Mr’ and ‘Mrs’, then you must have the corresponding ‘Herr’ and ‘Frau’ translations in your de locale file. Also, you must always add any new values to the end of the list.

class User < ActiveRecord::Base

include HasConstant

has_constant :salutations, lambda { I18n.t(:salutations) }

end

In the view

<% form_for @user || User.new do |form| %>

<%= form.select :salutation, User.salutations -%>
<%= form.submit 'submit' -%>

<% end %>

u = User.new(:salutation => ‘Mrs’)

u.salutation #=> ‘Mrs’

I18n.locale = :de

u.salutation #=> ‘Frau’

class User < ActiveRecord::Base

include HasConstant

has_constant :salutations, ['Mr', 'Mrs']

end

User.salutations #=> [‘Mr’, ‘Mrs’]

u = User.new(:salutation => ‘Dr’)

u.salutation #=> nil

u.salutation = ‘Mr’

u.salutation #=> ‘Mr’

class User

include Mongoid::Document
include HasConstant

has_constant :industries, ['IT/Development', 'Marketing']

end

User.industries #=> [‘IT/Development’, ‘Marketing’]

to index the values for querying just add :index => true

assuming I18n.t(:salutations) == [‘Mr’, ‘Mrs’]

class User

include Mongoid::Document

has_constant :salutations

end

User.salutations #=> [‘Mr’, ‘Mrs’]

class User

include HasConstant

has_constant :job_roles, ['Junior Developer', 'Developer', 'CTO', 'Other']

end

User.job_roles #=> [‘Junior Developer’, ‘Developer’, ‘CTO’, ‘Other’]

class User

include HasConstant

has_constant :salutation, lambda { I18n.t(:salutations) }
has_constant :industries, Proc.new { I18n.t(:industries) }

end

(assuming the correct translations exist)

I18n.locale = :en

User.salutations #=> [‘Mr’, ‘Mrs’]

I18n.locale = :de

User.salutations #=> [‘Herr’, ‘Frau’]

u = User.new(:salutation => ‘Herr’)

u.salutation #=> ‘Herr’

I18n.locale = :en

u.salutation #=> ‘Mr’

<%= form.input :salutation, :collection => User.salutations -%>

  • Fork the project.

  • Make your feature addition or bug fix.

  • Add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)

  • Send me a pull request. Bonus points for topic branches.

Copyright © 2010 mattbeedle. See LICENSE for details.

About

Store values as enumerations for translation, etc

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages