Skip to content

Commit

Permalink
Convert to class_attribute
Browse files Browse the repository at this point in the history
  • Loading branch information
jeremy committed Feb 1, 2010
1 parent 8ae25a8 commit e5ab4b0
Show file tree
Hide file tree
Showing 22 changed files with 113 additions and 83 deletions.
10 changes: 5 additions & 5 deletions actionmailer/lib/action_mailer/base.rb
Expand Up @@ -268,13 +268,13 @@ class Base < AbstractController::Base

private_class_method :new #:nodoc:

extlib_inheritable_accessor :default_params
class_attribute :default_params
self.default_params = {
:mime_version => "1.0",
:charset => "utf-8",
:content_type => "text/plain",
:parts_order => [ "text/plain", "text/enriched", "text/html" ]
}
}.freeze

class << self

Expand All @@ -284,9 +284,9 @@ def mailer_name
attr_writer :mailer_name
alias :controller_path :mailer_name

def default(value=nil)
self.default_params.merge!(value) if value
self.default_params
def default(value = nil)
self.default_params = default_params.merge(value).freeze if value
default_params
end

# Receives a raw email, parses it into an email object, decodes it,
Expand Down
14 changes: 5 additions & 9 deletions actionmailer/lib/action_mailer/delivery_methods.rb
Expand Up @@ -7,8 +7,7 @@ module DeliveryMethods
extend ActiveSupport::Concern

included do
extlib_inheritable_accessor :delivery_methods, :delivery_method,
:instance_writer => false
class_attribute :delivery_methods, :delivery_method

# Do not make this inheritable, because we always want it to propagate
cattr_accessor :raise_delivery_errors
Expand All @@ -17,7 +16,7 @@ module DeliveryMethods
cattr_accessor :perform_deliveries
self.perform_deliveries = true

self.delivery_methods = {}
self.delivery_methods = {}.freeze
self.delivery_method = :smtp

add_delivery_method :smtp, Mail::SMTP,
Expand Down Expand Up @@ -53,12 +52,9 @@ module ClassMethods
# :arguments => '-i -t'
#
def add_delivery_method(symbol, klass, default_options={})
unless respond_to?(:"#{symbol}_settings")
extlib_inheritable_accessor(:"#{symbol}_settings", :instance_writer => false)
end

class_attribute(:"#{symbol}_settings") unless respond_to?(:"#{symbol}_settings")
send(:"#{symbol}_settings=", default_options)
self.delivery_methods[symbol.to_sym] = klass
self.delivery_methods = delivery_methods.merge(symbol.to_sym => klass).freeze
end

def wrap_delivery_behavior(mail, method=nil) #:nodoc:
Expand Down Expand Up @@ -87,4 +83,4 @@ def wrap_delivery_behavior!(*args) #:nodoc:
self.class.wrap_delivery_behavior(message, *args)
end
end
end
end
2 changes: 1 addition & 1 deletion actionmailer/lib/action_mailer/deprecated_api.rb
Expand Up @@ -47,7 +47,7 @@ def template_root
end

def template_root=(root)
ActiveSupport::Deprecation.warn "template_root= is deprecated, use view_paths.unshift instead", caller[0,2]
ActiveSupport::Deprecation.warn "template_root= is deprecated, use prepend_view_path instead", caller[0,2]
self.view_paths = ActionView::Base.process_view_paths(root)
end

Expand Down
24 changes: 10 additions & 14 deletions actionmailer/test/base_test.rb
Expand Up @@ -254,7 +254,7 @@ def different_layout(layout_name='')
end

test "subject gets default from I18n" do
BaseMailer.default[:subject] = nil
BaseMailer.default :subject => nil
email = BaseMailer.welcome(:subject => nil)
assert_equal "Welcome", email.subject

Expand Down Expand Up @@ -331,22 +331,24 @@ def different_layout(layout_name='')
end

test "implicit multipart with several view paths uses the first one with template" do
old = BaseMailer.view_paths
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "another.path"))
BaseMailer.view_paths = [File.join(FIXTURE_LOAD_PATH, "another.path")] + old.dup
email = BaseMailer.welcome
assert_equal("Welcome from another path", email.body.encoded)
ensure
BaseMailer.view_paths.shift
BaseMailer.view_paths = old
end
end

test "implicit multipart with inexistent templates uses the next view path" do
old = BaseMailer.view_paths
begin
BaseMailer.view_paths.unshift(File.join(FIXTURE_LOAD_PATH, "unknown"))
BaseMailer.view_paths = [File.join(FIXTURE_LOAD_PATH, "unknown")] + old.dup
email = BaseMailer.welcome
assert_equal("Welcome", email.body.encoded)
ensure
BaseMailer.view_paths.shift
BaseMailer.view_paths = old
end
end

