smart_listing 0.9.1 → 0.9.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/smart_listing.rb CHANGED
@@ -1,116 +1,121 @@
1
1
  require "smart_listing/engine"
2
2
  require "kaminari"
3
3
  module SmartListing
4
- class Base
5
- if Rails.env.development?
6
- PAGE_SIZES = [3, 10, 20, 50, 100]
7
- else
8
- PAGE_SIZES = [10, 20, 50, 100]
9
- end
10
-
11
- attr_reader :name, :collection, :options, :per_page, :page, :sort_attr, :sort_order, :sort_extra, :partial, :count
12
-
13
- def initialize name, collection, options = {}
14
- @name = name
15
-
16
- @options = {
17
- :param_names => { # param names
18
- :page => "#{@name}_page".to_sym,
19
- :per_page => "#{@name}_per_page".to_sym,
20
- :sort_attr => "#{@name}_sort_attr".to_sym,
21
- :sort_order => "#{@name}_sort_order".to_sym,
22
- :sort_extra => "#{@name}_sort_extra".to_sym,
23
- },
24
- :partial => @name, # smart list partial name
25
- :array => false, # controls whether smart list should be using arrays or AR collections
26
- :max_count => nil, # limit number of rows
27
- :unlimited_per_page => false, # allow infinite page size
28
- :sort => true, # allow sorting
29
- :paginate => true, # allow pagination
30
- :href => nil, # set smart list target url (in case when different than current url)
31
- :default_sort_attr => nil, # default sort by
32
- :memorize_per_page => false,
33
- }.merge!(options)
34
-
35
- if @options[:array]
36
- @collection = collection.to_a
37
- else
38
- @collection = collection
39
- end
40
- end
41
-
42
- def setup params, cookies
43
- @page = params[param_names[:page]]
44
- @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
45
- @sort_attr = params[param_names[:sort_attr]] || @options[:default_sort_attr]
46
- @sort_order = ["asc", "desc"].include?(params[param_names[:sort_order]]) ? params[param_names[:sort_order]] : "desc"
47
- @sort_extra = params[param_names[:sort_extra]]
48
-
49
- cookies[param_names[:per_page]] = @per_page if @options[:memorize_per_page]
50
-
51
- @count = @collection.size
52
-
53
- if @options[:array]
54
- @collection = @collection.sort do |x, y|
55
- xval = x
56
- yval = y
57
- @sort_attr.split(".").each do |m|
58
- xval = xval.try(m)
59
- yval = yval.try(m)
60
- end
61
- xval = xval.upcase if xval.is_a?(String)
62
- yval = yval.upcase if yval.is_a?(String)
63
-
64
- if xval.nil? || yval.nil?
65
- xval.nil? ? 1 : -1
66
- else
67
- if @sort_order == "asc"
68
- (xval <=> yval) || (xval && !yval ? 1 : -1)
69
- else
70
- (yval <=> xval) || (yval && !xval ? 1 : -1)
71
- end
72
- end
73
- end if @options[:sort] && @sort_attr && !@sort_attr.empty?
74
- if @options[:paginate] && @per_page > 0
75
- @collection = ::Kaminari.paginate_array(@collection).page(@page).per(@per_page)
76
- if @collection.length == 0
77
- @collection = @collection.page(@collection.num_pages)
78
- end
79
- end
80
- else
81
- @collection = @collection.order("#{@sort_attr} #{@sort_order}") if @options[:sort] && @sort_attr && !@sort_attr.empty? && @sort_order
82
- if @options[:paginate] && @per_page > 0
83
- @collection = @collection.page(@page).per(@per_page)
84
- end
85
- end
86
- end
87
-
88
- def partial
89
- @options[:partial]
90
- end
91
-
92
- def param_names
93
- @options[:param_names]
94
- end
95
-
96
- def unlimited_per_page?
97
- !!@options[:unlimited_per_page]
98
- end
99
-
100
- def max_count
101
- @options[:max_count]
102
- end
103
-
104
- def href
105
- @options[:href]
106
- end
107
-
108
- def all_params
109
- ap = {}
110
- @options[:param_names].each do |k, v|
111
- ap[v] = self.send(k)
112
- end
113
- ap
114
- end
115
- end
4
+ class Base
5
+ if Rails.env.development?
6
+ DEFAULT_PAGE_SIZES = [3, 10, 20, 50, 100]
7
+ else
8
+ DEFAULT_PAGE_SIZES = [10, 20, 50, 100]
9
+ end
10
+
11
+ attr_reader :name, :collection, :options, :per_page, :page, :sort_attr, :sort_order, :sort_extra, :partial, :count
12
+
13
+ def initialize name, collection, options = {}
14
+ @name = name
15
+
16
+ @options = {
17
+ :param_names => { # param names
18
+ :page => "#{@name}_page".to_sym,
19
+ :per_page => "#{@name}_per_page".to_sym,
20
+ :sort_attr => "#{@name}_sort_attr".to_sym,
21
+ :sort_order => "#{@name}_sort_order".to_sym,
22
+ :sort_extra => "#{@name}_sort_extra".to_sym,
23
+ },
24
+ :partial => @name, # smart list partial name
25
+ :array => false, # controls whether smart list should be using arrays or AR collections
26
+ :max_count => nil, # limit number of rows
27
+ :unlimited_per_page => false, # allow infinite page size
28
+ :sort => true, # allow sorting
29
+ :paginate => true, # allow pagination
30
+ :href => nil, # set smart list target url (in case when different than current url)
31
+ :default_sort_attr => nil, # default sort by
32
+ :memorize_per_page => false,
33
+ :page_sizes => DEFAULT_PAGE_SIZES, # set available page sizes array
34
+ }.merge!(options)
35
+
36
+ if @options[:array]
37
+ @collection = collection.to_a
38
+ else
39
+ @collection = collection
40
+ end
41
+ end
42
+
43
+ def setup params, cookies
44
+ @page = params[param_names[:page]]
45
+ @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
46
+ @sort_attr = params[param_names[:sort_attr]] || @options[:default_sort_attr]
47
+ @sort_order = ["asc", "desc"].include?(params[param_names[:sort_order]]) ? params[param_names[:sort_order]] : "desc"
48
+ @sort_extra = params[param_names[:sort_extra]]
49
+
50
+ cookies[param_names[:per_page]] = @per_page if @options[:memorize_per_page]
51
+
52
+ @count = @collection.size
53
+
54
+ if @options[:array]
55
+ @collection = @collection.sort do |x, y|
56
+ xval = x
57
+ yval = y
58
+ @sort_attr.split(".").each do |m|
59
+ xval = xval.try(m)
60
+ yval = yval.try(m)
61
+ end
62
+ xval = xval.upcase if xval.is_a?(String)
63
+ yval = yval.upcase if yval.is_a?(String)
64
+
65
+ if xval.nil? || yval.nil?
66
+ xval.nil? ? 1 : -1
67
+ else
68
+ if @sort_order == "asc"
69
+ (xval <=> yval) || (xval && !yval ? 1 : -1)
70
+ else
71
+ (yval <=> xval) || (yval && !xval ? 1 : -1)
72
+ end
73
+ end
74
+ end if @options[:sort] && @sort_attr && !@sort_attr.empty?
75
+ if @options[:paginate] && @per_page > 0
76
+ @collection = ::Kaminari.paginate_array(@collection).page(@page).per(@per_page)
77
+ if @collection.length == 0
78
+ @collection = @collection.page(@collection.num_pages)
79
+ end
80
+ end
81
+ else
82
+ @collection = @collection.order("#{@sort_attr} #{@sort_order}") if @options[:sort] && @sort_attr && !@sort_attr.empty? && @sort_order
83
+ if @options[:paginate] && @per_page > 0
84
+ @collection = @collection.page(@page).per(@per_page)
85
+ end
86
+ end
87
+ end
88
+
89
+ def partial
90
+ @options[:partial]
91
+ end
92
+
93
+ def param_names
94
+ @options[:param_names]
95
+ end
96
+
97
+ def unlimited_per_page?
98
+ !!@options[:unlimited_per_page]
99
+ end
100
+
101
+ def max_count
102
+ @options[:max_count]
103
+ end
104
+
105
+ def href
106
+ @options[:href]
107
+ end
108
+
109
+ def page_sizes
110
+ @options[:page_sizes]
111
+ end
112
+
113
+ def all_params
114
+ ap = {}
115
+ @options[:param_names].each do |k, v|
116
+ ap[v] = self.send(k)
117
+ end
118
+ ap
119
+ end
120
+ end
116
121
  end
@@ -1,3 +1,3 @@
1
1
  module SmartListing
2
- VERSION = "0.9.1"
2
+ VERSION = "0.9.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: smart_listing
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.1
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sology
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-11-28 00:00:00.000000000 Z
11
+ date: 2013-12-04 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -107,7 +107,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
107
107
  version: '0'
108
108
  requirements: []
109
109
  rubyforge_project:
110
- rubygems_version: 2.1.10
110
+ rubygems_version: 2.0.0
111
111
  signing_key:
112
112
  specification_version: 4
113
113
  summary: SmartListing helps creating sortable lists of ActiveRecord collections with