Module: ActiveRecordBaseExtension::ClassMethods
- Defined in:
- lib/baseapi/active_record/base_extension.rb
Instance Method Summary collapse
-
#_all ⇒ Object
override if necessary.
-
#_belongs_to(models, table, hash) ⇒ Object
override or create method ‘belongs_totable’ if necessary.
-
#_has_many(models, table, hash) ⇒ Object
override or create method ‘has_manytable’ if necessary.
-
#_where(models, column, values) ⇒ Object
override or create method ‘wherecolumn’ if necessary.
-
#clean_hash!(param) ⇒ Object
hash params empty delete.
- #column_call(models, column, values, callable, operator: 'or') ⇒ Object
-
#column_like(models, column, values, operator: 'or') ⇒ Object
column like search.
-
#column_match(models, column, values, operator: 'or') ⇒ Object
column exact match search.
-
#escape_like(string) ⇒ Object
escape like.
-
#get_associations(relate = nil) ⇒ Object
get relation tables.
-
#get_reserved_word_prefix ⇒ Object
reserved word prefix(count,page,order,orderby…).
-
#getNaturalValue(value) ⇒ Object
removal of the enclosing.
-
#getOperator(value, default = '=') ⇒ Object
return = or IS.
-
#getPrefix(value) ⇒ Object
get sql prefix ‘NOT’.
-
#getValue(column, value, *wraps) ⇒ Object
slice ‘!’ value.
- #relation_call(models, table, hash, callable, operator: 'or') ⇒ Object
-
#relation_like(models, table, hash, operator: 'or') ⇒ Object
like search.
-
#relation_match(models, table, hash, operator: 'or') ⇒ Object
Exact match search.
- #search(params) ⇒ Object
Instance Method Details
#_all ⇒ Object
override if necessary
20 21 22 |
# File 'lib/baseapi/active_record/base_extension.rb', line 20 def _all self.all end |
#_belongs_to(models, table, hash) ⇒ Object
override or create method ‘belongs_totable’ if necessary
70 71 72 |
# File 'lib/baseapi/active_record/base_extension.rb', line 70 def _belongs_to(models, table, hash) relation_match(models, table, hash) end |
#_has_many(models, table, hash) ⇒ Object
override or create method ‘has_manytable’ if necessary
78 79 80 |
# File 'lib/baseapi/active_record/base_extension.rb', line 78 def _has_many(models, table, hash) relation_match(models, table, hash) end |
#_where(models, column, values) ⇒ Object
override or create method ‘wherecolumn’ if necessary
28 29 30 |
# File 'lib/baseapi/active_record/base_extension.rb', line 28 def _where(models, column, values) column_match(models, column, values) end |
#clean_hash!(param) ⇒ Object
hash params empty delete
235 236 237 238 239 240 241 242 243 244 245 |
# File 'lib/baseapi/active_record/base_extension.rb', line 235 def clean_hash!(param) recursive_delete_if = -> (param) { param.each do |key, value| if value.is_a?(Hash) recursive_delete_if.call(value) end end param.delete_if { |k, v| v.blank? } } recursive_delete_if.call(param) if param.is_a?(Hash) end |
#column_call(models, column, values, callable, operator: 'or') ⇒ Object
59 60 61 62 63 |
# File 'lib/baseapi/active_record/base_extension.rb', line 59 def column_call(models, column, values, callable, operator:'or') column_values = values.instance_of?(Array) ? values : [values] models.where!(column_values.map{|value| callable.call(column, value)}.join(" #{operator} ")) models end |
#column_like(models, column, values, operator: 'or') ⇒ Object
column like search
48 49 50 51 52 |
# File 'lib/baseapi/active_record/base_extension.rb', line 48 def column_like(models, column, values, operator:'or') column_call(models, column, values, ->(column, value){ "#{getPrefix(value)} #{models.name.pluralize.underscore}.#{column} #{getOperator(value, 'like')} #{getValue("#{models.name.pluralize.underscore}.#{column}", escape_like(value), "%", "'")}" }, operator:operator) end |
#column_match(models, column, values, operator: 'or') ⇒ Object
column exact match search
37 38 39 40 41 |
# File 'lib/baseapi/active_record/base_extension.rb', line 37 def column_match(models, column, values, operator:'or') column_call(models, column, values, ->(column, value){ "#{getPrefix(value)} #{models.name.pluralize.underscore}.#{column} #{getOperator(value)} #{getValue("#{models.name.pluralize.underscore}.#{column}", value, "'")}" }, operator:operator) end |
#escape_like(string) ⇒ Object
escape like
185 186 187 |
# File 'lib/baseapi/active_record/base_extension.rb', line 185 def escape_like(string) string.gsub(/[\\%_]/){|m| "\\#{m}"} end |
#get_associations(relate = nil) ⇒ Object
get relation tables
193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
# File 'lib/baseapi/active_record/base_extension.rb', line 193 def get_associations(relate = nil) associations = { 'belongs_to' => [], 'has_many' => [], } self.reflect_on_all_associations.each do |association| associations.each do |key, value| if association.class.to_s == "ActiveRecord::Reflection::#{key.camelize}Reflection" associations[key].push(association.name.to_s) end end end relate.present? ? associations[relate] : associations end |
#get_reserved_word_prefix ⇒ Object
reserved word prefix(count,page,order,orderby…)
14 15 16 |
# File 'lib/baseapi/active_record/base_extension.rb', line 14 def get_reserved_word_prefix @reserved_word_prefix end |
#getNaturalValue(value) ⇒ Object
removal of the enclosing
174 175 176 177 178 179 180 |
# File 'lib/baseapi/active_record/base_extension.rb', line 174 def getNaturalValue(value) val = value.clone if ((/^[\'].+?[\']$/ =~ val) != nil) or ((/^[\"].+?[\"]$/ =~ val) != nil) val = val[1..val.length-2] end val end |
#getOperator(value, default = '=') ⇒ Object
return = or IS
129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/baseapi/active_record/base_extension.rb', line 129 def getOperator(value, default = '=') operator = default val = value.clone val.slice!(0) if val[0] == '!' if ['NULL', 'EMPTY'].include?(val.upcase) operator = 'IS' elsif val.length >= 2 and ['<=', '>='].include?(val[0..1]) operator = val[0..1] elsif ['<', '>'].include?(val[0]) operator = val[0] end operator end |
#getPrefix(value) ⇒ Object
get sql prefix ‘NOT’
122 123 124 |
# File 'lib/baseapi/active_record/base_extension.rb', line 122 def getPrefix(value) (value[0] == '!') ? 'NOT' : '' end |
#getValue(column, value, *wraps) ⇒ Object
slice ‘!’ value
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
# File 'lib/baseapi/active_record/base_extension.rb', line 148 def getValue(column, value, *wraps) original = value.clone val = value.clone val.slice!(0) if val[0] == '!' if val.upcase == 'NULL' val = 'NULL' elsif val.upcase == 'EMPTY' prefix = getPrefix(original) operator = prefix == 'NOT' ? 'AND' : 'OR' val = "NULL #{operator} #{prefix} #{column} = ''" elsif val.length >= 2 and ['<=', '>='].include?(val[0..1]) val.sub!(val[0..1], '') elsif ['<', '>'].include?(value[0]) val.sub!(val[0], '') else val = getNaturalValue(val) wraps.each do |wrap| val = "#{wrap}#{val}#{wrap}" end end return val end |
#relation_call(models, table, hash, callable, operator: 'or') ⇒ Object
109 110 111 112 113 114 115 116 117 |
# File 'lib/baseapi/active_record/base_extension.rb', line 109 def relation_call(models, table, hash, callable, operator:'or') hash.each do |column, value| if column.present? and value.present? relation_values = value.instance_of?(Array) ? value : [value] models.where!(relation_values.map{|value| callable.call(table.pluralize, column, value)}.join(" #{operator} ")) end end models end |
#relation_like(models, table, hash, operator: 'or') ⇒ Object
like search
98 99 100 101 102 |
# File 'lib/baseapi/active_record/base_extension.rb', line 98 def relation_like(models, table, hash, operator:'or') relation_call(models, table, hash, ->(table, column, value){ "#{getPrefix(value)} #{table}.#{column} #{getOperator(value, 'like')} #{getValue("#{table}.#{column}", escape_like(value), "%", "'")}" }, operator:operator) end |
#relation_match(models, table, hash, operator: 'or') ⇒ Object
Exact match search
87 88 89 90 91 |
# File 'lib/baseapi/active_record/base_extension.rb', line 87 def relation_match(models, table, hash, operator:'or') relation_call(models, table, hash, ->(table, column, value){ "#{getPrefix(value)} #{table}.#{column} #{getOperator(value)} #{getValue("#{table}.#{column}", value, "'")}" }, operator:operator) end |
#search(params) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
# File 'lib/baseapi/active_record/base_extension.rb', line 215 def search(params) models = self._all # recursive empty delete clean_hash!(params) # filter models.filtering!(params) # pager models.paging!(params) # sort models.sorting!(params) return models.uniq end |