lib/wcc/contentful/model.rb in wcc-contentful-0.4.0.pre.rc vs lib/wcc/contentful/model.rb in wcc-contentful-1.0.0.pre.rc1

- old
+ new

@@ -52,19 +52,33 @@ end end @@registry = {} + def self.store(preview = false) + if preview + if preview_store.nil? + raise ArgumentError, + 'You must include a contentful preview token in your WCC::Contentful.configure block' + end + preview_store + else + super() + end + end + # Finds an Entry or Asset by ID in the configured contentful space # and returns an initialized instance of the appropriate model type. # # Makes use of the {WCC::Contentful::Services#store configured store} # to access the Contentful CDN. - def self.find(id, context = nil) - return unless raw = store.find(id) + def self.find(id, options: nil) + options ||= {} + raw = store(options[:preview]) + .find(id, options.except(*WCC::Contentful::ModelMethods::MODEL_LAYER_CONTEXT_KEYS)) - new_from_raw(raw, context) + new_from_raw(raw, options) if raw.present? end # Creates a new initialized instance of the appropriate model type for the # given raw value. The raw value must be the same format as returned from one # of the stores for a given object. @@ -75,10 +89,12 @@ end # Accepts a content type ID as a string and returns the Ruby constant # stored in the registry that represents this content type. def self.resolve_constant(content_type) + raise ArgumentError, 'content_type cannot be nil' unless content_type + const = @@registry[content_type] return const if const const_name = constant_from_content_type(content_type).to_s begin @@ -125,9 +141,27 @@ # Returns the current registry of content type names to constants. def self.registry return {} unless @@registry @@registry.dup.freeze + end + + def self.reload! + registry = self.registry + registry.each do |(content_type, klass)| + const_name = klass.name + begin + const = Object.const_missing(const_name) + register_for_content_type(content_type, klass: const) if const + rescue NameError => e + msg = "Error when reloading constant #{const_name} - #{e}" + if defined?(Rails) && Rails.logger + Rails.logger.error msg + else + puts msg + end + end + end end # Checks if a content type has already been registered to a class and returns # that class. If nil, the generated WCC::Contentful::Model::{content_type} class # will be resolved for this content type.