1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

remove detail initialization metaprogramming

This metaprogrammed method doesn't seem to be a bottleneck, so lets just
use a regular method so it's easier to understand.  We can follow up
with more interesting techniques for cache manipulation soon.
This commit is contained in:
Aaron Patterson 2016-02-11 15:42:45 -08:00
parent 448276651c
commit 737b718ea0

View file

@ -22,7 +22,7 @@ module ActionView
def self.register_detail(name, &block) def self.register_detail(name, &block)
self.registered_details << name self.registered_details << name
initialize = registered_details.map { |n| "@details[:#{n}] = details[:#{n}] || default_#{n}" } Accessors::DEFAULT_PROCS[name] = block
Accessors.send :define_method, :"default_#{name}", &block Accessors.send :define_method, :"default_#{name}", &block
Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1 Accessors.module_eval <<-METHOD, __FILE__, __LINE__ + 1
@ -34,16 +34,12 @@ module ActionView
value = value.present? ? Array(value) : default_#{name} value = value.present? ? Array(value) : default_#{name}
_set_detail(:#{name}, value) if value != @details[:#{name}] _set_detail(:#{name}, value) if value != @details[:#{name}]
end end
remove_possible_method :initialize_details
def initialize_details(details)
#{initialize.join("\n")}
end
METHOD METHOD
end end
# Holds accessors for the registered details. # Holds accessors for the registered details.
module Accessors #:nodoc: module Accessors #:nodoc:
DEFAULT_PROCS = {}
end end
register_detail(:locale) do register_detail(:locale) do
@ -195,15 +191,23 @@ module ActionView
include ViewPaths include ViewPaths
def initialize(view_paths, details = {}, prefixes = []) def initialize(view_paths, details = {}, prefixes = [])
@details, @details_key = {}, nil @details_key = nil
@cache = true @cache = true
@prefixes = prefixes @prefixes = prefixes
@rendered_format = nil @rendered_format = nil
@details = initialize_details({}, details)
self.view_paths = view_paths self.view_paths = view_paths
initialize_details(details)
end end
def initialize_details(target, details)
registered_details.each do |k|
target[k] = details[k] || Accessors::DEFAULT_PROCS[k].call
end
target
end
private :initialize_details
# Override formats= to expand ["*/*"] values and automatically # Override formats= to expand ["*/*"] values and automatically
# add :html as fallback to :js. # add :html as fallback to :js.
def formats=(values) def formats=(values)