lhs 15.6.1 → 15.7.0
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 +4 -4
- data/lib/lhs/concerns/item/update.rb +3 -3
- data/lib/lhs/concerns/record/custom_setters.rb +20 -0
- data/lib/lhs/record.rb +3 -12
- data/lib/lhs/version.rb +1 -1
- data/spec/item/update_spec.rb +58 -0
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 83b054afd85f87629645a1ba8755eced33f871132a594120f5128444d66bb091
|
4
|
+
data.tar.gz: a448d72f632fe3375f7987ec73105b650ed76ffa5369c3f2ead984714888bcac
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 342399e3bde742b37688f20ca329a19f81c692d08770f579f51d750b47169e38059dde880879e2ca24280bde5a62cb15aae3e944a24dc9c39685ab407277fff3
|
7
|
+
data.tar.gz: 561c772fea5c40562a0838f1cfa440ea67f733f88f915885733553379525acffcbcdb9cce510d9bddf9a0fc69c2b59a0b0f31b62c5eed852ef18c8a50a87af7a
|
@@ -25,9 +25,9 @@ class LHS::Item < LHS::Proxy
|
|
25
25
|
|
26
26
|
def update!(params, options = {}, partial_update = false)
|
27
27
|
options ||= {}
|
28
|
-
|
29
|
-
_data.merge_raw!(
|
30
|
-
data_sent = partial_update ?
|
28
|
+
partial_record = _record.new(LHS::Data.new(params, _data.parent, _record))
|
29
|
+
_data.merge_raw!(partial_record._data)
|
30
|
+
data_sent = partial_update ? partial_record._data : _data
|
31
31
|
url = href || record.find_endpoint(id: id).compile(id: id)
|
32
32
|
response_data = record.request(
|
33
33
|
options.merge(
|
@@ -0,0 +1,20 @@
|
|
1
|
+
require 'active_support'
|
2
|
+
|
3
|
+
class LHS::Record
|
4
|
+
|
5
|
+
module CustomSetters
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
private
|
9
|
+
|
10
|
+
def apply_custom_setters!
|
11
|
+
return if !_data.item? || !_data._raw.respond_to?(:keys)
|
12
|
+
raw = _data._raw
|
13
|
+
custom_setters = raw.keys.find_all { |key| public_methods.include?("#{key}=".to_sym) }
|
14
|
+
custom_setters.each do |setter|
|
15
|
+
value = raw.delete(setter)
|
16
|
+
send("#{setter}=", value)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
data/lib/lhs/record.rb
CHANGED
@@ -7,6 +7,8 @@ class LHS::Record
|
|
7
7
|
'lhs/concerns/record/configuration'
|
8
8
|
autoload :Create,
|
9
9
|
'lhs/concerns/record/create'
|
10
|
+
autoload :CustomSetters,
|
11
|
+
'lhs/concerns/record/custom_setters'
|
10
12
|
autoload :Destroy,
|
11
13
|
'lhs/concerns/record/destroy'
|
12
14
|
autoload :Endpoints,
|
@@ -45,6 +47,7 @@ class LHS::Record
|
|
45
47
|
include Chainable
|
46
48
|
include Configuration
|
47
49
|
include Create
|
50
|
+
include CustomSetters
|
48
51
|
include Destroy
|
49
52
|
include Endpoints
|
50
53
|
include Equality
|
@@ -87,16 +90,4 @@ class LHS::Record
|
|
87
90
|
def respond_to_missing?(name, include_all = false)
|
88
91
|
_data.respond_to_missing?(name, include_all)
|
89
92
|
end
|
90
|
-
|
91
|
-
private
|
92
|
-
|
93
|
-
def apply_custom_setters!
|
94
|
-
return if !_data.item? || !_data._raw.respond_to?(:keys)
|
95
|
-
raw = _data._raw
|
96
|
-
custom_setters = raw.keys.find_all { |key| public_methods.include?("#{key}=".to_sym) }
|
97
|
-
custom_setters.each do |setter|
|
98
|
-
value = raw.delete(setter)
|
99
|
-
send("#{setter}=", value)
|
100
|
-
end
|
101
|
-
end
|
102
93
|
end
|
data/lib/lhs/version.rb
CHANGED
data/spec/item/update_spec.rb
CHANGED
@@ -48,6 +48,64 @@ describe LHS::Item do
|
|
48
48
|
expect(item.name).to eq 'Andrea'
|
49
49
|
expect(item.likes).not_to eq 'Banana'
|
50
50
|
end
|
51
|
+
|
52
|
+
context 'with custom setters' do
|
53
|
+
before do
|
54
|
+
class Booking < LHS::Record
|
55
|
+
endpoint 'http://bookings/bookings'
|
56
|
+
|
57
|
+
def appointments=(appointments)
|
58
|
+
super(
|
59
|
+
appointments.map { |appointment| appointment[:id] }
|
60
|
+
)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
let(:item) do
|
66
|
+
Booking.new(id: 'abc')
|
67
|
+
end
|
68
|
+
|
69
|
+
it 'updates data using custom setters before send to backend' do
|
70
|
+
stub_request(:post, "http://bookings/bookings")
|
71
|
+
.with(body: {
|
72
|
+
id: 'abc',
|
73
|
+
appointments: [1, 2, 3]
|
74
|
+
}.to_json)
|
75
|
+
.to_return(status: 200)
|
76
|
+
item.update(appointments: [{ id: 1 }, { id: 2 }, { id: 3 }])
|
77
|
+
expect(item.appointments.to_a).to eq([1, 2, 3])
|
78
|
+
end
|
79
|
+
|
80
|
+
context 'with nested items' do
|
81
|
+
before do
|
82
|
+
class Booking < LHS::Record
|
83
|
+
endpoint 'http://bookings/bookings'
|
84
|
+
has_one :appointment_proposal
|
85
|
+
end
|
86
|
+
|
87
|
+
class AppointmentProposal < LHS::Record
|
88
|
+
def appointments_attributes=(attributes)
|
89
|
+
self.appointments = attributes.map { |attribute| { 'date_time': attribute[:date] } }
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
let(:item) do
|
95
|
+
Booking.new(id: 'abc', appointment_proposal: { appointments: [] })
|
96
|
+
end
|
97
|
+
|
98
|
+
it 'updates data using custom setters before send to backend' do
|
99
|
+
stub_request(:post, "http://bookings/bookings")
|
100
|
+
.with(body: {
|
101
|
+
appointments: [{ date_time: '2018-01-18' }]
|
102
|
+
}.to_json)
|
103
|
+
.to_return(status: 200)
|
104
|
+
item.appointment_proposal.update(appointments_attributes: [{ date: '2018-01-18' }])
|
105
|
+
expect(item.appointment_proposal.appointments.as_json).to eq([{ 'date_time' => '2018-01-18' }])
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
51
109
|
end
|
52
110
|
|
53
111
|
context 'update!' do
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: lhs
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 15.
|
4
|
+
version: 15.7.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- https://github.com/local-ch/lhs/graphs/contributors
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-09-27 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activemodel
|
@@ -238,6 +238,7 @@ files:
|
|
238
238
|
- lib/lhs/concerns/record/chainable.rb
|
239
239
|
- lib/lhs/concerns/record/configuration.rb
|
240
240
|
- lib/lhs/concerns/record/create.rb
|
241
|
+
- lib/lhs/concerns/record/custom_setters.rb
|
241
242
|
- lib/lhs/concerns/record/destroy.rb
|
242
243
|
- lib/lhs/concerns/record/endpoints.rb
|
243
244
|
- lib/lhs/concerns/record/equality.rb
|
@@ -468,7 +469,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
468
469
|
requirements:
|
469
470
|
- Ruby >= 2.3.0
|
470
471
|
rubyforge_project:
|
471
|
-
rubygems_version: 2.7.
|
472
|
+
rubygems_version: 2.7.7
|
472
473
|
signing_key:
|
473
474
|
specification_version: 4
|
474
475
|
summary: 'REST services accelerator: Rails gem providing an easy, active-record-like
|