Sha256: b9aa85093854713748006f0c730b07394a4a5224e70add0cd77d84b56d1cdb1c

Contents?: true

Size: 1.63 KB

Versions: 2

Compression:

Stored size: 1.63 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
              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
          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

2 entries across 2 versions & 1 rubygems

Version Path
rails_look_up_table-1.0.0 lib/rails_look_up_table.rb
rails_look_up_table-0.4.0 lib/rails_look_up_table.rb