Module: SimpleMysqlApi::SimpleMysqlApiMethods::ClassMethods

Defined in:
lib/simple_mysql_api/class_methods.rb

Overview

Class methods for modles

Instance Method Summary collapse

Instance Method Details

#attributes(c_name = nil) ⇒ Object

Returns the hash of attributes of the table and their type eg. User.attributes returns [:type=>“integer”, :param_name=>:id, :type=>“string”, :param_name=>:name]



22
23
24
25
26
27
# File 'lib/simple_mysql_api/class_methods.rb', line 22

def attributes(c_name=nil)
  self.columns.inject([]) do |r,e|
    r << {:attr=>e.name, :type=>e.type.to_s, :param_name => (c_name ? (c_name.downcase+ "_" + e.name) : e.name).to_sym }
    r
  end      
end

#belongs_to_search(act_relation, params) ⇒ Object

For joining belongs to relational models and searches their params



145
146
147
148
149
150
151
152
# File 'lib/simple_mysql_api/class_methods.rb', line 145

def belongs_to_search(act_relation, params)
  self.tables_by_relation(:belongs_to).each do |c_name,values|
    act_relation = act_relation.joins("LEFT JOIN #{values[:t_name]} #{values[:t_name]} ON #{self.table_name}.#{values[:f_key]}=#{values[:t_name]}.#{c_name.constantize.pri_key}")
    new_attributes = c_name.constantize.searchable_attributes(c_name)#c_name.constantize.searchable_attributes
    act_relation = search_conditions(new_attributes,params,act_relation,c_name)
  end
  act_relation
end

#boolean_search(attr, params, act_relation, t_type) ⇒ Object

for boolean type eg. price=“true”



115
116
117
118
# File 'lib/simple_mysql_api/class_methods.rb', line 115

def boolean_search(attr,params,act_relation,t_type)
  val = params[attr[:param_name]].match(/(true|t|yes|y|1)$/i) != nil ? 1 : 0
  act_relation = act_relation.where(["CAST(#{t_type.constantize.table_name}.#{attr[:attr]} AS CHAR) = ?",val.to_s])
end

#date_search(attr, params, act_relation, t_type) ⇒ Object

for the datetime eg. created_at =“2012-08-28 16:21:37 0530” created_at = “2012-08-28 16:21:37 0530..2012-09-28 16:21:37 0530”“ created_at=”<2012-08-28 16:21:37 0530“ created_at=”>2012-08-28 16:21:37 +0530“



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/simple_mysql_api/class_methods.rb', line 89

def date_search(attr,params,act_relation,t_type)
  attr_opp =  /[<,>]/.match(params[attr[:param_name]]).to_s + /[=]/.match(params[attr[:param_name]]).to_s
  attr_opp = params[attr[:param_name]].index("..") ? ".." : attr_opp
  table_name = "#{t_type.constantize.table_name}" 
  case(attr_opp)
    when ".."
      attr_val = params[attr[:param_name]].split("..").inject([]){|r,e| r << e}
      act_relation = act_relation.where(["#{table_name}.#{attr[:attr]} >= ? and #{table_name}.#{attr[:attr]} <= ?",attr_val[0],attr_val[1]]) if attr_val
    when "<",">","<=",">="
      attr_val = params[attr[:param_name]].split(attr_opp)[1]
      act_relation = act_relation.where(["#{table_name}.#{attr[:attr]} #{attr_opp} ?",attr_val]) if attr_val
    else
      attr_val = params[attr[:param_name]].split(attr_opp)[1]
      act_relation = act_relation.where(["#{table_name}.#{attr[:attr]} = ?",attr_val]) if attr_val
   end if attr_opp
   act_relation
end

#equal_search(attr, params, act_relation, t_type) ⇒ Object

for any type eg. price=“12”



109
110
111
# File 'lib/simple_mysql_api/class_methods.rb', line 109

def equal_search(attr,params,act_relation,t_type)
  act_relation = act_relation.where(["#{t_type.constantize.table_name}.#{attr[:attr]} = ?",params[attr[:param_name]]])
end

#foreign_keysObject

Returns all foreign keys of the model



121
122
123
124
125
126
# File 'lib/simple_mysql_api/class_methods.rb', line 121

def foreign_keys
  self.reflect_on_all_associations.inject([]) do |r, e|
    r << e.foreign_key
    r
  end.uniq
end

#has_many_search(act_relation, params) ⇒ Object

For joining has many relational models and searches their params



155
156
157
158
159
160
161
162
# File 'lib/simple_mysql_api/class_methods.rb', line 155

def has_many_search(act_relation, params)
  self.tables_by_relation(:has_many).each do |c_name,values|
    act_relation = act_relation.joins("LEFT JOIN #{values[:t_name]} #{values[:t_name]} ON #{self.table_name}.#{self.pri_key}=#{values[:t_name]}.#{values[:f_key]}")
    new_attributes = c_name.constantize.searchable_attributes(c_name)#c_name.constantize.searchable_attributes
    act_relation = search_conditions(new_attributes,params,act_relation,c_name)
  end
  act_relation
