Module: ActiveRecordRelationExtension
- Defined in:
- lib/baseapi/active_record/relation_extension.rb
Instance Method Summary collapse
-
#filtering!(params) ⇒ Object
カラムの検索.
-
#paging!(params) ⇒ Object
ページャー.
-
#sorting!(params) ⇒ Object
ソート.
Instance Method Details
#filtering!(params) ⇒ Object
カラムの検索
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 |
# File 'lib/baseapi/active_record/relation_extension.rb', line 9 def filtering!(params) models = self associations = self.model.get_associations() params.each do |key, value| if key.present? and value.present? if column_names.include?(key) # 配列に対応する values = value.instance_of?(Array) ? value : [value] values.reject!(&:blank?) # カラム用関数が定義されていればそれを使用 function_name = self.model.methods.include?("_where_#{key}".to_sym) ? "_where_#{key}" : '_where' models = self.model.send(function_name, models, key, values) end # belongs_to, has_manyの検索 associations.keys.each do |association| if associations[association].include?(key) and value.instance_of?(ActionController::Parameters)#hash型はActionController::Parameters # カラム用関数が定義されていればそれを使用 function_name = self.model.methods.include?("_#{association}_#{key}".to_sym) ? "_#{association}_#{key}" : "_#{association}" models = self.model.send(function_name, models, key, value) end end end end return models end |
#paging!(params) ⇒ Object
ページャー
38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/baseapi/active_record/relation_extension.rb', line 38 def paging!(params) count = params[:count].present? ? params[:count].to_i : -1; page = params[:page].present? ? params[:page].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
ソート
53 54 55 56 57 |
# File 'lib/baseapi/active_record/relation_extension.rb', line 53 def sorting!(params) if params[:order].present? and params[:orderby].present? order!({params[:orderby] => params[:order]}) end end |