Class: SmartListing::Base
- Inherits:
-
Object
- Object
- SmartListing::Base
- Defined in:
- lib/smart_listing.rb
Constant Summary collapse
- DEFAULT_PAGE_SIZES =
[10, 20, 50, 100]
Instance Attribute Summary collapse
-
#collection ⇒ Object
readonly
Returns the value of attribute collection.
-
#count ⇒ Object
readonly
Returns the value of attribute count.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#page ⇒ Object
readonly
Returns the value of attribute page.
-
#partial ⇒ Object
readonly
Returns the value of attribute partial.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#sort_attr ⇒ Object
readonly
Returns the value of attribute sort_attr.
-
#sort_extra ⇒ Object
readonly
Returns the value of attribute sort_extra.
-
#sort_order ⇒ Object
readonly
Returns the value of attribute sort_order.
Instance Method Summary collapse
- #all_params ⇒ Object
- #callback_href ⇒ Object
- #href ⇒ Object
-
#initialize(name, collection, options = {}) ⇒ Base
constructor
A new instance of Base.
- #kaminari_options ⇒ Object
- #max_count ⇒ Object
- #page_sizes ⇒ Object
- #param_names ⇒ Object
- #setup(params, cookies) ⇒ Object
- #unlimited_per_page? ⇒ Boolean
Constructor Details
#initialize(name, collection, options = {}) ⇒ Base
Returns a new instance of Base.
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/smart_listing.rb', line 15 def initialize name, collection, = {} @name = name = { :param_names => { # param names :page => "#{@name}_page".to_sym, :per_page => "#{@name}_per_page".to_sym, :sort_attr => "#{@name}_sort_attr".to_sym, :sort_order => "#{@name}_sort_order".to_sym, :sort_extra => "#{@name}_sort_extra".to_sym, }, :partial => @name, # smart list partial name :array => false, # controls whether smart list should be using arrays or AR collections :max_count => nil, # limit number of rows :unlimited_per_page => false, # allow infinite page size :sort => true, # allow sorting :paginate => true, # allow pagination :href => nil, # set smart list target url (in case when different than current url) :callback_href => nil, # set smart list callback url (in case when different than current url) :default_sort_attr => nil, # default sort by :memorize_per_page => false, :page_sizes => DEFAULT_PAGE_SIZES, # set available page sizes array :kaminari_options => {}, # Kaminari's paginate helper options }.merge!() if [:array] @collection = collection.to_a else @collection = collection end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def collection @collection end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def count @count end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def page @page end |
#partial ⇒ Object (readonly)
Returns the value of attribute partial.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def partial @partial end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def per_page @per_page end |
#sort_attr ⇒ Object (readonly)
Returns the value of attribute sort_attr.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def sort_attr @sort_attr end |
#sort_extra ⇒ Object (readonly)
Returns the value of attribute sort_extra.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def sort_extra @sort_extra end |
#sort_order ⇒ Object (readonly)
Returns the value of attribute sort_order.
13 14 15 |
# File 'lib/smart_listing.rb', line 13 def sort_order @sort_order end |
Instance Method Details
#all_params ⇒ Object
133 134 135 136 137 138 139 |
# File 'lib/smart_listing.rb', line 133 def all_params ap = {} [:param_names].each do |k, v| ap[v] = self.send(k) end ap end |
#callback_href ⇒ Object
121 122 123 |
# File 'lib/smart_listing.rb', line 121 def callback_href [:callback_href] end |
#href ⇒ Object
117 118 119 |
# File 'lib/smart_listing.rb', line 117 def href [:href] end |
#kaminari_options ⇒ Object
129 130 131 |
# File 'lib/smart_listing.rb', line 129 def [:kaminari_options] end |
#max_count ⇒ Object
113 114 115 |
# File 'lib/smart_listing.rb', line 113 def max_count [:max_count] end |
#page_sizes ⇒ Object
125 126 127 |
# File 'lib/smart_listing.rb', line 125 def page_sizes [:page_sizes] end |
#param_names ⇒ Object
105 106 107 |
# File 'lib/smart_listing.rb', line 105 def param_names [:param_names] end |
#setup(params, cookies) ⇒ Object
47 48 49 50 51 52 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 |
# File 'lib/smart_listing.rb', line 47 def setup params, @page = params[param_names[:page]] @per_page = !params[param_names[:per_page]] || params[param_names[:per_page]].empty? ? ([:memorize_per_page] && [param_names[:per_page]].to_i > 0 ? [param_names[:per_page]].to_i : page_sizes.first) : params[param_names[:per_page]].to_i @per_page = DEFAULT_PAGE_SIZES.first unless DEFAULT_PAGE_SIZES.include?(@per_page) @sort_attr = params[param_names[:sort_attr]] || [:default_sort_attr] @sort_order = ["asc", "desc"].include?(params[param_names[:sort_order]]) ? params[param_names[:sort_order]] : "desc" @sort_extra = params[param_names[:sort_extra]] [param_names[:per_page]] = @per_page if [:memorize_per_page] @count = @collection.size @count = @count.length if @count.is_a?(Hash) # Reset @page if greater than total number of pages no_pages = (@count.to_f / @per_page.to_f).ceil.to_i if @page.to_i > no_pages @page = no_pages end if [:array] @collection = @collection.sort do |x, y| xval = x yval = y @sort_attr.split(".").each do |m| xval = xval.try(m) yval = yval.try(m) end xval = xval.upcase if xval.is_a?(String) yval = yval.upcase if yval.is_a?(String) if xval.nil? || yval.nil? xval.nil? ? 1 : -1 else if @sort_order == "asc" (xval <=> yval) || (xval && !yval ? 1 : -1) else (yval <=> xval) || (yval && !xval ? 1 : -1) end end end if [:sort] && @sort_attr && !@sort_attr.empty? if [:paginate] && @per_page > 0 @collection = ::Kaminari.paginate_array(@collection).page(@page).per(@per_page) if @collection.length == 0 @collection = @collection.page(@collection.num_pages) end end else @collection = @collection.order("#{@sort_attr} #{@sort_order}") if [:sort] && @sort_attr && !@sort_attr.empty? && @collection.column_names.include?(@sort_attr) && @sort_order if [:paginate] && @per_page > 0 @collection = @collection.page(@page).per(@per_page) end end end |
#unlimited_per_page? ⇒ Boolean
109 110 111 |
# File 'lib/smart_listing.rb', line 109 def unlimited_per_page? !![:unlimited_per_page] end |