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
74
75
76
|
# File 'lib/activerecord/base.rb', line 13
def inherited(subclass)
super
return if subclass.to_s.underscore == "active_record/schema_migration"
class_eval " class << \#{subclass}\n attr_reader :search_option, :search_fields\n\n def search_\#{subclass.to_s.underscore}(words, option = nil, fields = [])\n formatted_words = format_search(words, option)\n\n fields = fields.empty? ? @search_fields.dup : fields\n\n search_scope = []\n search_params = {}\n fields.each do |field|\n search_params[field] = formatted_words\n search_scope << field.to_s + \" LIKE :\" + field.to_s\n end\n\n where([search_scope.join(\" OR \"), search_params])\n end\n\n def start_with(words)\n search_\#{subclass.to_s.underscore}(words, :start_with)\n end\n\n def end_with(words)\n search_\#{subclass.to_s.underscore}(words, :end_with)\n end\n\n def search_anywhere(words)\n search_\#{subclass.to_s.underscore}(words, :anywhere)\n end\n\n def search_fields(fields)\n @search_fields = [fields].flatten\n end\n alias_method :search_field, :search_fields\n\n def format_search(words, option)\n if !option.nil?\n format_with_option(words, option)\n else\n o = @search_option.nil? ? :anywhere : @search_option\n format_with_option(words, o)\n end\n end\n\n def format_with_option(words, option)\n case option\n when :start_with then words + '%'\n when :end_with then '%' + words\n when :anywhere then '%' + words + '%'\n else\n raise SearchError\n end\n end\n\n def search_option(option = :anywhere)\n @search_option = option\n end\n end\n EOF\nend\n"
|