standardapi 5.0.0.rc1 → 5.0.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: 32b4ca6314bc038faf8b73560d327096f4c4bf53
4
- data.tar.gz: 1162cb994c7921a7ffc4eea033a40b0e5f4f50b5
3
+ metadata.gz: b36f880979bf42f01bf760a716e446580c28e640
4
+ data.tar.gz: bf9b2403a472cc7e3aa27a7accb8123dbf8d81a8
5
5
  SHA512:
6
- metadata.gz: deaa898296ded369ca9c9b2289eaab9c803977d9f51f5283bf20d546da33b41e9d6fa2c7750195a9c438a87b59434f29fd0eca79421bce83db6774244276456c
7
- data.tar.gz: eaeb860e977eee866a9f98ce6ef4b10f697a95a2a6d3e33a286fc390e45102fc287b3139c14ab1a6868b04a115deacabe745c15286f11de76ab86922f6a440c8
6
+ metadata.gz: 1c407ac90c71b0d6c43806c064b8b74c0b19ecbe2b2d842b6d0bade57303061e4d56b633c4948d5ee9642407ebcc4cb67693e2224c9a51cb6181fea2855ca3be
7
+ data.tar.gz: 542ef97bdf8d1cd448bb245c334c04d246e86a9827af2fd9eed918df5201d12b36b273f156ade841bcd8cc31a5cda7181b336de86fb477ebb51581b530589d6e
data/README.md CHANGED
@@ -1,6 +1,7 @@
1
1
  # StandardAPI
2
2
 
