lhs 21.2.2 → 21.2.3.pre.preload.pre.providers.pre.too.1

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: 97ca91f932322b206403f8179975b5b9b82b3963518af2c914ea3dce57323ffc
4
- data.tar.gz: 13849b65f18f9dce4748f11d693cb79ad3c4c467b3d790235de8d9b75ee410c5
3
+ metadata.gz: 86d0b18d2781746245a64d4b1594cae9933712760d984685d61e19b680a27f43
4
+ data.tar.gz: 9b89ffa0271afe18cea56f049c539d19f40c498fd0da8351a94abacbc3bf27da
5
5
  SHA512:
6
- metadata.gz: 1ab0338a4892d6ff23ea4698019d732733503d8f4c23eeff50beb7ceb6d41905d324c46d9e16e543514ec5c704c4392ce902f9c48ccf92aa4f01ef6b54833498
7
- data.tar.gz: 401fbcaf2c25f3eca613a41bf1ca8815867c3df9a3e2ed18d3234da8b5d7c62e0499065f7d72f1d3bdde14ae1cf9dea7f017258647a9156c1c4a4d2f26b52ba7
6
+ metadata.gz: 4afb8fd86eaa9127581720a1abdc2057033be1fe3196b074a6798224e01e25ad586d92b9683ebf77b70be2137fe6c575d119773f227f2986a4be04b7fccd0e37
7
+ data.tar.gz: 5f929ebc2f5f8d88300019c372507750ca48b4ad53930d89a0a34cf87621224dcbfd95908d0ca84fbbafe9c3d712db2cbfe01e46bd1db95f80dcf9ee222baa15
@@ -27,11 +27,28 @@ module AutoloadRecords
27
27
  @app.call(env)
28
28
  end
29
29
 
30
- def self.require_records
31
- Dir.glob(Rails.root.join('app', 'models', '**', '*.rb')).each do |file|
32
- require_dependency file if File.read(file).match('LHS::Record')
30
+ def self.model_files
31
+ Dir.glob(Rails.root.join('app', 'models', '**', '*.rb'))
32
+ end
33
+
34
+ def self.require_direct_inheritance
35
+ model_files.map do |file|
36
+ next unless File.read(file).match('LHS::Record')
37
+ require_dependency file
38
+ file.split('models/').last.gsub('.rb', '').classify
39
+ end.compact
40
+ end
41
+
42
+ def self.require_inheriting_records(parents)
43
+ model_files.each do |file|
44
+ next if parents.none? { |parent| File.read(file).match(parent) }
45
+ require_dependency file
33
46
  end
34
47
  end
48
+
49
+ def self.require_records
50
+ require_inheriting_records(require_direct_inheritance)
51
+ end
35
52
  end
36
53
  end
37
54
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LHS
4
- VERSION = '21.2.2'
4
+ VERSION = '21.2.3.pre.preload-providers-too.1'
5
5
  end
@@ -4,18 +4,45 @@ require "rails_helper"
4
4
 
5
5
  describe LHS, type: :request do
6
6
  context 'autoloading' do
7
- it "pre/re-loads all LHS classes initialy,|
8
- because it's necessary for endpoint-to-record-class-discovery",
9
- reset_before: false do
10
- all_endpoints = LHS::Record::Endpoints.all
11
- expect(all_endpoints['http://datastore/v2/users']).to be_present
12
- expect(all_endpoints['http://datastore/v2/users/{id}']).to be_present
7
+
8
+ let(:endpoints) { LHS::Record::Endpoints.all }
9
+
10
+ it "pre/re-loads all LHS classes initialy, because it's necessary for endpoint-to-record-class-discovery", reset_before: false do
11
+
12
+ expect(endpoints['http://datastore/v2/users']).to be_present
13
+ expect(endpoints['http://datastore/v2/users/{id}']).to be_present
14
+
13
15
  expect(
14
- User.endpoints.detect { |endpoint| endpoint.url == 'http://datastore/v2/users' }
16
+ DummyUser.endpoints.detect { |endpoint| endpoint.url == 'http://datastore/v2/users' }
15
17
  ).to be_present
16
18
  expect(
17
- User.endpoints.detect { |endpoint| endpoint.url == 'http://datastore/v2/users/{id}' }
19
+ DummyUser.endpoints.detect { |endpoint| endpoint.url == 'http://datastore/v2/users/{id}' }
18
20
  ).to be_present
19
21
  end
