yodatra 0.3.6 → 0.3.7

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: e6ce8fae3026a73118bcc2345e60dc4e1e93eabc
4
- data.tar.gz: d6f0c9ac4382981d4fd48d8baec3864ed3018f0d
3
+ metadata.gz: 5e006ebdfb069d9c91cd2c9309627500a53292eb
4
+ data.tar.gz: b1985c1d88db58d34ddcea1ab36dd34cc5c28f53
5
5
  SHA512:
6
- metadata.gz: 9ee95b6a0f7cf1b024c9a07a55e4babcc2381c1d408be1d4cfa2a51250ab9aab93dcafaf4241587428c99f231f2168617e9c50001b701e09061a42168a54bab2
7
- data.tar.gz: a593575a605d5d1f134ad7a4c72b219b73d46e946a2957132a82d4a245b133c7aa183feb1fb72b59c608fd2aa457deb55292a1478d2b5be346e15a89f6e6552e
6
+ metadata.gz: e49e4b3968858f06e20ba0593bc9231f4f0e114d86f1c4b8668ff44b6270a4bcf4daa09a427afe59da5a13229cedcb911d6f77e7a8084bf9a468d3790b4650b6
7
+ data.tar.gz: f5ed3b5319b89f81b2977a583e625378b6c6e0139e1dc89ab6e3054a8938f72581795a8d39e5ce16d65861c86855f45656cdff165b220b4e35919fa79094d31c
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  Yodatra
2
2
  ===
3
- [![Build Status](https://travis-ci.org/squareteam/yodatra.png?branch=master)](https://travis-ci.org/squareteam/yodatra) [![Coverage Status](https://coveralls.io/repos/squareteam/yodatra/badge.png)](https://coveralls.io/r/squareteam/yodatra) [![Gem Version](https://badge.fury.io/rb/yodatra.png)](http://badge.fury.io/rb/yodatra) [![Code Climate](https://codeclimate.com/github/squareteam/yodatra.png)](https://codeclimate.com/github/squareteam/yodatra) [![Dependency Status](https://gemnasium.com/squareteam/di.coffee.png)](https://gemnasium.com/squareteam/di.coffee)
3
+ [![Build Status](https://travis-ci.org/squareteam/yodatra.png?branch=master)](https://travis-ci.org/squareteam/yodatra) [![Coverage Status](https://coveralls.io/repos/squareteam/yodatra/badge.png)](https://coveralls.io/r/squareteam/yodatra) [![Gem Version](https://badge.fury.io/rb/yodatra.png)](http://badge.fury.io/rb/yodatra) [![Code Climate](https://codeclimate.com/github/squareteam/yodatra.png)](https://codeclimate.com/github/squareteam/yodatra) [![Dependency Status](https://gemnasium.com/squareteam/yodatra.png)](https://gemnasium.com/squareteam/yodatra)
4
4
 
5
5
  Backend development you shall do. And yodatra you shall use.
6
6
 
@@ -63,14 +63,14 @@ module Yodatra
63
63
  READ_ALL = :read_all
64
64
  get ALL_ROUTE do
65
65
  retrieve_resources READ_ALL do |resource|
66
- resource.all.as_json(read_scope).to_json
66
+ resource.all.as_json(read_scope)
67
67
  end
68
68
  end
69
69
 
70
70
  READ_ONE = :read
71
71
  get ONE_ROUTE do
72
72
  retrieve_resources READ_ONE do |resource|
73
- resource.as_json(read_scope).to_json
73
+ resource.as_json(read_scope)
74
74
  end
75
75
  end
76
76
 
@@ -82,9 +82,9 @@ module Yodatra
82
82
 
83
83
  if @one.id.nil?
84
84
  status 400
85
- @one.errors.full_messages.to_json
85
+ @one.errors.full_messages
86
86
  else
87
- @one.as_json(read_scope).to_json
87
+ @one.as_json(read_scope)
88
88
  end
89
89
  end
90
90
  end
@@ -94,10 +94,10 @@ module Yodatra
94
94
  retrieve_resources UPDATE_ONE do |resource|
95
95
  hash = self.send("#{model_name.underscore}_params".to_sym)
96
96
  if resource.update_attributes(hash)
97
- resource.as_json(read_scope).to_json
97
+ resource.as_json(read_scope)
98
98
  else
99
99
  status 400
100
- resource.errors.full_messages.to_json
100
+ resource.errors.full_messages
101
101
  end
102
102
  end
103
103
  end
@@ -106,10 +106,10 @@ module Yodatra
106
106
  delete ONE_ROUTE do
107
107
  retrieve_resources DELETE_ONE do |resource|
108
108
  if resource.destroy
109
- resource.as_json(read_scope).to_json
109
+ resource.as_json(read_scope)
110
110
  else
111
111
  status 400
112
- resource.errors.full_messages.to_json
112
+ resource.errors.full_messages
113
113
  end
114
114
  end
115
115
  end
@@ -157,7 +157,7 @@ module Yodatra
157
157
  end
158
158
 
159
159
  resource.where(search_terms.reduce(:or)).limit(100).
160
- flatten.as_json(read_scope).to_json
160
+ flatten.as_json(read_scope)
161
161
  end
162
162
  end
163
163
  end
@@ -169,9 +169,10 @@ module Yodatra
169
169
  # Defines a nested route or not and retrieves the correct resource (or resources)
170
170
  # @param disables is the name to check if it was disabled
171
171
  # @param &block to be yield with the retrieved resource
172
- def retrieve_resources(disables)
172
+ # @returns resource in json format
173
+ def retrieve_resources(action)
173
174
  pass unless involved?
174
- no_route if disabled? disables
175
+ no_route if disabled? action
175
176
 
176
177
  model = model_name.constantize
177
178
  nested = nested_resources if nested?
@@ -179,13 +180,19 @@ module Yodatra
179
180
  if model.nil? || nested.nil? && nested?
180
181
  raise ActiveRecord::RecordNotFound
181
182
  else
182
- model = nested if nested?
183
+ resource = nested? ? nested : model
184
+
185
+ # Check access to the resource
186
+ method = "prepare_#{action}"
187
+ resource = send(method, resource) if respond_to? method
188
+
189
+ # ONE resource else COLLECTION
183
190
  one_id = nested? ? params[:captures].fourth : params[:captures].second if params[:captures].length == 4
184
- model = model.find one_id unless one_id.nil?
185
- yield(model)
191
+ resource = resource.find one_id unless one_id.nil?
192
+ yield(resource).to_json
186
193
  end
187
194
  rescue ActiveRecord::RecordNotFound
188
- record_not_found
195
+ record_not_found.to_json
189
196
  end
190
197
 
191
198
  def nested?
@@ -259,7 +266,7 @@ module Yodatra
259
266
 
260
267
  def record_not_found
261
268
  status 404
262
- ['record not found'].to_json
269
+ ['record not found']
263
270
  end
264
271
 
265
272
  end
@@ -1,3 +1,3 @@
1
1
  module Yodatra
2
- VERSION = '0.3.6'
2
+ VERSION = '0.3.7'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yodatra
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.6
4
+ version: 0.3.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Paul Bonaud
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-09-06 00:00:00.000000000 Z
11
+ date: 2014-09-16 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack