Skip to content

Commit

Permalink
Allow standalone actions to inherit configuration from the right fram…
Browse files Browse the repository at this point in the history
…ework. Added Configuration#modules in order to configure the additional modules to include by default
  • Loading branch information
jodosha committed May 28, 2014
1 parent ceb7214 commit 5e2a4a7
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/lotus/action/configurable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@ module Lotus
module Action
module Configurable
def self.included(base)
config = Lotus::Controller::Configuration.for(base)

base.class_eval do
include Utils::ClassAttribute

class_attribute :configuration
self.configuration ||= Controller.configuration.duplicate
self.configuration = config
end

config.load!(base)
end

protected
Expand Down
26 changes: 26 additions & 0 deletions lib/lotus/controller/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,17 @@
require 'lotus/utils/string'
require 'lotus/utils/class'

module Lotus
module Controller
class Configuration
DEFAULT_ERROR_CODE = 500

def self.for(base)
namespace = Utils::String.new(base).namespace
framework = Utils::Class.load!("(#{namespace}|Lotus)::Controller")
framework.configuration.duplicate
end

def initialize
reset!
end
Expand Down Expand Up @@ -33,23 +42,40 @@ def action_module(value = nil)
end
end

def modules(&blk)
if block_given?
@modules.push(blk)
else
@modules
end
end

def duplicate
Configuration.new.tap do |c|
c.handle_exceptions = handle_exceptions
c.handled_exceptions = handled_exceptions.dup
c.action_module = action_module
c.modules = modules.dup
end
end

def reset!
@handle_exceptions = true
@handled_exceptions = {}
@modules = []
@action_module = ::Lotus::Action
end

def load!(base)
modules.each do |mod|
base.class_eval(&mod)
end
end

protected
attr_accessor :handled_exceptions
attr_writer :action_module
attr_writer :modules
end
end
end
77 changes: 77 additions & 0 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,30 +80,105 @@ module CustomAction
end
end

describe 'modules' do
before do
class FakeAction
end unless defined?(FakeAction)

module FakeCallable
def call(params)
[status, {}, ['Callable']]
end

def status
200
end
end unless defined?(FakeCallable)

module FakeStatus
def status
318
end
end
end

after do
Object.send(:remove_const, :FakeAction)
Object.send(:remove_const, :FakeCallable)
end

describe 'when not previously configured' do
it 'is empty' do
@configuration.modules.must_be_empty
end
end

describe 'when previously configured' do
before do
@configuration.modules do
include FakeCallable
end
end

it 'allows to configure additional modules to include' do
@configuration.modules do
include FakeStatus
end

@configuration.modules.each do |mod|
FakeAction.class_eval(&mod)
end

code, _, body = FakeAction.new.call({})
code.must_equal 318
body.must_equal ['Callable']
end
end

it 'allows to configure modules to include' do
@configuration.modules do
include FakeCallable
end

@configuration.modules.each do |mod|
FakeAction.class_eval(&mod)
end

code, _, body = FakeAction.new.call({})
code.must_equal 200
body.must_equal ['Callable']
end
end

describe 'duplicate' do
before do
@configuration.reset!
@configuration.modules { include Kernel }
@config = @configuration.duplicate
end

it 'returns a copy of the configuration' do
@config.handle_exceptions.must_equal @configuration.handle_exceptions
@config.handled_exceptions.must_equal @configuration.handled_exceptions
@config.action_module.must_equal @configuration.action_module
@config.modules.must_equal @configuration.modules
end

it "doesn't affect the original configuration" do
@config.handle_exceptions = false
@config.handle_exception ArgumentError => 400
@config.action_module CustomAction
@config.modules { include Comparable }

@config.handle_exceptions.must_equal false
@config.handled_exceptions.must_equal Hash[ArgumentError => 400]
@config.action_module.must_equal CustomAction
@config.modules.size.must_equal 2

@configuration.handle_exceptions.must_equal true
@configuration.handled_exceptions.must_equal Hash[]
@configuration.action_module.must_equal ::Lotus::Action
@configuration.modules.size.must_equal 1
end
end

Expand All @@ -112,6 +187,7 @@ module CustomAction
@configuration.handle_exceptions = false
@configuration.handle_exception ArgumentError => 400
@configuration.action_module CustomAction
@configuration.modules { include Kernel }

@configuration.reset!
end
Expand All @@ -120,6 +196,7 @@ module CustomAction
@configuration.handle_exceptions.must_equal(true)
@configuration.handled_exceptions.must_equal({})
@configuration.action_module.must_equal(::Lotus::Action)
@configuration.modules.must_equal([])
end
end
end
13 changes: 13 additions & 0 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -539,6 +539,11 @@ module MusicPlayer
configure do
handle_exception ArgumentError => 400
action_module MusicPlayer::Action

modules do
include Lotus::Action::Cookies
include Lotus::Action::Session
end
end
end

Expand Down Expand Up @@ -571,4 +576,12 @@ def call(params)
end
end
end

class StandaloneAction
include MusicPlayer::Action

def call(params)
raise ArgumentError
end
end
end
11 changes: 11 additions & 0 deletions test/integration/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,15 @@
code, _, _ = MusicPlayer::Controllers::Artists::Show.new.call({})
code.must_equal 404
end

it 'allows standalone actions to inherith framework configuration' do
code, _, _ = MusicPlayer::StandaloneAction.new.call({})
code.must_equal 400
end

it 'includes modules from configuration' do
modules = MusicPlayer::Controllers::Artists::Show.included_modules
modules.must_include(Lotus::Action::Cookies)
modules.must_include(Lotus::Action::Session)
end
end
1 change: 1 addition & 0 deletions test/throw_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class DomainLogicException < StandardError
end

Lotus::Controller.configure do
reset!
handle_exception DomainLogicException => 400
end

Expand Down

0 comments on commit 5e2a4a7

Please sign in to comment.