Skip to content

josei/flyweight

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flyweight

A plugin for flyweight pattern fields in models.

github.com/josei/flyweight

Example

Suppose this code:

class Profile
  flyweight :education
  flyweight :parents_education, :with=>:educations
end

en:
  educations:
    none: 'No education'
    primary: 'Primary school'
    high_school: 'High school'
    bachelor: 'Bachelor degree'
    graduate: 'Graduate'
    master: 'Master'
    mba: 'MBA'
    doctor: 'Doctor'

That’s equivalent to:

class Profile
  def self.educations_values
    [:none, :primary, :high_school, :bachelor, :graduate, :master, :mba, :doctor ]
  end

  validates :education, :inclusion=>{:in=>educations_values}
  validates :parent_education, :inclusion=>{:in=>educations_values}

  def education_to_s
    I18n::t("educations.#{education}")
  end
  def parent_education_to_s
    I18n::t("educations.#{parent_education}")
  end
end

en:
  educations:
    none: 'No education'
    primary: 'Primary school'
    high_school: 'High school'
    bachelor: 'Bachelor degree'
    graduate: 'Graduate'
    master: 'Master'
    mba: 'MBA'
    doctor: 'Doctor'

This makes it easy to manage a property’s allowed values and i18n them. The plugin also integrates nicely with simple_form

LICENSE

Copyright © 2012 José Ignacio Fernández (jose <at> traitperception.com), released under the MIT license

About

Plugin for flyweight fields in Rails models

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages