HasNamedBootstraps

This is alpha code. The API is subject (and almost certain) to change. It shouldn’t hurt your computer or data, but if it does, I am not obligated to do anything, even feel sorry for you.

has_named_bootstraps is a simple way to load bootstrapped records into class-level constants, making application-level data integrity easier. You can also generate a list in another constant of all such bootstraps, which is handy for generating <select>s and writing certain kinds of tests.

Getting the bootstrapped data in beforehand is up to you. I like seed_fu (github.com/mbleigh/seed-fu) for this personally. If has_named_bootstraps can’t find a bootstrap, by default it’ll raise an error. It’s also possible to have it log a warning and continue, or continue silently.

Example

class Department < ActiveRecord::Base
  has_named_bootstraps(
    :R_AND_D         => 'R&D',
    :MARKETING       => 'Marketing', 
    :HANDSOME_DEVILS => 'Web Development' 
  )
end

>> Department.find(:all)
=> [#<Department id: 1, name: "R&D">, #<Department id: 2, name: "Marketing">, #<Department id: 3, name: "Web Development">]
>> Department::R_AND_D
=> #<Department id: 1, name: "R&D">
>> Department::MARKETING
=> #<Department id: 2, name: "Marketing">
>> Department::HANDSOME_DEVILS
=> #<Department id: 3, name: "Web Development">

With a master list:

class Dog < ActiveRecord::Base
  has_named_bootstraps(
    {:FIDO => 'Fido', :ROVER => 'Rover', :SPOT  => 'Spot'},
    :master_list => :GOOD_DOGS
  )
end

>> Dog.find(:all)
=> [#<Dog id: 1, name: "Spot">, #<Dog id: 2, name: "Fido">, #<Dog id: 3, name: "Rover">]
>> Dog::GOOD_DOGS
=> [#<Dog id: 1, name: "Spot">, #<Dog id: 2, name: "Fido">, #<Dog id: 3, name: "Rover">]

See HasNamedBootstraps::ClassMethods for other options.

TODO

make should_have_named_bootstraps detect multiple missing bootstraps (not just the first)

write tests for shoulda macros, document same

Rails 3 compatibility

Markdown README for GitHub

License and copyright

Copyright © 2010 Children’s Hospital Boston, released under the GNU Lesser General Public License.

Acknowledgements

Thanks to Wyatt Greene for feedback, Dan Croak for patches and Harold Gimenez for Markdown advice. Boston.rb can beat up your Ruby group.