From a2637e9f1fba92bc0b8dbf461ce9f4f8ffb4cfaa Mon Sep 17 00:00:00 2001 From: Yehuda Katz Date: Thu, 12 Mar 2009 13:19:13 -0600 Subject: Try to build a new AC::Base on top of AbstractController --- actionpack/lib/action_controller/new_base.rb | 5 +++ actionpack/lib/action_controller/new_base/base.rb | 26 ++++++++++++++ .../lib/action_controller/new_base/hide_actions.rb | 28 +++++++++++++++ .../lib/action_controller/new_base/url_for.rb | 40 ++++++++++++++++++++++ 4 files changed, 99 insertions(+) create mode 100644 actionpack/lib/action_controller/new_base.rb create mode 100644 actionpack/lib/action_controller/new_base/base.rb create mode 100644 actionpack/lib/action_controller/new_base/hide_actions.rb create mode 100644 actionpack/lib/action_controller/new_base/url_for.rb (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/new_base.rb b/actionpack/lib/action_controller/new_base.rb new file mode 100644 index 0000000000..2cef221de3 --- /dev/null +++ b/actionpack/lib/action_controller/new_base.rb @@ -0,0 +1,5 @@ +module ActionController + autoload :AbstractBase, "action_controller/new_base/base" + autoload :HideActions, "action_controller/new_base/hide_actions" + autoload :UrlFor, "action_controller/new_base/url_for" +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/base.rb b/actionpack/lib/action_controller/new_base/base.rb new file mode 100644 index 0000000000..ebe7c8dda6 --- /dev/null +++ b/actionpack/lib/action_controller/new_base/base.rb @@ -0,0 +1,26 @@ +module ActionController + class AbstractBase < AbstractController::Base + attr_internal :request, :response, :params + + def self.controller_name + @controller_name ||= controller_path.split("/").last + end + + def controller_name() self.class.controller_name end + + def self.controller_path + @controller_path ||= self.name.sub(/Controller$/, '').underscore + end + + def controller_path() self.class.controller_path end + + def self.action_methods + @action_names ||= Set.new(self.public_instance_methods - self::CORE_METHODS) + end + + def self.action_names() action_methods end + + def action_methods() self.class.action_names end + def action_names() action_methods end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/hide_actions.rb b/actionpack/lib/action_controller/new_base/hide_actions.rb new file mode 100644 index 0000000000..9847d7b086 --- /dev/null +++ b/actionpack/lib/action_controller/new_base/hide_actions.rb @@ -0,0 +1,28 @@ +module ActionController + module HideActions + def self.included(klass) + klass.class_eval do + extend ClassMethods + extlib_inheritable_accessor :hidden_actions + self.hidden_actions ||= Set.new + end + end + + def action_methods() self.class.action_names end + def action_names() action_methods end + + module ClassMethods + def hide_action(*args) + args.each do |arg| + self.hidden_actions << arg.to_s + end + end + + def action_methods + @action_names ||= Set.new(super.reject {|name| self.hidden_actions.include?(name.to_s)}) + end + + def self.action_names() action_methods end + end + end +end \ No newline at end of file diff --git a/actionpack/lib/action_controller/new_base/url_for.rb b/actionpack/lib/action_controller/new_base/url_for.rb new file mode 100644 index 0000000000..af5b21012b --- /dev/null +++ b/actionpack/lib/action_controller/new_base/url_for.rb @@ -0,0 +1,40 @@ +module ActionController + module UrlFor + def initialize_current_url + @url = UrlRewriter.new(request, params.clone) + end + + # Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in + # the form of a hash, just like the one you would use for url_for directly. Example: + # + # def default_url_options(options) + # { :project => @project.active? ? @project.url_name : "unknown" } + # end + # + # As you can infer from the example, this is mostly useful for situations where you want to centralize dynamic decisions about the + # urls as they stem from the business domain. Please note that any individual url_for call can always override the defaults set + # by this method. + def default_url_options(options = nil) + end + + def rewrite_options(options) #:nodoc: + if defaults = default_url_options(options) + defaults.merge(options) + else + options + end + end + + def url_for(options = {}) + options ||= {} + case options + when String + options + when Hash + @url.rewrite(rewrite_options(options)) + else + polymorphic_url(options) + end + end + end +end \ No newline at end of file -- cgit v1.2.3