standardapi 5.0.0.3 → 5.0.0.4

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: 95b8156eb29862ad327ce11d49a4991e3f6b1c4d
4
- data.tar.gz: cb2829d52583507520c82f5dce5ebc3578b84239
3
+ metadata.gz: 1ffafde0be5ac4af61ae465486cc2b425ee97274
4
+ data.tar.gz: 90e2e0e35898cee9bb550c2378f8708166512f06
5
5
  SHA512:
6
- metadata.gz: 8f84451e96a06a5950ee99bd5fb93f3a90c1ddb9eafac3dd2319619a21d30590c37d22f2a3a355ba9c032b436244267837ed4dc292852c3e24d683f95a8f6e28
7
- data.tar.gz: a090714604a263d66564644b4ecf9ef5cf0fbacff189ce58c6f58f72675778b94a4e00ab3af22f0d67e9bfdbcdbbae7c687929d49916efe6f70810fcc66d3f81
6
+ metadata.gz: a080f851a213f0759fe8d9554e65588135169a5f99ca72ee52239917c8082f9420490b17d5cb2d3f5df41e0c50babb33c7ac56ac23a50fffe0cc99ce5b67ba7d
7
+ data.tar.gz: 3bef590610082ecf5de5042b16e16dc4e5b558de7cfd65f0426e8a12b54119793497695acaa9819cde7bdf77303c625540a2c3f87da8c2521a3a5cfb4bcce410
@@ -146,9 +146,39 @@ module StandardAPI
146
146
  def includes
147
147
  @includes ||= StandardAPI::Includes.normalize(params[:include])
148
148
  end
149
+
150
+ def required_orders
151
+ if model.column_names.include?('id')
152
+ [:id]
153
+ else
154
+ []
155
+ end
156
+ end
149
157
 
150
158
  def orders
151
- @orders ||= StandardAPI::Orders.sanitize(params[:order], model_orders)
159
+ exluded_required_orders = required_orders.map(&:to_s)
160
+
161
+ case params[:order]
162
+ when Hash, ActionController::Parameters
163
+ exluded_required_orders -= params[:order].keys.map(&:to_s)
164
+ when Array
165
+ params[:order].flatten.each do |v|
166
+ case v
167
+ when Hash, ActionController::Parameters
168
+ exluded_required_orders -= v.keys.map(&:to_s)
169
+ when String
170
+ exluded_required_orders.delete(v)
171
+ end
172
+ end
173
+ when String
174
+ exluded_required_orders.delete(params[:order])
175
+ end
176
+
177
+ if !exluded_required_orders.empty?
178
+ params[:order] = exluded_required_orders.unshift(params[:order])
179
+ end
180
+
181
+ @orders ||= StandardAPI::Orders.sanitize(params[:order], model_orders | required_orders)
152
182
  end
153
183
 
154
184
  def excludes
@@ -1,13 +1,18 @@
1
1
  module StandardAPI
2
2
  module RouteHelpers
3
3
 
4
- # Shorthand for adding resources.
4
+ # StandardAPI wrapper for ActionDispatch::Routing::Mapper::Resources#resources
5
+ #
6
+ # Includes the following routes
7
+ #
8
+ # GET /schema
9
+ # GET /calculate
5
10
  #
6
11
  # For example
7
12
  #
8
13
  # standard_resources :views
9
14
  #
10
- # Is equivilent to:
15
+ # is equivilent to:
11
16
  #
12
17
  # resources :api_keys do
13
18
  # get :schema, on: :collection
@@ -19,8 +24,36 @@ module StandardAPI
19
24
  resources(*resources, options) do
20
25
  get :schema, on: :collection
21
26
  get :calculate, on: :collection
27
+ block.call if block
22
28
  end
23
29
  end
24
-
30
+
31
+ # StandardAPI wrapper for ActionDispatch::Routing::Mapper::Resources#resource
32
+ #
33
+ # Includes the following routes
34
+ #
35
+ # GET /schema
36
+ # GET /calculate
37
+ #
38
+ # For example:
39
+ #
40
+ # standard_resource :account
41
+ #
42
+ # is equivilent to:
43
+ #
44
+ # resource :account do
45
+ # get :schema, on: :collection
46
+ # get :calculate, on: :collection
47
+ # end
48
+ def standard_resource(*resource, &block)
49
+ options = resource.extract_options!.dup
50
+
51
+ resource(*resource, options) do
52
+ get :schema, on: :collection
53
+ get :calculate, on: :collection
54
+ block.call if block
55
+ end
56
+ end
57
+
25
58
  end
26
- end
59
+ end
@@ -13,7 +13,7 @@ module StandardAPI::TestCase
13
13
  def self.included(klass)
14
14
  begin
15
15
  controller_class_name = klass.name.gsub(/Test$/, '')
16
- controller_class = controller_class_name.constantize
16
+ controller_class_name.constantize
17
17
  rescue NameError => e
18
18
  raise e if e.message != "uninitialized constant #{controller_class_name}"
19
19
  end
@@ -48,6 +48,25 @@ module StandardAPI::TestCase
48
48
  end
49
49
  end
50
50
  end
51
+
52
+ def supports_format(format)
53
+ count = controller_class.view_paths.count do |path|
54
+ !Dir.glob("#{path.instance_variable_get(:@path)}/{#{model.name.underscore},application}/**/*.#{format}*").empty?
55
+ end
56
+
57
+ count > 0
58
+ end
59
+
60
+ def required_orders
61
+ controller_class.new.send(:required_orders)
62
+ end
63
+
64
+ def controller_class
65
+ controller_class_name = self.class.name.gsub(/Test$/, '')
66
+ controller_class_name.constantize
67
+ rescue NameError => e
68
+ raise e if e.message != "uninitialized constant #{controller_class_name}"
69
+ end
51
70
 
