lib/wcc/contentful.rb in wcc-contentful-0.3.0.pre.rc3 vs lib/wcc/contentful.rb in wcc-contentful-0.3.0
- old
+ new
@@ -10,10 +10,11 @@
require 'wcc/contentful/helpers'
require 'wcc/contentful/services'
require 'wcc/contentful/simple_client'
require 'wcc/contentful/store'
require 'wcc/contentful/content_type_indexer'
+require 'wcc/contentful/model_validators'
require 'wcc/contentful/model'
require 'wcc/contentful/model_methods'
require 'wcc/contentful/model_singleton_methods'
require 'wcc/contentful/model_builder'
@@ -23,12 +24,10 @@
# initializer.
module WCC::Contentful
class << self
# Gets the current configuration, after calling WCC::Contentful.configure
attr_reader :configuration
-
- attr_reader :types
end
# Configures the WCC::Contentful gem to talk to a Contentful space.
# This must be called first in your initializer, before #init! or accessing the
# client.
@@ -51,10 +50,12 @@
# class Page < WCC::Contentful::Model::Page; end
# Page.find_by(slug: 'about-us')
def self.init!
raise ArgumentError, 'Please first call WCC:Contentful.configure' if configuration.nil?
+ @mutex ||= Mutex.new
+
# we want as much as possible the raw JSON from the API so use the management
# client if possible
client = Services.instance.management_client ||
Services.instance.client
@@ -72,8 +73,37 @@
WCC::Contentful::DelayedSyncJob.perform_later
end
WCC::Contentful::ModelBuilder.new(@types).build_models
+ # Extend all model types w/ validation & extra fields
+ @types.each_value do |t|
+ file = File.dirname(__FILE__) + "/contentful/model/#{t.name.underscore}.rb"
+ require file if File.exist?(file)
+ end
+
require_relative 'contentful/client_ext' if defined?(::Contentful)
end
+
+ # Runs validations over the content types returned from the Contentful API.
+ # Validations are configured on predefined model classes using the
+ # `validate_field` directive. Example:
+ # validate_field :top_button, :Link, :optional, link_to: 'menuButton'
+ # This results in a WCC::Contentful::ValidationError
+ # if the 'topButton' field in the 'menu' content type is not a link.
+ def self.validate_models!
+ # Ensure application models are loaded before we validate
+ Dir[Rails.root.join('app/models/**/*.rb')].each { |file| require file } if defined?(Rails)
+
+ content_types = WCC::Contentful::ModelValidators.transform_content_types_for_validation(
+ @content_types
+ )
+ errors = WCC::Contentful::Model.schema.call(content_types)
+ raise WCC::Contentful::ValidationError, errors.errors unless errors.success?
+ end
+
+ # TODO: https://zube.io/watermarkchurch/development/c/2234 init graphql
+ # def self.init_graphql!
+ # require 'wcc/contentful/graphql'
+ # etc...
+ # end
end