snfoil-rails 0.4.4 → 0.5.2

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
  SHA256:
3
- metadata.gz: cbabacb778a70d87face8ff2953666e66b509f575ab91c7010b3c7bf71a050a2
4
- data.tar.gz: 1dfea0059c61d63532022c2537482456942ca05c33991f64a2b725b452ef1c0a
3
+ metadata.gz: 73e5f3f298f380d6961452b753e3d5ff22f35a4a466b99590cd2409a78ea339e
4
+ data.tar.gz: 2ce8432635e0d889791954955d984d7b81d2d99888dbcbd5ab728e98019eadc6
5
5
  SHA512:
6
- metadata.gz: 5bdc25d574bcc9e1a95f5783beadcdfd7d44d87384d105f5ce7534b51c1ea4e5837365d0758c4152c9940293445ecdc484d781df1ada201148d01d24bdff1e31
7
- data.tar.gz: 7d80030755877131ec54644096f696b051c3930de228c56d767eb035c6288b0c1f06d610dbabf8b646283e05812d95af607084ac277c937efabd24eaebf826ca
6
+ metadata.gz: 8767df32da2db2772c633123436bcef81a98f9fe2c37cc1739922e8f07543e0cb593a3fb0a126bc15bfa0cb9abd4f7a419a951c925413c54951e9f4f16b01d45
7
+ data.tar.gz: d2e717c282cf33e7f1a5dd7f42feffe0c1b726c472368d477837f1e64676b2f9dc933d71231c4de5d84f2eb55546c42001dc9bffdb912f26d4d029bf7e710658
@@ -40,27 +40,38 @@ module SnFoil
40
40
 
41
41
  def render_change(model, **options)
42
42
  if model.errors.empty?
43
- render json: serializer(**options).new(model, **options).serializable_hash
43
+ params
44
+ render json: serializer(**options).new(model,
45
+ **options,
46
+ params: (options[:controller_params] || options[:params] || {})
47
+ .merge(current_entity: current_entity)).serializable_hash
44
48
  else
45
- render model.errors, status: :unprocessable_entity
49
+ render json: model.errors, status: :unprocessable_entity
46
50
  end
47
51
  end
48
52
 
49
53
  def render_destroy(model, **_options)
50
54
  if model.errors.empty?
51
- render status: :no_content
55
+ render json: {}, status: :no_content
52
56
  else
53
- render model.errors, status: :unprocessable_entity
57
+ render json: model.errors, status: :unprocessable_entity
54
58
  end
55
59
  end
56
60
 
57
61
  def render_index(results, **options)
58
- render json: serializer(**options).new(paginate(results, **options), **options, meta: meta(results, **options))
62
+ render json: serializer(**options).new(paginate(results, **options),
63
+ **options,
64
+ params: (options[:controller_params] || options[:params] || {})
65
+ .merge(current_entity: current_entity),
66
+ meta: meta(results, **options))
59
67
  .serializable_hash
60
68
  end
61
69
 
62
70
  def render_show(model, **options)
63
- render json: serializer(**options).new(model, **options).serializable_hash
71
+ render json: serializer(**options).new(model,
72
+ **options,
73
+ params: (options[:controller_params] || options[:params] || {})
74
+ .merge(current_entity: current_entity)).serializable_hash
64
75
  end
65
76
 
66
77
  private
@@ -50,6 +50,7 @@ module SnFoil
50
50
  end
51
51
 
52
52
  def meta(results, **options)
53
+ results = paginate(results, **options)
53
54
  total_pages = results.respond_to?(:total_pages) ? results.total_pages : nil
54
55
  total_count = results.respond_to?(:total_count) ? results.total_count : nil
55
56
 
@@ -28,7 +28,7 @@ module SnFoil
28
28
  end
29
29
 
30
30
  def current_context(**options)
31
- @current_context ||= context(**options).new(context_user)
31
+ @current_context ||= context(**options).new(context_entity)
32
32
  end
33
33
 
34
34
  protected
@@ -75,7 +75,8 @@ module SnFoil
75
75
  options
76
76
  end
77
77
 
78
- def context_user
78
+ def context_entity
79
+ return current_entity if defined? current_entity
79
80
  return current_user if defined? current_user
80
81
  end
81
82
  end
@@ -77,7 +77,7 @@ module SnFoil
77
77
  def data_id(attributes, data)
78
78
  if data[:id]
79
79
  attributes[:id] = data[:id]
80
- elsif object[:data][:'local:id']
80
+ elsif data[:'local:id']
81
81
  attributes[:lid] = data[:'local:id']
82
82
  end
83
83
  attributes
@@ -117,6 +117,7 @@ module SnFoil
117
117
  resource_data = data.dig(:relationships, key, :data)
118
118
  return attributes unless resource_data
119
119
 
120
+ resource_data = data_id(resource_data, resource_data)
120
121
  attribute_data = lookup_relationship(resource_data)
121
122
  relationship_data = { data: attribute_data || resource_data }
122
123
  attributes[opts.fetch(:key) { key }] = deserializer.new(relationship_data, **options, included: included).parse
@@ -128,6 +129,7 @@ module SnFoil
128
129
  return attributes unless array_data
129
130
 
130
131
  attributes[opts.fetch(:key) { key }] = array_data.map do |resource_data|
132
+ resource_data = data_id(resource_data, resource_data)
131
133
  attribute_data = lookup_relationship(resource_data)
132
134
  relationship_data = { data: attribute_data || resource_data }
133
135
  deserializer.new(relationship_data, **options, included: included).parse
@@ -136,7 +138,7 @@ module SnFoil
136
138
  end
137
139
 
138
140
  def lookup_relationship(type:, id: nil, lid: nil, **_opts)
139
- raise ::ArgumentError, 'missing keyword: id or lid' unless id || lid
141
+ raise ::ArgumentError, "missing keyword: id or lid for type: #{type}" unless id || lid
140
142
 
141
143
  included&.find do |x|
142
144
  x[:type].eql?(type) &&
@@ -2,6 +2,6 @@
2
2
 
3
3
  module SnFoil
4
4
  module Rails
5
- VERSION = '0.4.4'
5
+ VERSION = '0.5.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: snfoil-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.5.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matthew Howes
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2020-03-15 00:00:00.000000000 Z
12
+ date: 2020-06-03 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport
@@ -45,14 +45,14 @@ dependencies:
45
45
  requirements:
46
46
  - - "~>"
47
47
  - !ruby/object:Gem::Version
48
- version: '0.4'
48
+ version: '0.5'
49
49
  type: :runtime
50
50
  prerelease: false
51
51
  version_requirements: !ruby/object:Gem::Requirement
52
52
  requirements:
53
53
  - - "~>"
54
54
  - !ruby/object:Gem::Version
55
- version: '0.4'
55
+ version: '0.5'
56
56
  - !ruby/object:Gem::Dependency
57
57
  name: bundler
58
58
  requirement: !ruby/object:Gem::Requirement