lib/scrivito/model_library.rb in scrivito_sdk-1.3.0.rc2 vs lib/scrivito/model_library.rb in scrivito_sdk-1.3.0.rc3

- old
+ new

@@ -18,14 +18,15 @@ # # @api public # @see Scrivito.models # def clear_cache - @pages = nil - @widgets = nil - @objs = nil - @paths = nil + @pages = nil + @widgets = nil + @objs = nil + @paths = nil + @require_dependencies = nil end # # Configures which models Scrivito assumes as pages, widgets and objs. # @@ -42,43 +43,35 @@ # @api public # @see Scrivito.models # @return [Scrivito::ClassCollection] available page classes # def pages - @pages ||= load_models('page', Scrivito::BasicObj) + @pages ||= build_class_collection { valid_page_classes } end # # Lists available widget models. # # @api public # @see Scrivito.models # @return [Scrivito::ClassCollection] available widget classes # def widgets - @widgets ||= load_models('widget', Scrivito::BasicWidget) + @widgets ||= build_class_collection { valid_widget_classes } end # # Lists available CMS object models. # # @api public # @see Scrivito.models # @return [Scrivito::ClassCollection] available CMS object classes # def objs - @objs ||= load_obj_models + @objs ||= build_class_collection { BasicObj.descendants.select(&:name) } end - def load_obj_models - base_class = Scrivito::BasicObj - model_classes = load_models_from_paths('obj', base_class) + - load_custom_models('obj', base_class) + - load_custom_models('page', base_class) - ClassCollection.new(model_classes) - end - # # Lists available paths to scan for models. # # @api public # @see Scrivito.models @@ -88,44 +81,45 @@ @paths ||= (autoload_model_paths + custom_paths).map(&:to_s) end private - def load_models(type, base_class) - model_classes = load_models_from_paths(type, base_class) + load_custom_models(type, base_class) - ClassCollection.new(model_classes) + def build_class_collection + require_dependencies + ClassCollection.new(yield) end - def load_models_from_paths(type, base_class) - paths.map { |path| load_models_from_path(type, path, base_class) }.flatten + def valid_widget_classes + BasicWidget.descendants.select { |descendant| valid_widget_class?(descendant) } end - def load_models_from_path(type, path, base_class) - candidates_from_path(type, path).map do |file_path| - load_model(file_path.gsub(path, '').gsub('.rb', '').camelize, base_class) - end + def valid_widget_class?(model_class) + model_class = model_class.to_s + return true if custom_widgets.include?(model_class) + + model_class != 'Widget' && model_class.ends_with?('Widget') end - def candidates_from_path(type, path) - Dir["#{path}/**/*.rb"].select do |file_path| - case type - when 'page' then file_path.ends_with?('page.rb') - when 'widget' then file_path.ends_with?('_widget.rb') - else true - end - end + def valid_page_classes + BasicObj.descendants.select { |descendant| valid_page_class?(descendant) } end - def load_model(class_name, base_class) - model_class = class_name.constantize - model_class if model_class.ancestors.include?(base_class) - rescue LoadError - # Ignore files with invalid models, e.g. empty files. + def valid_page_class?(model_class) + model_class = model_class.to_s + model_class.downcase.ends_with?('page') || custom_pages.include?(model_class) end - def load_custom_models(type, base_class) - send("custom_#{type}s").map do |class_name| - load_model(class_name, base_class) + def require_dependencies + @require_dependencies ||= begin + paths.each do |path| + Dir["#{path}/**/*.rb"].sort.each do |file_path| + begin + require_dependency(file_path) + rescue TypeError => e + raise e if file_path.starts_with?(Rails.root.to_s) + end + end + end end end def autoload_model_paths ActiveSupport::Dependencies.autoload_paths.select do |path|