single site / multi sites option in progress + integrating bushi.do + revert back to mongoid rc.7

This commit is contained in:
dinedine 2011-04-04 01:59:41 +02:00
parent 551d8a5761
commit c0a5a4b443
21 changed files with 196 additions and 218 deletions

View File

@ -7,7 +7,7 @@ gem 'rails', '>= 3.0.5'
gem 'warden'
gem 'devise', '= 1.1.3'
gem 'mongoid', '~> 2.0.0.rc.7'
gem 'mongoid', '2.0.0.rc.7'
gem 'bson_ext', '~> 1.2.1'
gem 'locomotive_mongoid_acts_as_tree', '0.1.5.5', :require => 'mongoid_acts_as_tree'
gem 'will_paginate'

View File

@ -177,7 +177,7 @@ GEM
mimetype-fu (0.1.2)
mongo (1.2.4)
bson (>= 1.2.4)
mongoid (2.0.0)
mongoid (2.0.0.rc.7)
activemodel (~> 3.0)
mongo (~> 1.2)
tzinfo (~> 0.3.22)
@ -256,7 +256,7 @@ GEM
treetop (1.4.9)
polyglot (>= 0.3.1)
trollop (1.16.2)
tzinfo (0.3.25)
tzinfo (0.3.26)
unicorn (3.5.0)
kgio (~> 2.3)
rack
@ -299,7 +299,7 @@ DEPENDENCIES
locomotive_mongoid_acts_as_tree (= 0.1.5.5)
mimetype-fu
mocha!
mongoid (~> 2.0.0.rc.7)
mongoid (= 2.0.0.rc.7)
pickle
rails (>= 3.0.5)
rmagick (= 2.12.2)

View File

@ -8,7 +8,7 @@ module Admin::SitesHelper
def main_site_url(site = current_site, options = {})
# TODO: to be refactored
if multi_sites_enabled?
if multi_sites?
url = "http://#{site.subdomain}.#{Locomotive.config.domain}"
url += ":#{request.port}" if request.port != 80
else
@ -27,15 +27,15 @@ module Admin::SitesHelper
end
def manage_subdomain_or_domains?
Locomotive.config.manage_subdomain || Locomotive.config.manage_domains
Locomotive.config.manage_subdomain? || Locomotive.config.manage_domains?
end
def manage_domains?
Locomotive.config.manage_domains
Locomotive.config.manage_domains?
end
def multi_sites_enabled?
Locomotive.multi_sites_enabled?
def multi_sites?
Locomotive.config.multi_sites?
end
end

View File

@ -2,35 +2,35 @@ module Extensions
module Site
module SubdomainDomains
unloadable
def enable_subdomain_n_domains_if_multi_sites
if Locomotive.config.multi_sites? || Locomotive.config.manage_subdomain_n_domains?
extend ActiveSupport::Concern
## fields ##
field :subdomain
field :domains, :type => Array, :default => []
included do
## indexes
index :domains
## fields ##
field :subdomain
field :domains, :type => Array, :default => []
## validations ##
validates_presence_of :subdomain
validates_uniqueness_of :subdomain
validates_exclusion_of :subdomain, :in => Locomotive.config.multi_sites.reserved_subdomains
validates_format_of :subdomain, :with => Locomotive::Regexps::SUBDOMAIN, :allow_blank => true
validate :domains_must_be_valid_and_unique
## indexes
index :domains
## callbacks ##
before_save :add_subdomain_to_domains
## validations ##
validates_presence_of :subdomain
validates_uniqueness_of :subdomain
validates_exclusion_of :subdomain, :in => Locomotive.config.multi_sites.reserved_subdomains
validates_format_of :subdomain, :with => Locomotive::Regexps::SUBDOMAIN, :allow_blank => true
validate :domains_must_be_valid_and_unique
## named scopes ##
scope :match_domain, lambda { |domain| { :any_in => { :domains => [*domain] } } }
scope :match_domain_with_exclusion_of, lambda { |domain, site|
{ :any_in => { :domains => [*domain] }, :where => { :_id.ne => site.id } }
}
## callbacks ##
before_save :add_subdomain_to_domains
## named scopes ##
scope :match_domain, lambda { |domain| { :any_in => { :domains => [*domain] } } }
scope :match_domain_with_exclusion_of, lambda { |domain, site|
{ :any_in => { :domains => [*domain] }, :where => { :_id.ne => site.id } }
}
send :include, InstanceMethods
end
end
module InstanceMethods
@ -53,7 +53,7 @@ module Extensions
end
def full_subdomain
"#{self.subdomain}.#{Locomotive.config.default_domain}"
"#{self.subdomain}.#{Locomotive.config.domain}"
end
protected

