Module: ActiveRecordRelationExtension
- Defined in:
- lib/baseapi/active_record/relation_extension.rb
Instance Method Summary collapse
-
#filtering!(params) ⇒ Object
column search.
-
#joins_array!(joins) ⇒ Object
joins as array params.
-
#paging!(params) ⇒ Object
pager.
-
#sorting!(params) ⇒ Object
sort.
Instance Method Details
#filtering!(params) ⇒ Object
column search
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/baseapi/active_record/relation_extension.rb', line 5 def filtering!(params) models = self params.each do |key, value| if key.present? and value.present? # this model search if column_names.include?(key) # array change values = value.instance_of?(Array) ? value : [value] values.reject!(&:blank?) # call function function_name = self.model.methods.include?("_where_#{key}".to_sym) ? "_where_#{key}" : '_where' models = self.model.send(function_name, models, key, values) # belongs_to, has_many search else relationSearch = -> (models, currentModel, key, value, prefix = '', joins = []) { associations = currentModel.get_associations() associations.keys.each do |association| if currentModel.column_names.include?(key) # call function function_chains = joins.clone function_chains.push key function_name = self.model.methods.include?("_#{prefix}_#{function_chains.join('_')}".to_sym) ? "_#{prefix}_#{function_chains.join('_')}" : "_#{prefix}" table_name = currentModel.name.underscore hash = {key => value} return self.model.send(function_name, models, table_name, hash) elsif associations[association].include?(key) # prefix = first association prefix = association if prefix == '' joins.push key models.joins_array!(joins) currentModel = key.camelize.singularize.constantize value.each do |k, v| # this fnuction collback relationSearch.call(models, currentModel, k, v, prefix, joins) end end end return models } models = relationSearch.call(models, self.model, key, value) end end end return models end |
#joins_array!(joins) ⇒ Object
joins as array params
133 134 135 136 137 138 139 140 141 142 143 144 |
# File 'lib/baseapi/active_record/relation_extension.rb', line 133 def joins_array!(joins) param = nil joins.reverse.each_with_index do |join, index| join = join.to_sym if index == 0 param = join else param = {join => param} end end joins!(param) end |
#paging!(params) ⇒ Object
pager
53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/baseapi/active_record/relation_extension.rb', line 53 def paging!(params) prefix = self.model.get_reserved_word_prefix count = params["#{prefix}count".to_sym].present? ? params["#{prefix}count".to_sym].to_i : -1; page = params["#{prefix}page".to_sym].present? ? params["#{prefix}page".to_sym].to_i : 1; if count > 0 if count.present? and count limit!(count) if page offset!((page - 1) * count) end end end end |
#sorting!(params) ⇒ Object
sort
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/baseapi/active_record/relation_extension.rb', line 69 def sorting!(params) prefix = self.model.get_reserved_word_prefix if params["#{prefix}order".to_sym].present? and params["#{prefix}orderby".to_sym].present? # array exchange orderby = params["#{prefix}orderby".to_sym] orderbys = orderby.instance_of?(Array) ? orderby : [orderby] order = params["#{prefix}order".to_sym] orders = order.instance_of?(Array) ? order : [order] # multiple order orderbys.each_with_index do |orderby, index| if orders[index].present? and ['DESC', 'ASC'].include?(orders[index].upcase) order = orders[index].upcase # dot notation example: company.name joins_tables = orderby.split(".") column_name = joins_tables.pop table_name = joins_tables.count > 0 ? joins_tables.last.pluralize.underscore : self.model.to_s.pluralize.underscore # table_name parent table parent_table_name = joins_tables.count > 1 ? joins_tables.last(2).first : self.model.to_s.pluralize.underscore # parent_table get association association = parent_table_name.camelize.singularize.constantize.reflect_on_association(table_name.singularize) # If you have specified class_name in belongs_to method (for example, you have changed the foreign key) # example: # class Project < ActiveRecord::Base # belongs_to :manager, foreign_key: 'manager_id', class_name: 'User' # belongs_to :leader, foreign_key: 'leader_id', class_name: 'User' # end if association and association.[:class_name].present? association_table_name = association.[:class_name].pluralize.underscore table_alias = table_name.pluralize.underscore # check next if !ActiveRecord::Base.connection.tables.include?(association_table_name) # join joins!("INNER JOIN `#{association_table_name}` AS `#{table_alias}` ON `#{table_alias}`.`id` = `#{parent_table_name}`.`#{table_alias.singularize}_id`") # order order!("`#{table_alias}`.`#{column_name}` #{order}") # belongs_to along the rails convention # example: # class Project < ActiveRecord::Base # belongs_to :manager # end else # joins_tables exists check is_next = false joins_tables.each do |table| is_next = true and break if !ActiveRecord::Base.connection.tables.include?(table.pluralize.underscore) end next if is_next # table exists check next if !ActiveRecord::Base.connection.tables.include?(table_name) # column_name exists check next if !table_name.camelize.singularize.constantize.column_names.include?(column_name) # joins joins_array!(joins_tables) # order order!("`#{table_name}`.`#{column_name}` #{order}") end end end end end |