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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 58e5aa906cbdb420349c0ae94d74186ca8191c7f
4
- data.tar.gz: 8a362d7d2fb5f90921260e93ee04df163c868520
3
+ metadata.gz: 67dc838f6d2096fd1f791c9781490db311b9b4b2
4
+ data.tar.gz: 96d425ce24603f45e9ea714e55b347d5fa9838ae
5
5
  SHA512:
6
- metadata.gz: c89fa1bc6e4e0b2d8165a67dd3bb4d761b6d2681aac2c352fcc4da496f1dd048e4147be27d4193c90858123b7e34730b7e4f670f67cc693cb2941145efb724d0
7
- data.tar.gz: e92218bbf59865553d2012606db1071a20310cdf129ed753d24ae78486abd4292b1a381dc8fd757aba24a87763380ea16db8abb0177283630aec870c4ebdd0ec
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
- (model.attribute_names & attrs.keys.map(&:to_s)).each do |test_key|
18
- assert_equal normalize_to_json(test_key, attrs[test_key.to_sym]), json[test_key]
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
- (model.attribute_names & attrs.keys.map(&:to_s)).each do |test_key|
35
- assert_equal normalize_to_json(test_key, attrs[test_key.to_sym]), json[test_key]
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
- attrs = attributes_for(singular_name, :nested).select{ |k,v| !model.readonly_attributes.include?(k.to_s) }
55
- create_webmocks(attrs)
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
- assert_difference("#{model.name}.count") do
58
- post :create, singular_name => attrs, include: includes, :format => 'json'
59
- assert_response :created
60
- assert assigns(singular_name)
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
- json = JSON.parse(response.body)
63
- assert json.is_a?(Hash)
64
- includes.each do |included|
65
- assert json.key?(included.to_s), "#{included.inspect} not included in response"
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
- association = assigns(:record).class.reflect_on_association(included)
68
- if ['belongs_to', 'has_one'].include?(association.macro)
69
- assigns(:record).send(included).attributes do |key, value|
70
- assert_equal json[included.to_s][key.to_s], value
71
- end
72
- else
73
- assigns(:record).send(included).first.attributes.each do |key, value|
74
- assert_equal json[included.to_s][0][key.to_s], value
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
- if order.is_a?(Hash)
29
- order.values.last.each do |o|
30
- get :index, order: {order.keys.first => o}, format: 'json'
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
- create_model
47
- get :index, include: includes, format: 'json'
48
- includes.each do |included|
49
- assert JSON.parse(response.body)[0].key?(included.to_s), "#{included.inspect} not included in response"
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 ['belongs_to', 'has_one'].include?(association.macro)
26
- assigns(:record).send(included).attributes do |key, value|
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.attributes.each do |key, value|
31
- assert_equal json[included.to_s][0][key.to_s], value
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.attribute_names & attrs.keys.map(&:to_s)).each do |test_key|
15
- assert_equal normalize_attribute(test_key, attrs[test_key.to_sym]), m.reload.send(test_key)
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
- assert_equal normalize_attribute(test_key, attrs[test_key.to_sym]), m.reload.send(test_key)
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
- m = create_model
46
- attrs = attributes_for(singular_name, :nested).select{|k,v| !model.readonly_attributes.include?(k) }
47
- create_webmocks(attrs)
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
- put :update, id: m.id, include: includes, singular_name => attrs, format: 'json'
52
+ put :update, id: m.id, include: includes, singular_name => attrs, format: 'json'
50
53
 
51
- json = JSON.parse(response.body)
52
- includes.each do |included|
53
- assert json.key?(included.to_s), "#{included.inspect} not included in response"
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
- association = assigns(:record).class.reflect_on_association(included)
56
- if ['belongs_to', 'has_one'].include?(association.macro)
57
- assigns(:record).send(included).attributes do |key, value|
58
- assert_equal json[included.to_s][key.to_s], value
59
- end
60
- else
61
- assigns(:record).send(included).first.attributes.each do |key, value|
62
- assert_equal json[included.to_s][0][key.to_s], value
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
- if defined?(model_class_name.constantize)
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
- klass.controller_class.action_methods.each do |action|
28
- if const_defined?("StandardAPI::TestCase::#{action.capitalize}Tests")
29
- # Include the test if there is a route
30
- # if Rails.application.routes.url_for(controller: @controller.controller_path, action: 'destroy', only_path: true)
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 normalize_attribute(attribute, value)
60
- validators = self.class.model.validators_on(attribute)
61
- value
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
- models = @records if !models
1
+ records = @records if !records
2
2
 
3
- json.array! models, partial: 'application/model', as: :model, includes: includes
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
- model = @record if !model
1
+ record = @record if !record
2
2
 
3
- json.partial! 'application/model', model: model, includes: includes
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
- return @model if defined?(@model)
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.6
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-08-06 00:00:00.000000000 Z
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/_model.json.jbuilder
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