Skip to content

Commit

Permalink
all tests pass
Browse files Browse the repository at this point in the history
  • Loading branch information
Didier Lafforgue committed Feb 18, 2012
1 parent e7469f9 commit 5e6949c
Show file tree
Hide file tree
Showing 13 changed files with 5,117 additions and 316 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
.bundle/
log/*.log
pkg/
spec/dummy/log/*.log
14 changes: 4 additions & 10 deletions README.textile
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,11 @@ bc. gem 'locomotive_heroku', :require => 'locomotive/heroku'

h2. Configuration

Then, you have two way to let Heroku know about your API key and your application name:
Then, you have to let Heroku know about your API key and your application name
Modify your Locomotive config file (**config/locomotive.rb**)

1. Modify your Locomotive config file (**config/locomotive.rb**)

bc. config.heroku = { :api_key => '<YOUR HEROKU API KEY>', :app_name => '<MY HEROKU APP NAME>' }

2. Pass the API key as an Heroku env variable

Open your terminal and at the root of your application on your machine,

bc. heroku config:add HEROKU_API_KEY=<YOUR HEROKU API KEY>
bc.. config.hosting = :heroku
config.heroku = { :api_key => '<YOUR HEROKU API KEY>', :app_name => '<MY HEROKU APP NAME>' }

h2. Storage: Amazon S3

Expand Down
5 changes: 4 additions & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,7 @@ Bundler::GemHelper.install_tasks

# === RSpec ===
require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)
RSpec::Core::RakeTask.new(:spec)

# === Default task ===
task :default => :spec
40 changes: 18 additions & 22 deletions lib/locomotive/heroku.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
puts "\t...loading heroku extension"

# require 'locomotive/config'
require 'heroku-api'
require 'locomotive/heroku/patches'
# require 'locomotive/heroku/custom_domain'
# require 'locomotive/heroku/first_installation'
require 'locomotive/heroku/custom_domain'
require 'locomotive/heroku/first_installation'
require 'locomotive/heroku/enabler'

module Locomotive
module Heroku
Expand All @@ -14,7 +14,13 @@ class << self

def domains
if @domains.nil?
@domains = self.connection.get_domains(self.app_name)
response = self.connection.get_domains(self.app_name)

if response.status == 200
@domains = response.body.map { |h| h['domain'] }.reject { |n| n.starts_with?('*') }
else
@domains = []
end
else
@domains
end
Expand All @@ -25,22 +31,26 @@ def connection

raise 'The Heroku API key is mandatory' if ENV['HEROKU_API_KEY'].blank? && Locomotive.config.heroku[:api_key].blank?

@connection = ::Heroku::API.new(:api_key => ENV['HEROKU_API_KEY'] || Locomotive.config.heroku[:api_key], :mock => ENV['MOCK_HEROKU'].present?)
@connection = ::Heroku::API.new(:api_key => ENV['HEROKU_API_KEY'] || Locomotive.config.heroku[:api_key])
end

def app_name
return @app_name if @app_name

raise 'The Heroku application name is mandatory' if ENV['APP_NAME'].blank? && Locomotive.config.heroku[:app_name].blank?

@app_name = Locomotive.config.heroku[:app_name] || ENV['APP_NAME']
@app_name = Locomotive.config.heroku[:app_name] || ENV['APP_NAME']
end
end

def self.add_domain(name)
Locomotive.log "[add heroku domain] #{name}"
self.connection.post_domains(self.app_name, name)
self.domains << name

response = self.connection.post_domain(self.app_name, name)

if response.status >= 200 && response.status < 300
self.domains << name
end
end

def self.remove_domain(name)
Expand All @@ -49,19 +59,5 @@ def self.remove_domain(name)
self.domains.delete(name)
end

def self.enable
Locomotive.config.domain = 'heroku.com' unless Locomotive.config.multi_sites?

# rack_cache: disabled because of Varnish
Locomotive.config.rack_cache = false

# hooks
Locomotive::Site.send :include, Locomotive::Heroku::CustomDomain
Locomotive::Site.send :include, Locomotive::Heroku::FirstInstallation

# initialize the API connection
self.connection
end

end
end
42 changes: 15 additions & 27 deletions lib/locomotive/heroku/custom_domain.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,32 @@ module CustomDomain
extend ActiveSupport::Concern

included do
after_save :add_heroku_domains
after_save :sync_heroku_domains
after_destroy :remove_heroku_domains

alias_method_chain :add_subdomain_to_domains, :heroku
end

module InstanceMethods

protected
protected

def add_subdomain_to_domains_with_heroku
unless self.domains_change.nil?
@heroku_domains_change = {
:added => self.domains_change.last - self.domains_change.first - [self.full_subdomain_was] - [self.full_subdomain],
:removed => self.domains_change.first - self.domains_change.last - [self.full_subdomain_was] - [self.full_subdomain]
}
def sync_heroku_domains
# all the Heroku domains should also referenced in the site, if not then remove them from Heroku.
Locomotive::Heroku.domains.each do |domain|
unless self.domains_without_subdomain.include?(domain)
Locomotive::Heroku.remove_domain(domain)
end

add_subdomain_to_domains_without_heroku
end

def add_heroku_domains
return if @heroku_domains_change.nil?

@heroku_domains_change[:added].each do |name|
Locomotive::Heroku.add_domain(name)
end
@heroku_domains_change[:removed].each do |name|
Locomotive::Heroku.remove_domain(name)
# add the site domains should referenced in the Heroku domains, if not then add them to Heroku
self.domains_without_subdomain.each do |domain|
unless Locomotive::Heroku.domains.include?(domain)
Locomotive::Heroku.add_domain(domain)
end
end
end

def remove_heroku_domains
self.domains_without_subdomain.each do |name|
Locomotive::Heroku.remove_domain(name)
end
def remove_heroku_domains
self.domains_without_subdomain.each do |domain|
Locomotive::Heroku.remove_domain(domain)
end

end

end
Expand Down
24 changes: 24 additions & 0 deletions lib/locomotive/heroku/enabler.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Locomotive

def self.enable_heroku
Locomotive.config.domain = 'heroku.com' unless Locomotive.config.multi_sites?

# we can manage domains within Heroku no matter what the value of the multi_sites option is
Locomotive.config.manage_domains = true
Locomotive.config.manage_subdomain = false unless Locomotive.config.multi_sites?

# rack_cache: disabled because of Varnish
Locomotive.config.rack_cache = false

# hooks
Locomotive::Site.send :include, Locomotive::Heroku::CustomDomain
Locomotive::Site.send :include, Locomotive::Heroku::FirstInstallation

# initialize the API connection
Locomotive::Heroku.connection

# load all the domains
Locomotive::Heroku.domains
end

end
Loading

0 comments on commit 5e6949c

Please sign in to comment.