Class: SmartListing::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/smart_listing.rb

Constant Summary collapse

DEFAULT_PAGE_SIZES =
[10, 20, 50, 100]

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {}
  @name = name

  @options = {
    :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!(options)

  if @options[:array]
    @collection = collection.to_a
  else 
    @collection = collection
  end
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection.



13
14
15
# File 'lib/smart_listing.rb', line 13

def collection
  @collection
end

#countObject (readonly)

Returns the value of attribute count.



13
14
15
# File 'lib/smart_listing.rb', line 13

def count
  @count
end

#nameObject (readonly)

Returns the value of attribute name.



13
14
15
# File 'lib/smart_listing.rb', line 13

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



13
14
15
# File 'lib/smart_listing.rb', line 13

def options
  @options
end

#pageObject (readonly)

Returns the value of attribute page.



13
14
15
# File 'lib/smart_listing.rb', line 13

def page
  @page
end

#partialObject (readonly)

Returns the value of attribute partial.



13
14
15
# File 'lib/smart_listing.rb', line 13

def partial
  @partial
end

#per_pageObject (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_attrObject (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_extraObject (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_orderObject (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_paramsObject



133
134
135
136
137
138
139
# File 'lib/smart_listing.rb', line 133

def all_params
  ap = {}
  @options[:param_names].each do |k, v|
    ap[v] = self.send(k)
  end
  ap
end

#callback_hrefObject



121
122
123
# File 'lib/smart_listing.rb', line 121

def callback_href
  @options[:callback_href]
end

#hrefObject



117
118
119
# File 'lib/smart_listing.rb', line 117

def href
  @options[:href]
end

#kaminari_optionsObject



129
130
131
# File 'lib/smart_listing.rb', line 129

def kaminari_options
  @options[:kaminari_options]
end

#max_countObject



113
114
115
# File 'lib/smart_listing.rb', line 113

def max_count
  @options[:max_count]
end

#page_sizesObject



125
126
127
# File 'lib/smart_listing.rb', line 125

def page_sizes
  @options[:page_sizes]
end

#param_namesObject



105
106
107
# File 'lib/smart_listing.rb', line 105

def param_names
  @options[: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, cookies
  @page = params[param_names[:page]]
  @per_page = !params[param_names[:per_page]] || params[param_names[:per_page]].empty? ? (@options[:memorize_per_page] && cookies[param_names[:per_page]].to_i > 0 ? cookies[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]] || @options[: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]]

  cookies[param_names[:per_page]] = @per_page if @options[: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 @options[: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 @options[:sort] && @sort_attr && !@sort_attr.empty?
    if @options[: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 @options[:sort] && @sort_attr && !@sort_attr.empty? && @collection.column_names.include?(@sort_attr) && @sort_order
    if @options[:paginate] && @per_page > 0
      @collection = @collection.page(@page).per(@per_page)
    end
  end
end

#unlimited_per_page?Boolean

Returns:

  • (Boolean)


109
110
111
# File 'lib/smart_listing.rb', line 109

def unlimited_per_page?
  !!@options[:unlimited_per_page]
end