View File

@ -2,6 +2,9 @@ class Site
include Locomotive::Mongoid::Document
## Extensions ##
extend Extensions::Site::SubdomainDomains
## fields ##
field :name
field :meta_keywords
@ -22,6 +25,9 @@ class Site
after_create :create_default_pages!
after_destroy :destroy_pages
## behaviours ##
enable_subdomain_n_domains_if_multi_sites
## methods ##
def all_pages_in_once

View File

@ -6,7 +6,7 @@
- content_for :submenu do
= render 'admin/shared/menu/settings'
- if multi_sites_enabled?
- if multi_sites?
- content_for :buttons do
= admin_button_tag t('.new_site'), new_admin_site_url, :class => 'new'

View File

@ -16,8 +16,7 @@ module Locomotive
# Add additional load paths for your own custom dirs
# config.load_paths += %W( #{config.root}/extras )
# config.autoload_paths += %W( #{config.root}/app/models/extensions #{config.root}/app/models/extensions/site #{config.root}/app/models/extensions/page #{config.root}/app/models/extensions/asset)
# config.autoload_paths += %W( #{config.root}/app/models/extensions/site )
config.autoload_paths += %W( #{config.root}/app/models/extensions #{config.root}/app/models/extensions/site #{config.root}/app/models/extensions/page #{config.root}/app/models/extensions/asset)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named

View File

@ -1,5 +1,4 @@
require File.dirname(__FILE__) + '/../../lib/locomotive.rb'
# require_or_load 'extensions/site/subdomain_domains'
Locomotive.configure do |config|
@ -61,64 +60,4 @@ Locomotive.configure do |config|
# config.mailer_sender = 'support'
# # => 'support@heroku.com' (Heroku), 'support@bushi.do' (Bushido), 'support@example.com' (Dev) or 'support@<your_hosting_platform>' (Multi-sites)
config.mailer_sender = 'support'
# ===================================================
# do |deployment|
#
# # the possible options are: server, heroku, bushido or auto (default)
# # if you select 'auto', Locomotive will look after specific ENV variables to check
# # the matching platform (Heroku and Bushido set their own ENV variable).
# deployment.target = :auto
#
# # in case you select heroku, it requires your Heroku credentials in order to manage domains.
# # If you do not want to store the credentials here, you can add them directly into the Heroku ENV
# # (see Heroku documentation)
# # deployment.heroku = {
# # :login => '<your_heroku_login>'
# # :password => '<your_heroku_password>'
# # }
# end
#
# # define the domain name used in production.cn
# deployment.domain = 'example.com'
# set the default domain name used in development/test. By default, 'example.com' is used.
# config.local_domain = 'example.com'
# Locomotive can serve one single site or many. The "many sites" option requires more constraints for
# the domain name (see config.domain for more explanation)
#
# Ex:
# config.site_mode = :single
# or
# config.site_mode = :many
# config.site_mode = :single
# if not defined, locomotive will use example.com as main domain name. Remove prefix www from your domain name.
# Ex:
# config.default_domain = Rails.env.production? ? 'mydomain.com' : 'example.com'
#
# If you use locomotive for a single site in Heroku, use "heroku.com" as default domain name.bzc,
# tell if the application is hosted on Bushido.
# If enabled, there's no further configuration needed.
# Bushido will take care of eveything
#
# Ex:
# config.bushido = true
# config.bushido = ENV['HOSTING_PLATFORM'] == 'bushido'
# Locomotive uses the DelayedJob gem for the theme import module.
# In case you want to deploy to Heroku, you will have to pay for an extra dyno.
# If you do not mind about importing theme without DelayedJob, disable it.
# config.delayed_job = false
#
# # default locale (for now, only en, de, fr and pt-BR are supported)
# config.default_locale = :en
#
# # Configure the e-mail address which will be shown in the DeviseMailer, NotificationMailer, ...etc
# config.mailer_sender = ENV['BUSHIDO_DOMAIN'] ? "support@#{ENV['BUSHIDO_DOMAIN']}" : 'support@example.com'
end

View File

@ -38,11 +38,10 @@ Capybara.javascript_driver = :selenium
# of your scenarios, as this makes it hard to discover errors in your application.
ActionController::Base.allow_rescue = false
Locomotive.configure do |config|
config.default_domain = 'example.com'
end
Capybara.default_host = 'test.example.com'
# Capybara.app_host = 'http://test.example.com'
require File.expand_path(File.dirname(__FILE__) + '/../../spec/support/carrierwave')
require File.expand_path(File.dirname(__FILE__) + '/../../spec/support/carrierwave')
require File.expand_path(File.dirname(__FILE__) + '/../../spec/support/locomotive')
Locomotive.configure_for_test

View File

@ -44,8 +44,6 @@ module Locomotive
end
def self.after_configure
# ActionMailer::Base.default_url_options[:host] = self.config.default_domain + (Rails.env.development? ? ':3000' : '')
# multi sites support
self.configure_multi_sites
@ -61,7 +59,7 @@ module Locomotive
:key => self.config.cookie_key
}
self.define_various_helpers
self.define_subdomain_and_domains_options
# Load all the dynamic classes (custom fields)
begin
@ -73,41 +71,33 @@ module Locomotive
end
def self.configure_multi_sites
if self.multi_sites_enabled?
if self.config.multi_sites?
domain_name = self.config.multi_sites.domain
raise '[Error] Locomotive needs a domain name when used as a multi sites platform' if domain_name.blank?
# Site.send :include, Extensions::Site::SubdomainDomains
self.config.domain = domain_name
end
end
def self.configure_hosting
if Rails.env.production?
# Heroku support
self.enable_heroku if self.heroku?
# Heroku support
self.enable_heroku if self.heroku?
# Bushido support
self.enable_bushido if self.bushido?
end
# Bushido support
self.enable_bushido if self.bushido?
end
def self.define_various_helpers
if self.multi_sites_enabled?
def self.define_subdomain_and_domains_options
if self.config.multi_sites?
self.config.manage_subdomain = self.config.manage_domains = true
else
# FIXME: (Did) modify the code below if Locomotive handles a new hosting solution
# Note: (Did) modify the code below if Locomotive handles a new hosting solution
self.config.manage_domains = self.heroku? || self.bushido?
self.config.manage_subdomain = self.bushido?
end
end
def self.multi_sites_enabled?
self.config.multi_sites != false
end
def self.logger(message)
if self.config.enable_logs == true
Rails.logger.info(message)

