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



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

Parameters:

  • ActiveRecordRelation

    models

  • String

    table table name

  • Hash

    hash column name => search values



62
63
64
# File 'lib/baseapi/active_record/base_extension.rb', line 62

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



70
71
72
# File 'lib/baseapi/active_record/base_extension.rb', line 70

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



20
21
22
# File 'lib/baseapi/active_record/base_extension.rb', line 20

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



51
52
53
54
55
# File 'lib/baseapi/active_record/base_extension.rb', line 51

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

Parameters:

  • ActiveRecordRelation

    models

  • String

    column column name

  • Array/String

    values search values



40
41
42
43
44
# File 'lib/baseapi/active_record/base_extension.rb', line 40

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}", value, "%", "'")}"
  }, 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



29
30
31
32
33
# File 'lib/baseapi/active_record/base_extension.rb', line 29

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

#get_associations(relate = nil) ⇒ Object

get relation tables

Parameters:

  • String

    relate ‘belongs_to’,‘hasmany’..

Returns:

  • Hash associations relation name => talbe name array



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/baseapi/active_record/base_extension.rb', line 162

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

#getNaturalValue(value) ⇒ Object

removal of the enclosing

Parameters:

  • String

    value

Returns:

  • String value



151
152
153
154
155
156
# File 'lib/baseapi/active_record/base_extension.rb', line 151

def getNaturalValue(value)
  if ((/^[\'].+?[\']$/ =~ value) != nil) and ((/^[\"].+?[\"]$/ =~ value) != nil)
    value.gsub!(/^[\'\"]/, '').gsub!(/[\'\"]$/, '')
  end
  value
end

#getOperator(value, default = '=') ⇒ Object

return = or IS

Parameters:

  • String

    value

Returns:

  • String operator



121
122
123
# File 'lib/baseapi/active_record/base_extension.rb', line 121

def getOperator(value, default = '=')
  ['NULL', 'EMPTY'].include?(value.gsub('!', '').upcase) ? 'IS' : default
end

#getPrefix(value) ⇒ Object

get sql prefix ‘NOT’

Parameters:

  • String

    value

Returns:

  • String value



114
115
116
# File 'lib/baseapi/active_record/base_extension.rb', line 114

def getPrefix(value)
  (value[0] == '!') ? 'NOT' : ''
end

#getValue(column, value, *wraps) ⇒ Object

slice ‘!’ value

Parameters:

  • String

    column

  • String

    value

  • String

    wraps ‘ or %

Returns:

  • String value or sql



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/baseapi/active_record/base_extension.rb', line 130

def getValue(column, value, *wraps)
  original = value.clone
  value.slice!(0) if value[0] == '!'
  if value.upcase == 'NULL'
    value = 'NULL'
  elsif value.upcase == 'EMPTY'
    prefix = getPrefix(original)
    operator = prefix == 'NOT' ? 'AND' : 'OR'
    value = "NULL #{operator} #{prefix} #{column} = ''"
  else
    value = getNaturalValue(value)
    wraps.each do |wrap|
      value = "#{wrap}#{value}#{wrap}"
    end
  end
  return value
end

#relation_call(models, table, hash, callable, operator: 'or') ⇒ Object

Parameters:

  • ActiveRecordRelation

    models

  • String

    table table name

  • Hash

    hash column name => search values

  • Callable

    callable



101
102
103
104
105
106
107
108
109
# File 'lib/baseapi/active_record/base_extension.rb', line 101

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

Parameters:

  • ActiveRecordRelation

    models

  • String

    table table name

  • Hash

    hash column name => search values



90
91
92
93
94
# File 'lib/baseapi/active_record/base_extension.rb', line 90

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}", value, "%", "'")}"
  }, 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



79
80
81
82
83
# File 'lib/baseapi/active_record/base_extension.rb', line 79

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

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



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
# File 'lib/baseapi/active_record/base_extension.rb', line 184

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