require "scrivito/errors" # @api public module Scrivito # # Configures the Scrivito SDK. # The configuration keys +tenant+ and +api_key+ _must_ be provided. # # @example # Scrivito.configure do |config| # config.tenant = 'my-tenant-name' # config.api_key = 'secret' # end # # @api public # def self.configure yield Configuration end # # Configures which models Scrivito assumes as pages and widgets. # # @api public # # In order to display a page class selection dialog and a widget class selection dialog, Scrivito # needs to know which page models and widget models are available. That models can be defined in # the Rails application itself or in 3rd-party gems. # # Scrivito will automatically assume all the classes descending from {Scrivito::BasicObj}, whose # class names are ending with +Page+ are pages (e.g. +MyPage+, +HomePage+, +BlogPostPage+ etc.). # It will also automatically assume all the classes descending from {Scrivito::BasicWidget}, whose # class names are ending with +Widget+ are widgets (e.g. +TextWidget+, +ImageWdidget+ etc.). # # Scrivito will recursively scan for such models in all directories from # {Scrivito::ModelLibrary#paths Scrivito.models.paths}, which is an array of strings. # It by default includes the directory +app/models+ of the Rails application. For example it will # find following page and widget models: # # +app/models/my_page.rb+ # # +app/models/my_widget.rb+ # # +app/models/my_namespace/my_other_page.rb+ # # +app/models/my_namespace/my_other_widget.rb+ # # +app/models/my_namespace/my_other_namespace/my_other_special_page.rb+ # # +app/models/my_namespace/my_other_namespace/my_other_special_widget.rb+ # # Also {Scrivito::ModelLibrary#paths Scrivito.models.paths} will include all directories ending # with +app/models+ of any available Rails engine, as long as that directory is included in the # autoload paths of Rails (which is the default). For example it will find following page and # widget models in an engine: # # +/../some_engine/app/models/my_page.rb+ # # +/../some_engine/app/models/my_widget.rb+ # # +/../some_engine/app/models/my_namespace/my_other_page.rb+ # # +/../some_engine/app/models/my_namespace/my_other_widget.rb+ # # +/../some_engine/app/models/my_namespace/my_other_namespace/my_other_special_page.rb+ # # +/../some_engine/app/models/my_namespace/my_other_namespace/my_other_special_widget.rb+ # # You can add custom directories to scan for models and register single page and widget models # with {Scrivito::ModelLibrary#define Scrivito.models.define} (see examples below). # # The loaded pages can be inspected with {Scrivito::ModelLibrary#pages Scrivito.models.pages}, # which will return a {Scrivito::ClassCollection} containing all available pages. # The loaded widgets can be inspected with {Scrivito::ModelLibrary#widgets Scrivito.models.widgets}, # which will return a {Scrivito::ClassCollection} containing all available widgets. # # The scan results are cached. If +Rails.application.config.cache_classes+ is +false+, then the # cache will be cleared on every request. Otherwise the cache will be kept between the requests. # You can clear the cache with {Scrivito::ModelLibrary#clear_cache Scrivito.models.clear_cache}. # # @example Register a custom path # Scrivito.models.define do # paths << Rails.root + 'lib/special_models' # end # # @example Register pages and widgets with inconvenient names # Scrivito.models.define do # page 'MyCrazyPageModel' # page 'MyOtherCrazyPageModel1', 'MyOtherCrazyPageModel2' # # widget 'MyCrazyWidgetModel' # widget 'MyOtherCrazyWidgetModel1', 'MyOtherCrazyWidgetModel2' # end # # @example Iterate over available pages # Scrivito.models.pages.each do |page_class| # puts page_class.name # end # #=> "MyPage" # #=> "MyOtherPage" # # ... # # @example Iterate over available widgets # Scrivito.models.widgets.each do |widget_class| # puts widget_class.name # end # #=> "MyWidget" # #=> "MyOtherWidget" # # ... # def self.models @models ||= ModelLibrary.new end def self.autoload_all_sources source_files = Dir.glob(File.expand_path("../scrivito/*.rb", __FILE__)).map do |file| File.basename(file) end source_files.each do |file| name = file.gsub(".rb", "") autoload name.camelcase, "scrivito/#{name}" end end def self.obj_class_deprecated_message %{ The SDK now allows defining classes and attributes directly in your application source code. Please run the migration tool to have your model migrated automatically in order to use this version of the SDK. See https://scrivito.com/obj-class-migration for details. } end def self.raise_obj_class_deprecated_error raise ScrivitoError, obj_class_deprecated_message end def self.print_obj_class_deprecated_warning Deprecation.warn(obj_class_deprecated_message) end autoload_all_sources end require 'scrivito/sdk_engine' if defined?(Rails)