Skip to content

Commit

Permalink
Refactoring for current_host, current_server etc.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomas von Deyen committed Mar 23, 2012
1 parent 3bb7dc8 commit 8679bfb
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 65 deletions.
2 changes: 1 addition & 1 deletion Guardfile
Expand Up @@ -2,7 +2,7 @@
# More info at https://github.com/guard/guard#readme

guard 'spork', :cucumber_env => { 'RAILS_ENV' => 'test' }, :rspec_env => { 'RAILS_ENV' => 'test' } do
watch(%r{lib/})
watch(%r{^lib/})
watch('spec/dummy/config/application.rb')
watch('spec/dummy/config/environment.rb')
watch(%r{^spec/dummy/config/environments/.+\.rb$})
Expand Down
3 changes: 2 additions & 1 deletion app/controllers/alchemy_crm/admin/deliveries_controller.rb
Expand Up @@ -26,7 +26,8 @@ def create
@delivery.send_chunks(
:language_id => session[:language_id],
:protocol => request.protocol,
:host => request.host_with_port
:host => request.host,
:port => request.port
)
flash[:notice] = "Das Mailing wurde für den Versand vorbereitet."
end
Expand Down
7 changes: 0 additions & 7 deletions app/controllers/alchemy_crm/admin/mailings_controller.rb
Expand Up @@ -3,9 +3,6 @@ module AlchemyCrm
module Admin
class MailingsController < Alchemy::Admin::ResourcesController

include ControllerHelpers

before_filter :set_options_for_helpers, :only => [:show]
before_filter :load_newsletters, :only => [:new, :edit, :copy]

helper 'AlchemyCrm::Mailings'
Expand Down Expand Up @@ -41,10 +38,6 @@ def update

private

def set_options_for_helpers
@options = {:language_id => session[:language_id], :host => request.host_with_port, :protocol => request.protocol}
end

def load_newsletters
@newsletters = Newsletter.all
end
Expand Down
14 changes: 4 additions & 10 deletions app/controllers/alchemy_crm/mailings_controller.rb
@@ -1,9 +1,6 @@
module AlchemyCrm
class MailingsController < AlchemyCrm::BaseController

include ControllerHelpers
before_filter :set_options_for_helpers

def show
@mailing = Mailing.find_by_sha1(params[:m])
if @mailing.nil? && !params[:id].blank?
Expand All @@ -12,14 +9,11 @@ def show
@page = @mailing.page
if params[:r].present?
@recipient = @mailing.recipients.find_by_sha1(params[:r])
@contact = @recipient.contact || Contact.new_from_recipient(@recipient)
else
@contact = Contact.fake
@recipient = Recipient.new_from_contact(@contact)
end
@contact = @recipient ? @recipient.contact || Contact.new_from_recipient(@recipient) : Contact.fake
end

private

def set_options_for_helpers
@options = {:language_id => session[:language_id], :host => request.host_with_port, :protocol => request.protocol}
end

end
Expand Down
31 changes: 31 additions & 0 deletions app/helpers/alchemy_crm/mailings_helper.rb
Expand Up @@ -145,5 +145,36 @@ def tracked_link_tag(*args)
end
end

def current_host
(defined?(request)).nil? || request.nil? ? @options[:host] : request.host
end

def current_server
if (defined?(request)).nil? || request.nil?
protocol = @options.nil? || @options[:protocol].blank? ? 'http://' : @options[:protocol]
else
protocol = request.protocol
end
if (defined?(request)).nil? || request.nil?
if @options
port = @options[:port] && @options[:port] != 80 ? @options[:port] : nil
else
port = nil
end
else
port = request.port != 80 ? request.port : nil
end
[protocol, current_host, port ? ":#{port}" : nil].join
end

def current_language
return @language if @language
if @options && @options[:language_id]
@language = Alchemy::Language.find(@options[:language_id])
else
@language = Alchemy::Language.get_default
end
end

end
end
30 changes: 10 additions & 20 deletions app/mailers/alchemy_crm/mailings_mailer.rb
@@ -1,29 +1,10 @@
module AlchemyCrm
class MailingsMailer < ActionMailer::Base

include ControllerHelpers

default :from => AlchemyCrm::Config.get(:mail_from)

helper "AlchemyCrm::Mailings"
helper_method :logged_in?, :configuration, :session

# Faking that we are not logged in
def logged_in?
false
end

# Session Hash for Alchemy::PagesHelper
def session
{
:language_id => @language.id,
:language_code => @language.code
}
end

def configuration(name)
Alchemy::Config.get(name)
end
helper_method :logged_in?, :configuration

# Renders the email sent to the mailing recipient
# It takes the layout from +layouts/alchemy_crm/mailings.erb+ and renders a html and a text part from it.
Expand All @@ -40,5 +21,14 @@ def build(mailing, recipient, options = {})
end
end

# Faking that we are not logged in
def logged_in?
false
end

