/
associations.rb
73 lines (64 loc) · 2.87 KB
/
associations.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
module Searchgasm
module ActiveRecord
# = Searchgasm ActiveRecord Associations
#
# These methods hook into ActiveRecords association methods and add in searchgasm functionality.
module Associations
module AssociationCollection
# This is an alias method chain. It hook into ActiveRecord's "find" method for associations and checks to see if Searchgasm should get involved.
def find_with_searchgasm(*args)
options = args.extract_options!
args << sanitize_options_with_searchgasm(options)
find_without_searchgasm(*args)
end
# See build_conditions under Searchgasm::ActiveRecord::Base. This is the same thing but for associations.
def build_conditions(options = {}, &block)
conditions = @reflection.klass.build_conditions(options, &block)
conditions.scope = scope(:find)[:conditions]
conditions
end
# See build_conditions! under Searchgasm::ActiveRecord::Base. This is the same thing but for associations.
def build_conditions!(options = {}, &block)
conditions = @reflection.klass.build_conditions!(options, &block)
conditions.scope = scope(:find)[:conditions]
conditions
end
# See build_search under Searchgasm::ActiveRecord::Base. This is the same thing but for associations.
def build_search(options = {}, &block)
conditions = @reflection.klass.build_search(options, &block)
conditions.scope = scope(:find)[:conditions]
conditions
end
# See build_conditions! under Searchgasm::ActiveRecord::Base. This is the same thing but for associations.
def build_search!(options = {}, &block)
conditions = @reflection.klass.build_search!(options, &block)
conditions.scope = scope(:find)[:conditions]
conditions
end
end
module HasManyAssociation
# This is an alias method chain. It hook into ActiveRecord's "calculate" method for has many associations and checks to see if Searchgasm should get involved.
def count_with_searchgasm(*args)
column_name, options = @reflection.klass.send(:construct_count_options_from_args, *args)
count_without_searchgasm(column_name, sanitize_options_with_searchgasm(options))
end
end
end
end
end
ActiveRecord::Associations::AssociationCollection.send(:include, Searchgasm::ActiveRecord::Associations::AssociationCollection)
module ActiveRecord
module Associations
class AssociationCollection
alias_method_chain :find, :searchgasm
end
end
end
ActiveRecord::Associations::HasManyAssociation.send(:include, Searchgasm::ActiveRecord::Associations::HasManyAssociation)
module ActiveRecord
module Associations
class HasManyAssociation
alias_method_chain :count, :searchgasm
end
end
end