lib/simple_navigation.rb in simple-navigation-2.7.1 vs lib/simple_navigation.rb in simple-navigation-2.7.2

- old
+ new

@@ -14,11 +14,11 @@ require 'simple_navigation/railtie' if Rails::VERSION::MAJOR == 3 # A plugin for generating a simple navigation. See README for resources on usage instructions. module SimpleNavigation - mattr_accessor :config_files, :config_file_path, :default_renderer, :controller, :template, :explicit_current_navigation, :rails_env, :rails_root, :registered_renderers + mattr_accessor :config_files, :config_file_paths, :default_renderer, :controller, :template, :explicit_current_navigation, :rails_env, :rails_root, :registered_renderers self.config_files = {} self.registered_renderers = { :list => SimpleNavigation::Renderer::List, :links => SimpleNavigation::Renderer::Links, @@ -28,31 +28,56 @@ class << self # Sets the config file path and installs the ControllerMethods in ActionController::Base. def init_rails - SimpleNavigation.config_file_path = SimpleNavigation.default_config_file_path unless SimpleNavigation.config_file_path + SimpleNavigation.config_file_paths ||= [SimpleNavigation.default_config_file_path] ActionController::Base.send(:include, SimpleNavigation::ControllerMethods) end def default_config_file_path File.join(SimpleNavigation.rails_root, 'config') end # Returns true if the config_file for specified context does exist. def config_file?(navigation_context = :default) - File.exists?(config_file_name(navigation_context)) + !!config_file(navigation_context) end + + # Returns the config file for the given navigation context or nil if no matching config file can be found. + # If multiple config_paths are set, it returns the first matching file. + # + def config_file(navigation_context = :default) + config_file_paths.collect { |path| File.join(path, config_file_name(navigation_context)) }.detect {|full_path| File.exists?(full_path)} + end + # Returns the name of the config file for the given navigation_context + def config_file_name(navigation_context = :default) + prefix = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_" + "#{prefix}navigation.rb" + end + + # Returns a nice sentence including all config_paths + def config_file_paths_sentence + self.config_file_paths.to_sentence( + :last_word_connector => ', or ', + :two_words_connector => ' or ' + ) + end + + # Sets the config_file_path + def config_file_path=(path) + self.config_file_paths = [path] + end + # Reads the config_file for the specified navigation_context and stores it for later evaluation. def load_config(navigation_context = :default) - raise "config_file_path is not set!" unless self.config_file_path - raise "Config file '#{config_file_name(navigation_context)}' does not exists!" unless config_file?(navigation_context) + raise "Config file for #{navigation_context} context not found in #{config_file_paths_sentence}!" unless config_file?(navigation_context) if SimpleNavigation.rails_env == 'production' - self.config_files[navigation_context] ||= IO.read(config_file_name(navigation_context)) + self.config_files[navigation_context] ||= IO.read(config_file(navigation_context)) else - self.config_files[navigation_context] = IO.read(config_file_name(navigation_context)) + self.config_files[navigation_context] = IO.read(config_file(navigation_context)) end end def set_template_from(context) SimpleNavigation.controller = extract_controller_from context @@ -72,15 +97,9 @@ end # Returns the ItemContainer that contains the items for the primary navigation def primary_navigation config.primary_navigation - end - - # Returns the path to the config_file for the given navigation_context - def config_file_name(navigation_context = :default) - file_name = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_" - File.join(config_file_path, "#{file_name}navigation.rb") end def explicit_navigation_args self.controller.instance_variable_get(:"@sn_current_navigation_args") end