adding config class, initializer is moved during generation, host app can overwrite things

Oleg committed Oct 14, 2010
commit de331515892d421b6640cbd18f0e82d21278c85e
Showing 7 changed files with 79 additions and 3 deletions
app/views/layouts/cms_admin.html.erb +1 -1
@@ @@ -2,7 +2,7 @@
<html>
<head>
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
- <title>Comfortable Mexican Sofa</title>
+ <title><%= ComfortableMexicanSofa.config.cms_title %></title>
<%= csrf_meta_tag %>
<%= stylesheet_link_tag :cms %>
<%= javascript_include_tag :cms %>
config/initializers/comfortable_mexican_sofa.rb +7 -0
@@ @@ -0,0 +1,7 @@
+ # Comfortable Mexican Sofa initializer. Change defaults to whatever you require
+
+ ComfortableMexicanSofa.configure do |config|
+ config.cms_title = 'ComfortableMexicanSofa'
+ config.authentication = 'CmsAuthentication'
+ config.multiple_sites = false
+ end
\ No newline at end of file
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb +22 -2
@@ @@ -2,7 +2,8 @@ if defined?(Rails) && Rails::VERSION::MAJOR == 3
require File.expand_path('comfortable_mexican_sofa/cms_engine', File.dirname(__FILE__))
end
- [ 'comfortable_mexican_sofa/cms_rails_extensions',
+ [ 'comfortable_mexican_sofa/cms_configuration',
+ 'comfortable_mexican_sofa/cms_rails_extensions',
'comfortable_mexican_sofa/cms_form_builder',
'comfortable_mexican_sofa/cms_acts_as_tree',
'../app/models/cms_block',
@@ @@ -34,6 +35,25 @@ FILE_ICONS = Dir.glob(File.expand_path('public/images/cms/file_icons/*.png', Rai
module ComfortableMexicanSofa
- # TODO
+ class << self
+
+ # Modify CMS configuration
+ # Example:
+ # ComfortableMexicanSofa.configure do |config|
+ # config.authentication = :http_auth
+ # config.http_auth_user = 'username'
+ # config.http_auth_pass = 'password'
+ # end
+ def configure
+ yield configuration
+ end
+
+ # Accessor for ComfortableMexicanSofa::Configuration
+ def configuration
+ @configuration ||= Configuration.new
+ end
+ alias :config :configuration
+
+ end
end
\ No newline at end of file
comfortable_mexican_sofa/cms_configuration.rb b/lib/comfortable_mexican_sofa/cms_configuration.rb +19 -0
@@ @@ -0,0 +1,19 @@
+ class ComfortableMexicanSofa::Configuration
+
+ # Don't like Comfortable Mexican Sofa? Set it to whatever you like. :(
+ attr_accessor :cms_title
+
+ # Module that will handle authentication to access cms-admin area
+ attr_accessor :authentication
+
+ # Enable cms to manage multiple sites
+ attr_accessor :multiple_sites
+
+ # Configuration defaults
+ def initialize
+ @cms_title = 'ComfortableMexicanSofa'
+ @authentication = 'CmsAuthentication'
+ @multiple_sites = false
+ end
+
+ end
\ No newline at end of file
generators/cms_generator.rb b/lib/generators/cms_generator.rb +4 -0
@@ @@ -9,6 +9,10 @@ class CmsGenerator < Rails::Generators::Base
migration_template 'db/migrate/01_create_cms.rb', 'db/migrate/create_cms.rb'
end
+ def generate_initialization
+ copy_file 'config/initializers/comfortable_mexican_sofa.rb', 'config/initializers/comfortable_mexican_sofa.rb'
+ end
+
def generate_public_assets
directory 'public/stylesheets/comfortable_mexican_sofa', 'public/stylesheets/comfortable_mexican_sofa'
directory 'public/javascripts/comfortable_mexican_sofa', 'public/javascripts/comfortable_mexican_sofa'
test/test_helper.rb +9 -0
@@ @@ -6,6 +6,15 @@ class ActiveSupport::TestCase
fixtures :all
include ActionDispatch::TestProcess
+ def setup
+ # resetting default configuration
+ ComfortableMexicanSofa.configure do |config|
+ config.cms_title = 'ComfortableMexicanSofa'
+ config.authentication = 'CmsAuthentication'
+ config.multiple_sites = false
+ end
+ end
+
# Example usage:
# assert_has_errors_on( @record, [:field_1, :field_2] )
# assert_has_errors_on( @record, {:field_1 => 'Message1', :field_2 => 'Message 2'} )
test/unit/cms_configuration_test.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ require File.dirname(__FILE__) + '/../test_helper'
+
+ class CmsConfigurationTest < ActiveSupport::TestCase
+
+ def test_configuration_presense
+ assert config = ComfortableMexicanSofa.configuration
+ assert_equal 'ComfortableMexicanSofa', config.cms_title
+ assert_equal 'CmsAuthentication', config.authentication
+ assert_equal false, config.multiple_sites
+ end
+
+ def test_initialization_overrides
+ ComfortableMexicanSofa.configuration.cms_title = 'New Title'
+ assert_equal 'New Title', ComfortableMexicanSofa.configuration.cms_title
+ end
+
+ end
\ No newline at end of file