serviceable 0.4 → 0.4.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.
Files changed (3) hide show
  1. data/README.md +9 -0
  2. data/lib/serviceable.rb +18 -17
  3. metadata +3 -2
data/README.md CHANGED
@@ -36,3 +36,12 @@ Query Params
36
36
 
37
37
  GET /posts.json?only=id,title
38
38
  [{"id":1,"title","First post!"}]
39
+
40
+ GET /posts.json?include=user
41
+ [{"id":1,"title":"First Post!","body":"Feels good to be first","created_at":"20130727T16:26:00Z","user":{"id":2,"first_name":"Jim","last_name":"Walker","display_name":"Jim W."}}]
42
+
43
+ Filter Params
44
+ -------------
45
+
46
+ GET /posts.json?where[tags][label]=Pinball
47
+ returns a set of posts tagged as "Pinball"
@@ -130,24 +130,25 @@ module Serviceable
130
130
  for assoc in (params[:where].keys rescue [])
131
131
  attrs = params[:where][assoc]
132
132
  if attrs.kind_of?(Hash)
133
+ puts "keys: #{attrs.keys}"
133
134
  for target_column in attrs.keys
134
- op = :eq if attrs[target_column].kind_of?(String)
135
- op ||= attrs[target_column].keys[0].to_sym
136
- value = attrs[target_column] if op==:eq
137
- value ||= is_time_column?(target_column) ? Time.parse(attrs[target_column][op]) : attrs[target_column][op]
138
- unless assoc.to_sym==object.to_s.pluralize.to_sym
139
- @collection = @collection.includes(assoc)
140
- end
141
- puts "op: #{op}"
142
- if op==:gt
143
- @collection = @collection.where("#{assoc}.#{target_column} > ?",value)
144
- elsif op==:lt
145
- @collection = @collection.where("#{assoc}.#{target_column} < ?",value)
146
- elsif op==:in
147
- @collection = @collection.where("#{assoc}.#{target_column} IN (?)",value)
148
- elsif op==:eq
149
- @collection = @collection.where(assoc => { target_column => value })
150
- end
135
+ if attrs[target_column].kind_of?(String)
136
+ @collection = @collection.where(assoc => { target_column => attrs[target_column] })
137
+ elsif attrs[target_column].kind_of?(Hash)
138
+ for op in attrs[target_column].keys.map(&:to_sym)
139
+ value = is_time_column?(target_column) ? Time.parse(attrs[target_column][op]) : attrs[target_column][op]
140
+ unless assoc.to_sym==object.to_s.pluralize.to_sym
141
+ @collection = @collection.includes(assoc)
142
+ end
143
+ if op==:gt
144
+ @collection = @collection.where("#{assoc}.#{target_column} > ?",value)
145
+ elsif op==:lt
146
+ @collection = @collection.where("#{assoc}.#{target_column} < ?",value)
147
+ elsif op==:in
148
+ @collection = @collection.where("#{assoc}.#{target_column} IN (?)",value)
149
+ end
150
+ end
151
+ end
151
152
  end
152
153
  else
153
154
  @collection = @collection.includes(assoc).where(assoc => attrs)
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: serviceable
3
3
  version: !ruby/object:Gem::Version
4
- hash: 3
4
+ hash: 13
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 4
9
- version: "0.4"
9
+ - 1
10
+ version: 0.4.1
10
11
  platform: ruby
11
12
  authors:
12
13
  - Aubrey Goodman