load_more 0.0.4 → 0.0.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.rdoc +20 -12
- data/lib/load_more/version.rb +1 -1
- data/lib/load_more.rb +62 -23
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9c9c52f3aeb1cda9630c813b0e16d1bb36d9b5c3
|
4
|
+
data.tar.gz: 1ebb8b3cb48541a66706c5319569815fde614204
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3219e548d7010f937cd5ef3bd1470ecf0692c1e19bc143e17231aba22905ac2e35ba3603c06e9a1caf673491f7adabd0677c3ff0560f8e8e442f8eefe32c8f7e
|
7
|
+
data.tar.gz: c636a66bfa144727169ff987270e81234fc435faf21a623b0753ab9443d719664069ca876501001f0e716ef7305eebf8a77cbccac75c16f89e24b3a026f2bdca
|
data/README.rdoc
CHANGED
@@ -8,22 +8,30 @@ Then run
|
|
8
8
|
bundle install
|
9
9
|
|
10
10
|
== Usage
|
11
|
+
|
12
|
+
=== Query
|
13
|
+
Article.load_more(last_load: params[:last_load_id])
|
14
|
+
Article.last_load(params:[last_load_id])
|
15
|
+
|
16
|
+
=== Configuring global default value
|
17
|
+
In +config/initializers/load_more.rb+:
|
18
|
+
LoadMore.configure do |config|
|
19
|
+
config.load_limit = 10
|
20
|
+
config.sort_column = :id
|
21
|
+
config.sort_method = :desc
|
22
|
+
end
|
23
|
+
The above is the default configuration.
|
24
|
+
|
25
|
+
=== Configuring default value for each model
|
11
26
|
In your model:
|
12
27
|
class Article < ActiveRecord::Base
|
13
|
-
|
14
|
-
self.
|
28
|
+
self.load_limit = 15
|
29
|
+
self.sort_column = :unique_id
|
30
|
+
self.sort_method = :desc
|
15
31
|
end
|
16
|
-
The default 'per_load' value is 10.
|
17
|
-
|
18
|
-
Then you can use
|
19
|
-
>> Article.load_more(last_load: params[:last_load_id])
|
20
|
-
or
|
21
|
-
>> Article.last_load(params:[last_load_id])
|
22
|
-
|
23
|
-
You can also customize the per_load value:
|
24
|
-
>> Article.load_more(per_load: 20, last_load: 99)
|
25
32
|
|
26
|
-
|
33
|
+
=== Configuring default values for each query
|
34
|
+
Article.load_more(load_limit: 20, sort_column: :sku, sort_method: :asc, last_load: 99)
|
27
35
|
|
28
36
|
== License
|
29
37
|
This project rocks and uses MIT-LICENSE.
|
data/lib/load_more/version.rb
CHANGED
data/lib/load_more.rb
CHANGED
@@ -1,42 +1,81 @@
|
|
1
1
|
require 'active_record' unless defined? ActiveRecord
|
2
2
|
|
3
3
|
module LoadMore
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
def per_load=(limit)
|
8
|
-
@@per_load = limit
|
9
|
-
end
|
4
|
+
def self.configure(&block)
|
5
|
+
yield @config ||= LoadMore::Configuration.new
|
6
|
+
end
|
10
7
|
|
11
|
-
|
12
|
-
|
13
|
-
|
8
|
+
def self.config
|
9
|
+
@config
|
10
|
+
end
|
14
11
|
|
15
|
-
|
16
|
-
|
17
|
-
last_load_id = options.delete(:last_load)
|
18
|
-
rel = order(id: :desc).limit(per_load)
|
19
|
-
rel = rel.where('id < ?', last_load_id) if last_load_id
|
20
|
-
rel
|
21
|
-
end
|
12
|
+
class Configuration
|
13
|
+
include ActiveSupport::Configurable
|
22
14
|
|
23
|
-
|
24
|
-
|
25
|
-
|
15
|
+
config_accessor :load_limit
|
16
|
+
config_accessor :sort_column
|
17
|
+
config_accessor :sort_method
|
26
18
|
end
|
27
19
|
|
28
|
-
module
|
20
|
+
module ActiveRecord
|
29
21
|
extend ActiveSupport::Concern
|
30
22
|
|
31
23
|
included do
|
32
24
|
end
|
33
25
|
|
34
26
|
module ClassMethods
|
35
|
-
def
|
36
|
-
|
27
|
+
def load_limit=(limit)
|
28
|
+
@@load_limit = limit
|
29
|
+
end
|
30
|
+
|
31
|
+
def load_limit
|
32
|
+
defined?(@@load_limit) ? @@load_limit : LoadMore.config.load_limit
|
33
|
+
end
|
34
|
+
|
35
|
+
def sort_column=(col)
|
36
|
+
@@sort_column = col
|
37
|
+
end
|
38
|
+
|
39
|
+
def sort_column
|
40
|
+
defined?(@@sort_column) ? @@sort_column : LoadMore.config.sort_column
|
41
|
+
end
|
42
|
+
|
43
|
+
def sort_method=(method)
|
44
|
+
@@sort_method = method
|
45
|
+
end
|
46
|
+
|
47
|
+
def sort_method
|
48
|
+
defined?(@@sort_method) ? @@sort_method : LoadMore.config.sort_method
|
49
|
+
end
|
50
|
+
|
51
|
+
def load_more(options = {})
|
52
|
+
load_limit = options.delete(:load_limit) || self.load_limit
|
53
|
+
sort_column = options.delete(:sort_column) || self.sort_column
|
54
|
+
sort_method = options.delete(:sort_method) || self.sort_method
|
55
|
+
last_load_id = options.delete(:last_load)
|
56
|
+
rel = order(sort_column => sort_method).limit(load_limit)
|
57
|
+
if last_load_id
|
58
|
+
where_query = if sort_method == :desc
|
59
|
+
"#{self.table_name}.#{sort_column} < ?"
|
60
|
+
else
|
61
|
+
"#{self.table_name}.#{sort_column} > ?"
|
62
|
+
end
|
63
|
+
rel = rel.where(where_query, last_load_id)
|
64
|
+
end
|
65
|
+
rel
|
66
|
+
end
|
67
|
+
|
68
|
+
def last_load(id = nil)
|
69
|
+
load_more(last_load: id)
|
37
70
|
end
|
38
71
|
end
|
39
72
|
end
|
40
73
|
end
|
41
74
|
|
42
|
-
|
75
|
+
LoadMore.configure do |config|
|
76
|
+
config.load_limit = 10
|
77
|
+
config.sort_column = :id
|
78
|
+
config.sort_method = :desc
|
79
|
+
end
|
80
|
+
|
81
|
+
ActiveRecord::Base.send :include, LoadMore::ActiveRecord
|