Module: Queryable::InstanceMethods

Defined in:
app/controllers/concerns/queryable.rb

Instance Method Summary collapse

Instance Method Details

#apply_query_with_meta(scope) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'app/controllers/concerns/queryable.rb', line 53

def apply_query_with_meta(scope)
  config = query_config
  meta = {}

  # 应用过滤、搜索、排序(不分页)
  base_scope = scope
  base_scope = apply_filtering(base_scope)    if config[:filtering][:enabled]
  base_scope = apply_searching(base_scope)    if config[:searching][:enabled]
  base_scope = apply_sorting(base_scope)      if config[:sorting][:enabled]

  # 获取总数(用于 meta)
  if config[:meta][:enabled]
    meta[config[:meta][:total_key]] = base_scope.count
  end

  # 应用分页
  paginated_scope = base_scope
  if config[:pagination][:enabled]
    paginated_scope = apply_pagination(base_scope)
  end

  {
    collection: paginated_scope,
    meta: config[:meta][:enabled] ? meta : nil
  }
end