Skip to content

Commit

Permalink
Merge pull request #782 from AlchemyCMS/configuration-methods
Browse files Browse the repository at this point in the history
Extract configuration methods into its own module.
  • Loading branch information
tvdeyen committed May 20, 2015
2 parents aaf378d + 6f3f391 commit db6eddf
Show file tree
Hide file tree
Showing 10 changed files with 71 additions and 78 deletions.
19 changes: 1 addition & 18 deletions app/controllers/alchemy/base_controller.rb
Expand Up @@ -2,6 +2,7 @@
#
module Alchemy
class BaseController < ApplicationController
include Alchemy::ConfigurationMethods
include Alchemy::ControllerActions
include Alchemy::Modules
include Alchemy::SSLProtection
Expand All @@ -11,8 +12,6 @@ class BaseController < ApplicationController
before_action :mailer_set_url_options
before_action :set_locale

helper_method :multi_site?

helper 'alchemy/admin/form'

rescue_from CanCan::AccessDenied do |exception|
Expand All @@ -27,22 +26,6 @@ def set_locale
::I18n.locale = Language.current.code
end

# Returns the configuration value of given key.
#
# Config file is in +config/alchemy/config.yml+
#
def configuration(name)
Alchemy::Config.get(name)
end

def multi_language?
Language.published.count > 1
end

def multi_site?
Site.count > 1
end

def not_found_error!(msg = "Not found \"#{request.fullpath}\"")
raise ActionController::RoutingError.new(msg)
end
Expand Down
20 changes: 0 additions & 20 deletions app/helpers/alchemy/base_helper.rb
Expand Up @@ -54,26 +54,6 @@ def render_message(type = :info, msg = nil, &blk)
end
end

# Returns the Alchemy configuration.
#
# *DO NOT REMOVE THIS HERE.*
#
# We need this, if an external engine or app includes this module into actionview.
#
def configuration(name)
Alchemy::Config.get(name)
end

# Returns true if Alchemy is in multi language mode
#
# *DO NOT REMOVE THIS HERE.*
#
# We need this, if an external engine or app includes this module into actionview.
#
def multi_language?
Alchemy::Language.published.count > 1
end

# Renders the flash partial (+alchemy/admin/partials/flash+)
#
# @param [String] notice The notice you want to display
Expand Down
29 changes: 29 additions & 0 deletions lib/alchemy/configuration_methods.rb
@@ -0,0 +1,29 @@
module Alchemy
module ConfigurationMethods
extend ActiveSupport::Concern

included do
helper_method :configuration, :multi_language?, :multi_site?
end

# Returns the configuration value of given key.
#
# Config file is in +config/alchemy/config.yml+
#
def configuration(name)
Config.get(name)
end

# Returns true if more than one language is published.
#
def multi_language?
Language.published.count > 1
end

# Returns true if more than one site exists.
#
def multi_site?
Site.count > 1
end
end
end
1 change: 0 additions & 1 deletion lib/alchemy/controller_actions.rb
Expand Up @@ -125,6 +125,5 @@ def store_current_alchemy_language(language)
Language.current = language
end
end

end
end
1 change: 1 addition & 0 deletions lib/alchemy/engine.rb
Expand Up @@ -25,6 +25,7 @@
require_relative './auth_accessors'
require_relative './cache_digests/template_tracker'
require_relative './config'
require_relative './configuration_methods'
require_relative './controller_actions'
require_relative './errors'
require_relative './essence'
Expand Down
2 changes: 1 addition & 1 deletion lib/alchemy/ssl_protection.rb
Expand Up @@ -19,7 +19,7 @@ module SSLProtection
# if you want to use the ssl protection.
#
def ssl_required?
!Rails.env.test? && configuration(:require_ssl)
!Rails.env.test? && Config.get(:require_ssl)
end

# Redirects current request to https.
Expand Down
22 changes: 22 additions & 0 deletions spec/controllers/base_controller_spec.rb
Expand Up @@ -25,5 +25,27 @@ module Alchemy
end
end

describe "#configuration" do
it "returns certain configuration options" do
allow(Config).to receive(:show).and_return({"some_option" => true})
expect(controller.configuration(:some_option)).to eq(true)
end
end

describe "#multi_language?" do
context "if more than one published language exists" do
it "returns true" do
allow(Alchemy::Language).to receive(:published).and_return double(count: 2)
expect(controller.multi_language?).to eq(true)
end
end

