Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename Configuration#modules to #prepare [WIP] #58

Merged
merged 1 commit into from
Dec 8, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

attr_writer :modules at the bottom of the file can be removed

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@AlfonsoUceda no we cannot remove it, please see line 503: c.modules = modules.dup


# 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