Module: ActiveRecordBaseExtension::ClassMethods

Defined in:
lib/baseapi/active_record/base_extension.rb

Instance Method Summary collapse

Instance Method Details

#_allObject

override if necessary

Returns:

  • ActiveRecordRelation



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    table table name

  • Hash

    hash column name => search values



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    table table name

  • Hash

    hash column name => search values



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    column column name

  • Array/String

    values search values



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    column column name

  • Array/String

    values search values

  • Callable

    callable Arel



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    column column name

  • Array/String

    values search values



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    column column name

  • Array/String

    values search values



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

Parameters:

  • String

    relate ‘belongs_to’,‘hasmany’..

Returns:

  • Hash associations relation name => talbe name array



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_prefixObject

reserved word prefix(count,page,order,orderby…)

Returns:

  • String



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

Parameters:

  • ActiveRecordRelation

    models

  • Hash

    hash column name => search values

  • Callable

    callable Arel

  • String (Hash)

    a customizable set of options



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    table table name

  • Hash

    hash column name => search values



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    table table name

  • Hash

    hash column name => search values



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

Parameters:

  • Hash

    params getparams String/Array column search column value String orderby order column String order sort order (‘asc’ or ‘desc’) Integer count page count (all = ‘0’ or ‘-1’) Integer page paged



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