Sha256: ae49108b0ea9a45a9fb51f8ec46cbad367df1cf951a1cd34c59694841282ac5b
Contents?: true
Size: 1.7 KB
Versions: 1
Compression:
Stored size: 1.7 KB
Contents
module LookUp def lookup(table) model = table.to_s.camelize.constantize foreign_key = table.to_s.downcase.concat('_id') # getter define_method table.to_s do model.try(:find_by_id, read_attribute(foreign_key.to_sym)) end # setter that accepts :value, 'value' or Value object define_method "#{table.to_s}=" do |value| record = value.try(:is_a?, model) ? value : model.try(:find_or_create_by_name, value.to_s) write_attribute(foreign_key, record.id) unless respond_to?("#{record.name.downcase}?".to_sym) instance_exec(self) do |instance| instance.class.send(:define_method,"#{record.name.downcase}?") do instance.send(foreign_key.to_sym) == record.id end end self.class.singleton_class.instance_eval do define_method record.name.downcase.pluralize do includes(table.to_s.pluralize.to_sym).where(:status_id => record.id ) end end end end # getter for dirty attributes define_method "#{table.to_s}_was" do model.try(:find_by_id, attribute_was(foreign_key)) end model.all.each do |record| # returns collection of each lookup object eg Model.todos define_singleton_method record.name.downcase.pluralize do includes(table.to_s.pluralize.to_sym).where(foreign_key.to_sym => record.id ) end # adds boolean methods to check whether an object is of lookup type eg. @instance.pending? define_method "#{record.name.downcase}?" do send(foreign_key.to_sym) == record.id end end end end class ActiveRecord::Base extend LookUp end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
rails_look_up_table-0.6.0 | lib/rails_look_up_table.rb |