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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 47539dc513cd2fa7d7c0046ea2317aee4eeb5d0e
4
- data.tar.gz: 95478dbdf6aadad5dde010f7b06d4249229b4c73
3
+ metadata.gz: 9c9c52f3aeb1cda9630c813b0e16d1bb36d9b5c3
4
+ data.tar.gz: 1ebb8b3cb48541a66706c5319569815fde614204
5
5
  SHA512:
6
- metadata.gz: 165b7854d5fe4d082dd45848b44d249000ad57c0940b2f6d77e23ff04c46b040f869ebb9fc220bff9402cd1897c1504fbd61ce7bd0a1862f8176413f976d0195
7
- data.tar.gz: 3601b69f85dcb31fb4bfff22ad7ea9c1ac9861994cfcdb6cf94c1b0c29d63f07f118f35be9ae25b48ba73db5e57b85ef9f95c27b891730215748eb17090dc2ea
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
- acts_as_load_more
14
- self.per_load = 15
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
- By default it use id as the sort column.
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.
@@ -1,3 +1,3 @@
1
1
  module LoadMore
2
- VERSION = "0.0.4"
2
+ VERSION = "0.0.5"
3
3
  end
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
- module ActiveRecord
5
- @@per_load = 10
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
- def per_load
12
- @@per_load
13
- end
8
+ def self.config
9
+ @config
10
+ end
14
11
 
15
- def load_more(options = {})
16
- per_load = options.delete(:per_load) || self.per_load
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
- def last_load(id = nil)
24
- load_more(last_load: id)
25
- end
15
+ config_accessor :load_limit
16
+ config_accessor :sort_column
17
+ config_accessor :sort_method
26
18
  end
27
19
 
28
- module ActsAsLoadMore
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 acts_as_load_more
36
- extend LoadMore::ActiveRecord
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
- ActiveRecord::Base.send :include, LoadMore::ActsAsLoadMore
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
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: load_more
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.4
4
+ version: 0.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jiajia Wang