View File

@ -2,21 +2,24 @@ module Locomotive
class Configuration
@@defaults = {
:name => 'LocomotiveApp',
:domain => 'example.com',
# :multi_sites => false,
# :default_domain => 'example.com',
# :reserved_subdomains => %w{www admin email blog webmail mail support help site sites},
# :forbidden_paths => %w{layouts snippets stylesheets javascripts assets admin system api},
:reserved_slugs => %w{stylesheets javascripts assets admin images api pages edit},
:locales => %w{en de fr pt-BR},
:cookie_key => '_locomotive_session',
:enable_logs => false,
# :heroku => false,
# :bushido => false,
:delayed_job => true,
:default_locale => :en,
:mailer_sender => 'support' #support@example.com'
:name => 'LocomotiveApp',
:domain => 'example.com',
# :multi_sites => false,
# :default_domain => 'example.com',
# :reserved_subdomains => %w{www admin email blog webmail mail support help site sites},
# :forbidden_paths => %w{layouts snippets stylesheets javascripts assets admin system api},
:reserved_slugs => %w{stylesheets javascripts assets admin images api pages edit},
:locales => %w{en de fr pt-BR},
:cookie_key => '_locomotive_session',
:enable_logs => false,
# :heroku => false,
# :bushido => false,
:hosting => :auto,
:delayed_job => true,
:default_locale => :en,
:mailer_sender => 'support', #support@example.com'
:manage_subdomain => false,
:manage_manage_domains => false
}
cattr_accessor :settings
@ -29,6 +32,22 @@ module Locomotive
@@settings
end
def multi_sites?
self.multi_sites != false
end
def manage_subdomain?
self.manage_subdomain == true
end
def manage_domains?
self.manage_domains == true
end
def manage_subdomain_n_domains?
self.manage_subdomain? && self.manage_domains?
end
def method_missing(name, *args, &block)
self.settings.send(name, *args, &block)
end

View File

@ -24,14 +24,13 @@ module Locomotive
def enable_bushido
self.config.domain = ENV['APP_TLD']
self.enhance_site_model
self.enhance_site_model_with_bushido
self.bushido_domains = ::Bushido::App.domains
self.bushido_subdomain = ::Bushido::App.subdomain
end
def enhance_site_model
Site.send :include, Extensions::Site::SubdomainDomains
def enhance_site_model_with_bushido
Site.send :include, Locomotive::Hosting::Bushido::CustomDomain
end

View File

