Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Improved multisite support.

* When creating a new site, automatically create a default language if no languages are already present.
* Related amendments and additional specs.
  • Loading branch information...
commit 9e4657524292a40effd9e33558d3d6ecd4322fa6 1 parent 7c0760c
@hmans hmans authored
View
17 app/models/alchemy/site.rb
@@ -15,5 +15,22 @@ class Site < ActiveRecord::Base
def current?
self.class.current == self
end
+
+ before_create do
+ # If no languages are present, create a default language based
+ # on the host app's Alchemy configuration.
+
+ if languages.empty?
+ default_language = Alchemy::Config.get(:default_language)
+ languages.build(
+ name: default_language['name'],
+ language_code: default_language['code'],
+ frontpage_name: default_language['frontpage_name'],
+ page_layout: default_language['page_layout'],
+ public: true,
+ default: true
+ )
+ end
+ end
end
end
View
24 lib/alchemy/seeder.rb
@@ -9,7 +9,6 @@ class << self
# Run the alchemy:db:seed rake task to seed your database.
def seed!
create_default_site
- create_default_language
create_root_page
end
@@ -83,33 +82,12 @@ def create_default_site
)
if site.new_record?
site.save!
- log "Created default site."
+ log "Created default site with default language."
else
log "Default site was already present.", :skip
end
end
- def create_default_language
- desc "Creating default language"
- default_language = Alchemy::Config.get(:default_language)
- lang = Alchemy::Language.find_or_initialize_by_language_code(
- :name => default_language['name'],
- :language_code => default_language['code'],
- :frontpage_name => default_language['frontpage_name'],
- :page_layout => default_language['page_layout'],
- :public => true,
- :default => true,
- :site => Site.first
- )
- if lang.new_record?
- if lang.save!
- log "Created language #{lang.name}."
- end
- else
- log "Language #{lang.name} was already present.", :skip
- end
- end
-
def create_root_page
desc "Creating root page"
root = Alchemy::Page.find_or_initialize_by_name(
View
5 lib/alchemy/upgrader.rb
@@ -31,8 +31,9 @@ def upgrade_to_sites
desc "Creating default site and migrating existing languages to it"
if Site.count == 0
Alchemy::Site.transaction do
- site = Alchemy::Site.create!(host: '*', name: 'Default Site')
- Alchemy::Language.update_all(site_id: site.id)
+ site = Alchemy::Site.new(host: '*', name: 'Default Site')
+ site.languages << Alchemy::Language.all
+ site.save!
log "Done."
end
else
View
1  spec/factories.rb
@@ -31,6 +31,7 @@
frontpage_name 'Intro'
page_layout 'intro'
public true
+ site { Alchemy::Site.first }
factory :klingonian do
name 'Klingonian'
View
30 spec/models/site_spec.rb
@@ -5,6 +5,36 @@ module Alchemy
let(:site) { FactoryGirl.create(:site) }
let(:another_site) { FactoryGirl.create(:site, name: 'Another Site', host: 'another.com') }
+ describe 'new instances' do
+ subject { FactoryGirl.build(:site) }
+
+ it 'should start out with on languages' do
+ subject.languages.should be_empty
+ end
+
+ context 'when being saved' do
+ context 'when it has no languages yet' do
+ it 'should automatically create a default language' do
+ subject.save!
+ subject.languages.count.should == 1
+ subject.languages.first.should be_default
+ end
+ end
+
+ context 'when it already has a language' do
+ let(:language) { FactoryGirl.build(:language, site: nil) }
+ before { subject.languages << language }
+
+ it 'should not create any additional languages' do
+ subject.languages.should == [language]
+
+ expect { subject.save! }.
+ to_not change(subject, "languages")
+ end
+ end
+ end
+ end
+
describe '.current' do
context 'when set to a site' do
before { Site.current = site }
View
1  spec/spec_helper.rb
@@ -56,6 +56,7 @@ def configure
end
# Ensuring that the locale is always resetted to :en before running any tests
config.before(:each) do
+ Alchemy::Site.current = nil
::I18n.locale = :en
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.