gyroscope 0.1.1 → 1.0.0

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: 921456f0511457b6ac95020900184bbd72dda476
4
- data.tar.gz: 58b80e30d3121376dc532814056305217c8fa6c9
3
+ metadata.gz: fa8748d31c73c8bff773acbfc2e9dbbbae9b9ce1
4
+ data.tar.gz: ce805aa11148b58ffc62e0f555ca801df0c793dd
5
5
  SHA512:
6
- metadata.gz: 15335b433d70c5b3c3854269f7e3ad4f449d7b92164684572ac064478bde20812432b678b43847225fce13ce2d6127620868197b26939d605ae307d5c6bbd90f
7
- data.tar.gz: 4e63ef51e2f8e1f302190215b71cb85f008ff7713cac02ffab9a19b016677baa8f4d6f5bea987e96312af5eba7d63a8cfb38c966cfdb28eb748f25046f27a763
6
+ metadata.gz: 018dcabf4fc922a448ebfed4d045405df0790118a432985b9997984b93f25b2d37bca1a58b64e3e30f9a511a1d2d5c7d61a6fe659b67d4dcd2d9a38419ec1b2d
7
+ data.tar.gz: 7e8a9b60dadc2b352b5d3372bc9710368777942f830fcc985f562cdc56596715aa161ee1412cb6110bbb4a284b4f808ec4aa6c65b4c28ead60f3f1726eb2aa9f
@@ -3,24 +3,6 @@ require "active_model"
3
3
 
4
4
  module Gyroscope
5
5
  class SearchBase
6
- include Virtus.model strict: true, required: false
7
- include ActiveModel::Validations
8
-
9
- attribute :page, Integer, default: 1
10
- attribute :per_page, Integer, default: 30
11
- attribute :order, String
12
-
13
- validate :validate_order
14
-
15
- def initialize(*args)
16
- super(*args)
17
- normalize_order attributes[:order]
18
- end
19
-
20
- def target_model
21
- "::#{self.class.name.demodulize}".constantize
22
- end
23
-
24
6
  class ValidationError < StandardError
25
7
  attr_reader :searcher
26
8
 
@@ -37,27 +19,57 @@ module Gyroscope
37
19
  end
38
20
  end
39
21
 
22
+ include Virtus.model strict: true, required: false
23
+ include ActiveModel::Validations
24
+
25
+ attribute :page, Integer, default: 1
26
+ attribute :per_page, Integer, default: 30
27
+ attribute :order, String
28
+
29
+ validate :validate_order
30
+
31
+ def initialize(attrs)
32
+ super(normalize_order(attrs.dup))
33
+ end
34
+
35
+ def target_model
36
+ "::#{self.class.name.demodulize}".constantize
37
+ end
38
+
40
39
  def search
41
40
  # is there a more generic ActiveModel validation error?
42
41
  unless valid?
43
42
  Rails.logger.debug("the errors: #{errors.inspect}")
43
+
44
+ fail ValidationError.new(self)
44
45
  end
45
- fail ValidationError.new(self) unless valid?
46
46
 
47
47
  build_search_scope
48
48
  end
49
49
 
50
- def normalize_order(ordering)
51
- return unless attributes[:order]
50
+ private
52
51
 
53
- unless /\./.match ordering
54
- self.order = "#{target_model.table_name}.#{ordering}"
52
+ def normalize_order(attrs)
53
+ order = attrs[:order] || attrs["order"]
54
+
55
+ return attrs unless order.present?
56
+
57
+ if order.is_a?(Hash)
58
+ attrs[:order] = order.map {|(column, direction)| tablize_order("#{column} #{direction}")}.join(",")
55
59
  else
56
- self.order = ordering
60
+ attrs[:order] = tablize_order(order)
57
61
  end
62
+
63
+ attrs
58
64
  end
59
65
 
60
- private
66
+ def tablize_order(ordering)
67
+ unless /\./.match ordering
68
+ "#{target_model.table_name}.#{ordering}"
69
+ else
70
+ ordering
71
+ end
72
+ end
61
73
 
62
74
  def base_scope
63
75
  scope = target_model
@@ -86,16 +98,16 @@ module Gyroscope
86
98
 
87
99
  def validate_order
88
100
  return unless attributes[:order]
89
- split_order = attributes[:order].split(' ')
101
+ attributes[:order].split(",").each do |order|
102
+ key, direction = order.split(' ')
90
103
 
91
- key, direction = split_order
92
-
93
- errors.add(:order, 'Ordering on disallowed key') unless allowed_order_keys.include? key
94
- errors.add(:order, 'Invalid sort direction') unless direction && %w(asc desc).include?(direction.downcase)
104
+ errors.add(:order, 'Ordering on disallowed key') unless allowed_order_keys.include? key
105
+ errors.add(:order, 'Invalid sort direction') unless direction && %w(asc desc).include?(direction.downcase)
106
+ end
95
107
  end
96
108
 
97
109
  def allowed_order_keys
98
- target_model.new.attributes.keys.map do |key|
110
+ target_model.new.persistable_attribute_names.map do |key|
99
111
  "#{target_model.table_name}.#{key}"
100
112
  end
101
113
  end
@@ -1,3 +1,3 @@
1
1
  module Gyroscope
2
- VERSION = "0.1.1"
2
+ VERSION = "1.0.0"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: gyroscope
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Wilhelm
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2016-04-18 00:00:00.000000000 Z
12
+ date: 2017-04-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activemodel
@@ -152,7 +152,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
152
152
  version: '0'
153
153
  requirements: []
154
154
  rubyforge_project:
155
- rubygems_version: 2.5.1
155
+ rubygems_version: 2.4.5.1
156
156
  signing_key:
157
157
  specification_version: 4
158
158
  summary: Build ActiveRecord search scopes from params hash