3
- StandardAPI makes it easy to expose a query interface for your Rails models
3
+ StandardAPI makes it easy to expost a [REST](https://en.wikipedia.org/wiki/Representational_state_transfer)
4
+ interface to your Rails models.
4
5
 
5
6
  # Installation
6
7
 
@@ -10,15 +11,79 @@ In your Gemfile
10
11
 
11
12
  gem 'standardapi', require: 'standard_api'
12
13
 
13
- StandardAPI is a module you can include into any controllers you want to have
14
- API access to, or in the ApplicationController, giving all inherited controller
15
- access.
14
+ # Usage
15
+
16
+ StandardAPI is a module that can be included into any controller to expose a API
17
+ for. Alternatly, it can be included into `ApplicationController`, giving all
18
+ inherited controllers an exposed API.
16
19
 
17
20
  class ApplicationController < ActiveController::Base
18
21
  include StandardAPI::Controller
19
22
 
20
23
  end
21
24
 
25
+ By default any paramaters passed to update and create are whitelisted with by
26
+ the method named after the model the controller represents. For example, the
27
+ following will only allow the `caption` attribute of the `Photo` model to be
28
+ updated.
29
+
30
+ class PhotosController < ApplicationController
31
+ include StandardAPI
32
+
33
+ def photo_params
34
+ [:caption]
35
+ end
36
+ end
37
+
38
+ If greater control of the allowed paramaters is required, the `model_params`
39
+ method can be overridden. It simply returns a set of `StrongParameters`.
40
+
41
+ class PhotosController < ApplicationController
42
+ include StandardAPI
43
+
44
+ def model_params
45
+ if @photo.author == current_user
46
+ [:caption]
47
+ else
48
+ [:comment]
49
+ end
50
+ end
51
+ end
52
+
53
+ Similarly, the ordering and includes (including of relationships in the reponse)
54
+ is whitelisted as well.
55
+
56
+ Full Example:
57
+
58
+ class PhotosController < ApplicationController
59
+ including StandardAPI
60
+
61
+ # Allowed paramaters
62
+ def photo_params
63
+ [:caption]
64
+ end
65
+
66
+ # Allowed orderings
67
+ def photo_orders
68
+ [:id, :created_at, :updated_at]
69
+ end
70
+
71
+ # Allowed includes
72
+ def photo_includes
73
+ { author: [:photos] }
74
+ end
75
+
76
+ end
77
+
78
+ Note how includes can also support nested includes. So in this case when
79
+ including the author, the photos that the author took can also be included.
80
+
81
+ # Interface Specification
82
+
83
+ # Testing
84
+
85
+ ##
86
+
22
87
  And example contoller and it's tests.
23
88
 
24
89
  class PhotosController < ApplicationController
@@ -44,6 +44,7 @@ module StandardAPI
44
44
  end
45
45
 
46
46
  def create
47
+ puts model_params.inspect
47
48
  record = model.new(model_params)
48
49
  instance_variable_set("@#{model.model_name.singular}", record)
49
50
 
@@ -59,7 +59,7 @@ module StandardAPI
59
59
 
60
60
  def cached_at_columns_for_includes(includes)
61
61
  includes.select{|k,v| ![:where, :limit, :order].include?(k.to_sym) }.map { |k, v|
62
- ["#{k}_cached_at"] + cached_at_columns_for_includes(v).map{|v| "#{k}_#{v}"}
62
+ ["#{k}_cached_at"] + cached_at_columns_for_includes(v).map { |v2| "#{k}_#{v2}" }
63
63
  }.flatten
64
64
  end
65
65
 
@@ -11,14 +11,21 @@ require File.expand_path(File.join(__FILE__, '../test_case/update_tests'))
11
11
  module StandardAPI::TestCase
12
12
 
13
13
  def self.included(klass)
14
- model_class_name = klass.controller_class.name.gsub(/Controller$/, '').singularize
14
+ begin
15
+ controller_class_name = klass.name.gsub(/Test$/, '')
16
+ controller_class = controller_class_name.constantize
17
+ rescue NameError => e
18
+ raise e if e.message != "uninitialized constant #{controller_class_name}"
19
+ end
15
20
 
16
21
  [:filters, :orders, :includes].each do |attribute|
17
22
  klass.send(:class_attribute, attribute)
18
23
  end
19
24
 
20
25
  begin
26
+ model_class_name = klass.name.gsub(/ControllerTest$/, '').singularize
21
27
  model_class = model_class_name.constantize
28
+
22
29
  klass.send(:filters=, model_class.attribute_names)
23
30
  klass.send(:orders=, model_class.attribute_names)
24
31
  klass.send(:includes=, model_class.reflect_on_all_associations.map(&:name))
@@ -34,6 +41,7 @@ module StandardAPI::TestCase
34
41
  acc
35
42
  end
36
43
 
44
+
37
45
  klass.controller_class.action_methods.each do |action|
38
46
  if const_defined?("StandardAPI::TestCase::#{action.capitalize}Tests") && routes[klass.controller_class.controller_path][action]
39
47
  klass.include("StandardAPI::TestCase::#{action.capitalize}Tests".constantize)
@@ -43,7 +43,7 @@ module StandardAPI
43
43
  attrs = attributes_for(singular_name, :nested).select{|k,v| !model.readonly_attributes.include?(k.to_s) }
44
44
  create_webmocks(attrs)
45
45
  put :update, params: {:id => m.id, singular_name => attrs}, format: :json
46
- assert_response :ok
46
+ assert_response :ok, "Updating #{m.class.name} with #{attrs.inspect}"
47
47
 
48
48
  # (m.attribute_names & attrs.keys.map(&:to_s)).each do |test_key|
49
49
  view_attributes(m.reload).select { |x| attrs.keys.map(&:to_s).include?(x) }.each do |key, value|
@@ -78,7 +78,8 @@ module StandardAPI
78
78
  create_webmocks(attrs)
79
79
 
80
80
  put :update, params: {:id => m.id, :include => includes, singular_name => attrs}, format: :json
81
-
81
+ assert_response :ok, "Updating #{m.class.name} with #{attrs.inspect}"
82
+
82
83
  json = JSON.parse(response.body)
83
84
  includes.each do |included|
84
85
  assert json.key?(included.to_s), "#{included.inspect} not included in response"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standardapi
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.0.0.rc1
4
+ version: 5.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - James Bracy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-18 00:00:00.000000000 Z
11
+ date: 2016-08-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -16,84 +16,120 @@ dependencies:
16
16
  requirements:
17
17
  - - "~>"
18
18
  - !ruby/object:Gem::Version
19
- version: 5.0.0.rc1
19
+ version: '5.0'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 5.0.0
20
23
  type: :runtime
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
24
27
  - - "~>"
25
28
  - !ruby/object:Gem::Version
26
- version: 5.0.0.rc1
29
+ version: '5.0'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 5.0.0
27
33
  - !ruby/object:Gem::Dependency
28
34
  name: activesupport
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
37
  - - "~>"
32
38
  - !ruby/object:Gem::Version
33
- version: 5.0.0.rc1
39
+ version: '5.0'
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: 5.0.0
34
43
  type: :runtime
35
44
  prerelease: false
36
45
  version_requirements: !ruby/object:Gem::Requirement
37
46
  requirements:
38
47
  - - "~>"
39
48
  - !ruby/object:Gem::Version
40
- version: 5.0.0.rc1
49
+ version: '5.0'
50
+ - - ">="
51
+ - !ruby/object:Gem::Version
52
+ version: 5.0.0
41
53
  - !ruby/object:Gem::Dependency
42
54
  name: actionpack
43
55
  requirement: !ruby/object:Gem::Requirement
44
56
  requirements:
45
57
  - - "~>"
46
58
  - !ruby/object:Gem::Version
47
- version: 5.0.0.rc1
59
+ version: '5.0'
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: 5.0.0
48
63
  type: :runtime
49
64
  prerelease: false
50
65
  version_requirements: !ruby/object:Gem::Requirement
51
66
  requirements:
52
67
  - - "~>"
53
68
  - !ruby/object:Gem::Version
54
- version: 5.0.0.rc1
69
+ version: '5.0'
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: 5.0.0
55
73
  - !ruby/object:Gem::Dependency
56
74
  name: activerecord-sort
57
75
  requirement: !ruby/object:Gem::Requirement
58
76
  requirements:
59
77
  - - "~>"
60
78
  - !ruby/object:Gem::Version
61
- version: 5.0.0.rc1
79
+ version: '5.0'
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: 5.0.0
62
83
  type: :runtime
63
84
  prerelease: false
64
85
  version_requirements: !ruby/object:Gem::Requirement
65
86
  requirements:
66
87
  - - "~>"
67
88
  - !ruby/object:Gem::Version
68
- version: 5.0.0.rc1
89
+ version: '5.0'
90
+ - - ">="
91
+ - !ruby/object:Gem::Version
92
+ version: 5.0.0
69
93
  - !ruby/object:Gem::Dependency
70
94
  name: activerecord-filter
71
95
  requirement: !ruby/object:Gem::Requirement
72
96
  requirements:
73
97
  - - "~>"
74
98
  - !ruby/object:Gem::Version
75
- version: 5.0.0.rc1
99
+ version: '5.0'
100
+ - - ">="
101
+ - !ruby/object:Gem::Version
102
+ version: 5.0.0
76
103
  type: :runtime
77
104
  prerelease: false
78
105
  version_requirements: !ruby/object:Gem::Requirement
79
106
  requirements:
80
107
  - - "~>"
81
108
  - !ruby/object:Gem::Version
82
- version: 5.0.0.rc1
109
+ version: '5.0'
110
+ - - ">="
111
+ - !ruby/object:Gem::Version
112
+ version: 5.0.0
83
113
  - !ruby/object:Gem::Dependency
84
114
  name: jbuilder
85
115
  requirement: !ruby/object:Gem::Requirement
86
116
  requirements:
87
117
  - - "~>"
88
118
  - !ruby/object:Gem::Version
89
- version: '2.3'
119
+ version: '2.6'
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: 2.6.0
90
123
  type: :runtime
91
124
  prerelease: false
92
125
  version_requirements: !ruby/object:Gem::Requirement
93
126
  requirements:
94
127
  - - "~>"
95
128
  - !ruby/object:Gem::Version
96
- version: '2.3'
129
+ version: '2.6'
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: 2.6.0
97
133
  - !ruby/object:Gem::Dependency
98
134
  name: bundler
99
135
  requirement: !ruby/object:Gem::Requirement
@@ -220,6 +256,20 @@ dependencies:
220
256
  - - ">="
221
257
  - !ruby/object:Gem::Version
222
258
  version: '0'
259
+ - !ruby/object:Gem::Dependency
260
+ name: rails-controller-testing
261
+ requirement: !ruby/object:Gem::Requirement
262
+ requirements:
263
+ - - ">="
264
+ - !ruby/object:Gem::Version
265
+ version: '0'
266
+ type: :development
267
+ prerelease: false
268
+ version_requirements: !ruby/object:Gem::Requirement
269
+ requirements:
270
+ - - ">="
271
+ - !ruby/object:Gem::Version
272
+ version: '0'
223
273
  description: StandardAPI makes it easy to expose a query interface for your Rails
224
274
  models
225
275
  email:
@@ -269,9 +319,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
269
319
  version: '0'
270
320
  required_rubygems_version: !ruby/object:Gem::Requirement
271
321
  requirements:
272
- - - ">"
322
+ - - ">="
273
323
  - !ruby/object:Gem::Version
274
- version: 1.3.1
324
+ version: '0'
275
325
  requirements: []
276
326
  rubyforge_project:
277
327
  rubygems_version: 2.5.1