Skip to content

Commit

Permalink
Allow Moonshine::Manifest#template to be used at the class and instance
Browse files Browse the repository at this point in the history
level.
  • Loading branch information
technicalpickles committed Mar 2, 2010
1 parent 2595553 commit cf2c448
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 19 deletions.
15 changes: 12 additions & 3 deletions lib/moonshine/manifest.rb
Expand Up @@ -53,6 +53,11 @@ def self.rails_env
ENV["RAILS_ENV"] || 'production'
end

# HAX for cases where we evaluate ERB that refers to Rails.env
def self.env
rails_env

This comment has been minimized.

Copy link
@andrewroth

andrewroth Mar 9, 2010

Contributor

good call, I'd been changing things to RAILS_ENV or worse RAILS_ENV = ENV['RAILS_ENV'] unless defined?(RAILS_ENV)

end

# The current environment's database configuration
def database_environment
configuration[:database][rails_env.to_sym]
Expand Down Expand Up @@ -121,19 +126,19 @@ def on_stage(*args)
end
end

def local_template_dir
def self.local_template_dir
@local_template_dir ||= rails_root.join('app/manifests/templates')
end

def local_template(pathname)
def self.local_template(pathname)
(local_template_dir + pathname.basename).expand_path
end

# Render the ERB template located at <tt>pathname</tt>. If a template exists
# with the same basename at <tt>RAILS_ROOT/app/manifests/templates</tt>, it
# is used instead. This is useful to override templates provided by plugins
# to customize application configuration files.
def template(pathname, b = binding)
def self.template(pathname, b = binding)
pathname = Pathname.new(pathname) unless pathname.kind_of?(Pathname)

template_contents = if local_template(pathname).exist?
Expand All @@ -146,6 +151,10 @@ def template(pathname, b = binding)
ERB.new(template_contents).result(b)
end

def template(pathname, b = binding)
self.class.template(pathname, b)
end

# config/moonshine.yml
if moonshine_yml.exist?
configure(YAML::load(ERB.new(moonshine_yml.read).result))
Expand Down
35 changes: 19 additions & 16 deletions test/moonshine/test_manifest.rb
Expand Up @@ -5,6 +5,12 @@ module Moonshine::Iptables

class Moonshine::ManifestTest < Test::Unit::TestCase

def teardown
if @manifest && application_template && application_template.exist?
application_template.delete
end
end

def test_loads_configuration
assert_not_nil Moonshine::Manifest.configuration
assert_not_nil Moonshine::Manifest.configuration[:application]
Expand All @@ -15,36 +21,33 @@ def test_loads_environment_specific_configuration
end

def test_moonshine_templates
@manifest = Moonshine::Manifest.new
@manifest = Moonshine::Manifest::Rails.new
@manifest.configure(:application => 'bar')

moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'templates', 'passenger.conf.erb')
moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'manifest', 'rails', 'templates', 'passenger.vhost.erb')
template_contents = 'moonshine template: <%= configuration[:application] %>'
moonshine_template.expects(:exist?).returns(true)
moonshine_template.expects(:read).returns(template_contents)

application_template = @manifest.rails_root.join('app', 'manifests', 'templates', 'passenger.conf.erb')
application_template.expects(:exist?).returns(false)

@manifest.stubs(:local_template).returns(application_template)

assert_equal 'moonshine template: bar', @manifest.template(moonshine_template)
assert_match 'ServerName yourapp.com', @manifest.template(moonshine_template)
end

def application_template
@application_template ||= @manifest.rails_root.join('app', 'manifests', 'templates', 'passenger.conf.erb')
end

def test_app_templates_override_moonshine_templates
@manifest = Moonshine::Manifest.new
@manifest.configure(:application => 'bar')

moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'templates', 'passenger.conf.erb')
application_template.open('w') {|f| f.write "application template: <%= configuration[:application] %>" }

moonshine_template = Pathname.new(__FILE__).dirname.join('..', '..', 'lib', 'moonshine', 'manifest', 'rails', 'templates', 'passenger.conf.erb')
application_template = @manifest.rails_root.join('app', 'manifests', 'templates', 'passenger.conf.erb')
template_contents = 'application template: <%= configuration[:application] %>'
application_template.expects(:exist?).returns(true)
application_template.expects(:read).returns(template_contents)

@manifest.stubs(:local_template).returns(application_template)
assert application_template.exist?, "#{application_template} should exist, but didn't"
assert moonshine_template.exist?, "#{moonshine_template} should exist, but didn't"

assert_equal 'application template: bar', @manifest.template(moonshine_template)
# should return the output from that existing thing
assert_match 'application template: bar', @manifest.template(moonshine_template)
end

def test_loads_plugins
Expand Down

0 comments on commit cf2c448

Please sign in to comment.