22
+
23
+ it "also pre/re-loads all LHS classes that inherited from an LHS provider, because it's necessary for endpoint-to-record-class-discovery", reset_before: false do
24
+
25
+ expect(endpoints['http://customers']).to be_present
26
+ expect(endpoints['http://customers/{id}']).to be_present
27
+
28
+ expect(
29
+ DummyCustomer.endpoints.detect { |endpoint| endpoint.url == 'http://customers' }
30
+ ).to be_present
31
+ expect(
32
+ DummyCustomer.endpoints.detect { |endpoint| endpoint.url == 'http://customers/{id}' }
33
+ ).to be_present
34
+
35
+ customer_request = stub_request(:get, "http://customers/1")
36
+ .with(
37
+ headers: {
38
+ 'Authorization' => 'token123'
39
+ }
40
+ )
41
+ .to_return(body: { name: 'Steve' }.to_json)
42
+
43
+ DummyCustomer.find(1)
44
+
45
+ expect(customer_request).to have_been_requested
46
+ end
20
47
  end
21
48
  end
@@ -5,7 +5,7 @@ class ErrorHandlingWithChainsController < ApplicationController
5
5
  # Example where the query chain is resolved
6
6
  # in the view (during render 'show')
7
7
  def fetch_in_view
8
- @records = Record
8
+ @records = DummyRecord
9
9
  .handle(LHC::Error, ->(error) { handle_error(error) })
10
10
  .where(color: 'blue')
11
11
  render 'show'
@@ -15,7 +15,7 @@ class ErrorHandlingWithChainsController < ApplicationController
15
15
  # Example where the query chain is resolved
16
16
  # before the view is rendered
17
17
  def fetch_in_controller
18
- @records = Record
18
+ @records = DummyRecord
19
19
  .handle(LHC::Error, ->(error) { handle_error(error) })
20
20
  .where(color: 'blue').fetch
21
21
  render 'show'
@@ -3,8 +3,8 @@
3
3
  class ExtendedRollbarController < ApplicationController
4
4
 
5
5
  def extended_rollbar
6
- Record.where(color: 'blue').fetch
7
- Record.where(color: 'red').fetch
6
+ DummyRecord.where(color: 'blue').fetch
7
+ DummyRecord.where(color: 'red').fetch
8
8
  raise "Let's see if rollbar logs information about what kind of requests where made around here!"
9
9
  end
10
10
  end
@@ -4,12 +4,12 @@ class OptionBlocksController < ApplicationController
4
4
 
5
5
  def first
6
6
  LHS::OptionBlocks::CurrentOptionBlock.options = { params: { request: 'first' } }
7
- Record.where(request: 'second').fetch
7
+ DummyRecord.where(request: 'second').fetch
8
8
  render text: 'ok'
9
9
  end
10
10
 
11
11
  def second
12
- Record.where(request: 'second').fetch
12
+ DummyRecord.where(request: 'second').fetch
13
13
  render text: 'ok'
14
14
  end
15
15
  end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ class DummyCustomer < Providers::CustomerSystem
4
+ endpoint 'http://customers'
5
+ endpoint 'http://customers/{id}'
6
+ end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class Record < LHS::Record
3
+ class DummyRecord < LHS::Record
4
4
  endpoint 'http://datastore/v2/records'
5
5
  endpoint 'http://datastore/v2/records/{id}'
6
6
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class User < LHS::Record
3
+ class DummyUser < LHS::Record
4
4
  endpoint 'http://datastore/v2/users'
5
5
  endpoint 'http://datastore/v2/users/{id}'
6
6
  end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Providers
4
+ class CustomerSystem < LHS::Record
5
+ provider(headers: { 'Authorization': 'token123' })
6
+ end
7
+ end
@@ -13,7 +13,8 @@ describe 'Option Blocks', type: :request do
13
13
  .to_return(status: 200)
14
14
  end
15
15
 
16
- it 'always ensures option blocks are always reset for new requests', dummy_models: true do
16
+ it 'always ensures option blocks are always reset for new requests',
17
+ dummy_models: true, reset_before: true do
17
18
  get '/option_blocks/first'
18
19
  expect(first_request).to have_been_made.once
19
20
  get '/option_blocks/second'
@@ -664,6 +664,28 @@ describe LHS::Record do
664
664
  expect(nested_request).to have_been_requested
665
665
  expect(place.customer.salesforce.name).to eq 'Steve'
666
666
  end
667
+
668
+ context 'included data has a configured record endpoint option' do
669
+ before do
670
+ class SalesforceCustomer < LHS::Record
671
+ endpoint 'https://salesforce/customers/{id}', headers: { 'Authorization': 'Bearer 123' }
672
+ end
673
+ end
674
+
675
+ let!(:nested_request) do
676
+ stub_request(:get, "https://salesforce/customers/1")
677
+ .with(headers: { 'Authorization' => 'Bearer 123' })
678
+ .to_return(body: {
679
+ name: 'Steve'
680
+ }.to_json)
681
+ end
682
+
683
+ it 'includes data that has been nested in an additional structure' do
684
+ place = Place.includes(customer: :salesforce).find(1)
685
+ expect(nested_request).to have_been_requested
686
+ expect(place.customer.salesforce.name).to eq 'Steve'
687
+ end
688
+ end
667
689
  end
668
690
 
669
691
  context 'include empty structures' do
@@ -12,10 +12,15 @@ end
12
12
 