@ -23,14 +23,16 @@ module Locomotive
end
def enable_heroku
# raise 'Heroku application name is mandatory' if self.config.heroku[:name].blank?
self.config.domain = 'heroku.com'
self.config.heroku ||= {}
self.config.heroku[:name] = ENV['APP_NAME']
raise 'Heroku application name is mandatory' if self.config.heroku[:name].blank?
self.open_heroku_connection
self.enhance_site_model
self.enhance_site_model_with_heroku
# "cache" domains for better performance
self.heroku_domains = self.heroku_connection.list_domains(self.config.heroku[:name]).collect { |h| h[:domain] }
@ -38,13 +40,12 @@ module Locomotive
def open_heroku_connection
login = self.config.heroku[:login] || ENV['HEROKU_LOGIN']
password = self.config.heroku[:password] rescue ENV['HEROKU_PASSWORD']
password = self.config.heroku[:password] || ENV['HEROKU_PASSWORD']
self.heroku_connection = ::Heroku::Client.new(login, password)
end
def enhance_site_model
Site.send :include, Extensions::Site::SubdomainDomains
def enhance_site_model_with_heroku
Site.send :include, Locomotive::Hosting::Heroku::CustomDomain
end

View File

@ -6,7 +6,6 @@ module Locomotive
extend ActiveSupport::Concern
included do
after_save :add_heroku_domains
after_destroy :remove_heroku_domains

View File

@ -3,7 +3,7 @@ module Locomotive
class DefaultConstraint
def self.matches?(request)
if Locomotive.multi_sites_enabled?
if Locomotive.config.multi_sites?
domain, subdomain = domain_and_subdomain(request)
subdomain = 'www' if subdomain.blank?

View File

@ -19,7 +19,7 @@ module Locomotive
def fetch_site
Locomotive.logger "[fetch site] host = #{request.host} / #{request.env['HTTP_HOST']}"
if Locomotive.multi_sites_enabled?
if Locomotive.config.multi_sites?
@current_site ||= Site.match_domain(request.host).first
else
@current_site ||= Site.first

View File

@ -20,7 +20,7 @@ Gem::Specification.new do |s|
s.add_dependency "rails", ">= 3.0.5"
s.add_dependency "warden"
s.add_dependency "devise", "1.1.3"
s.add_dependency "mongoid", "~> 2.0.0.rc.7"
s.add_dependency "mongoid", "2.0.0.rc.7"
s.add_dependency "bson_ext", "~> 1.2.0"
s.add_dependency "locomotive_mongoid_acts_as_tree", "0.1.5.5"
s.add_dependency "will_paginate"

View File

