module Maini::Utils::ActiveRecord::Inquirer::ClassMethods

Public Instance Methods

inquirer(field, *options) click to toggle source

Allows you to automatically create inquiry methods for string field. For example, if you have an Order model which has a status field containing 'approved' or 'delivered' you may wish to have a approved? or delivered? method on the model.

class Order < ActiveRecord::Baser
  STATUSES = ['approved', 'delivered']
  inquirer :status, *STATUSES
end

order = Order.new(:status => 'approved')
order.approved?       #=> true
order.delivered?      #=> false
# File lib/maini/utils/active_record/inquirer.rb, line 27
def inquirer(field, *options)
  options.each do |option|
    define_method "#{option}?" do
      self.read_attribute(field).to_s == option.to_s
    end
  end
end