yap 0.2.1 → 0.3.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: f4209a6b39a6416deb044ff771ee02c69ca4a4aa
4
- data.tar.gz: 263b674a929599199a7834784e6c8952ae98bc78
3
+ metadata.gz: f6b0e9e0e3dc3083f3d0b547b73c9798aea4df38
4
+ data.tar.gz: cb97d4f95634d81f5d5f9a7fd5bf15dbce071fff
5
5
  SHA512:
6
- metadata.gz: 0f09333d7565e16f89b1f6ed11df04dfaeadc772fcb5e6bc9af9d805592ddfc6d15f3855742fd6170fcf94b50bca1571c3cbd55ff1d4bca14aa4e9eb1583f97e
7
- data.tar.gz: 9d3f5de083c85b3ad493f93bcfd669a0f29e06d0a3dd693bec90ad124dc29a33043c10dfe5b52d82472eb41e3f9c8389a24cdfa80e647d114776c9995b1bf07a
6
+ metadata.gz: d10b61aed2e0f9757ca7b08006b4b60640a661c052f9d7dcbad5bd3bde800393f488f92ecd9add039bff0a574d44d9ee5af1485979f51ca003dd8c1504170235
7
+ data.tar.gz: fb430be1608425b2b82b06029c033812c987f1974cd652ff6401b5371f1015d889e89d52ea4727e66c5abe49fa1d570d8f4525257ed65ccf85e532882a55dc33
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # yap v0.2.1
1
+ # yap v0.3.2
2
2
 
3
3
  Yet another paginator for Ruby on Rails, which adds a `paginate` scope to your ActiveRecords.
4
4
 
@@ -45,7 +45,7 @@ internal naming from users and to make sorting by associations possible (more on
45
45
 
46
46
  Assuming you included `Yap` into `User`, you can now do something like this:
47
47
 
48
- User.paginate # => Page 1 with default order and size.
48
+ User.paginate # => Page 1 with default order and size
49
49
  User.paginate(
50
50
  page: 1,
51
51
  per_page: 10,
@@ -56,16 +56,19 @@ Assuming you included `Yap` into `User`, you can now do something like this:
56
56
  User.last_page # => Last page as a number for defaults
57
57
  User.last_page(params) # => Last page for given params. Works the same way as paginate.
58
58
 
59
- User.filter('gender' => 'f') # => All female users.
59
+ User.filter('gender' => 'f') # => All female users
60
60
  User.filter(
61
61
  'team_id' => '1,2',
62
62
  'gender' => 'm'
63
- ) # => All males of teams 1 and 2.
64
- User.filter('team_id' => '!null') # => All users with any team.
63
+ ) # => All males of teams 1 and 2
64
+ User.filter(
65
+ 'date_of_birth' => '1990-01-01..1991-01-01'
66
+ ) # => All users born in 1990
67
+ User.filter('team_id' => '!null') # => All users with any team
65
68
  User.paginate(
66
69
  page: 1,
67
70
  filter: { 'team' => 'null' }
68
- ) # => Combining filter and pagination.
71
+ ) # => Combining filter and pagination
69
72
 
70
73
  User.paginate(params) # => Passing parameters in controller (http://localhost/users?filter[gender]=f)
71
74
 