end

#mysql_search(options = {}) ⇒ Object

Main method: Used for search You have params= “Joh”, city: “New” set has_many, belongs_to to true if you want to search for associated models Search: User.search(params, has_many: true, belongs_to: false)



133
134
135
136
137
138
139
140
141
142
# File 'lib/simple_mysql_api/class_methods.rb', line 133

def mysql_search(options={})
  params = options[:search_params]
  search_params = options[:custom_params] || nil
  act_relation = self
  attributes = (search_params||self.attributes).delete_if{|v| (self.primary_key==v[:attr].to_s || self.foreign_keys.include?(v[:attr].to_s)) }
  act_relation = belongs_to_search(act_relation, params) if options[:belongs_to] && options[:belongs_to]==true
  act_relation = has_many_search(act_relation, params) if options[:has_many] && options[:has_many]==true
  act_relation = search_conditions(attributes,params,act_relation,self.to_s)
  act_relation.select("DISTINCT #{self.table_name}.*")
end

#pri_keyObject

Returns the primary key



30
31
32
# File 'lib/simple_mysql_api/class_methods.rb', line 30

def pri_key
  self.primary_key
end

#range_search(attr, params, act_relation, t_type) ⇒ Object

for the integer, float attributes eg. price=“12” price=“12-100” price=“<100” price=“>100”



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/simple_mysql_api/class_methods.rb', line 67

def range_search(attr,params,act_relation,t_type)
  attr_opp =  /[-,<,>]/.match(params[attr[:param_name]]).to_s + /[=]/.match(params[attr[:param_name]]).to_s
  table_name = "#{t_type.constantize.table_name}" 
  case(attr_opp)
    when "-"
     attr_val = params[attr[:param_name]].split("-").inject([]){|r,e| r << e.to_f}
     act_relation = act_relation.where(["CAST(#{table_name}.#{attr[:attr]} AS DECIMAL) >= ? and CAST(#{table_name}.#{attr[:attr]} AS DECIMAL) <= ?",attr_val[0],attr_val[1]]) if attr_val
    when "<",">","<=",">="
     attr_val = params[attr[:param_name]].split(attr_opp)[1].to_f
     act_relation = act_relation.where(["CAST(#{table_name}.#{attr[:attr]} AS DECIMAL) #{attr_opp} ?",attr_val]) if attr_val
    else
     attr_val = params[attr[:param_name]].to_f
     act_relation = act_relation.where(["CAST(#{table_name}.#{attr[:attr]} AS DECIMAL) = ?",attr_val]) if attr_val
   end if attr_opp
   act_relation
end

#search_conditions(attributes, params, act_relation, t_type) ⇒ Object

Adds the conditions for the attributes passed based on the attributes type



40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/simple_mysql_api/class_methods.rb', line 40

def search_conditions(attributes,params,act_relation,t_type)
  attributes.each do |value|
    case(value[:type])
      when "string","text"
        act_relation = text_search(value,params,act_relation,t_type) 
      when "integer","float"
        act_relation = range_search(value,params,act_relation,t_type)
      when "datetime"
        act_relation = date_search(value,params,act_relation,t_type)
      when "boolean"
        act_relation = boolean_search(value,params,act_relation,t_type)
    end if params[value[:param_name]]
  end
  act_relation
end

#searchable_attributes(c_name = nil) ⇒ Object

returns all the attributes except primary keys and foreign keys



35
36
37
# File 'lib/simple_mysql_api/class_methods.rb', line 35

def searchable_attributes(c_name=nil)
  self.attributes(c_name).delete_if{|v| (self.primary_key==v[:attr].to_s || self.foreign_keys.include?(v[:attr].to_s)) }
end

#tables_by_relation(rel) ⇒ Object

Returns the asscociated models with their table name, class name, forignkey eg. User.tables_by_relation(:has_many) returns :f_key=>“user_id”}



12
13
14
15
16
17
# File 'lib/simple_mysql_api/class_methods.rb', line 12

def tables_by_relation(rel)
  self.reflect_on_all_associations(rel).inject({}) do |r, e|
    r[e.class_name] = {:t_name=> e.table_name, :f_key=> e.foreign_key } 
    r
  end
end

#text_search(attr, params, act_relation, t_type) ⇒ Object

for the text search if it contains the string



57
58
59
60
# File 'lib/simple_mysql_api/class_methods.rb', line 57

def text_search(attr,params,act_relation,t_type)
  obj = "%#{params[attr[:param_name]].downcase}%"
  act_relation.where(["LOWER(#{t_type.constantize.table_name}.#{attr[:attr]}) like ?",obj])
end