Skip to content

Commit

Permalink
Refactored Merb::Controller.callable_actions
Browse files Browse the repository at this point in the history
  • Loading branch information
fabien committed May 27, 2008
1 parent 93b512e commit c0609a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
33 changes: 21 additions & 12 deletions lib/merb-core/controller/merb_controller.rb
Expand Up @@ -102,17 +102,7 @@ def _shown_actions
# ==== Returns
# SimpleSet[String]:: A set of actions that should be callable.
def callable_actions
unless @callable_actions
callables = []
klass = self
begin
callables << (klass.public_instance_methods(false) + klass._shown_actions) - klass._hidden_actions
klass = klass.superclass
end until klass == Merb::AbstractController || klass == Object
actions = callables.flatten.reject{|action| action =~ /^_.*/}
@callable_actions = Merb::SimpleSet.new(actions)
end
@callable_actions
@callable_actions ||= Merb::SimpleSet.new(_callable_methods)
end

# This is a stub method so plugins can implement param filtering if they want.
Expand All @@ -128,6 +118,22 @@ def _filter_params(params)
params
end

private

# All methods that are callable as actions.
#
# ==== Returns
# Array:: A list of method names that are also actions
def _callable_methods
callables = []
klass = self
begin
callables << (klass.public_instance_methods(false) + klass._shown_actions) - klass._hidden_actions
klass = klass.superclass
end until klass == Merb::AbstractController || klass == Object
callables.flatten.reject{|action| action =~ /^_.*/}
end

end # class << self

# The location to look for a template for a particular controller, context,
Expand Down Expand Up @@ -229,7 +235,10 @@ def cookies() @_cookies ||= _setup_cookies end
# ==== Returns
# Hash:: The session that was extracted from the request object.
def session() request.session end


# Hide any methods that may have been exposed as actions before.
hide_action(*_callable_methods)

private

# Create a default cookie jar, and pre-set a fixation cookie
Expand Down
4 changes: 4 additions & 0 deletions spec/public/controller/base_spec.rb
Expand Up @@ -9,6 +9,10 @@
end
end

it "should not have any callable actions by default" do
Merb::Controller.callable_actions.should be_empty
end

it "should dispatch to callable actions" do
controller = dispatch_to(Merb::Test::Fixtures::Controllers::Base, :index)
controller.body.should == "index"
Expand Down

0 comments on commit c0609a9

Please sign in to comment.