Skip to content
Browse files

Convert to class_attribute

  • Loading branch information...
1 parent 8ae25a8 commit e5ab4b0d07ade8d89d633ca744c0eafbc53ee921 @jeremy jeremy committed Jan 31, 2010
View
10 actionmailer/lib/action_mailer/base.rb
@@ -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
@@ -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,
View
14 actionmailer/lib/action_mailer/delivery_methods.rb
@@ -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
@@ -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,
@@ -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:
@@ -87,4 +83,4 @@ def wrap_delivery_behavior!(*args) #:nodoc:
self.class.wrap_delivery_behavior(message, *args)
end
end
-end
+end
View
2 actionmailer/lib/action_mailer/deprecated_api.rb
@@ -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
View
24 actionmailer/test/base_test.rb
@@ -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
@@ -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
@@ -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
View
6 actionmailer/test/delivery_methods_test.rb
@@ -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
@@ -167,4 +169,4 @@ def teardown
assert_equal(0, DeliveryMailer.deliveries.length)
end
-end
+end
View
10 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
@@ -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
View
8 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:
@@ -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
View
18 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
@@ -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
@@ -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?
@@ -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.
@@ -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.
@@ -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
View
9 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.
@@ -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)
View
4 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
@@ -50,7 +52,7 @@ module Helpers
include AbstractController::Helpers
included do
- extlib_inheritable_accessor(:helpers_path)
+ class_attribute :helpers_path
self.helpers_path = []
end
View
9 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
@@ -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
@@ -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)
View
21 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
@@ -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
@@ -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
@@ -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)
View
10 actionpack/lib/action_controller/metal/renderers.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/class/attribute'
+
module ActionController
def self.add_renderer(key, &block)
Renderers.add(key, &block)
@@ -7,8 +9,8 @@ module Renderers
extend ActiveSupport::Concern
included do
- extlib_inheritable_accessor :_renderers
- self._renderers = {}
+ class_attribute :_renderers
+ self._renderers = {}.freeze
end
module ClassMethods
@@ -30,9 +32,11 @@ def _handle_render_options(options)
end
def use_renderers(*args)
+ new = _renderers.dup
args.each do |key|
- _renderers[key] = RENDERERS[key]
+ new[key] = RENDERERS[key]
end
+ self._renderers = new.freeze
_write_render_options
end
alias use_renderer use_renderers
View
6 actionpack/lib/action_controller/metal/request_forgery_protection.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/class/attribute'
+
module ActionController #:nodoc:
class InvalidAuthenticityToken < ActionControllerError #:nodoc:
end
@@ -13,7 +15,7 @@ module RequestForgeryProtection
cattr_accessor :request_forgery_protection_token
# Controls whether request forgergy protection is turned on or not. Turned off by default only in test mode.
- extlib_inheritable_accessor :allow_forgery_protection
+ class_attribute :allow_forgery_protection
self.allow_forgery_protection = true
helper_method :form_authenticity_token
@@ -107,7 +109,7 @@ def form_authenticity_param
end
def protect_against_forgery?
- allow_forgery_protection
+ self.class.allow_forgery_protection
end
end
end
View
7 actionpack/lib/action_controller/metal/url_for.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/class/attribute'
+
module ActionController
# In <b>routes.rb</b> one defines URL-to-controller mappings, but the reverse
# is also possible: an URL can be generated from one of your routing definitions.
@@ -85,9 +87,8 @@ module UrlFor
included do
ActionController::Routing::Routes.install_helpers(self)
- extlib_inheritable_accessor :default_url_options,
- :instance_writer => false, :instance_reader => false
- self.default_url_options ||= {}
+ class_attribute :default_url_options
+ self.default_url_options = {}
end
# Overwrite to implement a number of default options that all url_for-based methods will use. The default options should come in
View
3 actionpack/lib/action_view/base.rb
@@ -1,5 +1,6 @@
require 'active_support/core_ext/module/attr_internal'
require 'active_support/core_ext/module/delegation'
+require 'active_support/core_ext/class/attribute'
module ActionView #:nodoc:
class ActionViewError < StandardError #:nodoc:
@@ -244,7 +245,7 @@ def self.process_view_paths(value)
ActionView::PathSet.new(Array(value))
end
- extlib_inheritable_accessor :helpers
+ class_attribute :helpers
attr_reader :helpers
def self.for_controller(controller)
View
4 actionpack/lib/action_view/template/handler.rb
@@ -1,5 +1,5 @@
-require "active_support/core_ext/class/inheritable_attributes"
require "action_dispatch/http/mime_type"
+require 'active_support/core_ext/class/attribute'
# Legacy TemplateHandler stub
module ActionView
@@ -23,7 +23,7 @@ def compile(template)
end
class Template::Handler
- extlib_inheritable_accessor :default_format
+ class_attribute :default_format
self.default_format = Mime::HTML
def self.call(template)
View
3 actionpack/lib/action_view/template/resolver.rb
@@ -1,5 +1,6 @@
require "pathname"
require "active_support/core_ext/class"
+require "active_support/core_ext/array/wrap"
require "action_view/template"
module ActionView
@@ -11,7 +12,7 @@ class Resolver
def self.register_detail(name, options = {})
registered_details[name] = lambda do |val|
- val ||= yield
+ val = Array.wrap(val || yield)
val |= [nil] unless options[:allow_nil] == false
val
end
View
4 actionpack/test/controller/view_paths_test.rb
@@ -142,9 +142,9 @@ class C < ActionController::Base; end
assert_paths A, "a/path"
assert_paths A, *B.view_paths
assert_paths C, *original_load_paths
-
+
C.view_paths = []
- assert_nothing_raised { C.view_paths << 'c/path' }
+ assert_nothing_raised { C.append_view_path 'c/path' }
assert_paths C, "c/path"
end
end
View
9 activerecord/lib/active_record/observer.rb
@@ -1,3 +1,5 @@
+require 'active_support/core_ext/class/attribute'
+
module ActiveRecord
# Observer classes respond to lifecycle callbacks to implement trigger-like
# behavior outside the original class. This is a great way to reduce the
@@ -85,15 +87,16 @@ module ActiveRecord
# singletons and that call instantiates and registers them.
#
class Observer < ActiveModel::Observer
- extlib_inheritable_accessor(:observed_methods){ [] }
+ class_attribute :observed_methods
+ self.observed_methods = []
def initialize
super
observed_subclasses.each { |klass| add_observer!(klass) }
end
def self.method_added(method)
- observed_methods << method if ActiveRecord::Callbacks::CALLBACKS.include?(method.to_sym)
+ self.observed_methods += [method] if ActiveRecord::Callbacks::CALLBACKS.include?(method.to_sym)
end
protected
@@ -106,7 +109,7 @@ def add_observer!(klass)
# Check if a notifier callback was already added to the given class. If
# it was not, add it.
- self.observed_methods.each do |method|
+ self.class.observed_methods.each do |method|
callback = :"_notify_observers_for_#{method}"
if (klass.instance_methods & [callback, callback.to_s]).empty?
klass.class_eval "def #{callback}; notify_observers(:#{method}); end"
View
8 activesupport/lib/active_support/rescuable.rb
@@ -1,4 +1,4 @@
-require 'active_support/core_ext/class/inheritable_attributes'
+require 'active_support/core_ext/class/attribute'
require 'active_support/core_ext/proc'
require 'active_support/core_ext/string/inflections'
require 'active_support/core_ext/array/extract_options'
@@ -9,7 +9,7 @@ module Rescuable
extend Concern
included do
- class_inheritable_accessor :rescue_handlers
+ class_attribute :rescue_handlers
self.rescue_handlers = []
end
@@ -67,7 +67,7 @@ def rescue_from(*klasses, &block)
end
# put the new handler at the end because the list is read in reverse
- rescue_handlers << [key, options[:with]]
+ self.rescue_handlers += [[key, options[:with]]]
end
end
end
@@ -83,7 +83,7 @@ def rescue_with_handler(exception)
def handler_for_rescue(exception)
# We go from right to left because pairs are pushed onto rescue_handlers
# as rescue_from declarations are found.
- _, rescuer = rescue_handlers.reverse.detect do |klass_name, handler|
+ _, rescuer = self.class.rescue_handlers.reverse.detect do |klass_name, handler|
# The purpose of allowing strings in rescue_from is to support the
# declaration of handler associations for exception classes whose
# definition is yet unknown.
View
7 railties/lib/rails/generators/test_case.rb
@@ -1,4 +1,5 @@
-require 'active_support/core_ext/class/inheritable_attributes'
+require 'active_support/core_ext/class/attribute'
+require 'active_support/core_ext/module/delegation'
require 'active_support/core_ext/hash/reverse_merge'
require 'rails/generators'
require 'fileutils'
@@ -28,8 +29,8 @@ module Generators
class TestCase < ActiveSupport::TestCase
include FileUtils
- extlib_inheritable_accessor :destination_root, :current_path, :generator_class,
- :default_arguments, :instance_writer => false
+ class_attribute :destination_root, :current_path, :generator_class, :default_arguments
+ delegate :destination_root, :current_path, :generator_class, :default_arguments, :to => :'self.class'
# Generators frequently change the current path using +FileUtils.cd+.
# So we need to store the path at file load and revert back to it after each test.

0 comments on commit e5ab4b0

Please sign in to comment.
Something went wrong with that request. Please try again.