@ -8,99 +8,106 @@ describe 'Heroku support' do
end
context '#loaded' do
it 'has method to enable heroku' do
Locomotive.respond_to?(:enable_heroku).should be_true
end
it 'tells heroku is disabled' do
Locomotive.heroku?.should be_false
end
it 'does not add instance methods to Site' do
Site.instance_methods.include?(:add_heroku_domains).should be_false
Site.instance_methods.include?(:remove_heroku_domains).should be_false
end
end
context '#disabled' do
before(:each) do
Locomotive.configure do |config|
config.heroku = false
config.hosting = :none
end
end
it 'has a nil connection' do
Locomotive.heroku_connection.should be_nil
end
it 'tells heroku is disabled' do
Locomotive.heroku?.should be_false
end
it 'does not add methods to Site' do
Site.instance_methods.include?(:add_heroku_domains).should be_false
Site.instance_methods.include?(:remove_heroku_domains).should be_false
end
end
context '#enabled' do
it 'tells heroku is disabled' do
it 'tells heroku is enabled from ENV' do
ENV['HEROKU_SLUG'] = 'test'
Locomotive.config.hosting = :auto
Locomotive.heroku?.should be_true
end
it 'tells heroku is enabled when forcing it' do
configure_locomotive_with_heroku
Locomotive.heroku?.should be_true
end
it 'raises an exception if no app name is given' do
lambda {
configure_locomotive_with_heroku(:name => nil)
}.should raise_error
end
context 'dealing with heroku connection' do
it 'opens a heroku connection with provided credentials' do
configure_locomotive_with_heroku
Locomotive.heroku_connection.user.should == 'john@doe.net'
Locomotive.heroku_connection.password.should == 'easyone'
end
it 'opens a heroku connection with env credentials' do
ENV['HEROKU_LOGIN'] = 'john@doe.net'; ENV['HEROKU_PASSWORD'] = 'easyone'
Locomotive.configure { |config| config.heroku = true }
::Heroku::Client.any_instance.stubs(:list_domains).returns([])
ENV['HEROKU_LOGIN'] = 'john@doe.net'; ENV['HEROKU_PASSWORD'] = 'easyone'; ENV['APP_NAME'] = 'test'
Locomotive.configure { |config| config.hosting = :heroku; config.heroku = {} }
Locomotive.heroku_connection.user.should == 'john@doe.net'
Locomotive.heroku_connection.password.should == 'easyone'
end
end
context 'enhancing site' do
before(:each) do
configure_locomotive_with_heroku
(@site = Factory.build(:site)).stubs(:valid?).returns(true)
end
it 'calls add_heroku_domains after saving a site' do
@site.expects(:add_heroku_domains)
@site.save
end
it 'calls remove_heroku_domains after saving a site' do
@site.expects(:remove_heroku_domains)
@site.destroy
end
context 'adding domain' do
it 'does not add new domain if no delta' do
Locomotive.heroku_connection.expects(:add_domain).never
@site.save
end
it 'adds a new domain if new one' do
@site.domains = ['www.acme.fr']
Locomotive.heroku_connection.expects(:add_domain).with('locomotive', 'www.acme.fr')
@ -108,21 +115,21 @@ describe 'Heroku support' do
Locomotive.heroku_domains.should include('www.acme.fr')
end
end
context 'removing domain' do
it 'does not remove domain if no delta' do
Locomotive.heroku_connection.expects(:remove_domain).never
@site.destroy
end
it 'removes domains if we destroy a site' do
@site.stubs(:domains_without_subdomain).returns(['www.acme.com'])
Locomotive.heroku_connection.expects(:remove_domain).with('locomotive', 'www.acme.com')
@site.destroy
Locomotive.heroku_domains.should_not include('www.acme.com')
end
it 'removes domain if removed' do
@site.domains = ['www.acme.fr']; @site.save
@site.domains = ['www.acme.com']
@ -130,20 +137,33 @@ describe 'Heroku support' do
@site.save
Locomotive.heroku_domains.should_not include('www.acme.fr')
end
end
end
end
def configure_locomotive_with_heroku(options = {}, domains = nil)
::Heroku::Client.any_instance.stubs(:list_domains).with('locomotive').returns(domains || [
if options.has_key?(:name)
ENV['APP_NAME'] = options.delete(:name)
else
ENV['APP_NAME'] = 'locomotive'
end
::Heroku::Client.any_instance.stubs(:list_domains).with(ENV['APP_NAME']).returns(domains || [
{ :domain => "www.acme.com" }, { :domain => "example.com" }, { :domain => "www.example.com" }
])
Locomotive.configure do |config|
config.heroku = { :name => 'locomotive', :login => 'john@doe.net', :password => 'easyone' }.merge(options)
end
config.hosting = :heroku
config.heroku = { :login => 'john@doe.net', :password => 'easyone' }.merge(options)
end
end
after(:all) do
ENV['HEROKU_SLUG'] = ENV['APP_NAME'] = ENV['HEROKU_LOGIN'] = ENV['HEROKU_PASSWORD'] = nil
Locomotive.configure_for_test
end
end

View File

@ -7,6 +7,8 @@ require 'rspec/rails'
# in spec/support/ and its subdirectories.
Dir[Rails.root.join("spec/support/**/*.rb")].each {|f| require f}
Locomotive.configure_for_test
RSpec.configure do |config|
# == Mock Framework
#

View File

@ -0,0 +1,32 @@
module Locomotive
class TestController < ApplicationController
include Locomotive::Render
attr_accessor :output, :status, :current_site, :current_admin
def render(options = {})
self.output = options[:text]
self.status = options[:status]
end
def response
@_response ||= TestResponse.new
end
def request
@_request ||= TestRequest.new
end
end
class TestResponse < ActionDispatch::TestResponse
end
class TestRequest < ActionDispatch::TestRequest
attr_accessor :fullpath
end
end

View File

@ -1,37 +1,10 @@
Locomotive.configure do |config|
config.default_domain = 'example.com'
config.enable_logs = true
end
module Locomotive
class TestController < ApplicationController
include Locomotive::Render
attr_accessor :output, :status, :current_site, :current_admin
def render(options = {})
self.output = options[:text]
self.status = options[:status]
def Locomotive.configure_for_test
Locomotive.configure do |config|
config.multi_sites do |multi_sites|
multi_sites.domain = 'example.com'
multi_sites.reserved_subdomains = %w(www admin email blog webmail mail support help site sites)
end
def response
@_response ||= TestResponse.new
end
def request
@_request ||= TestRequest.new
end
config.hosting = :none
config.enable_logs = true
end
class TestResponse < ActionDispatch::TestResponse
end
class TestRequest < ActionDispatch::TestRequest
attr_accessor :fullpath
end
end
end