13
13
  class LHS::Record
14
14
 
15
- CHILDREN = []
15
+ DESCENDANTS = []
16
16
 
17
17
  def self.inherited(child)
18
- CHILDREN.push(child)
18
+ DESCENDANTS.push(child)
19
+ child.singleton_class.class_eval do
20
+ define_method(:inherited) do |grand_child|
21
+ DESCENDANTS.push(grand_child)
22
+ end
23
+ end
19
24
  super
20
25
  end
21
26
 
@@ -27,9 +32,28 @@ end
27
32
 
28
33
  def reset_lhs
29
34
  LHS::Record::Endpoints.all = {}
30
- LHS::Record::CHILDREN.each do |child|
31
- child.endpoints = [] if !child.name['LHS'] && defined?(child.endpoints)
32
- child.configuration({}) if !child.name['LHS']
35
+ LHS::Record::DESCENDANTS.each do |decendant|
36
+ decendant.endpoints = [] if !decendant.name['LHS'] && defined?(decendant.endpoints)
37
+ decendant.configuration({}) if !decendant.name['LHS']
38
+ end
39
+ end
40
+
41
+ def model_files_to_reload
42
+ Dir.glob(Rails.root.join('app', 'models', '**', '*.rb'))
43
+ end
44
+
45
+ def reload_direct_inheritance
46
+ model_files_to_reload.map do |file|
47
+ next unless File.read(file).match('LHS::Record')
48
+ load file
49
+ file.split('models/').last.gsub('.rb', '').classify
50
+ end.compact
51
+ end
52
+
53
+ def reload_inheriting_records(parents)
54
+ model_files_to_reload.each do |file|
55
+ next if parents.none? { |parent| File.read(file).match(parent) }
56
+ load file
33
57
  end
34
58
  end
35
59
 
@@ -37,9 +61,7 @@ RSpec.configure do |config|
37
61
  config.before do |spec|
38
62
  reset_lhc unless spec.metadata.key?(:reset_before) && spec.metadata[:reset_before] == false
39
63
  reset_lhs unless spec.metadata.key?(:reset_before) && spec.metadata[:reset_before] == false
40
- next unless spec.metadata.key?(:dummy_models) && spec.metadata[:dummy_models] == true
41
- Dir.glob(Rails.root.join('app', 'models', '**', '*.rb')).each do |file|
42
- load file if File.read(file).match('LHS::Record')
43
- end
64
+ next if !spec.metadata.key?(:dummy_models) || spec.metadata[:dummy_models] != true
65
+ reload_inheriting_records(reload_direct_inheritance)
44
66
  end
45
67
  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: 21.2.2
4
+ version: 21.2.3.pre.preload.pre.providers.pre.too.1
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: 2020-03-23 00:00:00.000000000 Z
11
+ date: 2020-03-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activemodel
@@ -381,8 +381,10 @@ files:
381
381
  - spec/dummy/app/mailers/.keep
382
382
  - spec/dummy/app/models/.keep
383
383
  - spec/dummy/app/models/concerns/.keep
384
- - spec/dummy/app/models/record.rb
385
- - spec/dummy/app/models/user.rb
384
+ - spec/dummy/app/models/dummy_customer.rb
385
+ - spec/dummy/app/models/dummy_record.rb
386
+ - spec/dummy/app/models/dummy_user.rb
387
+ - spec/dummy/app/models/providers/customer_system.rb
386
388
  - spec/dummy/app/views/error_handling_with_chains/error.html.erb
387
389
  - spec/dummy/app/views/error_handling_with_chains/show.html.erb
388
390
  - spec/dummy/app/views/form_for.html.erb
@@ -546,9 +548,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
546
548
  version: 2.3.0
547
549
  required_rubygems_version: !ruby/object:Gem::Requirement
548
550
  requirements:
549
- - - ">="
551
+ - - ">"
550
552
  - !ruby/object:Gem::Version
551
- version: '0'
553
+ version: 1.3.1
552
554
  requirements:
553
555
  - Ruby >= 2.3.0
554
556
  rubygems_version: 3.0.6
@@ -598,8 +600,10 @@ test_files:
598
600
  - spec/dummy/app/mailers/.keep
599
601
  - spec/dummy/app/models/.keep
600
602
  - spec/dummy/app/models/concerns/.keep
601
- - spec/dummy/app/models/record.rb
602
- - spec/dummy/app/models/user.rb
603
+ - spec/dummy/app/models/dummy_customer.rb
604
+ - spec/dummy/app/models/dummy_record.rb
605
+ - spec/dummy/app/models/dummy_user.rb
606
+ - spec/dummy/app/models/providers/customer_system.rb
603
607
  - spec/dummy/app/views/error_handling_with_chains/error.html.erb
604
608
  - spec/dummy/app/views/error_handling_with_chains/show.html.erb
605
609
  - spec/dummy/app/views/form_for.html.erb