Expand Down Expand Up @@ -503,16 +505,10 @@ def swap(klass, new_values)
end

def with_default(klass, new_values)
hash = klass.default
old_values = {}
new_values.each do |key, value|
old_values[key] = hash[key]
hash[key] = value
end
old = klass.default_params
klass.default(new_values)
yield
ensure
old_values.each do |key, value|
hash[key] = value
end
klass.default_params = old
end
end
6 changes: 4 additions & 2 deletions actionmailer/test/delivery_methods_test.rb
Expand Up @@ -45,7 +45,9 @@ def setup

def teardown
ActionMailer::Base.delivery_method = @old_delivery_method
ActionMailer::Base.delivery_methods.delete(:custom)
new = ActionMailer::Base.delivery_methods.dup
new.delete(:custom)
ActionMailer::Base.delivery_methods = new
end

test "allow to add custom delivery method" do
Expand Down Expand Up @@ -167,4 +169,4 @@ def teardown
assert_equal(0, DeliveryMailer.deliveries.length)
end

end
end
10 changes: 6 additions & 4 deletions actionpack/lib/abstract_controller/helpers.rb
@@ -1,4 +1,6 @@
require 'active_support/dependencies'
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/delegation'

module AbstractController
module Helpers
Expand All @@ -12,10 +14,10 @@ def self.next_serial
end

included do
extlib_inheritable_accessor(:_helpers) { Module.new }
extlib_inheritable_accessor(:_helper_serial) do
AbstractController::Helpers.next_serial
end
class_attribute :_helpers, :_helper_serial
delegate :_helpers, :to => :'self.class'
self._helpers = Module.new
self._helper_serial = ::AbstractController::Helpers.next_serial
end

module ClassMethods
Expand Down
8 changes: 6 additions & 2 deletions actionpack/lib/abstract_controller/layouts.rb
@@ -1,3 +1,6 @@
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/delegation'

module AbstractController
# Layouts reverse the common pattern of including shared headers and footers in many templates to isolate changes in
# repeated setups. The inclusion pattern has pages that look like this:
Expand Down Expand Up @@ -161,8 +164,9 @@ module Layouts
include Rendering

included do
extlib_inheritable_accessor(:_layout_conditions) { Hash.new }
extlib_inheritable_accessor(:_action_has_layout) { Hash.new }
class_attribute :_layout_conditions
delegate :_layout_conditions, :to => :'self.class'
self._layout_conditions = {}
_write_layout_method
end

Expand Down
18 changes: 11 additions & 7 deletions actionpack/lib/abstract_controller/rendering.rb
@@ -1,4 +1,7 @@
require "abstract_controller/base"
require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/array/wrap'

module AbstractController
class DoubleRenderError < Error
Expand All @@ -13,8 +16,9 @@ module Rendering
extend ActiveSupport::Concern

included do
extlib_inheritable_accessor :_view_paths
self._view_paths ||= ActionView::PathSet.new
class_attribute :_view_paths
delegate :_view_paths, :to => :'self.class'
self._view_paths = ActionView::PathSet.new
end

# An instance of a view class. The default view class is ActionView::Base
Expand Down Expand Up @@ -156,7 +160,6 @@ def _determine_template(options)
elsif options.key?(:file)
options[:_template_name] = options[:file]
end

name = (options[:_template_name] || options[:action] || action_name).to_s
options[:_prefix] ||= _prefix if (options.keys & [:partial, :file, :template]).empty?

Expand Down Expand Up @@ -201,7 +204,7 @@ def clear_template_caches!
# the default view path. You may also provide a custom view path
# (see ActionView::ViewPathSet for more information)
def append_view_path(path)
self.view_paths << path
self.view_paths = view_paths.dup + Array.wrap(path)
end

# Prepend a path to the list of view paths for this controller.
Expand All @@ -212,12 +215,12 @@ def append_view_path(path)
# (see ActionView::ViewPathSet for more information)
def prepend_view_path(path)
clear_template_caches!
self.view_paths.unshift(path)
self.view_paths = Array.wrap(path) + view_paths.dup
end

# A list of all of the default view paths for this controller.
def view_paths
self._view_paths
_view_paths
end

# Set the view paths.
Expand All @@ -228,7 +231,8 @@ def view_paths
def view_paths=(paths)
clear_template_caches!
self._view_paths = paths.is_a?(ActionView::PathSet) ? paths : ActionView::Base.process_view_paths(paths)
_view_paths.freeze
end
end
end
end
end
9 changes: 7 additions & 2 deletions actionpack/lib/action_controller/metal.rb
@@ -1,4 +1,4 @@
require 'active_support/core_ext/class/inheritable_attributes'
require 'active_support/core_ext/class/attribute'