@@ -124,3 +127,4 @@ If an option cannot be parsed it will raise `Yap::PaginationError` or `Yap::Filt
124
127
 
125
128
  * Methods for generating next, previous and last page links
126
129
  * Maximum for per_page
130
+ * Rescue from sql errors
data/lib/yap/filter.rb ADDED
@@ -0,0 +1,32 @@
1
+ module Yap
2
+ class Filter < Hash
3
+ def initialize
4
+ self[:where] = {}
5
+ self[:not] = {}
6
+ end
7
+
8
+ def parse!(column, values)
9
+ values.to_s.split(',').each do |value|
10
+ # Perform negative match if value starts with '!'.
11
+ if value =~/^!(.+)$/
12
+ match = :not
13
+ value = $1
14
+ else
15
+ match = :where
16
+ end
17
+
18
+ if value =~ /(.+)\.\.(.+)/
19
+ value = $1..$2
20
+ else
21
+ value = value.downcase == 'null' ? nil : value
22
+ end
23
+
24
+ # Ensure filter contains an array to append to.
25
+ self[match][column] ||= []
26
+
27
+ # Convert null to ruby nil to use 'IS NULL' in SQL.
28
+ self[match][column] << value
29
+ end
30
+ end
31
+ end
32
+ end
@@ -1,6 +1,7 @@
1
1
  require 'active_support/concern'
2
2
  require 'yap/exceptions'
3
3
  require 'yap/column_mapper'
4
+ require 'yap/filter'
4
5
 
5
6
  ##
6
7
  # ActiveRecords can be filtered by their attributes if either Yap or Filterable is included into the model. Filters can
@@ -43,30 +44,13 @@ module Yap
43
44
  private
44
45
 
45
46
  def self.extract_filter_params(params)
46
- filter = {
47
- where: {},
48
- not: {}
49
- }
47
+ filter = Filter.new
50
48
 
51
49
  failed = []
52
50
  params.each do |key, values|
53
51
  column = map_column(key.to_s.downcase)
54
52
  if column
55
- values.to_s.split(',').each do |value|
56
- # Perform negative match if value starts with '!'.
57
- if value =~/^!(.+)$/
58
- match = :not
59
- value = $1
60
- else
61
- match = :where
62
- end
63
-
64
- # Ensure filter contains an array to append to.
65
- filter[match][column] ||= []
66
-
67
- # Convert null to ruby nil to use 'IS NULL' in SQL.
68
- filter[match][column] << (value.downcase == 'null' ? nil : value)
69
- end
53
+ filter.parse!(column, values)
70
54
  else
71
55
  failed << key
72
56
  end
data/lib/yap.rb CHANGED
@@ -36,7 +36,7 @@ module Yap
36
36
  module ClassMethods
37
37
  def last_page(params)
38
38
  per_page = extract_number(params[:per_page], DEFAULTS.per_page)
39
- (count / per_page.to_f).ceil
39
+ (filter(params[:filter]).count / per_page.to_f).ceil
40
40
  end
41
41
  end
42
42
 
@@ -66,8 +66,9 @@ module Yap
66
66
  end
67
67
 
68
68
  def self.extract_number(number, default)
69
+ number ||= default
69
70
  begin
70
- number = number.present? ? Integer(number) : default
71
+ number = Integer(number)
71
72
  rescue
72
73
  raise PaginationError.new("'#{number}' is not a valid number.")
73
74
  end
@@ -77,13 +78,15 @@ module Yap
77
78
  end
78
79
 
79
80
  def self.extract_column(sort)
80
- column = map_column(sort.present? ? sort.to_s.downcase : DEFAULTS.sort)
81
+ sort ||= DEFAULTS.sort
82
+ column = map_column(sort.to_s.downcase)
81
83
  raise PaginationError.new("Cannot sort by '#{sort}'.") unless column
82
84
  column
83
85
  end
84
86
 
85
87
  def self.extract_direction(direction)
86
- dir = direction.present? ? direction.to_s.upcase : DEFAULTS.direction
88
+ direction ||= DEFAULTS.direction
89
+ dir = (direction).to_s.upcase
87
90
  raise PaginationError.new("'#{direction}' is not a valid direction. Use 'asc' or 'desc'.") unless %w[ASC DESC].include? dir
88
91
  dir
89
92
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yap
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Finn Glöe
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-19 00:00:00.000000000 Z
11
+ date: 2015-01-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -50,6 +50,7 @@ files:
50
50
  - lib/yap.rb
51
51
  - lib/yap/column_mapper.rb
52
52
  - lib/yap/exceptions.rb
53
+ - lib/yap/filter.rb
53
54
  - lib/yap/filterable.rb
54
55
  homepage: http://rubygems.org/gems/yap
55
56
  licenses: