Class: SmartListing::Base
- Inherits:
-
Object
- Object
- SmartListing::Base
- Defined in:
- lib/smart_listing.rb
Constant Summary collapse
- UNSAFE_PARAMS =
Params that should not be visible in pagination links (pages, per-page, sorting, etc.)
[:authenticity_token, :commit, :utf8, :_method, :script_name].freeze
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.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#partial ⇒ Object
readonly
Returns the value of attribute partial.
-
#per_page ⇒ Object
readonly
Returns the value of attribute per_page.
-
#sort ⇒ Object
readonly
Returns the value of attribute sort.
Instance Method Summary collapse
- #all_params(overrides = {}) ⇒ Object
- #base_param ⇒ 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_name(key) ⇒ Object
- #param_names ⇒ Object
- #remote? ⇒ Boolean
- #setup(params, cookies) ⇒ Object
- #sort_dirs ⇒ Object
- #sort_order(attribute) ⇒ Object
- #unlimited_per_page? ⇒ Boolean
Constructor Details
#initialize(name, collection, options = {}) ⇒ Base
Returns a new instance of Base.
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/smart_listing.rb', line 28 def initialize name, collection, = {} @name = name config_profile = .delete(:config_profile) = { :partial => @name, # SmartListing partial name :sort_attributes => :implicit, # allow implicitly setting sort attributes :default_sort => {}, # default sorting :href => nil, # set SmartListing target url (in case when different than current url) :remote => true, # SmartListing is remote by default :callback_href => nil, # set SmartListing callback url (in case when different than current url) }.merge(SmartListing.config(config_profile).).merge() if [:array] @collection = collection.to_a else @collection = collection end end |
Instance Attribute Details
#collection ⇒ Object (readonly)
Returns the value of attribute collection.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def collection @collection end |
#count ⇒ Object (readonly)
Returns the value of attribute count.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def count @count end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def name @name end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def end |
#page ⇒ Object (readonly)
Returns the value of attribute page.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def page @page end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def params @params end |
#partial ⇒ Object (readonly)
Returns the value of attribute partial.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def partial @partial end |
#per_page ⇒ Object (readonly)
Returns the value of attribute per_page.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def per_page @per_page end |
#sort ⇒ Object (readonly)
Returns the value of attribute sort.
24 25 26 |
# File 'lib/smart_listing.rb', line 24 def sort @sort end |
Instance Method Details
#all_params(overrides = {}) ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/smart_listing.rb', line 160 def all_params overrides = {} ap = {base_param => {}} [:param_names].each do |k, v| if overrides[k] ap[base_param][v] = overrides[k] else ap[base_param][v] = self.send(k) end end ap end |
#base_param ⇒ Object
176 177 178 |
# File 'lib/smart_listing.rb', line 176 def base_param "#{name}_smart_listing" end |
#callback_href ⇒ Object
140 141 142 |
# File 'lib/smart_listing.rb', line 140 def callback_href [:callback_href] end |
#href ⇒ Object
136 137 138 |
# File 'lib/smart_listing.rb', line 136 def href [:href] end |
#kaminari_options ⇒ Object
152 153 154 |
# File 'lib/smart_listing.rb', line 152 def [:kaminari_options] end |
#max_count ⇒ Object
132 133 134 |
# File 'lib/smart_listing.rb', line 132 def max_count [:max_count] end |
#page_sizes ⇒ Object
148 149 150 |
# File 'lib/smart_listing.rb', line 148 def page_sizes [:page_sizes] end |
#param_name(key) ⇒ Object
124 125 126 |
# File 'lib/smart_listing.rb', line 124 def param_name key "#{base_param}[#{param_names[key]}]" end |
#param_names ⇒ Object
120 121 122 |
# File 'lib/smart_listing.rb', line 120 def param_names [:param_names] end |
#remote? ⇒ Boolean
144 145 146 |
# File 'lib/smart_listing.rb', line 144 def remote? [:remote] end |
#setup(params, cookies) ⇒ Object
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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'lib/smart_listing.rb', line 49 def setup params, @params = params @params = @params.to_unsafe_h if @params.respond_to?(:to_unsafe_h) @params = @params.with_indifferent_access @params.except!(*UNSAFE_PARAMS) @page = get_param :page @per_page = !get_param(:per_page) || get_param(:per_page).empty? ? ([:memorize_per_page] && get_param(:per_page, ).to_i > 0 ? get_param(:per_page, ).to_i : page_sizes.first) : get_param(:per_page).to_i @per_page = page_sizes.first unless page_sizes.include?(@per_page) || (unlimited_per_page? && @per_page == 0) @sort = parse_sort(get_param(:sort)) || [:default_sort] sort_keys = ([:sort_attributes] == :implicit ? @sort.keys.collect{|s| [s, s]} : [:sort_attributes]) set_param(: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 if @per_page > 0 no_pages = (@count.to_f / @per_page.to_f).ceil.to_i if @page.to_i > no_pages @page = no_pages end end if [:array] if @sort && !@sort.empty? # when array we sort only by first attribute i = sort_keys.index{|x| x[0] == @sort.to_h.first[0]} @collection = @collection.sort do |x, y| xval = x yval = y sort_keys[i][1].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.to_h.first[1] == "asc" (xval <=> yval) || (xval && !yval ? 1 : -1) else (yval <=> xval) || (yval && !xval ? 1 : -1) end end end end if [:paginate] && @per_page > 0 @collection = ::Kaminari.paginate_array(@collection).page(@page).per(@per_page) if @collection.length == 0 @collection = @collection.page(@collection.total_pages) end end else # let's sort by all attributes # @collection = @collection.order(sort_keys.collect{|s| "#{s[1]} #{@sort[s[0]]}" if @sort[s[0]]}.compact) if @sort && !@sort.empty? if [:paginate] && @per_page > 0 @collection = @collection.page(@page).per(@per_page) end end end |
#sort_dirs ⇒ Object
156 157 158 |
# File 'lib/smart_listing.rb', line 156 def sort_dirs [:sort_dirs] end |
#sort_order(attribute) ⇒ Object
172 173 174 |
# File 'lib/smart_listing.rb', line 172 def sort_order attribute @sort && @sort[attribute].present? ? @sort[attribute] : nil end |
#unlimited_per_page? ⇒ Boolean
128 129 130 |
# File 'lib/smart_listing.rb', line 128 def unlimited_per_page? !![:unlimited_per_page] end |