wcc-contentful 1.3.0 → 1.3.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9026bfcc1e7745f7d8ecf3fb38928672f02d663d382fd1a3ca953ab33a78cb25
4
- data.tar.gz: 36ec7c7f47fb6e49d565d7ca82c5021ac8f88b46eabfe41754ac5c123ac0004d
3
+ metadata.gz: 3cbb012e1545d674abc930f6513ea342611beb586b2afe5ad5ce703903e5a9cb
4
+ data.tar.gz: da23bc050241c0ae55e1952fc48d892d2b2d768f33d314aef34f4a5f159db585
5
5
  SHA512:
6
- metadata.gz: 633116bd83752074876ccf76265eb64bc6a19a28b9a477bf2026925f0a84977cd651a234f5656b7a657a43e65b3320031e2385ca398bb2b846ab730693105799
7
- data.tar.gz: 32f1f08bac98d42d89a136c0b49d2bb1ee1a72e057326edd50a17834b53b2da7fecbbe1db1aded355d2849bc1d222d4f16d3e721024a3c105f786349ab6548fc
6
+ metadata.gz: e53a8c32ea3687f8e5e8bfaac3a4a15d940e21873c7068c20b26cca92b5ed2876a8f680949f2bf169ebae258a00c0665f63c314888a1c3379b69b37da2be97d5
7
+ data.tar.gz: 24e358dc0f8ec03873ffb27ff6d79c5a64f0fda24b5f66336482ca38f0b4b654227907df96955005fcc4cf04ed811e78070c3688f3c3d0629406666734c8fd93
data/README.md CHANGED
@@ -565,7 +565,10 @@ to multiple spaces within the same ruby process! You just have to create and in
565
565
  The {WCC::Contentful::ModelAPI} concern makes this straightforward. Start by creating your Namespace
566
566
  and including the concern:
567
567
  ```ruby
568
- # app/models/my_second_space.rb
568
+ # lib/my_second_space.rb
569
+
570
+ # Note: This class must be in the "lib" folder in :zeitwerk mode, otherwise Rails 6+ will unload all your constants
571
+ # that were created in the initializer. Your models which subclass this namespace may reside in the app/models directory.
569
572
  class MySecondSpace
570
573
  include WCC::Contentful::ModelAPI
571
574
  end
@@ -587,6 +590,11 @@ MySecondSpace.configure do |config|
587
590
  config.space = ENV['SECOND_CONTENTFUL_SPACE_ID']
588
591
  config.environment = ENV['CONTENTFUL_ENVIRONMENT']
589
592
  end
593
+
594
+ # Ensure that models are reloaded in Rails development mode
595
+ Rails.application.config.to_prepare do
596
+ MySecondSpace.reload!
597
+ end
590
598
  ```
591
599
 
592
600
  Finally, use it:
@@ -614,6 +622,22 @@ sync_engine = MySecondSpace.services.sync_engine
614
622
  Note that the above services are not accessible on {WCC::Contentful::Services.instance}
615
623
  or via the {WCC::Contentful::ServiceAccessors}.
616
624
 
625
+ #### Important Note when using Zeitwerk with Rails 6+
626
+ When using Rails >= 6 with `config.autoloader = :zeitwerk`, Rails will remove any models defined in `app/models` after
627
+ initialization and then load them again when they are referenced. If you `include WCC::Contentful::ModelAPI` in a class
628
+ defined inside the `app` directory, this will have the effect of deleting all configuration that was set in the initializer
629
+ as well as the constants generated from your schema.
630
+ This will result in one of two errors:
631
+
632
+ * `NameError (uninitialized constant MySecondSpace::MyContentType)`
633
+ if you try to reference a subclass such as `MyContentType < MySecondSpace::MyContentType`
634
+ * `ArgumentError (Not yet configured!)`
635
+ if you try to `MySecondSpace.find('xxxx')` to load an Entry or Asset
636
+
637
+ The solution is to have your secondary namespace in a folder which is not in the `autoload_paths`.
638
+ We suggest using `lib`, which will work so long as you have not added the `lib` folder to the `autoload_paths` as some
639
+ uninformed StackOverflow answers suggest you do.
640
+
617
641
  ### Using a sync store with a second space
618
642
 
619
643
  If you use something other than the CDNAdapter with your second space, you will
@@ -33,7 +33,7 @@ module WCC::Contentful::ActiveRecordShim
33
33
 
34
34
  class_methods do
