Class: Caboose::PageBarGenerator
- Inherits:
-
Object
- Object
- Caboose::PageBarGenerator
- Defined in:
- app/models/caboose/page_bar_generator.rb
Instance Attribute Summary collapse
-
#options ⇒ Object
Parameters: params: array of key/value pairs that must include the following: base_url: url without querystring onto which the parameters are added.
-
#params ⇒ Object
Parameters: params: array of key/value pairs that must include the following: base_url: url without querystring onto which the parameters are added.
Instance Method Summary collapse
- #generate ⇒ Object
- #get_vars ⇒ Object
-
#initialize(post_get, params = nil, options = nil) ⇒ PageBarGenerator
constructor
A new instance of PageBarGenerator.
- #limit ⇒ Object
- #offset ⇒ Object
- #ok(val) ⇒ Object
- #reorder ⇒ Object
- #sortable_table_headings(cols) ⇒ Object
- #where ⇒ Object
Constructor Details
#initialize(post_get, params = nil, options = nil) ⇒ PageBarGenerator
Returns a new instance of PageBarGenerator.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/models/caboose/page_bar_generator.rb', line 16 def initialize(post_get, params = nil, = nil) params = {} if params.nil? = {} if .nil? # Note: a few keys are required: # base_url, page, itemCount, itemsPerPage @params = {} @options = { 'sort' => '', 'desc' => false, 'base_url' => '', 'page' => 0, 'item_count' => 0, 'items_per_page' => 10 } params.each { |key, val| @params[key] = val } .each { |key, val| @options[key] = val } @params.each { |key, val| @params[key] = post_get[key].nil? ? val : post_get[key] } @options.each { |key, val| @options[key] = post_get[key].nil? ? val : post_get[key] } end |
Instance Attribute Details
#options ⇒ Object
Parameters: params: array of key/value pairs that must include the following: base_url: url without querystring onto which the parameters are added. itemCount: Total number of items.
In addition, the following parameters are not required but may be included in the array: itemsPerPage: Number of items you want to show per page. Defaults to 10 if not present. page: Current page number. Defaults to 0 if not present.
14 15 16 |
# File 'app/models/caboose/page_bar_generator.rb', line 14 def @options end |
#params ⇒ Object
Parameters: params: array of key/value pairs that must include the following: base_url: url without querystring onto which the parameters are added. itemCount: Total number of items.
In addition, the following parameters are not required but may be included in the array: itemsPerPage: Number of items you want to show per page. Defaults to 10 if not present. page: Current page number. Defaults to 0 if not present.
14 15 16 |
# File 'app/models/caboose/page_bar_generator.rb', line 14 def params @params end |
Instance Method Details
#generate ⇒ 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 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 |
# File 'app/models/caboose/page_bar_generator.rb', line 53 def generate # Check for necessary parameter values return false if !ok(@options['base_url']) # Error: base_url is required for the page bar generator to work. return false if !ok(@options['item_count']) # Error: itemCount is required for the page bar generator to work. # Set default parameter values if not present @options['items_per_page'] = 10 if @options["items_per_page"].nil? @options['page'] = 0 if @options["page"].nil? # Variables to make the search form work vars = get_vars() page = @options["page"] # Max links to show (must be odd) total_links = 9 prev_page = page - 1 next_page = page + 1 total_pages = (@options['item_count'].to_f / @options['items_per_page'].to_f).ceil if (total_pages < total_links) start = 0 stop = total_pages else start = page - (total_links/2).floor start = 0 if start < 0 stop = start + total_links if (stop > total_pages) stop = total_pages start = stop - total_links start = 0 if start < 0 end end base_url = @params['base_url'] str = '' str << "<p>Results Pages: showing page " + (page+1).to_s + " of #{total_pages}</p>\n" str << "<div class='page_links'>\n" if (page > 0) str << "<a href='#{base_url}?#{vars}&page=#{prev_page}'>Previous</a>" end for i in start..(stop-1) if (page != i) str << "<a href='#{base_url}?#{vars}&page=#{i}'>" + (i+1).to_s + "</a>" else str << "<span class='current_page'>" + (i+1).to_s + "</span>" end end if (page < (total_pages-1)) str << "<a href='#{base_url}?#{vars}&page=#{next_page}'>Next</a>" end str << "</div>\n" return str end |
#get_vars ⇒ Object
112 113 114 115 116 117 118 |
# File 'app/models/caboose/page_bar_generator.rb', line 112 def get_vars() vars = [] @params.each do |k,v| vars.push("#{k}=#{v}") if !v.nil? && v.length > 0 end return URI.escape(vars.join('&')) end |
#limit ⇒ Object
142 143 144 |
# File 'app/models/caboose/page_bar_generator.rb', line 142 def limit return @options['items_per_page'] end |
#offset ⇒ Object
146 147 148 |
# File 'app/models/caboose/page_bar_generator.rb', line 146 def offset return @options['page'] * @options['items_per_page'] end |
#ok(val) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'app/models/caboose/page_bar_generator.rb', line 39 def ok(val) return false if val.nil? return true if val.is_a? Array return true if val.is_a? Hash return true if val.is_a? Integer return true if val.is_a? Fixnum return true if val.is_a? Float return true if val.is_a? Bignum return true if val.is_a? TrueClass return true if val.is_a? FalseClass return false if val == "" return true end |
#reorder ⇒ Object
150 151 152 153 154 155 |
# File 'app/models/caboose/page_bar_generator.rb', line 150 def reorder if (!@options['sort'].nil? && @options['sort'].length > 0) return @options['sort'] end return "id" end |
#sortable_table_headings(cols) ⇒ Object
120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'app/models/caboose/page_bar_generator.rb', line 120 def sortable_table_headings(cols) vars = get_vars() str = '' # key = sort field, value = text to display cols.each do |sort, text| desc = @options['sort'] == sort ? (@options['desc'] ? 0 : 1) : 0 arrow = @options['sort'] == sort ? (@options['desc'] ? ' &uarr' : ' &darr') : '' link = @options['base_url'] + "?#{vars}&sort=#{sort}&desc=#{desc}" str += "<th><a href='#{link}'>#{text}#{arrow}</a></th>\n" end return str end |
#where ⇒ Object
134 135 136 137 138 139 140 |
# File 'app/models/caboose/page_bar_generator.rb', line 134 def where vars = {} @params.each do |k,v| vars[k] = v if !v.nil? && v.length > 0 end return vars end |