def configuration(name)
Alchemy::Config.get(name)
end

end
end
1 change: 0 additions & 1 deletion lib/alchemy_crm.rb
Expand Up @@ -11,7 +11,6 @@
require 'alchemy_crm/engine'
require 'alchemy_crm/newsletter_layout'
require "alchemy_crm/seeder"
require "alchemy_crm/controller_helpers"
else
raise "Alchemy CRM 2.0 needs Rails 3.0 or higher. You are currently using Rails #{Rails::VERSION::MAJOR}"
end
21 changes: 0 additions & 21 deletions lib/alchemy_crm/controller_helpers.rb

This file was deleted.

5 changes: 3 additions & 2 deletions spec/controllers/alchemy_crm/mailings_controller_spec.rb
Expand Up @@ -16,8 +16,8 @@ module AlchemyCrm
get :show, {:id => @mailing.id, :use_route => :alchemy_crm}
end

it "should not have a recipient" do
assigns(:recipient).should be(nil)
it "should have a recipient" do
assigns(:recipient).should_not be(nil)
end

it "should have a fake contact" do
Expand Down Expand Up @@ -71,6 +71,7 @@ module AlchemyCrm
before(:each) do
@contact = Contact.create!({:email => 'jon@doe.com', :firstname => 'Jon', :lastname => 'Doe', :salutation => 'mr', :verified => true}, :as => :admin)
@recipient = Recipient.create!(:email => 'foo@baz.org', :contact => @contact)
@delivery = Delivery.create!(:recipients => [@recipient], :mailing => @mailing)
end

it "should render the view." do
Expand Down
93 changes: 91 additions & 2 deletions spec/helpers/alchemy_crm/mailings_helper_spec.rb
Expand Up @@ -40,13 +40,18 @@ module AlchemyCrm
end

describe '#link_to_unsubscribe_page' do
it "should render a link to the unsubscribe page." do

before(:each) do
unsubscribe_page = mock_model('Page', {:urlname => 'unsubscribe'})
Alchemy::Page.stub!(:find_by_page_layout).and_return(unsubscribe_page)
helper.stub!(:current_host).and_return('example.com')
helper.stub!(:multi_language?).and_return(false)
end

it "should render a link to the unsubscribe page." do
helper.stub!(:current_host).and_return('example.com')
helper.link_to_unsubscribe_page.should match /<a.+href=.+example.com\/unsubscribe/
end

end

describe '#read_in_browser_notice' do
Expand Down Expand Up @@ -74,5 +79,89 @@ module AlchemyCrm
end
end

describe '#current_host' do
it "should return the host from request." do
helper.stub!(:request).and_return(OpenStruct.new({:host => 'example.com'}))
helper.current_host.should match /^example.com$/
end

it "should return the host from options[:host] if no request is present." do
helper.stub!(:request).and_return(nil)
helper.instance_variable_set('@options', {:host => 'example.com'})
helper.current_host.should match /^example.com$/
end
end

describe '#current_server' do

context "if no request given" do
it "should return the full server url from options." do
helper.stub!(:request).and_return(nil)
helper.instance_variable_set('@options', {
:port => 80,
:host => 'example.com'
})
helper.current_server.should match /^http:\/\/example.com$/
end
end

context "if request given" do
it "should return the full server url from request." do
helper.current_server.should match /^http:\/\/test.host$/
end
end

context "given a port different from 80 with options present" do
it "should return the full server url with port." do
helper.stub!(:request).and_return(nil)
helper.instance_variable_set('@options', {
:port => 3000,
:host => 'localhost'
})
helper.current_server.should match /^http:\/\/localhost:3000$/
end
end

context "given port 80 with options present" do
it "should return the full server url without port." do
helper.stub!(:request).and_return(nil)
helper.instance_variable_set('@options', {
:port => 80,
:host => 'example.com'
})
helper.current_server.should match /^http:\/\/example.com$/
end
end

context "given a port different from 80 with request present" do
it "should return the full server url with port." do
helper.stub!(:request).and_return(OpenStruct.new({:host => 'localhost', :port => 3000, :protocol => 'http://'}))
helper.current_server.should match /^http:\/\/localhost:3000$/
end
end

context "given port 80 with request present" do
it "should return the full server url without port." do
helper.current_server.should match /^http:\/\/test.host$/
end
end

end

describe '#current_language' do
it "should return the language from session[:language_id]." do
helper.current_language.should be_an_instance_of(Alchemy::Language)
end

it "should return the language from options[:language_id] if no session is present." do
helper.instance_variable_set('@options', {:language_id => Alchemy::Language.get_default.id})
helper.current_language.should be_an_instance_of(Alchemy::Language)
end

it "should return the default language if no session or options[:language_id] is present." do
helper.current_language.should be_an_instance_of(Alchemy::Language)
end
end

end
end

0 comments on commit 8679bfb

Please sign in to comment.