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.
-
#get_associations(relate = nil) ⇒ Object
get relation tables.
- #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
12 13 14 |
# File 'lib/baseapi/active_record/base_extension.rb', line 12 def _all self.all end |
#_belongs_to(models, table, hash) ⇒ Object
override or create method ‘belongs_totable’ if necessary
28 29 30 |
# File 'lib/baseapi/active_record/base_extension.rb', line 28 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
36 37 38 |
# File 'lib/baseapi/active_record/base_extension.rb', line 36 def _has_many(models, table, hash) relation_match(models, table, hash) end |
#_where(models, column, values) ⇒ Object
override or create method ‘wherecolumn’ if necessary
20 21 22 |
# File 'lib/baseapi/active_record/base_extension.rb', line 20 def _where(models, column, values) model.where!(column => values) end |
#get_associations(relate = nil) ⇒ Object
get relation tables
81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/baseapi/active_record/base_extension.rb', line 81 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 |
#relation_call(models, table, hash, callable, operator: 'or') ⇒ Object
67 68 69 70 71 72 73 74 75 76 |
# File 'lib/baseapi/active_record/base_extension.rb', line 67 def relation_call(models, table, hash, callable, operator:'or') models.joins!(table.to_sym) 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
56 57 58 59 60 |
# File 'lib/baseapi/active_record/base_extension.rb', line 56 def relation_like(models, table, hash, operator:'or') relation_call(models, table, hash, ->(table, column, value){ "#{table}.#{column} like '%#{value}%'" }, operator:operator) end |
#relation_match(models, table, hash, operator: 'or') ⇒ Object
Exact match search
45 46 47 48 49 |
# File 'lib/baseapi/active_record/base_extension.rb', line 45 def relation_match(models, table, hash, operator:'or') relation_call(models, table, hash, ->(table, column, value){ "#{table}.#{column} = '#{value}'" }, operator:operator) end |
#search(params) ⇒ Object
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/baseapi/active_record/base_extension.rb', line 103 def search(params) models = self._all # load self.get_associations().each do |association_key, relations| relations.each do |relation| models.includes!(relation.to_sym) end end # filter models.filtering!(params) # pager models.paging!(params) # sort models.sorting!(params) return models.uniq end |