52
71
  def model
53
72
  self.class.model
@@ -67,7 +86,7 @@ module StandardAPI::TestCase
67
86
 
68
87
  def create_webmocks(attributes)
69
88
  attributes.each do |attribute, value|
70
- validators = self.class.model.validators_on(attribute)
89
+ self.class.model.validators_on(attribute)
71
90
  end
72
91
  end
73
92
 
@@ -120,7 +139,7 @@ module StandardAPI::TestCase
120
139
 
121
140
  assert_predicate = -> (predicate) {
122
141
  get :index, params: {where: predicate}, format: 'json'
123
- assert_equal model.filter(predicate).to_sql, assigns(plural_name).to_sql
142
+ assert_equal model.filter(predicate).order('id ASC').to_sql, assigns(plural_name).to_sql
124
143
  }
125
144
 
126
145
  # TODO: Test array
@@ -152,9 +171,9 @@ module StandardAPI::TestCase
152
171
 
153
172
  def model=(val)
154
173
  @model = val
155
- filters = val.attribute_names
156
- orders = val.attribute_names
157
- includes = val.reflect_on_all_associations.map(&:name)
174
+ self.filters = val.attribute_names
175
+ self.orders = val.attribute_names
176
+ self.includes = val.reflect_on_all_associations.map(&:name)
158
177
  @model
159
178
  end
160
179
 
@@ -4,7 +4,7 @@ module StandardAPI
4
4
  extend ActiveSupport::Testing::Declarative
5
5
 
6
6
  test '#calculate.json' do
7
- m = create_model
7
+ create_model
8
8
  selects = [{ count: :id}, { maximum: :id }, { minimum: :id }, { average: :id }]
9
9
 
10
10
  get :calculate, params: {select: selects}, format: :json
@@ -14,7 +14,7 @@ module StandardAPI
14
14
 
15
15
  test '#calculate.json params[:where]' do
16
16
  m1 = create_model
17
- m2 = create_model
17
+ create_model
18
18
 
19
19
  selects = [{ count: :id}, { maximum: :id }, { minimum: :id }, { average: :id }]
20
20
  predicate = { id: { gt: m1.id } }
@@ -73,6 +73,8 @@ module StandardAPI
73
73
  end
74
74
 
75
75
  test '#create.html with invalid attributes renders edit action' do
76
+ return unless supports_format(:html)
77
+
76
78
  trait = FactoryGirl.factories[singular_name].definition.defined_traits.any? { |x| x.name.to_s == 'invalid' }
77
79
 
78
80
  if !trait
@@ -126,7 +128,7 @@ module StandardAPI
126
128
 
127
129
  view_attributes(m).each do |key, value|
128
130
  message = "Model / Attribute: #{m.class.name}##{key}"
129
- assert_equal m_json[key.to_s], normalize_to_json(m, key, value)
131
+ assert_equal m_json[key.to_s], normalize_to_json(m, key, value), message
130
132
  end
131
133
 
132
134
  end
@@ -12,7 +12,7 @@ module StandardAPI
12
12
 
13
13
  test '#index.json params[:limit]' do
14
14
  get :index, params: { limit: 1 }, format: :json
15
- assert_equal model.limit(1).to_sql, assigns(plural_name).to_sql
15
+ assert_equal model.limit(1).sort(required_orders).to_sql, assigns(plural_name).to_sql
16
16
  end
17
17
 
18
18
  test '#index.json params[:where]' do
@@ -26,13 +26,13 @@ module StandardAPI
26
26
  orders.each do |order|
27
27
  @controller.instance_variable_set('@orders', nil) # Hack for dealing with caching / multiple request per controller life
28
28
  get :index, params: { order: order }, format: :json
29
- assert_equal model.sort(order).to_sql, assigns(plural_name).to_sql
29
+ assert_equal model.sort(order).sort(required_orders).to_sql, assigns(plural_name).to_sql
30
30
  end
31
31
  end
32
32
 
33
33
  test '#index.json params[:offset]' do
34
34
  get :index, params: { offset: 13 }, format: :json
35
- assert_equal model.offset(13).to_sql, assigns(plural_name).to_sql
35
+ assert_equal model.offset(13).sort(required_orders).to_sql, assigns(plural_name).to_sql
36
36
  end
37
37
 
38
38
  test '#index.json params[:include]' do
@@ -83,7 +83,7 @@ module StandardAPI
83
83
  m = create_model
84
84
  @controller.current_mask[plural_name] = { id: m.id }
85
85
  get :index, format: :json
86
- assert_equal model.where(id: m.id).to_sql, assigns(plural_name).to_sql
86
+ assert_equal model.where(id: m.id).sort(required_orders).to_sql, assigns(plural_name).to_sql
87
87
  @controller.current_mask.delete(plural_name)
88
88
  end
89
89
  end
@@ -40,7 +40,7 @@ module StandardAPI
40
40
 
41
41
  view_attributes(m).each do |key, value|
42
42
  message = "Model / Attribute: #{m.class.name}##{key}"
43
- assert_equal m_json[key.to_s], normalize_to_json(m, key, value)
43
+ assert_equal m_json[key.to_s], normalize_to_json(m, key, value), message
44
44
  end
45
45
 
46
46
  end
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.3
4
+ version: 5.0.0.4
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-09-28 00:00:00.000000000 Z
11
+ date: 2016-10-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails