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.
- #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.
-
#get_associations(relate = nil) ⇒ Object
get relation tables.
-
#get_reserved_word_prefix ⇒ Object
reserved word prefix(count,page,order,orderby…).
- #relation_call(models, 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
44 45 46 |
# File 'lib/baseapi/active_record/base_extension.rb', line 44 def _all self.all end |
#_belongs_to(models, table, hash) ⇒ Object
override or create method ‘belongs_totable’ if necessary
97 98 99 |
# File 'lib/baseapi/active_record/base_extension.rb', line 97 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
105 106 107 |
# File 'lib/baseapi/active_record/base_extension.rb', line 105 def _has_many(models, table, hash) relation_match(models, table, hash) end |
#_where(models, column, values) ⇒ Object
override or create method ‘wherecolumn’ if necessary
52 53 54 |
# File 'lib/baseapi/active_record/base_extension.rb', line 52 def _where(models, column, values) column_match(models, column, values) end |
#column_call(models, column, values, callable, operator: 'or') ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 91 |
# File 'lib/baseapi/active_record/base_extension.rb', line 80 def column_call(models, column, values, callable, operator:'or') base_arel = nil column_values = values.instance_of?(Array) ? values : [values] column_values.each do |value| if column.present? and value.present? arel = callable.call(column, value) base_arel = arel_merge(base_arel, arel, operator: operator) end end models.where!(base_arel) models end |
#column_like(models, column, values, operator: 'or') ⇒ Object
column like search
71 72 73 74 |
# File 'lib/baseapi/active_record/base_extension.rb', line 71 def column_like(models, column, values, operator:'or') callable = arel_like(self) column_call(models, column, values, callable, operator: operator) end |
#column_match(models, column, values, operator: 'or') ⇒ Object
column exact match search
61 62 63 64 |
# File 'lib/baseapi/active_record/base_extension.rb', line 61 def column_match(models, column, values, operator: 'or') callable = arel_match(self) column_call(models, column, values, callable, operator: operator) end |
#get_associations(relate = nil) ⇒ Object
get relation tables
153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/baseapi/active_record/base_extension.rb', line 153 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…)
38 39 40 |
# File 'lib/baseapi/active_record/base_extension.rb', line 38 def get_reserved_word_prefix @reserved_word_prefix end |
#relation_call(models, hash, callable, operator: 'or') ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/baseapi/active_record/base_extension.rb', line 133 def relation_call(models, hash, callable, operator:'or') base_arel = nil hash.each do |column, values| if column.present? and values.present? relation_values = values.instance_of?(Array) ? values : [values] relation_values.each do |value| if column.present? and value.present? arel = callable.call(column, value) base_arel = arel_merge(base_arel, arel, operator: operator) end end end end models.where!(base_arel) models end |
#relation_like(models, table, hash, operator: 'or') ⇒ Object
like search
124 125 126 127 |
# File 'lib/baseapi/active_record/base_extension.rb', line 124 def relation_like(models, table, hash, operator: 'or') callable = arel_like(table.camelize.singularize.constantize) relation_call(models, hash, callable, operator: operator) end |
#relation_match(models, table, hash, operator: 'or') ⇒ Object
Exact match search
114 115 116 117 |
# File 'lib/baseapi/active_record/base_extension.rb', line 114 def relation_match(models, table, hash, operator: 'or') callable = arel_match(table.camelize.singularize.constantize) relation_call(models, hash, callable, operator: operator) end |
#search(params) ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/baseapi/active_record/base_extension.rb', line 18 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 |