Module: YepSearchable::SearchableResource::ClassMethods

Defined in:
lib/yep_searchable/searchable_resource.rb

Instance Method Summary collapse

Instance Method Details

#is_searchable_resource?Boolean

Returns:

  • (Boolean)


30
31
32
33
34
35
36
37
38
39
40
# File 'lib/yep_searchable/searchable_resource.rb', line 30

def is_searchable_resource?
  if YepSearchable.configuration.nil?
    return false
  end
  searchable_columns = YepSearchable.configuration.retrieve_option(self.to_s, 'searchable_columns')
  if searchable_columns.nil? or searchable_columns.empty?
    return false
  else
    return true
  end 
end

#search_by_page(only_ids = false, page = nil, number_of_rows = nil, propagate = true, *query_param) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/yep_searchable/searchable_resource.rb', line 76

def search_by_page(only_ids = false, page = nil, number_of_rows = nil, propagate = true, *query_param)
  while query_param.first.instance_of?(Array)
    query_param = query_param.first
  end
  
  if query_param.size == 1 and query_param.first.include?("$$$")
    query_param = query_param.first.split("$$$")
  end
  
  if query_param.nil? or query_param.empty?
    raise SearchableError, "Arguments not declared in search method"
  end
  
  if page
    is_valid = Integer(page.to_s) rescue false
    if !is_valid and is_valid <= 0
      raise SearchableError, "Argument page must be a valid number"
    end
  end
  
  if number_of_rows
    is_valid = Integer(number_of_rows.to_s) rescue false
    if !is_valid and is_valid <= 0
      raise SearchableError, "Argument number_of_rows must be a valid number"
    end
  end
  
  # query_param = arguments.split("$$$")
  query_string = nil
  
  searchable_columns = YepSearchable.configuration.retrieve_option(self.to_s, 'searchable_columns')
  if not searchable_columns
    raise SearchableError, "Model must have searchable_resource declaration"
  end
  
  query_param.each do |arg|
    if query_string.nil?
      query_string = "("
    else
      query_string = "#{query_string} OR ("
    end
    
    searchable_columns.each do |column|
      if query_string.last == "("
        query_string = "#{query_string} #{column.to_s} LIKE '%#{arg}%'"
      else
        query_string = "#{query_string} OR #{column.to_s} LIKE '%#{arg}%'"
      end
    end
    query_string = "#{query_string})"
  end
  
  if propagate
    relation_query_string = get_where_relations(query_param)
    
    if !relation_query_string.nil? and !relation_query_string.empty?
      if !query_string.nil? and !query_string.empty?
        query_string = "#{query_string} OR #{relation_query_string})"
      else
        query_string = "#{relation_query_string}"
      end
    end
    
    relation_query_string = get_where_yep_resources(query_param)
    
    if !relation_query_string.nil? and !relation_query_string.empty?
      if !query_string.nil? and !query_string.empty?
        query_string = "#{query_string} OR #{relation_query_string})"
      else
        query_string = "#{relation_query_string}"
      end
    end
  end
  
  if only_ids
    entity = []
    
    if page.nil? or number_of_rows.nil?
      entity = self.select(:id).where(query_string)
    else
      entity = self.select(:id).paginate(page: page, per_page: number_of_rows).where(query_string)
    end
    
    return_value = []
    
    entity.each do |ent|
      return_value << ent.id
    end
    
    return return_value
  else
    return_value = []
    if page.nil? or number_of_rows.nil?
      return_value = self.where(query_string)
    else            
      return_value = self.paginate(page: page, per_page: number_of_rows).where(query_string)
    end
    return return_value
  end
  
end

#search_only_ids(propagate, *query_params) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/yep_searchable/searchable_resource.rb', line 59

def search_only_ids(propagate, *query_params)
  if !propagate.to_s.eql?"true" and !propagate.to_s.eql?"false"
    if query_params.nil? or query_params.empty?
      query_params = []  
    end
    
    while query_params.first.instance_of?(Array)
      query_params = query_params.first
    end
    query_params << propagate
    
    propagate = true
  end
  
  return search_by_page(true, nil, nil, propagate, query_params)
end

#search_resource(propagate, *query_params) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/yep_searchable/searchable_resource.rb', line 42

def search_resource(propagate, *query_params)
  if !propagate.to_s.eql?"true" and !propagate.to_s.eql?"false"
    if query_params.nil? or query_params.empty?
      query_params = []  
    end
    
    while query_params.first.instance_of?(Array)
      query_params = query_params.first
    end
    query_params << propagate
    
    propagate = true
  end
  
  return search_by_page(false, nil, nil, propagate, query_params)
end

#searchable_resource(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/yep_searchable/searchable_resource.rb', line 8

def searchable_resource(options = {})
  if not options[:searchable_columns]
    raise SearchableError, "Searchable columns must be initialized on searchable_resource declaration"
  end
  
  attr_accessor :searchable_columns
  YepSearchable.configuration.include_option(self.to_s, "searchable_columns", options[:searchable_columns])
  
  if options[:yep_resources]
    options[:yep_resources].each do |resource|
      if resource[:class_name].nil? or resource[:foreign_key].nil?
        raise SearchableError, "class_name and foreign_key must be declared"
      end
      
      if !resource[:class_name].to_s.camelize.constantize.superclass.to_s.eql?"YepSearchable::YepResource"
        raise SearchableError, "Only classes that implement YepResource are allowed"
      end
    end
    YepSearchable.configuration.include_option(self.to_s, "yep_resources", options[:yep_resources])
  end 
end