Adds basic ActiveRecord-like associations to static data.
Add this line to your application's Gemfile:
gem "static_association"And then execute:
$ bundle
Or install it yourself as:
$ gem install static_association
Create your static association class:
class Day
  include StaticAssociation
  attr_accessor :name
  record id: 0 do |day|
    day.name = :monday
  end
endCalling record will allow you to create an instance of this static model,
a unique id is mandatory. The newly created object is yielded to the passed
block.
The Day class will gain the following methods:
- .all: returns all the static records defined in the class.
- .ids: returns an array of all the ids of the static records.
- .find: accepts a single id and returns the matching record. If the record does not exist, a- RecordNotFounderror is raised.
- .find_by_id: behaves similarly to the- .findmethod, except it returns- nilwhen a record does not exist.
- .find_by: finds the first record matching the specified conditions. If no record is found, returns- nil.
- find_by!behaves like- find_bybut raises a- StaticAssociation::RecordNotFounderror if no record is found.
- .where: accepts an array of ids and returns all records with matching ids.
Currently just a belongs_to association can be created. This behaviour can be
mixed into an ActiveRecord model:
class Event < ActiveRecord::Base
  extend StaticAssociation::AssociationHelpers
  belongs_to_static :day
endThis assumes your model has a field day_id.
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Run lint checks and tests (bundle exec rake)
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request