lhs 19.8.2 → 19.9.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: 9e57e3afbbffbdd6fdbcc8b64a947daae97b5faa5975ae8fe29b6b77d87b0d47
4
- data.tar.gz: 5d8363f9dcbd226f60428e6344967ef2369639fd36544b7a6d9112d5058b8d31
3
+ metadata.gz: 5ae06289222aa01014699262ca1538093d7388f5e6a4fa689c3351d2fd6cf1d7
4
+ data.tar.gz: f0308f117f2f2dc44d0e0ada944c74ed0819b91198756fcedfeecebf472b0da9
5
5
  SHA512:
6
- metadata.gz: 4f7ae19794cade9f046731c0c6cc948a87b8a04bada44e14c52853600b78c47f6ecfb53bc2bedbb16918cb0cf2788e3ac780873ebbeebbad544eb504494464aa
7
- data.tar.gz: b8092ac44ed946248fa686e95873ce841cd1b38dbebbc41f7547037840b56e8c8c5d168c1866382e9f6e6eb51c181e43f407febe9d5ff1b476b1df3a063f326e
6
+ metadata.gz: 4778c1456b9f0aea0308f7a505d27641aa6a0bb1dc18170724c80542d182df8f5198f2e1a5de6db749722e22a59d1c1ea701934f8f89f365c05a4cc00af061c3
7
+ data.tar.gz: c64c7dece11ba95192669df9ea91d33a40262156eeac930efdfd8e432a4bfc51add6350746c937203309d67012499d15880c5b5b694ac66740556b9798dd8be6
data/README.md CHANGED
@@ -2020,6 +2020,21 @@ With `includes` or `includes_all` (to enforce fetching all remote objects for pa
2020
2020
 
2021
2021
  Including linked resources/records is heavily influenced by [https://guides.rubyonrails.org/active_record_querying.html](https://guides.rubyonrails.org/active_record_querying.html#eager-loading-associations) and you should read it to understand this feature in all it's glory.
2022
2022
 
2023
+ #### Generate links from parameters
2024
+
2025
+ Sometimes you need to generate full hrefs/urls for records but you just have parameters that describe that record, like the ID.
2026
+
2027
+ For those usecases you can use `href_for(params)`:
2028
+
2029
+ ```ruby
2030
+ # app/controllers/some_controller.rb
2031
+
2032
+ Presence.create(place: { href: Place.href_for(123) })
2033
+ ```
2034
+ ```
2035
+ POST '/presences' { place: { href: "http://datastore/places/123" } }
2036
+ ```
2037
+
2023
2038
  #### Ensure the whole linked collection is included: includes_all
2024
2039
 
2025
2040
  In case endpoints are paginated and you are certain that you'll need all objects of a set and not only the first page/batch, use `includes_all`.
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'active_support'
4
+
5
+ class LHS::Record
6
+
7
+ module HrefFor
8
+ extend ActiveSupport::Concern
9
+
10
+ module ClassMethods
11
+ def href_for(args = nil)
12
+ return unless [Integer, String].include?(args.class)
13
+ params = { id: args }
14
+ find_endpoint(params).compile(params)
15
+ end
16
+ alias url_for href_for
17
+ end
18
+ end
19
+ end
@@ -23,6 +23,8 @@ class LHS::Record
23
23
  'lhs/concerns/record/find_by'
24
24
  autoload :First,
25
25
  'lhs/concerns/record/first'
26
+ autoload :HrefFor,
27
+ 'lhs/concerns/record/href_for'
26
28
  autoload :Last,
27
29
  'lhs/concerns/record/last'
28
30
  autoload :Mapping,
@@ -64,6 +66,7 @@ class LHS::Record
64
66
  include Find
65
67
  include FindBy
66
68
  include First
69
+ include HrefFor
67
70
  include LHS::IsHref
68
71
  include Last
69
72
  include LHS::Inspect
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '19.8.2'
4
+ VERSION = '19.9.0'
5
5
  end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rails_helper'
4
+
5
+ describe LHS::Record do
6
+ before do
7
+ class Record < LHS::Record
8
+ endpoint 'http://datastore/records/'
9
+ endpoint 'http://datastore/records/{id}'
10
+ end
11
+ end
12
+
13
+ context 'href_for' do
14
+
15
+ it 'injects variables and returns href' do
16
+ expect(Record.href_for(1)).to eq 'http://datastore/records/1'
17
+ expect(Record.href_for('vmasd241')).to eq 'http://datastore/records/vmasd241'
18
+ end
19
+
20
+ it 'also works with url_for (alias)' do
21
+ expect(Record.url_for(1)).to eq 'http://datastore/records/1'
22
+ expect(Record.url_for('vmasd241')).to eq 'http://datastore/records/vmasd241'
23
+ end
24
+ end
25
+ end
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: 19.8.2
4
+ version: 19.9.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: 2019-07-25 00:00:00.000000000 Z
11
+ date: 2019-08-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -252,6 +252,7 @@ files:
252
252
  - lib/lhs/concerns/record/find.rb
253
253
  - lib/lhs/concerns/record/find_by.rb
254
254
  - lib/lhs/concerns/record/first.rb
255
+ - lib/lhs/concerns/record/href_for.rb
255
256
  - lib/lhs/concerns/record/last.rb
256
257
  - lib/lhs/concerns/record/mapping.rb
257
258
  - lib/lhs/concerns/record/merge.rb
@@ -427,6 +428,7 @@ files:
427
428
  - spec/record/handle_includes_errors_spec.rb
428
429
  - spec/record/has_many_spec.rb
429
430
  - spec/record/has_one_spec.rb
431
+ - spec/record/href_for_spec.rb
430
432
  - spec/record/ignore_errors_spec.rb
431
433
  - spec/record/immutable_chains_spec.rb
432
434
  - spec/record/includes_all_spec.rb
@@ -639,6 +641,7 @@ test_files:
639
641
  - spec/record/handle_includes_errors_spec.rb
640
642
  - spec/record/has_many_spec.rb
641
643
  - spec/record/has_one_spec.rb
644
+ - spec/record/href_for_spec.rb
642
645
  - spec/record/ignore_errors_spec.rb
643
646
  - spec/record/immutable_chains_spec.rb
644
647
  - spec/record/includes_all_spec.rb