activerecord-filter 1.0.0 → 1.0.1

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: 7fa6a908a88c2552f0e8fc6ac2556d651fc9eb7b
4
- data.tar.gz: d21504e13db25b2095554fcc67b0852226444325
3
+ metadata.gz: e58dc2beee5f882a7ffc33cbedd1af29badedfb8
4
+ data.tar.gz: 1ae2e957182bac436a0416e522b5ca65e92122c4
5
5
  SHA512:
6
- metadata.gz: fae042c9f46ae8a21ea19deb8f846967e6992083b51ca0bc62b3ae548d5d287aed28198572adc294437d38f4def37e2a61fea05854754231bd09f17d3b133460
7
- data.tar.gz: 35d171276aaadc17f95f9984b05300962175dd1571179b6164c8560101f91e0e82374c21b7b6d8dfc97b089cf02539ec8291ea9b8d1a0dc629271a42d9f073e0
6
+ metadata.gz: 4d3f3fbbc571cccfee96bf9fdcd765d36325232d03898177fd8f6cc065682d6a610524e520087de2a6fb20203728c2dca01d03fc1abe752119814f7d6f8708fa
7
+ data.tar.gz: 1db8ff73b59f396551fd1eca0687a5ef940cf533f744e0b3b7b8f098e0c64664ddc1d5261256ed97bc810917e84e4885a2304f8d863ba8c04958ce16cfcfac20
data/README.md CHANGED
@@ -1,2 +1,63 @@
1
- # activerecord-filter
2
- A safe way to accept user parameters and query against your ActiveRecord Models
1
+ # ActiveRecord::filter
2
+
3
+ `ActiveRecord::filter` provides and easy way to accept user input and filter a query by the input.
4
+
5
+ Installtion
6
+ -----------
7
+
8
+ - Add `gem 'activerecord-filter', require: 'active_record/filter'
9
+ - Run `bundle install`
10
+
11
+ Examples
12
+ --------
13
+
14
+ Normal columns:
15
+
16
+ ```ruby
17
+ Property.filter(:id => 5).to_sql
18
+ # => "...WHERE properties.id = 5"
19
+
20
+ Property.filter(:id => [5, 10, 15]).to_sql
21
+ # => "...WHERE properties.id IN (5, 10, 15)"
22
+
23
+ Property.filter(:id => {:gt => 5}).to_sql
24
+ # => "...WHERE properties.id > 5"
25
+
26
+ Property.filter(:id => {:gte => 5}).to_sql
27
+ # => "...WHERE properties.id >= 5"
28
+
29
+ Property.filter(:id => {:lt => 5}).to_sql
30
+ # => "...WHERE properties.id < 5"
31
+
32
+ Property.filter(:id => {:lte => 5}).to_sql
33
+ # => "...WHERE properties.id <= 5"
34
+
35
+ Property.filter(:address_id => nil).to_sql
36
+ # => "...WHERE properties.address_id IS NULL..."
37
+
38
+ Property.filter(:address_id => false).to_sql
39
+ # => "...WHERE properties.address_id IS NULL..."
40
+
41
+ Property.filter(:address_id => true).to_sql
42
+ # => "...WHERE properties.address_id IS NOT NULL..."
43
+ ```
44
+
45
+ It can also work with array columns:
46
+
47
+ ```ruby
48
+ Property.filter(:tags => 'Skyscraper').to_sql
49
+ # => "...WHERE 'Skyscraper' = ANY(properties.tags)..."
50
+
51
+ Property.filter(:tags => ['Skyscraper', 'Brick']).to_sql
52
+ # => "...WHERE (properties.aliases && '{"Skyscraper", "Brick"}')..."
53
+ ```
54
+ It can also sort on relations:
55
+
56
+ ```ruby
57
+ Photo.filter(:property => 10).to_sql
58
+ # => "...WHERE photos.property_id = 5"
59
+
60
+ Photo.filter(:property => {name: 'Empire State'}).to_sql
61
+ # => "...INNER JOIN properties ON properties.id = photos.property_id
62
+ # => " WHERE properties.name = 'Empire State'"
63
+ ```
@@ -134,7 +134,6 @@ class ActiveRecord::Base
134
134
  when nil
135
135
  where(table[column].eq(nil))
136
136
  else
137
- puts value.inspect
138
137
  raise 'Not supported'
139
138
  end
140
139
  end
@@ -143,8 +142,21 @@ class ActiveRecord::Base
143
142
  table = options[:table_alias] ? arel_table.alias(options[:table_alias]) : arel_table
144
143
 
145
144
  case value
145
+ when Hash
146
+ resource = all
147
+ value.each_pair do |key, value|
148
+ resource = case key.to_sym
149
+ when :contains
150
+ resource.where(Arel::Nodes::Contains.new(table[column], Arel::Attributes::Array.new(value)))
151
+ when :overlaps
152
+ resource.where(Arel::Nodes::Overlaps.new(table[column], Arel::Attributes::Array.new(value)))
153
+ else
154
+ raise "Not Supported: #{key.to_sym}"
155
+ end
156
+ end
157
+ resource
146
158
  when Array
147
- where(Arel::Nodes::Overlaps.new(table[column], Arel::Attributes::Array.new(value)))
159
+ where(Arel::Nodes::Contains.new(table[column], Arel::Attributes::Array.new(value)))
148
160
  else
149
161
  any_column = Arel::Nodes::NamedFunction.new('ANY', [table[column]])
150
162
  predicate = Arel::Nodes::Equality.new(Arel::Nodes.build_quoted(value), any_column)
@@ -226,13 +238,14 @@ class ActiveRecord::Base
226
238
  when Hash
227
239
  if relation.polymorphic?
228
240
  raise 'no :as' if !value[:as]
229
- klass = value.delete(:as).classify.constantize
241
+ v = value.dup
242
+ klass = v.delete(:as).classify.constantize
230
243
  t1 = resource.arel_table
231
244
  t2 = klass.arel_table
232
245
  resource = resource.joins(t1.join(t2).on(
233
246
  t2[:id].eq(t1["#{relation.name}_id"]).and(t1["#{relation.name}_type"].eq(klass.name))
234
247
  ).join_sources.first)
235
- resource = resource.merge(klass.filter(value))
248
+ resource = resource.merge(klass.filter(v))
236
249
  else
237
250
  resource = resource.joins(relation.name) # if !resource.references?(relation.name)
238
251
  resource = resource.merge(relation.klass.filter(value))
@@ -19,7 +19,14 @@ module Arel
19
19
  type = if !o.relation[0]
20
20
  ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new(nil)
21
21
  else
22
- ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new("ActiveRecord::Type::#{o.relation[0].class}".constantize.new)
22
+ # TODO: temp fix, need to figure out how to look up ruby type ->
23
+ # postgres type or use the column we're quering on...
24
+ dt = if o.relation[0].class == Fixnum
25
+ dt = "Integer"
26
+ else
27
+ o.relation[0].class
28
+ end
29
+ ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Array.new("ActiveRecord::Type::#{dt}".constantize.new)
23
30
  end
24
31
 
25
32
  collector << quote(type.type_cast_for_database(o.relation))
@@ -1,5 +1,5 @@
1
1
  module ActiveRecord
2
2
  module Filter
3
- VERSION = '1.0.0'
3
+ VERSION = '1.0.1'
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activerecord-filter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jon Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-07-30 00:00:00.000000000 Z
11
+ date: 2015-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg