standardapi 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/standard_api/test_case/create_tests.rb +34 -23
- data/lib/standard_api/test_case/index_tests.rb +29 -14
- data/lib/standard_api/test_case/show_tests.rb +8 -4
- data/lib/standard_api/test_case/update_tests.rb +29 -20
- data/lib/standard_api/test_case.rb +53 -17
- data/lib/standard_api/views/application/_record.json.jbuilder +36 -0
- data/lib/standard_api/views/application/index.json.jbuilder +2 -2
- data/lib/standard_api/views/application/schema.json.jbuilder +30 -0
- data/lib/standard_api/views/application/show.json.jbuilder +2 -2
- data/lib/standard_api.rb +42 -4
- metadata +4 -3
- data/lib/standard_api/views/application/_model.json.jbuilder +0 -32
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 67dc838f6d2096fd1f791c9781490db311b9b4b2
|
4
|
+
data.tar.gz: 96d425ce24603f45e9ea714e55b347d5fa9838ae
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9ee824965d303ebe14f3bd0122df8332e4ed8f1c6ee28bd35ac3b3df1f817229cb23b2b668d4dcf8fcf5a81071ed52b22cf526e89cc29ade3cae51c66ca4d31b
|
7
|
+
data.tar.gz: e146f7d6db4c276b92fd4f899c83e41a6afb6384c7e10c6bd49b777ee062fa49e3979d0157b8de3a1889b505d072d10c62abd7f05bc0c098c3ed9d399d74c90c
|
@@ -4,7 +4,7 @@ module StandardAPI
|
|
4
4
|
extend ActiveSupport::Testing::Declarative
|
5
5
|
|
6
6
|
test '#create.json' do
|
7
|
-
attrs = attributes_for(singular_name).select{|k,v| !model.readonly_attributes.include?(k.to_s) }
|
7
|
+
attrs = attributes_for(singular_name, :nested).select{|k,v| !model.readonly_attributes.include?(k.to_s) }
|
8
8
|
create_webmocks(attrs)
|
9
9
|
|
10
10
|
assert_difference("#{model.name}.count") do
|
@@ -14,8 +14,11 @@ module StandardAPI
|
|
14
14
|
|
15
15
|
json = JSON.parse(response.body)
|
16
16
|
assert json.is_a?(Hash)
|
17
|
-
|
18
|
-
|
17
|
+
|
18
|
+
m = assigns(singular_name)
|
19
|
+
view_attributes(m.reload).select { |x| attrs.keys.map(&:to_s).include?(x) }.each do |key, value|
|
20
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
21
|
+
assert_equal normalize_to_json(m, key, attrs[key.to_sym]), json[key.to_s], message
|
19
22
|
end
|
20
23
|
end
|
21
24
|
end
|
@@ -31,8 +34,10 @@ module StandardAPI
|
|
31
34
|
|
32
35
|
json = JSON.parse(response.body)
|
33
36
|
assert json.is_a?(Hash)
|
34
|
-
|
35
|
-
|
37
|
+
m = assigns(singular_name).reload
|
38
|
+
view_attributes(m).select { |x| attrs.keys.map(&:to_s).include?(x) }.each do |key, value|
|
39
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
40
|
+
assert_equal normalize_attribute(m, key, attrs[key.to_sym]), value, message
|
36
41
|
end
|
37
42
|
end
|
38
43
|
end
|
@@ -51,27 +56,33 @@ module StandardAPI
|
|
51
56
|
end
|
52
57
|
|
53
58
|
test '#create.json params[:include]' do
|
54
|
-
|
55
|
-
|
59
|
+
travel_to Time.now do
|
60
|
+
attrs = attributes_for(singular_name, :nested).select{ |k,v| !model.readonly_attributes.include?(k.to_s) }
|
61
|
+
create_webmocks(attrs)
|
56
62
|
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
63
|
+
assert_difference("#{model.name}.count") do
|
64
|
+
post :create, singular_name => attrs, include: includes, :format => 'json'
|
65
|
+
assert_response :created
|
66
|
+
assert assigns(singular_name)
|
61
67
|
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
68
|
+
json = JSON.parse(response.body)
|
69
|
+
assert json.is_a?(Hash)
|
70
|
+
includes.each do |included|
|
71
|
+
assert json.key?(included.to_s), "#{included.inspect} not included in response"
|
66
72
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
73
|
+
association = assigns(:record).class.reflect_on_association(included)
|
74
|
+
next if !association
|
75
|
+
|
76
|
+
if ['belongs_to', 'has_one'].include?(association.macro.to_s)
|
77
|
+
view_attributes(assigns(:record).send(included)) do |key, value|
|
78
|
+
assert_equal json[included.to_s][key.to_s], normalize_to_json(assigns(:record), key, value)
|
79
|
+
end
|
80
|
+
else
|
81
|
+
m = assigns(:record).send(included).first.try(:reload)
|
82
|
+
view_attributes(m).each do |key, value|
|
83
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
84
|
+
assert_equal normalize_to_json(m, key, value), json[included.to_s][0][key.to_s], message
|
85
|
+
end
|
75
86
|
end
|
76
87
|
end
|
77
88
|
end
|
@@ -25,16 +25,9 @@ module StandardAPI
|
|
25
25
|
test '#index.json params[:order]' do
|
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
|
-
|
29
|
-
|
30
|
-
|
31
|
-
assert_equal model.sort(order.keys.first => o).to_sql, assigns(plural_name).to_sql
|
32
|
-
end
|
33
|
-
else
|
34
|
-
get :index, order: order, format: 'json'
|
35
|
-
assert_equal model.sort(order).to_sql, assigns(:records).to_sql
|
36
|
-
end
|
37
|
-
end
|
28
|
+
get :index, order: order, format: 'json'
|
29
|
+
assert_equal model.sort(order).to_sql, assigns(:records).to_sql
|
30
|
+
end
|
38
31
|
end
|
39
32
|
|
40
33
|
test '#index.json params[:offset]' do
|
@@ -43,10 +36,32 @@ module StandardAPI
|
|
43
36
|
end
|
44
37
|
|
45
38
|
test '#index.json params[:include]' do
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
39
|
+
travel_to Time.now do
|
40
|
+
create_model
|
41
|
+
get :index, include: includes, format: 'json'
|
42
|
+
|
43
|
+
json = JSON.parse(response.body)[0]
|
44
|
+
assert json.is_a?(Hash)
|
45
|
+
includes.each do |included|
|
46
|
+
assert json.key?(included.to_s), "#{included.inspect} not included in response"
|
47
|
+
|
48
|
+
association = assigns(:records).first.class.reflect_on_association(included)
|
49
|
+
next if !association
|
50
|
+
|
51
|
+
if ['belongs_to', 'has_one'].include?(association.macro.to_s)
|
52
|
+
m = assigns(:records).first.send(included)
|
53
|
+
view_attributes(m) do |key, value|
|
54
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
55
|
+
assert_equal json[included.to_s][key.to_s], normalize_to_json(m, key, value), message
|
56
|
+
end
|
57
|
+
else
|
58
|
+
m = assigns(:records).first.send(included).first.try(:reload)
|
59
|
+
view_attributes(m).each do |key, value|
|
60
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
61
|
+
assert_equal json[included.to_s][0][key.to_s], normalize_to_json(m, key, value), message
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
50
65
|
end
|
51
66
|
end
|
52
67
|
|
@@ -22,13 +22,17 @@ module StandardAPI
|
|
22
22
|
assert json.key?(included.to_s), "#{included.inspect} not included in response"
|
23
23
|
|
24
24
|
association = assigns(:record).class.reflect_on_association(included)
|
25
|
-
if
|
26
|
-
|
25
|
+
next if !association
|
26
|
+
|
27
|
+
if ['belongs_to', 'has_one'].include?(association.macro.to_s)
|
28
|
+
view_attributes(assigns(:record).send(included)) do |key, value|
|
27
29
|
assert_equal json[included.to_s][key.to_s], value
|
28
30
|
end
|
29
31
|
else
|
30
|
-
assigns(:record).send(included).first.
|
31
|
-
|
32
|
+
m = assigns(:record).send(included).first.try(:reload)
|
33
|
+
view_attributes(m).each do |key, value|
|
34
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
35
|
+
assert_equal normalize_to_json(m, key, value), json[included.to_s][0][key.to_s], message
|
32
36
|
end
|
33
37
|
end
|
34
38
|
end
|
@@ -11,8 +11,9 @@ module StandardAPI
|
|
11
11
|
put :update, id: m.id, singular_name => attrs, format: 'json'
|
12
12
|
assert_response :ok
|
13
13
|
|
14
|
-
(m.
|
15
|
-
|
14
|
+
view_attributes(m.reload).select { |x| attrs.keys.map(&:to_s).include?(x) }.each do |key, value|
|
15
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
16
|
+
assert_equal normalize_attribute(m, key, attrs[key.to_sym]), value, message
|
16
17
|
end
|
17
18
|
assert JSON.parse(@response.body).is_a?(Hash)
|
18
19
|
end
|
@@ -21,12 +22,13 @@ module StandardAPI
|
|
21
22
|
m = create_model
|
22
23
|
attrs = attributes_for(singular_name, :nested).select{|k,v| !model.readonly_attributes.include?(k.to_s) }
|
23
24
|
create_webmocks(attrs)
|
24
|
-
|
25
25
|
put :update, id: m.id, singular_name => attrs, format: 'json'
|
26
26
|
assert_response :ok
|
27
27
|
|
28
|
-
(m.attribute_names & attrs.keys.map(&:to_s)).each do |test_key|
|
29
|
-
|
28
|
+
# (m.attribute_names & attrs.keys.map(&:to_s)).each do |test_key|
|
29
|
+
view_attributes(m.reload).select { |x| attrs.keys.map(&:to_s).include?(x) }.each do |key, value|
|
30
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
31
|
+
assert_equal normalize_attribute(m, key, attrs[key.to_sym]), value, message
|
30
32
|
end
|
31
33
|
assert JSON.parse(@response.body).is_a?(Hash)
|
32
34
|
end
|
@@ -42,24 +44,31 @@ module StandardAPI
|
|
42
44
|
end
|
43
45
|
|
44
46
|
test '#update.json params[:include]' do
|
45
|
-
|
46
|
-
|
47
|
-
|
47
|
+
travel_to Time.now do
|
48
|
+
m = create_model
|
49
|
+
attrs = attributes_for(singular_name, :nested).select{|k,v| !model.readonly_attributes.include?(k) }
|
50
|
+
create_webmocks(attrs)
|
48
51
|
|
49
|
-
|
52
|
+
put :update, id: m.id, include: includes, singular_name => attrs, format: 'json'
|
50
53
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
+
json = JSON.parse(response.body)
|
55
|
+
includes.each do |included|
|
56
|
+
assert json.key?(included.to_s), "#{included.inspect} not included in response"
|
54
57
|
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
58
|
+
association = assigns(:record).class.reflect_on_association(included)
|
59
|
+
next if !association
|
60
|
+
|
61
|
+
if ['belongs_to', 'has_one'].include?(association.macro.to_s)
|
62
|
+
view_attributes(assigns(:record).send(included)) do |key, value|
|
63
|
+
message = "Model / Attribute: #{assigns(:record).send(included).class.name}##{key}"
|
64
|
+
assert_equal json[included.to_s][key.to_s], value, message
|
65
|
+
end
|
66
|
+
else
|
67
|
+
m = assigns(:record).send(included).first.try(:reload)
|
68
|
+
view_attributes(m).each do |key, value|
|
69
|
+
message = "Model / Attribute: #{m.class.name}##{key}"
|
70
|
+
assert_equal normalize_to_json(assigns(:record), key, value), json[included.to_s][0][key.to_s], message
|
71
|
+
end
|
63
72
|
end
|
64
73
|
end
|
65
74
|
end
|
@@ -15,20 +15,26 @@ module StandardAPI::TestCase
|
|
15
15
|
[:filters, :orders, :includes].each do |attribute|
|
16
16
|
klass.send(:class_attribute, attribute)
|
17
17
|
end
|
18
|
-
|
18
|
+
|
19
|
+
begin
|
19
20
|
model_class = model_class_name.constantize
|
20
21
|
klass.send(:filters=, model_class.attribute_names)
|
21
22
|
klass.send(:orders=, model_class.attribute_names)
|
22
23
|
klass.send(:includes=, model_class.reflect_on_all_associations.map(&:name))
|
24
|
+
rescue NameError => e
|
25
|
+
raise e if e.message != "uninitialized constant #{model_class_name}"
|
23
26
|
end
|
24
|
-
|
27
|
+
|
25
28
|
klass.extend(ClassMethods)
|
26
29
|
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
30
|
+
routes = Rails.application.routes.set.routes.inject({}) do |acc, r|
|
31
|
+
acc[r.defaults[:controller]] ||= {}
|
32
|
+
acc[r.defaults[:controller]][r.defaults[:action]] = true
|
33
|
+
acc
|
34
|
+
end
|
31
35
|
|
36
|
+
klass.controller_class.action_methods.each do |action|
|
37
|
+
if const_defined?("StandardAPI::TestCase::#{action.capitalize}Tests") && routes[klass.controller_class.controller_path][action]
|
32
38
|
klass.include("StandardAPI::TestCase::#{action.capitalize}Tests".constantize)
|
33
39
|
end
|
34
40
|
end
|
@@ -55,28 +61,47 @@ module StandardAPI::TestCase
|
|
55
61
|
validators = self.class.model.validators_on(attribute)
|
56
62
|
end
|
57
63
|
end
|
58
|
-
|
59
|
-
def
|
60
|
-
|
61
|
-
|
64
|
+
|
65
|
+
def normalizers
|
66
|
+
self.class.instance_variable_get('@normalizers')
|
67
|
+
end
|
68
|
+
|
69
|
+
def normalize_attribute(record, attribute, value)
|
70
|
+
if normalizers[self.class.model] && normalizers[self.class.model][attribute]
|
71
|
+
b = normalizers[self.class.model][attribute]
|
72
|
+
b.arity == 2 ? b.call(record, value) : b.call(value)
|
73
|
+
else
|
74
|
+
# validators = self.class.model.validators_on(attribute)
|
75
|
+
value
|
76
|
+
end
|
62
77
|
end
|
63
78
|
|
64
|
-
def normalize_to_json(attribute, value)
|
65
|
-
value = normalize_attribute(attribute, value)
|
79
|
+
def normalize_to_json(record, attribute, value)
|
80
|
+
value = normalize_attribute(record, attribute, value)
|
66
81
|
|
67
82
|
return nil if value.nil?
|
68
83
|
|
69
84
|
if model.column_types[attribute].is_a?(ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Decimal)
|
70
|
-
"#{value}"
|
71
|
-
elsif model.column_types[attribute].is_a?(ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter)
|
72
|
-
value.to_datetime.utc.iso8601.gsub(/\+00:00$/, 'Z')
|
85
|
+
"#{value.to_f}"
|
86
|
+
# elsif model.column_types[attribute].is_a?(ActiveRecord::AttributeMethods::TimeZoneConversion::TimeZoneConverter)
|
87
|
+
# value.to_datetime.utc.iso8601.gsub(/\+(00:00|UTC)$/, 'Z')
|
88
|
+
# value.as_json
|
73
89
|
else
|
74
|
-
value
|
90
|
+
value.as_json
|
75
91
|
end
|
76
92
|
end
|
77
93
|
|
94
|
+
def view_attributes(record)
|
95
|
+
return [] if record.nil?
|
96
|
+
record.attributes.select { |x| !@controller.send(:excludes_for, record.class).include?(x.to_sym) }
|
97
|
+
end
|
98
|
+
|
78
99
|
module ClassMethods
|
79
|
-
|
100
|
+
|
101
|
+
def self.extended(klass)
|
102
|
+
klass.instance_variable_set('@normalizers', {})
|
103
|
+
end
|
104
|
+
|
80
105
|
def include_filter_tests
|
81
106
|
model.instance_variable_get('@filters').each do |filter|
|
82
107
|
next if filter[1].is_a?(Proc) # Custom filter
|
@@ -120,6 +145,10 @@ module StandardAPI::TestCase
|
|
120
145
|
|
121
146
|
def model=(val)
|
122
147
|
@model = val
|
148
|
+
filters = val.attribute_names
|
149
|
+
orders = val.attribute_names
|
150
|
+
includes = val.reflect_on_all_associations.map(&:name)
|
151
|
+
@model
|
123
152
|
end
|
124
153
|
|
125
154
|
def model
|
@@ -140,6 +169,13 @@ module StandardAPI::TestCase
|
|
140
169
|
end
|
141
170
|
end
|
142
171
|
|
172
|
+
def normalize(*attributes, &block)
|
173
|
+
attributes.each do |attribute|
|
174
|
+
@normalizers[model] ||= {}
|
175
|
+
@normalizers[model][attribute] = block
|
176
|
+
end
|
177
|
+
end
|
178
|
+
|
143
179
|
end
|
144
180
|
|
145
181
|
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
record.attributes.each do |name, value|
|
2
|
+
# Skip if attribute is included in excludes
|
3
|
+
next if defined?(excludes) && excludes[record.model_name.singular].try(:find) { |x| x.to_s = name.to_s }
|
4
|
+
json.set! name, value
|
5
|
+
end
|
6
|
+
|
7
|
+
includes.each do |inc, subinc|
|
8
|
+
association = record.class.reflect_on_association(inc)
|
9
|
+
if association && association.klass < ActiveRecord::Base
|
10
|
+
json.set! inc do
|
11
|
+
collection = [:has_many, :has_and_belongs_to_many].include?(association.macro)
|
12
|
+
|
13
|
+
partial = if lookup_context.exists?(association.klass.model_name.element, controller_name)
|
14
|
+
association.klass.model_name.element
|
15
|
+
else
|
16
|
+
'record'
|
17
|
+
end
|
18
|
+
|
19
|
+
if collection
|
20
|
+
json.array! record.send(inc), partial: partial, as: :record, locals: { includes: subinc }
|
21
|
+
else
|
22
|
+
if record.send(inc).nil?
|
23
|
+
json.set! association.klass.model_name.element, nil
|
24
|
+
else
|
25
|
+
json.partial! partial, record: record.send(inc), includes: subinc
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
else
|
30
|
+
json.set! inc, record.send(inc)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
if !record.errors.blank?
|
35
|
+
json.set! 'errors', record.errors.to_h
|
36
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
|
1
|
+
records = @records if !records
|
2
2
|
|
3
|
-
json.array! models, partial: 'application/
|
3
|
+
json.array! models, partial: 'application/record', as: :record, includes: includes
|
@@ -0,0 +1,30 @@
|
|
1
|
+
mapping = {
|
2
|
+
'timestamp without time zone' => 'datetime',
|
3
|
+
'time without time zone' => 'datetime',
|
4
|
+
'text' => 'string',
|
5
|
+
'json' => 'hash',
|
6
|
+
'integer' => 'integer',
|
7
|
+
'character varying(255)' => 'string',
|
8
|
+
'character varying(128)' => 'string',
|
9
|
+
'character varying(50)' => 'string',
|
10
|
+
'character varying' => 'string',
|
11
|
+
'jsonb' => 'hash',
|
12
|
+
'inet' => 'string', #TODO: should be inet
|
13
|
+
'hstore' => 'hash',
|
14
|
+
'date' => 'datetime',
|
15
|
+
'numeric(16,2)' => 'decimal',
|
16
|
+
'numeric' => 'decimal',
|
17
|
+
'double precision' => 'decimal',
|
18
|
+
'ltree' => 'string',
|
19
|
+
'boolean' => 'boolean',
|
20
|
+
'geometry' => 'hash'
|
21
|
+
}
|
22
|
+
|
23
|
+
model.columns.each do |column|
|
24
|
+
json.set! column.name, {
|
25
|
+
type: mapping[column.sql_type],
|
26
|
+
primary_key: column.name == model.primary_key,
|
27
|
+
null: column.null,
|
28
|
+
array: column.array
|
29
|
+
}
|
30
|
+
end
|
@@ -1,3 +1,3 @@
|
|
1
|
-
|
1
|
+
record = @record if !record
|
2
2
|
|
3
|
-
json.partial! 'application/model',
|
3
|
+
json.partial! 'application/model', record: record, includes: includes
|
data/lib/standard_api.rb
CHANGED
@@ -19,9 +19,22 @@ module StandardAPI
|
|
19
19
|
|
20
20
|
def self.included(klass)
|
21
21
|
klass.hide_action :current_mask
|
22
|
-
klass.helper_method :includes, :orders
|
22
|
+
klass.helper_method :includes, :orders, :model
|
23
23
|
klass.prepend_view_path(File.join(File.dirname(__FILE__), 'standard_api', 'views'))
|
24
|
+
klass.extend(ClassMethods)
|
24
25
|
end
|
26
|
+
|
27
|
+
def ping
|
28
|
+
render :text => 'pong'
|
29
|
+
end
|
30
|
+
|
31
|
+
def tables
|
32
|
+
controllers = Dir[Rails.root.join('app/controllers/*_controller.rb')].map{ |path| path.match(/(\w+)_controller.rb/)[1].camelize+"Controller" }.map(&:safe_constantize)
|
33
|
+
controllers.select! { |c| c.ancestors.include?(self.class) && c != self.class }
|
34
|
+
controllers.map!(&:model).compact!.map!(&:table_name)
|
35
|
+
|
36
|
+
render json: controllers
|
37
|
+
end
|
25
38
|
|
26
39
|
def index
|
27
40
|
@records = resources.limit(params[:limit]).offset(params[:offset]).sort(orders)
|
@@ -66,11 +79,19 @@ module StandardAPI
|
|
66
79
|
@current_mask ||= {}
|
67
80
|
end
|
68
81
|
|
82
|
+
module ClassMethods
|
83
|
+
|
84
|
+
def model
|
85
|
+
return @model if defined?(@model)
|
86
|
+
@model = name.sub(/Controller\z/, '').singularize.camelize.safe_constantize
|
87
|
+
end
|
88
|
+
|
89
|
+
end
|
90
|
+
|
69
91
|
private
|
70
92
|
|
71
93
|
def model
|
72
|
-
|
73
|
-
@model = self.class.name.sub(/Controller\z/, '').singularize.camelize.constantize
|
94
|
+
self.class.model
|
74
95
|
end
|
75
96
|
|
76
97
|
def model_params
|
@@ -85,6 +106,19 @@ module StandardAPI
|
|
85
106
|
self.send "#{model.model_name.singular}_orders"
|
86
107
|
end
|
87
108
|
|
109
|
+
def excludes_for(klass)
|
110
|
+
if defined?(ApplicationHelper) && ApplicationHelper.instance_methods.include?(:excludes)
|
111
|
+
excludes = Class.new.send(:include, ApplicationHelper).new.excludes.with_indifferent_access
|
112
|
+
excludes.try(:[], klass.model_name.singular) || []
|
113
|
+
else
|
114
|
+
[]
|
115
|
+
end
|
116
|
+
end
|
117
|
+
|
118
|
+
def model_excludes
|
119
|
+
excludes_for(model)
|
120
|
+
end
|
121
|
+
|
88
122
|
def resources
|
89
123
|
model.filter(params[:where]).where(current_mask[model.table_name])
|
90
124
|
end
|
@@ -94,7 +128,11 @@ module StandardAPI
|
|
94
128
|
end
|
95
129
|
|
96
130
|
def orders
|
97
|
-
@orders ||= params[:order]
|
131
|
+
@orders ||= StandardAPI::Orders.sanitize(params[:order], model_orders)
|
132
|
+
end
|
133
|
+
|
134
|
+
def excludes
|
135
|
+
@excludes ||= model_excludes
|
98
136
|
end
|
99
137
|
|
100
138
|
# Used in #calculate
|
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: 1.0.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Bracy
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-09-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord-sort
|
@@ -198,8 +198,9 @@ files:
|
|
198
198
|
- lib/standard_api/test_case/index_tests.rb
|
199
199
|
- lib/standard_api/test_case/show_tests.rb
|
200
200
|
- lib/standard_api/test_case/update_tests.rb
|
201
|
-
- lib/standard_api/views/application/
|
201
|
+
- lib/standard_api/views/application/_record.json.jbuilder
|
202
202
|
- lib/standard_api/views/application/index.json.jbuilder
|
203
|
+
- lib/standard_api/views/application/schema.json.jbuilder
|
203
204
|
- lib/standard_api/views/application/show.json.jbuilder
|
204
205
|
homepage: https://github.com/waratuman/standardapi
|
205
206
|
licenses:
|
@@ -1,32 +0,0 @@
|
|
1
|
-
model.attributes.each do |name, value|
|
2
|
-
json.set! name, value
|
3
|
-
end
|
4
|
-
|
5
|
-
includes.each do |inc, subinc|
|
6
|
-
association = model.class.reflect_on_association(inc)
|
7
|
-
if association.klass < ActiveRecord::Base
|
8
|
-
json.set! inc do
|
9
|
-
collection = [:has_many, :has_and_belongs_to_many].include?(association.macro)
|
10
|
-
|
11
|
-
partial = if lookup_context.exists?(association.klass.model_name.element, controller_name)
|
12
|
-
# [controller_name, association.klass.model_name.element].join('/')
|
13
|
-
association.klass.model_name.element
|
14
|
-
else
|
15
|
-
# 'application/model'
|
16
|
-
'model'
|
17
|
-
end
|
18
|
-
|
19
|
-
if collection
|
20
|
-
json.array! model.send(inc), partial: partial, as: :model, locals: { includes: subinc }
|
21
|
-
else
|
22
|
-
json.partial! partial, model: model.send(inc), includes: subinc
|
23
|
-
end
|
24
|
-
end
|
25
|
-
else
|
26
|
-
json.set! inc, model.send(inc)
|
27
|
-
end
|
28
|
-
end
|
29
|
-
|
30
|
-
if !model.errors.blank?
|
31
|
-
json.set! 'errors', model.errors.to_h
|
32
|
-
end
|