apicasso 0.1.6 → 0.2.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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 623f4bebf4617b89d81b3ef5af3e5ed5db0e19cb
|
4
|
+
data.tar.gz: d8431c10ff78ffa0e168bec56ad018227a87f2d8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9c958b5138df397df95ea4d95dde071fc95e153b20d40401f5d8813adcba0c9c5d9e4aff17488ba56a4ed0a676026c0419dfce1df0b1661c13fb06b4bc991d4f
|
7
|
+
data.tar.gz: 167d8058e029c805a004135ca25711dae1fa11740fc4929dfebf5f44abc0eca447bcc0c2106ec1b9112545a622eb2bf6b6cac4bfb952914c132afd720e1d80e6
|
@@ -100,5 +100,12 @@ module Apicasso
|
|
100
100
|
authorize! opts[:action], opts[:resource] if opts[:resource].present?
|
101
101
|
authorize! opts[:action], opts[:object] if opts[:object].present?
|
102
102
|
end
|
103
|
+
|
104
|
+
def set_access_control_headers
|
105
|
+
response.headers['Access-Control-Allow-Origin'] = '*'
|
106
|
+
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, PATCH, DELETE, OPTIONS'
|
107
|
+
response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token, Auth-Token, Email, X-User-Token, X-User-Email'
|
108
|
+
response.headers['Access-Control-Max-Age'] = '1728000'
|
109
|
+
end
|
103
110
|
end
|
104
111
|
end
|
@@ -19,7 +19,7 @@ module Apicasso
|
|
19
19
|
# the page 42 of that collection. Usage:
|
20
20
|
# GET /sites?sort=+name,-updated_at&q[domain_eq]=domain.com&page=42&per_page=42
|
21
21
|
def index
|
22
|
-
render json:
|
22
|
+
render json: index_json
|
23
23
|
end
|
24
24
|
|
25
25
|
# GET /:resource/1
|
@@ -78,13 +78,6 @@ module Apicasso
|
|
78
78
|
|
79
79
|
private
|
80
80
|
|
81
|
-
def set_access_control_headers
|
82
|
-
response.headers['Access-Control-Allow-Origin'] = '*'
|
83
|
-
response.headers['Access-Control-Allow-Methods'] = 'POST, GET, PUT, PATCH, DELETE, OPTIONS'
|
84
|
-
response.headers['Access-Control-Allow-Headers'] = 'Origin, Content-Type, Accept, Authorization, Token, Auth-Token, Email, X-User-Token, X-User-Email'
|
85
|
-
response.headers['Access-Control-Max-Age'] = '1728000'
|
86
|
-
end
|
87
|
-
|
88
81
|
# Common setup to stablish which model is the resource of this request
|
89
82
|
def set_root_resource
|
90
83
|
@root_resource = params[:resource].classify.constantize
|
@@ -123,6 +116,13 @@ module Apicasso
|
|
123
116
|
authorize! :read, resource.name.underscore.to_sym
|
124
117
|
@records = resource.ransack(parsed_query).result
|
125
118
|
reorder_records if params[:sort].present?
|
119
|
+
select_fields if params[:select].present?
|
120
|
+
end
|
121
|
+
|
122
|
+
# Selects a fieldset that should be returned, instead of all fields
|
123
|
+
# from records.
|
124
|
+
def select_fields
|
125
|
+
@records = @records.select(*params[:select].split(','))
|
126
126
|
end
|
127
127
|
|
128
128
|
# Reordering of records which happens when receiving `params[:sort]`
|
@@ -132,20 +132,50 @@ module Apicasso
|
|
132
132
|
|
133
133
|
# Raw paginated records object
|
134
134
|
def paginated_records
|
135
|
-
|
136
|
-
|
135
|
+
accessible_records
|
136
|
+
.paginate(page: params[:page], per_page: params[:per_page])
|
137
|
+
end
|
138
|
+
|
139
|
+
# Records that can be accessed from current Apicasso::Key scope
|
140
|
+
# permissions
|
141
|
+
def accessible_records
|
142
|
+
@records.accessible_by(current_ability).unscope(:order)
|
143
|
+
end
|
144
|
+
|
145
|
+
# The response for index action, which can be a pagination of a record collection
|
146
|
+
# or a grouped count of attributes
|
147
|
+
def index_json
|
148
|
+
if params[:group].present?
|
149
|
+
accessible_records.group(params[:group].split(',')).count
|
150
|
+
else
|
151
|
+
collection_response
|
152
|
+
end
|
137
153
|
end
|
138
154
|
|
139
155
|
# Parsing of `paginated_records` with pagination variables metadata
|
140
|
-
def
|
156
|
+
def built_paginated
|
141
157
|
{ entries: entries_json }.merge(pagination_metadata_for(paginated_records))
|
142
158
|
end
|
143
159
|
|
160
|
+
# All records matching current query and it's total
|
161
|
+
def built_unpaginated
|
162
|
+
{ entries: accessible_records, total: accessible_records.size }
|
163
|
+
end
|
164
|
+
|
144
165
|
# Parsed JSON to be used as response payload
|
145
166
|
def entries_json
|
146
167
|
JSON.parse(paginated_records.to_json(include: parsed_include))
|
147
168
|
end
|
148
169
|
|
170
|
+
# Returns the collection checking if it needs pagination
|
171
|
+
def collection_response
|
172
|
+
if params[:per_page].to_i == -1
|
173
|
+
built_unpaginated
|
174
|
+
else
|
175
|
+
built_paginated
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
149
179
|
# Only allow a trusted parameter "white list" through,
|
150
180
|
# based on resource's schema.
|
151
181
|
def object_params
|
data/lib/apicasso/version.rb
CHANGED