Module: FastExists::ActiveRecord::Extension::ClassMethods

Defined in:
lib/fast_exists/active_record/extension.rb

Instance Method Summary collapse

Instance Method Details

#fast_exists(*attributes) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/fast_exists/active_record/extension.rb', line 11

def fast_exists(*attributes)
  options = attributes.extract_options!

  class_attribute :fast_exists_attributes, default: {} unless respond_to?(:fast_exists_attributes)

  attributes.map(&:to_sym).each do |attr|
    self.fast_exists_attributes = fast_exists_attributes.merge(attr => options)

    # Generate attribute_exists?(val)
    singleton_class.define_method("#{attr}_exists?") do |value|
      fast_exists?(attr, value)
    end

    # Generate attribute_available?(val)
    singleton_class.define_method("#{attr}_available?") do |value|
      fast_available?(attr, value)
    end
  end

  extend FastExists::ActiveRecord::ModelMethods
  include FastExists::ActiveRecord::Hooks
end