35
35
  def model_name
36
- WCC::Contentful::Helpers.constant_from_content_type(content_type)
36
+ ActiveModel::Name.new(self, nil, WCC::Contentful::Helpers.constant_from_content_type(content_type))
37
37
  end
38
38
 
39
39
  def const_get(name)
@@ -46,7 +46,7 @@ module WCC::Contentful::ActiveRecordShim
46
46
  end
47
47
 
48
48
  def table_name
49
- model_name.tableize
49
+ model_name.plural.tableize
50
50
  end
51
51
 
52
52
  def unscoped
@@ -96,7 +96,7 @@ module WCC::Contentful::ModelAPI
96
96
  loop do
97
97
  begin
98
98
  # The app may have defined a model and we haven't loaded it yet
99
- const = parent.const_missing(const_name)
99
+ const = parent.const_get(const_name)
100
100
  return const if const && const < self
101
101
  rescue NameError => e
102
102
  raise e unless e.message =~ /uninitialized constant (.+::)*#{const_name}$/
@@ -156,7 +156,7 @@ module WCC::Contentful::ModelAPI
156
156
  const_name = klass.name
157
157
  begin
158
158
  # the const_name is fully qualified so search from root
159
- const = Object.const_missing(const_name)
159
+ const = Object.const_get(const_name)
160
160
  register_for_content_type(content_type, klass: const) if const
161
161
  rescue NameError => e
162
162
  msg = "Error when reloading constant #{const_name} - #{e}"
@@ -2,6 +2,6 @@
2
2
 
3
3
  module WCC
4
4
  module Contentful
5
- VERSION = '1.3.0'
5
+ VERSION = '1.3.2'
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wcc-contentful
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Watermark Dev
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-08-19 00:00:00.000000000 Z
11
+ date: 2023-04-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: byebug
@@ -493,7 +493,7 @@ licenses:
493
493
  metadata:
494
494
  documentation_uri: https://watermarkchurch.github.io/wcc-contentful/1.3/wcc-contentful
495
495
  rubygems_mfa_required: 'true'
496
- post_install_message:
496
+ post_install_message:
497
497
  rdoc_options: []
498
498
  require_paths:
499
499
  - lib
@@ -508,8 +508,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
508
508
  - !ruby/object:Gem::Version
509
509
  version: '0'
510
510
  requirements: []
511
- rubygems_version: 3.1.6
512
- signing_key:
511
+ rubygems_version: 3.3.7
512
+ signing_key:
513
513
  specification_version: 4
514
514
  summary: '[![Gem Version](https://badge.fury.io/rb/wcc-contentful.svg)](https://rubygems.org/gems/wcc-contentful)
