| Name | Total Lines | Lines of Code | Total Coverage | Code Coverage |
|---|---|---|---|---|
| app/helpers/pagination_helper.rb | 87 | 53 | 89.66%
|
84.91%
|
Code reported as executed by Ruby looks like this...and this: this line is also marked as covered.Lines considered as run by rcov, but not reported by Ruby, look like this,and this: these lines were inferred by rcov (using simple heuristics).Finally, here's a line marked as not executed.
1 require 'will_paginate' |
2 |
3 =begin How to Use the Pagination Helper |
4 hash requires: |
5 :model * the type of data that needs pagination |
6 :per_pages * an array of integers, each integer representing |
7 a selection of pagination size for the user |
8 :filters * a list of possible filters that can be applied |
9 to the data of type :model |
10 a name for each filter |
11 :display * the text the user sees |
12 :proc => lambda * the processing done to each instance of the :model |
13 :sorts |
14 for each sort |
15 "name" * the string that will appear in the URL of a GET |
16 or AJAX request |
17 => lambda * the lambda expression that handles the sorting |
18 =end |
19 module PaginationHelper |
20 # For the hash that's passed to handle_ap_event, these are the required |
21 # fields. |
22 AP_REQUIRED_KEYS = [:model, :filters] |
23 AP_DEFAULT_PER_PAGE = 30 |
24 AP_DEFAULT_PAGE = 1 |
25 |
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 |
57 |
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 |
65 |
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 "section" then to_include = [:group] |
74 when "revision_timestamp" then to_include = [:current_submission_used] |
75 when "marking_state" then to_include = [{:current_submission_used => :result}] |
76 when "total_mark" then to_include = [{:current_submission_used => :result}] |
77 when "grace_credits_used" then to_include = [:grace_period_deductions] |
78 end |
79 items = hash[:filters][filter][:proc].call(object_hash, to_include) |
80 if !sort_by.blank? |
81 items = items.sort{|a,b| hash[:sorts][sort_by].call(a,b)} |
82 end |
83 return items |
84 end |
85 |
86 end |
87 |
Generated on Tue Feb 07 00:07:35 -0500 2012 with rcov 0.9.10