Refactor ActionMailer to not use hide_actions

This commit is contained in:
Piotr Sarnacki 2010-08-03 19:36:53 +02:00
parent 4131a2d804
commit 79bd92b783
7 changed files with 40 additions and 69 deletions

View File

@ -26,6 +26,7 @@ $:.unshift(actionpack_path) if File.directory?(actionpack_path) && !$:.include?(
require 'abstract_controller'
require 'action_view'
require 'action_dispatch'
# Common Active Support usage in Action Mailer
require 'active_support/core_ext/class'

View File

@ -5,7 +5,6 @@ require 'active_support/core_ext/array/wrap'
require 'active_support/core_ext/object/blank'
require 'active_support/core_ext/proc'
require 'action_mailer/log_subscriber'
require 'action_mailer/hide_actions'
module ActionMailer #:nodoc:
# Action Mailer allows you to send email from your application using a mailer model and views.
@ -341,13 +340,13 @@ module ActionMailer #:nodoc:
include AbstractController::Helpers
include AbstractController::Translation
include AbstractController::AssetPaths
include AbstractController::UrlFor
cattr_reader :protected_instance_variables
@@protected_instance_variables = []
helper ActionMailer::MailHelper
include ActionMailer::OldApi
include ActionMailer::HideActions
delegate :register_observer, :to => Mail
delegate :register_interceptor, :to => Mail
@ -364,9 +363,8 @@ module ActionMailer #:nodoc:
class << self
def inherited(klass)
klass.with_hiding_actions do
super(klass)
end
klass.class_eval { @action_methods = nil }
end
def mailer_name
@ -732,9 +730,6 @@ module ActionMailer #:nodoc:
container.add_part(part)
end
class_attribute :default_url_options
self.default_url_options = {}
ActiveSupport.run_load_hooks(:action_mailer, self)
end
end

View File

@ -1,46 +0,0 @@
require 'active_support/core_ext/class/attribute'
module ActionMailer
# ActionController::HideActions adds the ability to prevent public methods on a controller
# to be called as actions.
module HideActions
extend ActiveSupport::Concern
included do
class_attribute :hidden_actions
self.hidden_actions = Set.new.freeze
end
private
module ClassMethods
# Sets all of the actions passed in as hidden actions.
#
# ==== Parameters
# *args<#to_s>:: A list of actions
def hide_action(*args)
self.hidden_actions = hidden_actions.dup.merge(args.map(&:to_s)).freeze
end
# Run block and add all the new action_methods to hidden_actions.
# This is used in inherited method.
def with_hiding_actions
yield
clear_action_methods!
hide_action(*action_methods)
clear_action_methods!
end
def clear_action_methods!
@action_methods = nil
end
# Overrides AbstractController::Base#action_methods to remove any methods
# that are listed as hidden methods.
def action_methods
@action_methods ||= super.reject { |name| hidden_actions.include?(name) }
end
end
end
end

View File

@ -24,4 +24,5 @@ module AbstractController
autoload :Translation
autoload :AssetPaths
autoload :ViewPaths
autoload :UrlFor
end

View File

@ -0,0 +1,28 @@
module AbstractController
module UrlFor
extend ActiveSupport::Concern
include ActionDispatch::Routing::UrlFor
def _routes
raise "In order to use #url_for, you must include routing helpers explicitly. " \
"For instance, `include Rails.application.routes.url_helpers"
end
module ClassMethods
def _routes
nil
end
def action_methods
@action_methods ||= begin
if _routes
super - _routes.named_routes.helper_names
else
super
end
end
end
end
end
end

View File

@ -2,7 +2,7 @@ module ActionController
module UrlFor
extend ActiveSupport::Concern
include ActionDispatch::Routing::UrlFor
include AbstractController::UrlFor
def url_options
options = {}
@ -16,18 +16,5 @@ module ActionController
:_path_segments => request.symbolized_path_parameters
)
end
def _routes
raise "In order to use #url_for, you must include routing helpers explicitly. " \
"For instance, `include Rails.application.routes.url_helpers"
end
module ClassMethods
def action_methods
@action_methods ||= begin
super - _routes.named_routes.helper_names
end
end
end
end
end

View File

@ -337,7 +337,12 @@ module ActionDispatch
# Yes plz - JP
included do
routes.install_helpers(self)
singleton_class.send(:define_method, :_routes) { @_routes || routes }
singleton_class.send(:define_method, :_routes) { routes }
end
def initialize(*)
@_routes = nil
super
end
define_method(:_routes) { @_routes || routes }