Class: SmartListing::Base

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

Constant Summary collapse

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.



13
14
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
# File 'lib/smart_listing.rb', line 13

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)
    :default_sort_attr  => nil,         # default sort by
    :memorize_per_page  => false,
  }.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.



11
12
13
# File 'lib/smart_listing.rb', line 11

def collection
  @collection
end

#countObject (readonly)

Returns the value of attribute count.



11
12
13
# File 'lib/smart_listing.rb', line 11

def count
  @count
end

#nameObject (readonly)

Returns the value of attribute name.



11
12
13
# File 'lib/smart_listing.rb', line 11

def name
  @name
end

#optionsObject (readonly)

Returns the value of attribute options.



11
12
13
# File 'lib/smart_listing.rb', line 11

def options
  @options
end

#pageObject (readonly)

Returns the value of attribute page.



11
12
13
# File 'lib/smart_listing.rb', line 11

def page
  @page
end

#partialObject (readonly)

Returns the value of attribute partial.



11
12
13
# File 'lib/smart_listing.rb', line 11

def partial
  @partial
end

#per_pageObject (readonly)

Returns the value of attribute per_page.



11
12
13
# File 'lib/smart_listing.rb', line 11

def per_page
  @per_page
end

#sort_attrObject (readonly)

Returns the value of attribute sort_attr.



11
12
13
# File 'lib/smart_listing.rb', line 11

def sort_attr
  @sort_attr
end

#sort_extraObject (readonly)

Returns the value of attribute sort_extra.



11
12
13
# File 'lib/smart_listing.rb', line 11

def sort_extra
  @sort_extra
end

#sort_orderObject (readonly)

Returns the value of attribute sort_order.



11
12
13
# File 'lib/smart_listing.rb', line 11

def sort_order
  @sort_order
end

Instance Method Details

#all_paramsObject



108
109
110
111
112
113
114
# File 'lib/smart_listing.rb', line 108

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

#hrefObject



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

def href
  @options[:href]
end

#max_countObject



100
101
102
# File 'lib/smart_listing.rb', line 100

def max_count
  @options[:max_count]
end

#param_namesObject



92
93
94
# File 'lib/smart_listing.rb', line 92

def param_names
  @options[:param_names]
end

#setup(params, cookies) ⇒ Object



42
43
44
45
46
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
# File 'lib/smart_listing.rb', line 42

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
  @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

  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? && @sort_order
    if @options[:paginate] && @per_page > 0
      @collection = @collection.page(@page).per(@per_page)
    end
  end
end

#unlimited_per_page?Boolean

Returns:

  • (Boolean)


96
97
98
# File 'lib/smart_listing.rb', line 96

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