| Module | PaginationHelper |
| In: |
app/helpers/pagination_helper.rb
|
| AP_REQUIRED_KEYS | = | [:model, :filters] | For the hash that‘s passed to handle_ap_event, these are the required fields. | |
| AP_DEFAULT_PER_PAGE | = | 30 | ||
| AP_DEFAULT_PAGE | = | 1 |
# File app/helpers/pagination_helper.rb, line 66
66: def get_filtered_items(hash, filter, sort_by, object_hash)
67: to_include = []
68: #eager load only the tables needed for the type of sort, eager load the rest
69: #of the tables after the groupings have been paginated
70: case sort_by
71: when "group_name" then to_include = [:group]
72: when "repo_name" then to_include = [:group]
73: when "revision_timestamp" then to_include = [:current_submission_used]
74: when "marking_state" then to_include = [{:current_submission_used => :result}]
75: when "total_mark" then to_include = [{:current_submission_used => :result}]
76: when "grace_credits_used" then to_include = [:grace_period_deductions]
77: end
78: items = hash[:filters][filter][:proc].call(object_hash, to_include)
79: if !sort_by.blank?
80: items = items.sort{|a,b| hash[:sorts][sort_by].call(a,b)}
81: end
82: return items
83: end
# File app/helpers/pagination_helper.rb, line 58
58: def get_filters(params)
59: result = {}
60: params[:filters].each do |filter_key, filter|
61: result[filter[:display]] = filter_key
62: end
63: return result
64: end
# File app/helpers/pagination_helper.rb, line 26
26: def handle_paginate_event(hash, object_hash, params)
27: # First, let's make sure we have the required fields.
28: # I'll take the keys from the hash, and difference it from
29: # the AP_REQUIRED_FIELDS. If there are any symbols left over
30: # in the difference, the hash wasn't complete, and we should bail out.
31: if (AP_REQUIRED_KEYS - hash.keys).size > 0
32: # Fail loud, fail proud
33: raise "handle_paginate_event received an incomplete parameter hash"
34: end
35: # Ok, we have everything we need, let's get to work
36: filter = params[:filter]
37: desc = params[:desc]
38: sorts = hash[:sorts]
39: filters = hash[:filters]
40: if(!filters.include?(filter))
41: raise "Could not find filter #{filter}"
42: end
43: items = get_filtered_items(hash, filter, params[:sort_by], object_hash)
44: if !params[:desc].blank?
45: items.reverse!
46: end
47: if params[:per_page].blank?
48: params[:per_page] = AP_DEFAULT_PER_PAGE
49: end
50: if params[:page].blank?
51: params[:page] = AP_DEFAULT_PAGE
52: end
53:
54: return items.paginate(:per_page => params[:per_page], :page => params[:page]).clone, items.size
55:
56: end