require "helpful_configuration"
require "rails_connector/cms_base_model"
require "rails_connector/blob"
module RailsConnector
# RailsConnector::Configuration is used to en/disable and initialize addons.
class Configuration
DEFAULT_MODE = :live
#
# Default fields which the +DefaultUserController+ will store in the session.
#
DEFAULT_STORE_USER_ATTRS_IN_SESSION = [:login, :first_name, :last_name, :email, :id]
@features = {}
class << self
# Automatically generate editmarkers when rendering liquid templates in editor mode.
attr_accessor :auto_liquid_editmarkers
# there are three available modes for the rails connector:
# live (show released contents only),
# preview (show edited contents)
# editor (show edited contents and editor interface (e.g. edit markers))
# Default mode is live.
attr_accessor :mode
# default options for SearchRequest
attr_writer :search_options
def search_options
@search_options || local_config_file["search"].symbolize_keys
end
def mode=(new_mode) #:nodoc:#
new_mode = new_mode.to_sym
raise ArgumentError, "Unknown Mode #{new_mode}" unless [:editor, :preview, :live].include?(new_mode)
@mode = new_mode
end
def mode #:nodoc:#
@mode || DEFAULT_MODE
end
#
# Sets the array of fields which the +DefaultUserController+ will store in the session.
# Defaults to +DEFAULT_STORE_USER_ATTRS_IN_SESSION+.
#
attr_writer :store_user_attrs_in_session
#
# Returns fields which the +DefaultUserController+ stores in the session.
#
def store_user_attrs_in_session
@store_user_attrs_in_session || DEFAULT_STORE_USER_ATTRS_IN_SESSION
end
def editor_interface_enabled? #:nodoc:#
mode == :editor
end
def use_edited_content? #:nodoc:#
mode == :preview || mode == :editor
end
def register_features(*features) #:nodoc:
features.each do |f|
@features.merge!(f.to_sym => false)
end
end
alias :register_feature :register_features
def registered_features #:nodoc:
@features.keys
end
# Rails Connector Addons can be enabled as follows (in config/rails_connector.rb):
#
# RailsConnector::Configuration.enable(
# :search,
# :time_machine,
# :pdf_generator,
# :crm
# )
#
def enable(*features)
features.each do |f|
assert_feature_is_known(f)
@features[f.to_sym] = true
end
initialize_addon_mixins
end
# Returns true if +feature+ is enabled, else false.
def enabled?(feature)
assert_feature_is_known(feature)
@features[feature.to_sym] == true
end
# Configures your CMS instance name.
#
# Example call as to be used in rails_connector.rb:
# RailsConnector::Configuration.instance_name = 'internet'
def instance_name=(name)
RailsConnector::CmsBaseModel.instance_name = name
end
def after_initialize #:nodoc:
enable_authentication
# Hier muss explizit der Namespace verwendet werden, da diese Methode von Rails
# während der Initialisierung aufgerufen wird.
::RailsConnector::Obj.configure_for_content(
::RailsConnector::Configuration.use_edited_content? ? :edited : :released
)
end
def to_prepare #:nodoc:
unless Rails.configuration.cache_classes
after_initialize
NamedLink.reset_cache
initialize_addon_mixins
end
end
def configure_cms_database
if RailsConnector::CmsBaseModel.superclass.to_s == "ActiveRecord::Base"
CmsBaseModel.configure_database("cms")
else
CmsBaseModel.configure_database(local_config_file["cms_database"])
Blob.configure(local_config_file["cms_blob_storage"])
end
end
attr_accessor :choose_homepage_callback #:nodoc:
# Configure a callback to be invoked when the rails connector delivers the homepage.
# The given callback will receive the rack env
# and must return an Obj to be used as the homepage.
# If no callback is configured, Obj.homepage will be used as the default.
def choose_homepage(&block)
self.choose_homepage_callback = block
end
def license #:nodoc:
license_file.read
end
def license_file #:nodoc:
@license_file.presence || Rails.root + "config" + "license.xml"
end
def license_file=(license_file) #:nodoc:
@license_file = license_file.presence && Pathname(license_file)
end
def cms_routes(*args) #:nodoc:
raise <<-EOS.gsub(/\s+/, ' ')
Calling RailsConnector::Configuration.cms_routes is not needed anymore.
Please remove it from config/routes.rb
EOS
end
def use_x_sendfile=(value) #:nodoc:
raise 'Configuration.use_x_sendfile is now available as Rails configuration:'\
' config.action_dispatch.x_sendfile_header = "X-Sendfile"'
end
def initialize_addon_mixins #:nodoc:
::ApplicationController.__send__(:helper, :cms)
if enabled?(:search)
require "rails_connector/ses"
RailsConnector::SES.enable
end
RailsConnector::Crm.enable if enabled?(:crm)
::Obj.__send__(:include, RailsConnector::Syndicateable) if enabled?(:rss)
::Obj.__send__(:include, RailsConnector::Commentable) if enabled?(:comments)
::Obj.__send__(:include, RailsConnector::Rateable) if enabled?(:ratings)
end
def local_config_file #:nodoc:
@local_config_file ||= read_local_config_file
end
def local_config_file_name #:nodoc:
(Rails.root + "config/rails_connector.yml").expand_path
end
# for test purposes only
def reset_local_config_file_cache #:nodoc:
@local_config_file = nil
end
protected
def read_local_config_file
contents = YAML.load_file(local_config_file_name) if File.exists?(local_config_file_name)
contents ||= {}
config = HelpfulConfiguration.new(
contents,
local_config_file_name
)
config.explain(
"cms_database",
"a hash of options, including type, that specify the database configuration"
)
config.explain(
"cms_blob_storage",
"a hash of options, including type, that specify the cms blob storage configuration"
)
config.explain(
"search",
"a hash of options that specify access to the search server"
)
config
end
def assert_feature_is_known(f) #:nodoc:
raise ArgumentError, "unknown feature: #{f.inspect}" unless @features.keys.include?(f.to_sym)
end
def enable_authentication #:nodoc:
# Wenn das OMC-Features an ist, dann braucht man keine Standardimplementierung
# für die Authentifizierung.
unless enabled?(:crm)
::ApplicationController.__send__(:include, RailsConnector::Authenticable)
end
end
end
# defaults
self.auto_liquid_editmarkers = true
register_features(
:search, :time_machine, :pdf_generator, :rss, :comments, :ratings,
:crm, :seo_sitemap, :google_analytics
)
end
class ConfigurationError < StandardError # :nodoc:
end
end