Skip to content

Commit

Permalink
Merge 617e36f into 87eef1d
Browse files Browse the repository at this point in the history
  • Loading branch information
Trung Lê committed Dec 5, 2014
2 parents 87eef1d + 617e36f commit 122445f
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 23 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ class Show
include Lotus::Action

before { ... } # do some authentication stuff
before {|params| @article = Article.find params[:id] }
before { |params| @article = Article.find params[:id] }

def call(params)
end
Expand Down Expand Up @@ -962,8 +962,9 @@ Lotus::Controller.configure do
# Configure the modules to be included/extended/prepended by default.
# Argument: proc, empty by default
#
modules do
prepare do
include Lotus::Action::Sessions
using SomeMiddleWare
prepend MyLibrary::Session::Store
end
end
Expand Down
32 changes: 18 additions & 14 deletions lib/lotus/controller/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -274,26 +274,19 @@ def action_module(value = nil)
end

# Specify the default modules to be included when `Lotus::Action` (or the
# `action_module`) is included. This also works with
# `Lotus::Controller.action`.
# `action_module`) is included.
#
# If not set, this option will be ignored.
#
# This is part of a DSL, for this reason when this method is called with
# an argument, it will set the corresponding instance variable. When
# called without, it will return the already set value, or the default.
# an argument, it will set the corresponding instance variable.
#
# @overload modules(blk)
# @overload prepare(blk)
# Adds the given block
# @param value [Proc] specify the modules to be included
#
# @overload modules
# Gets the value
# @return [Array] the list of the specified procs
# @param value [Proc] the included block
#
# @since 0.2.0
#
# @see Lotus::Controller::Dsl#action
# @see Lotus::Controller#duplicate
#
# @example Getting the value
Expand All @@ -307,9 +300,10 @@ def action_module(value = nil)
# require 'lotus/action/session'
#
# Lotus::Controller.configure do
# modules do
# prepare do
# include Lotus::Action::Cookies
# include Lotus::Action::Session
# before { do_something }
# end
# end
#
Expand All @@ -326,11 +320,11 @@ def action_module(value = nil)
# end
# end
# end
def modules(&blk)
def prepare(&blk)
if block_given?
@modules.push(blk)
else
@modules
raise ArgumentError.new('Please provide a proc or a block')
end
end

Expand Down Expand Up @@ -507,6 +501,16 @@ def duplicate
end
end

# Return included modules
#
# @return [Array<Proc>] array of included blocks
#
# @since 0.2.0
# @api private
#
# @see Lotus::Controller::Configuration#prepare
attr_reader :modules

# Reset all the values to the defaults
#
# @since 0.2.0
Expand Down
18 changes: 13 additions & 5 deletions test/configuration_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,23 @@ def status
end
end

describe 'when prepare with no block' do
it 'raises error' do
exception = -> { @configuration.prepare }.must_raise(ArgumentError)
exception.message.must_equal 'Please provide a proc or a block'
end

end

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

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

Expand All @@ -137,7 +145,7 @@ def status
end

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

Expand Down Expand Up @@ -265,7 +273,7 @@ def hash
describe 'duplicate' do
before do
@configuration.reset!
@configuration.modules { include Kernel }
@configuration.prepare { include Kernel }
@configuration.format custom: 'custom/format'
@configuration.default_format :html
@configuration.default_charset 'latin1'
Expand All @@ -286,7 +294,7 @@ def hash
@config.handle_exceptions = false
@config.handle_exception ArgumentError => 400
@config.action_module CustomAction
@config.modules { include Comparable }
@config.prepare { include Comparable }
@config.format another: 'another/format'
@config.default_format :json
@config.default_charset 'utf-8'
Expand Down
4 changes: 2 additions & 2 deletions test/fixtures.rb
Original file line number Diff line number Diff line change
Expand Up @@ -676,7 +676,7 @@ module MusicPlayer
handle_exception ArgumentError => 400
action_module MusicPlayer::Action

modules do
prepare do
include Lotus::Action::Cookies
include Lotus::Action::Session
include MusicPlayer::Controllers::Authentication
Expand Down Expand Up @@ -778,7 +778,7 @@ module FullStack
Controller = Lotus::Controller.duplicate(self) do
handle_exceptions false

modules do
prepare do
include Lotus::Action::Glue
include Lotus::Action::Session
end
Expand Down

0 comments on commit 122445f

Please sign in to comment.