context "if less than two published languages exists" do
it "returns false" do
allow(Alchemy::Language).to receive(:published).and_return double(count: 1)
expect(controller.multi_language?).to eq(false)
end
end
end
end
end
23 changes: 0 additions & 23 deletions spec/helpers/base_helper_spec.rb
Expand Up @@ -17,29 +17,6 @@ module Alchemy
end
end

describe "#configuration" do
it "should return certain configuration options" do
allow(Config).to receive(:show).and_return({"some_option" => true})
expect(helper.configuration(:some_option)).to eq(true)
end
end

describe "#multi_language?" do
context "if more than one published language exists" do
it "should return true" do
allow(Alchemy::Language).to receive(:published).and_return double(count: 2)
expect(helper.multi_language?).to eq(true)
end
end

context "if less than two published languages exists" do
it "should return false" do
allow(Alchemy::Language).to receive(:published).and_return double(count: 1)
expect(helper.multi_language?).to eq(false)
end
end
end

describe '#page_or_find' do
let(:page) { FactoryGirl.create(:public_page) }

Expand Down
27 changes: 13 additions & 14 deletions spec/helpers/pages_helper_spec.rb
@@ -1,26 +1,25 @@
# encoding: utf-8
require 'spec_helper'

include Alchemy::BaseHelper

module Alchemy
describe PagesHelper do

# Fixtures
let(:language) { mock_model('Language', :code => 'en') }
let(:default_language) { Language.default }
let(:language_root) { FactoryGirl.create(:language_root_page) }
let(:public_page) { FactoryGirl.create(:public_page) }
let(:visible_page) { FactoryGirl.create(:public_page, :visible => true) }
let(:restricted_page) { FactoryGirl.create(:public_page, :visible => true, :restricted => true) }
let(:level_2_page) { FactoryGirl.create(:public_page, :parent_id => visible_page.id, :visible => true, :name => 'Level 2') }
let(:level_3_page) { FactoryGirl.create(:public_page, :parent_id => level_2_page.id, :visible => true, :name => 'Level 3') }
let(:level_4_page) { FactoryGirl.create(:public_page, :parent_id => level_3_page.id, :visible => true, :name => 'Level 4') }
let(:klingonian) { FactoryGirl.create(:klingonian) }
let(:klingonian_language_root) { FactoryGirl.create(:language_root_page, :language => klingonian) }
let(:klingonian_public_page) { FactoryGirl.create(:public_page, :language => klingonian, :parent_id => klingonian_language_root.id) }
let(:language) { mock_model('Language', :code => 'en') }
let(:default_language) { Language.default }
let(:language_root) { FactoryGirl.create(:language_root_page) }
let(:public_page) { FactoryGirl.create(:public_page) }
let(:visible_page) { FactoryGirl.create(:public_page, :visible => true) }
let(:restricted_page) { FactoryGirl.create(:public_page, :visible => true, :restricted => true) }
let(:level_2_page) { FactoryGirl.create(:public_page, :parent_id => visible_page.id, :visible => true, :name => 'Level 2') }
let(:level_3_page) { FactoryGirl.create(:public_page, :parent_id => level_2_page.id, :visible => true, :name => 'Level 3') }
let(:level_4_page) { FactoryGirl.create(:public_page, :parent_id => level_3_page.id, :visible => true, :name => 'Level 4') }
let(:klingonian) { FactoryGirl.create(:klingonian) }
let(:klingonian_language_root) { FactoryGirl.create(:language_root_page, :language => klingonian) }
let(:klingonian_public_page) { FactoryGirl.create(:public_page, :language => klingonian, :parent_id => klingonian_language_root.id) }

before do
helper.controller.class_eval { include Alchemy::ConfigurationMethods }
allow(Config).to receive(:get) { |arg| arg == :url_nesting ? true : Config.parameter(arg) }
@root_page = language_root # We need this instance variable in the helpers
end
Expand Down
5 changes: 4 additions & 1 deletion spec/helpers/url_helper_spec.rb
@@ -1,12 +1,15 @@
require 'spec_helper'

include Alchemy::BaseHelper
include Alchemy::ElementsHelper

module Alchemy
describe UrlHelper do
let(:page) { mock_model(Page, urlname: 'testpage', language_code: 'en') }

before do
helper.controller.class_eval { include Alchemy::ConfigurationMethods }
end

context 'page path helpers' do
describe "#show_page_path_params" do
context "when multi_language" do
Expand Down

0 comments on commit db6eddf

Please sign in to comment.