Module: LegacyMigrations::Squirrel::Hook

Included in:
ActiveRecord::Base
Defined in:
lib/legacy_migrations/squirrel/squirrel.rb

Overview

When included in AR::Base, it chains the #find method to allow for block execution.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 30

def self.included base
  if ! base.instance_methods.include?('find_without_squirrel') &&
       base.instance_methods.include?('find')
    base.class_eval do
      alias_method :find_without_squirrel, :find
      alias_method :find, :find_with_squirrel
    end
  end
  if ! base.instance_methods.include?('scoped_without_squirrel') &&
       base.instance_methods.include?('scoped')
    base.class_eval do
      alias_method :scoped_without_squirrel, :scoped
      alias_method :scoped, :scoped_with_squirrel
    end
  end
end

Instance Method Details

#find_with_squirrel(*args, &blk) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 11

def find_with_squirrel *args, &blk
  args ||= [:all]
  if blk || (args.last.is_a?(Hash) && args.last.has_key?(:paginate))
    query = Query.new(self, &blk)
    query.execute(*args)
  else
    find_without_squirrel(*args)
  end
end

#scoped_with_squirrel(*args, &blk) ⇒ Object



21
22
23
24
25
26
27
28
# File 'lib/legacy_migrations/squirrel/squirrel.rb', line 21

def scoped_with_squirrel *args, &blk
  if blk
    query = Query.new(self, &blk)
    self.scoped(query.to_find_parameters)
  else
    scoped_without_squirrel(*args)
  end
end