lhs 15.6.1 → 15.7.0

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: 9511ca1ef684cea377c435b2eb41c8c85063c41705583c16020f6b51e8ac7a20
4
- data.tar.gz: e746bf7da546c199bb559ef9f2b9eee449083958a60cb0f8138290916580b148
3
+ metadata.gz: 83b054afd85f87629645a1ba8755eced33f871132a594120f5128444d66bb091
4
+ data.tar.gz: a448d72f632fe3375f7987ec73105b650ed76ffa5369c3f2ead984714888bcac
5
5
  SHA512:
6
- metadata.gz: 641c4079a479490d5af2c4b9ebbb552f1f8d6ba4509c1d5d09ef1ee2c7d41d39470779f83ec0e55739a81f05edd0ad1e86705bd1d65318b6faf36995a308362c
7
- data.tar.gz: e1371c23a194e4100d3f3c72ea1d7617d19c9bc6a645695ea752fce37c861b052a55c262b6640617d60a7cb7bdea4fee75617f247a3d260dd5a8e6663004667e
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
- partial_data = LHS::Data.new(params, _data.parent, record)
29
- _data.merge_raw!(partial_data)
30
- data_sent = partial_update ? partial_data : _data
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
@@ -1,3 +1,3 @@
1
1
  module LHS
2
- VERSION = '15.6.1'
2
+ VERSION = '15.7.0'
3
3
  end
@@ -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.6.1
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-08-20 00:00:00.000000000 Z
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.6
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