515
515
  [![Build Status](https://circleci.com/gh/watermarkchurch/wcc-contentful.svg?style=svg)](https://circleci.com/gh/watermarkchurch/wcc-contentful)
@@ -766,14 +766,19 @@ summary: '[![Gem Version](https://badge.fury.io/rb/wcc-contentful.svg)](https://
766
766
  step. However the gem is also capable of connecting to multiple spaces within the
767
767
  same ruby process! You just have to create and initialize a namespace. The {WCC::Contentful::ModelAPI}
768
768
  concern makes this straightforward. Start by creating your Namespace and including
769
- the concern: ```ruby # app/models/my_second_space.rb class MySecondSpace include
770
- WCC::Contentful::ModelAPI end # app/models/other_page.rb class OtherPage < MySecondSpace::Page
771
- end ``` Then configure it in an initializer: ```ruby # config/initializers/my_second_space.rb
769
+ the concern: ```ruby # lib/my_second_space.rb # Note: This class must be in the
770
+ "lib" folder in :zeitwerk mode, otherwise Rails 6+ will unload all your constants
771
+ # that were created in the initializer. Your models which subclass this namespace
772
+ may reside in the app/models directory. class MySecondSpace include WCC::Contentful::ModelAPI
773
+ end # app/models/other_page.rb class OtherPage < MySecondSpace::Page end ``` Then
774
+ configure it in an initializer: ```ruby # config/initializers/my_second_space.rb
772
775
  MySecondSpace.configure do |config| # Make sure to point to a different schema file
773
776
  from your first space! config.schema_file = Rails.root.join(''db/second-contentful-schema.json'') config.access_token
774
777
  = ENV[''SECOND_CONTENTFUL_ACCESS_TOKEN''] config.preview_token = ENV[''SECOND_CONTENTFUL_PREVIEW_ACCESS_TOKEN'']
775
778
  config.space = ENV[''SECOND_CONTENTFUL_SPACE_ID''] config.environment = ENV[''CONTENTFUL_ENVIRONMENT'']
776
- end ``` Finally, use it: ```ruby OtherPage.find(''1234'') # GET https://cdn.contentful.com/spaces/other-space/environments/other-env/entries/1234
779
+ end # Ensure that models are reloaded in Rails development mode Rails.application.config.to_prepare
780
+ do MySecondSpace.reload! end ``` Finally, use it: ```ruby OtherPage.find(''1234'')
781
+ # GET https://cdn.contentful.com/spaces/other-space/environments/other-env/entries/1234
777
782
  # => #<OtherPage:0x0000000005c71a78 @created_at=2018-04-16 18:41:17 UTC...> Page.find(''1234'')
778
783
  # GET https://cdn.contentful.com/spaces/first-space/environments/first-env/entries/1234
779
784
  # => #<Page:0x0000000001271b70 @created_at=2018-04-15 12:02:14 UTC...> ``` The
@@ -783,14 +788,27 @@ summary: '[![Gem Version](https://badge.fury.io/rb/wcc-contentful.svg)](https://
783
788
  preview_client = MySecondSpace.services.preview_client # => #<WCC::Contentful::SimpleClient::Preview:0x00007f888ccafa00
784
789
  sync_engine = MySecondSpace.services.sync_engine # => #<WCC::Contentful::SyncEngine:0x00007f88960b6b40
785
790
  ``` Note that the above services are not accessible on {WCC::Contentful::Services.instance}
786
- or via the {WCC::Contentful::ServiceAccessors}. ### Using a sync store with a second
787
- space If you use something other than the CDNAdapter with your second space, you
788
- will need to find a way to trigger `MySecondSpace.services.sync_engine.next` to
789
- keep it up-to-date. The {WCC::Contentful::Engine} will only manage the global SyncEngine
790
- configured by the global {WCC::Contentful.configure}. The easiest way to do this
791
- is to set up your own Rails route to respond to Contentful webhooks and then configure
792
- the second Contentful space to send webhooks to this route. You could also do this
793
- by triggering it periodically in a background job using Sidekiq and [sidekiq-scheduler](https://github.com/Moove-it/sidekiq-scheduler). ##
791
+ or via the {WCC::Contentful::ServiceAccessors}. #### Important Note when using
792
+ Zeitwerk with Rails 6+ When using Rails >= 6 with `config.autoloader = :zeitwerk`,
793
+ Rails will remove any models defined in `app/models` after initialization and then
794
+ load them again when they are referenced. If you `include WCC::Contentful::ModelAPI`
795
+ in a class defined inside the `app` directory, this will have the effect of deleting
796
+ all configuration that was set in the initializer as well as the constants generated
797
+ from your schema. This will result in one of two errors: * `NameError (uninitialized
798
+ constant MySecondSpace::MyContentType)` if you try to reference a subclass such
799
+ as `MyContentType < MySecondSpace::MyContentType` * `ArgumentError (Not yet configured!)`
800
+ if you try to `MySecondSpace.find(''xxxx'')` to load an Entry or Asset The solution
801
+ is to have your secondary namespace in a folder which is not in the `autoload_paths`.
802
+ We suggest using `lib`, which will work so long as you have not added the `lib`
803
+ folder to the `autoload_paths` as some uninformed StackOverflow answers suggest
804
+ you do. ### Using a sync store with a second space If you use something other
805
+ than the CDNAdapter with your second space, you will need to find a way to trigger
806
+ `MySecondSpace.services.sync_engine.next` to keep it up-to-date. The {WCC::Contentful::Engine}
807
+ will only manage the global SyncEngine configured by the global {WCC::Contentful.configure}. The
808
+ easiest way to do this is to set up your own Rails route to respond to Contentful
809
+ webhooks and then configure the second Contentful space to send webhooks to this
810
+ route. You could also do this by triggering it periodically in a background job
811
+ using Sidekiq and [sidekiq-scheduler](https://github.com/Moove-it/sidekiq-scheduler). ##
794
812
  Development After checking out the repo, run `bin/setup` to install dependencies.
795
813
  Then, run `bundle exec rspec` to run the tests. You can also run `bin/console` for
796
814
  an interactive prompt that will allow you to experiment. ## Contributing Bug reports