serviceable 0.4.2 → 0.4.3
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.
- data/README.md +43 -10
- data/lib/serviceable.rb +1 -2
- metadata +3 -3
data/README.md
CHANGED
@@ -1,5 +1,4 @@
|
|
1
|
-
Serviceable
|
2
|
-
=========
|
1
|
+
# Serviceable
|
3
2
|
|
4
3
|
Serviceable aims to reduce code duplication for common patterns, such as JSON/XML
|
5
4
|
API endpoints. Instead of repeating the same patterns in multiple controllers and
|
@@ -19,8 +18,7 @@ Route:
|
|
19
18
|
resources :posts
|
20
19
|
|
21
20
|
|
22
|
-
Standard CRUD
|
23
|
-
-------------
|
21
|
+
## Standard CRUD
|
24
22
|
|
25
23
|
POST /posts.json
|
26
24
|
GET /posts.json
|
@@ -28,20 +26,55 @@ Standard CRUD
|
|
28
26
|
PUT /posts/1.json
|
29
27
|
DELETE /posts/1.json
|
30
28
|
|
31
|
-
Query Params
|
32
|
-
|
29
|
+
## Query Params
|
30
|
+
|
31
|
+
Full listing returned when no query params are given
|
33
32
|
|
34
33
|
GET /posts.json
|
35
34
|
[{"id":1,"title":"First Post!","body":"Feels good to be first","created_at":"20130727T16:26:00Z"}]
|
36
35
|
|
36
|
+
Use the <code>only</code> param to specify fields on the collection
|
37
|
+
|
37
38
|
GET /posts.json?only=id,title
|
38
39
|
[{"id":1,"title","First post!"}]
|
39
|
-
|
40
|
+
|
41
|
+
Use the <code>include</code> param to specify associated objects or collections
|
42
|
+
|
40
43
|
GET /posts.json?include=user
|
41
44
|
[{"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
45
|
|
43
|
-
|
44
|
-
|
46
|
+
Combine params to configure the result contents
|
47
|
+
|
48
|
+
GET /posts.json?only=id,title&include[user][only]=first_name,last_name
|
49
|
+
[{"id":1,"title":"First Post!","user":{"first_name":"Jim","last_name":"Walker"}}]
|
50
|
+
|
51
|
+
## Filter Params
|
52
|
+
|
53
|
+
Use the <code>where</code> param to filter the results
|
54
|
+
|
55
|
+
GET /posts.json?where[posts][published]=true
|
56
|
+
|
57
|
+
Use associated objects or collections to filter the set
|
45
58
|
|
46
59
|
GET /posts.json?where[tags][label]=Pinball
|
47
|
-
|
60
|
+
|
61
|
+
## Integrating with jQuery
|
62
|
+
|
63
|
+
Working with a serviceable endpoint is easy with jQuery. Here's an example of a request for all posts
|
64
|
+
tagged as "Pinball" including only post id and title:
|
65
|
+
|
66
|
+
$.ajax({
|
67
|
+
url: '/posts.json',
|
68
|
+
type: 'GET',
|
69
|
+
data: {
|
70
|
+
where: {
|
71
|
+
tags: {
|
72
|
+
label: 'Pinball'
|
73
|
+
}
|
74
|
+
},
|
75
|
+
only: 'id,title'
|
76
|
+
},
|
77
|
+
success: function(xhr,msg,data) {
|
78
|
+
console.log("received "+data.responseJSON.length+" results");
|
79
|
+
}
|
80
|
+
});
|
data/lib/serviceable.rb
CHANGED
@@ -150,7 +150,7 @@ module Serviceable
|
|
150
150
|
elsif op==:lt
|
151
151
|
@collection = @collection.where("#{assoc}.#{target_column} < ?",value)
|
152
152
|
elsif op==:in
|
153
|
-
@collection = @collection.where("#{assoc}.#{target_column} IN (?)",value)
|
153
|
+
@collection = @collection.where("#{assoc}.#{target_column} IN (?)",value.split(','))
|
154
154
|
end
|
155
155
|
end
|
156
156
|
end
|
@@ -168,7 +168,6 @@ module Serviceable
|
|
168
168
|
|
169
169
|
define_method("is_time_column?") do |column|
|
170
170
|
object.to_s.capitalize.constantize.columns.select {|e| e.name==column.to_s}.first.type == :timestamp rescue false
|
171
|
-
# !!column[-3,3]=='_at'
|
172
171
|
end
|
173
172
|
|
174
173
|
define_method("is_boolean_column?") do |column|
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: serviceable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 9
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 3
|
10
|
+
version: 0.4.3
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Aubrey Goodman
|