module ActionController
# ActionController::Metal provides a way to get a valid Rack application from a controller.
Expand Down Expand Up @@ -90,7 +90,12 @@ def call(env)
end
end

extlib_inheritable_accessor(:middleware_stack) { ActionDispatch::MiddlewareStack.new }
class_attribute :middleware_stack
self.middleware_stack = ActionDispatch::MiddlewareStack.new

def self.inherited(base)
self.middleware_stack = base.middleware_stack.dup
end

def self.use(*args)
middleware_stack.use(*args)
Expand Down
4 changes: 3 additions & 1 deletion actionpack/lib/action_controller/metal/helpers.rb
@@ -1,3 +1,5 @@
require 'active_support/core_ext/class/attribute'

module ActionController
# The Rails framework provides a large number of helpers for working with +assets+, +dates+, +forms+,
# +numbers+ and model objects, to name a few. These helpers are available to all templates
Expand Down Expand Up @@ -50,7 +52,7 @@ module Helpers
include AbstractController::Helpers

included do
extlib_inheritable_accessor(:helpers_path)
class_attribute :helpers_path
self.helpers_path = []
end

Expand Down
9 changes: 6 additions & 3 deletions actionpack/lib/action_controller/metal/hide_actions.rb
@@ -1,11 +1,14 @@
require 'active_support/core_ext/class/attribute'

module ActionController
# ActionController::HideActions adds the ability to prevent public methods on a controller
# to be called as actions.
module HideActions
extend ActiveSupport::Concern

included do
extlib_inheritable_accessor(:hidden_actions) { Set.new }
class_attribute :hidden_actions
self.hidden_actions = Set.new
end

private
Expand All @@ -14,7 +17,7 @@ module HideActions
# action name is in the list of hidden actions.
def action_method?(action_name)
self.class.visible_action?(action_name) do
!hidden_actions.include?(action_name) && super
!self.class.hidden_actions.include?(action_name) && super
end
end

Expand All @@ -24,7 +27,7 @@ module ClassMethods
# ==== Parameters
# *args<#to_s>:: A list of actions
def hide_action(*args)
hidden_actions.merge(args.map! {|a| a.to_s })
self.hidden_actions = hidden_actions.dup.merge(args.map(&:to_s))
end

def inherited(klass)
Expand Down
21 changes: 12 additions & 9 deletions actionpack/lib/action_controller/metal/mime_responds.rb
@@ -1,11 +1,12 @@
require 'abstract_controller/collector'
require 'active_support/core_ext/class/attribute'

module ActionController #:nodoc:
module MimeResponds #:nodoc:
extend ActiveSupport::Concern

included do
extlib_inheritable_accessor :responder, :mimes_for_respond_to, :instance_writer => false
class_attribute :responder, :mimes_for_respond_to
self.responder = ActionController::Responder
clear_respond_to
end
Expand Down Expand Up @@ -38,18 +39,20 @@ def respond_to(*mimes)
only_actions = Array(options.delete(:only))
except_actions = Array(options.delete(:except))

new = mimes_for_respond_to.dup
mimes.each do |mime|
mime = mime.to_sym
mimes_for_respond_to[mime] = {}
mimes_for_respond_to[mime][:only] = only_actions unless only_actions.empty?
mimes_for_respond_to[mime][:except] = except_actions unless except_actions.empty?
new[mime] = {}
new[mime][:only] = only_actions unless only_actions.empty?
new[mime][:except] = except_actions unless except_actions.empty?
end
self.mimes_for_respond_to = new.freeze
end

# Clear all mimes in respond_to.
#
def clear_respond_to
self.mimes_for_respond_to = ActiveSupport::OrderedHash.new
self.mimes_for_respond_to = ActiveSupport::OrderedHash.new.freeze
end
end

Expand Down Expand Up @@ -218,12 +221,12 @@ def respond_to(*mimes, &block)
#
def respond_with(*resources, &block)
raise "In order to use respond_with, first you need to declare the formats your " <<
"controller responds to in the class level" if mimes_for_respond_to.empty?
"controller responds to in the class level" if self.class.mimes_for_respond_to.empty?

if response = retrieve_response_from_mimes(&block)
options = resources.extract_options!
options.merge!(:default_response => response)
(options.delete(:responder) || responder).call(self, resources, options)
(options.delete(:responder) || self.class.responder).call(self, resources, options)
end
end

Expand All @@ -235,8 +238,8 @@ def respond_with(*resources, &block)
def collect_mimes_from_class_level #:nodoc:
action = action_name.to_sym

mimes_for_respond_to.keys.select do |mime|
config = mimes_for_respond_to[mime]
self.class.mimes_for_respond_to.keys.select do |mime|
config = self.class.mimes_for_respond_to[mime]

if config[:except]
!config[:except].include?(action)
Expand Down

0 comments on commit e5ab4b0

Please sign in to comment.