Permalink
Browse files

Add option for class_attribute default (#29270)

* Allow a default value to be declared for class_attribute

* Convert to using class_attribute default rather than explicit setter

* Removed instance_accessor option by mistake

* False is a valid default value

* Documentation
  • Loading branch information...
dhh committed May 29, 2017
1 parent edc90c8 commit 1c275d812f35f53f93cd96184a4f319983766cc5
Showing with 111 additions and 161 deletions.
  1. +1 −2 actioncable/lib/action_cable/channel/periodic_timers.rb
  2. +1 −2 actioncable/lib/action_cable/connection/identification.rb
  3. +1 −2 actionmailer/lib/action_mailer/base.rb
  4. +2 −4 actionmailer/lib/action_mailer/delivery_methods.rb
  5. +1 −2 actionpack/lib/abstract_controller/caching.rb
  6. +2 −5 actionpack/lib/abstract_controller/helpers.rb
  7. +1 −2 actionpack/lib/action_controller/metal.rb
  8. +1 −2 actionpack/lib/action_controller/metal/conditional_get.rb
  9. +1 −2 actionpack/lib/action_controller/metal/etag_with_template_digest.rb
  10. +1 −2 actionpack/lib/action_controller/metal/flash.rb
  11. +2 −3 actionpack/lib/action_controller/metal/helpers.rb
  12. +1 −2 actionpack/lib/action_controller/metal/params_wrapper.rb
  13. +1 −2 actionpack/lib/action_controller/metal/renderers.rb
  14. +9 −8 actionview/lib/action_view/helpers/form_helper.rb
  15. +3 −3 actionview/lib/action_view/layouts.rb
  16. +1 −4 actionview/lib/action_view/template/handlers/builder.rb
  17. +3 −6 actionview/lib/action_view/template/handlers/erb.rb
  18. +1 −3 actionview/lib/action_view/view_paths.rb
  19. +2 −5 activejob/lib/active_job/queue_name.rb
  20. +1 −3 activejob/lib/active_job/queue_priority.rb
  21. +2 −3 activemodel/lib/active_model/attribute_methods.rb
  22. +1 −2 activemodel/lib/active_model/serializers/json.rb
  23. +1 −2 activemodel/lib/active_model/validations.rb
  24. +1 −2 activerecord/lib/active_record/attribute_decorators.rb
  25. +1 −2 activerecord/lib/active_record/attribute_methods/dirty.rb
  26. +2 −5 activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb
  27. +1 −2 activerecord/lib/active_record/attributes.rb
  28. +1 −2 activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
  29. +1 −2 activerecord/lib/active_record/enum.rb
  30. +6 −14 activerecord/lib/active_record/fixtures.rb
  31. +1 −2 activerecord/lib/active_record/inheritance.rb
  32. +2 −4 activerecord/lib/active_record/integration.rb
  33. +1 −2 activerecord/lib/active_record/locking/optimistic.rb
  34. +7 −20 activerecord/lib/active_record/model_schema.rb
  35. +1 −2 activerecord/lib/active_record/nested_attributes.rb
  36. +1 −2 activerecord/lib/active_record/readonly_attributes.rb
  37. +2 −4 activerecord/lib/active_record/reflection.rb
  38. +2 −5 activerecord/lib/active_record/scoping/default.rb
  39. +1 −2 activerecord/lib/active_record/timestamp.rb
  40. +11 −0 activesupport/CHANGELOG.md
  41. +1 −2 activesupport/lib/active_support/callbacks.rb
  42. +11 −2 activesupport/lib/active_support/core_ext/class/attribute.rb
  43. +2 −5 activesupport/lib/active_support/reloader.rb
  44. +1 −2 activesupport/lib/active_support/rescuable.rb
  45. +9 −1 activesupport/test/core_ext/class/attribute_test.rb
  46. +4 −4 railties/lib/rails/generators/testing/behaviour.rb
  47. +1 −2 railties/lib/rails/test_unit/reporter.rb
@@ -4,8 +4,7 @@ module PeriodicTimers
extend ActiveSupport::Concern
included do
class_attribute :periodic_timers, instance_reader: false
self.periodic_timers = []
class_attribute :periodic_timers, instance_reader: false, default: []
after_subscribe :start_periodic_timers
after_unsubscribe :stop_periodic_timers
@@ -6,8 +6,7 @@ module Identification
extend ActiveSupport::Concern
included do
class_attribute :identifiers
self.identifiers = Set.new
class_attribute :identifiers, default: Set.new
end
class_methods do
@@ -459,8 +459,7 @@ def _protected_ivars # :nodoc:
helper ActionMailer::MailHelper
class_attribute :default_params
self.default_params = {
class_attribute :default_params, default: {
mime_version: "1.0",
charset: "UTF-8",
content_type: "text/plain",
@@ -7,8 +7,6 @@ module DeliveryMethods
extend ActiveSupport::Concern
included do
class_attribute :delivery_methods, :delivery_method
# Do not make this inheritable, because we always want it to propagate
cattr_accessor :raise_delivery_errors
self.raise_delivery_errors = true
@@ -19,8 +17,8 @@ module DeliveryMethods
cattr_accessor :deliver_later_queue_name
self.deliver_later_queue_name = :mailers
self.delivery_methods = {}.freeze
self.delivery_method = :smtp
class_attribute :delivery_methods, default: {}.freeze
class_attribute :delivery_method, default: :smtp
add_delivery_method :smtp, Mail::SMTP,
address: "localhost",
@@ -37,8 +37,7 @@ def cache_configured?
config_accessor :enable_fragment_cache_logging
self.enable_fragment_cache_logging = false
class_attribute :_view_cache_dependencies
self._view_cache_dependencies = []
class_attribute :_view_cache_dependencies, default: []
helper_method :view_cache_dependencies if respond_to?(:helper_method)
end
@@ -5,11 +5,8 @@ module Helpers
extend ActiveSupport::Concern
included do
class_attribute :_helpers
self._helpers = Module.new
class_attribute :_helper_methods
self._helper_methods = Array.new
class_attribute :_helpers, default: Module.new
class_attribute :_helper_methods, default: Array.new
end
class MissingHelperError < LoadError
@@ -208,8 +208,7 @@ def reset_session
@_request.reset_session
end
class_attribute :middleware_stack
self.middleware_stack = ActionController::MiddlewareStack.new
class_attribute :middleware_stack, default: ActionController::MiddlewareStack.new
def self.inherited(base) # :nodoc:
base.middleware_stack = middleware_stack.dup
@@ -7,8 +7,7 @@ module ConditionalGet
include Head
included do
class_attribute :etaggers
self.etaggers = []
class_attribute :etaggers, default: []
end
module ClassMethods
@@ -22,8 +22,7 @@ module EtagWithTemplateDigest
include ActionController::ConditionalGet
included do
class_attribute :etag_with_template_digest
self.etag_with_template_digest = true
class_attribute :etag_with_template_digest, default: true
ActiveSupport.on_load :action_view, yield: true do
etag do |options|
@@ -3,8 +3,7 @@ module Flash
extend ActiveSupport::Concern
included do
class_attribute :_flash_types, instance_accessor: false
self._flash_types = []
class_attribute :_flash_types, instance_accessor: false, default: []
delegate :flash, to: :request
add_flash_types(:alert, :notice)
@@ -53,9 +53,8 @@ class << self; attr_accessor :helpers_path; end
include AbstractController::Helpers
included do
class_attribute :helpers_path, :include_all_helpers
self.helpers_path ||= []
self.include_all_helpers = true
class_attribute :helpers_path, default: []
class_attribute :include_all_helpers, default: true
end
module ClassMethods
@@ -159,8 +159,7 @@ def _default_wrap_model
end
included do
class_attribute :_wrapper_options
self._wrapper_options = Options.from_hash(format: [])
class_attribute :_wrapper_options, default: Options.from_hash(format: [])
end
module ClassMethods
@@ -26,8 +26,7 @@ module Renderers
RENDERERS = Set.new
included do
class_attribute :_renderers
self._renderers = Set.new.freeze
class_attribute :_renderers, default: Set.new.freeze
end
# Used in <tt>ActionController::Base</tt>
@@ -1606,14 +1606,15 @@ class FormBuilder
include ModelNaming
# The methods which wrap a form helper call.
class_attribute :field_helpers
self.field_helpers = [:fields_for, :fields, :label, :text_field, :password_field,
:hidden_field, :file_field, :text_area, :check_box,
:radio_button, :color_field, :search_field,
:telephone_field, :phone_field, :date_field,
:time_field, :datetime_field, :datetime_local_field,
:month_field, :week_field, :url_field, :email_field,
:number_field, :range_field]
class_attribute :field_helpers, default: [
:fields_for, :fields, :label, :text_field, :password_field,
:hidden_field, :file_field, :text_area, :check_box,
:radio_button, :color_field, :search_field,
:telephone_field, :phone_field, :date_field,
:time_field, :datetime_field, :datetime_local_field,
:month_field, :week_field, :url_field, :email_field,
:number_field, :range_field
]
attr_accessor :object_name, :object, :options
@@ -204,9 +204,9 @@ module Layouts
include ActionView::Rendering
included do
class_attribute :_layout, :_layout_conditions, instance_accessor: false
self._layout = nil
self._layout_conditions = {}
class_attribute :_layout, instance_accessor: false
class_attribute :_layout_conditions, instance_accessor: false, default: {}
_write_layout_method
end
@@ -1,9 +1,7 @@
module ActionView
module Template::Handlers
class Builder
# Default format used by Builder.
class_attribute :default_format
self.default_format = :xml
class_attribute :default_format, default: :xml
def call(template)
require_engine
@@ -14,7 +12,6 @@ def call(template)
end
private
def require_engine # :doc:
@required ||= begin
require "builder"
@@ -9,16 +9,13 @@ class ERB
# Specify trim mode for the ERB compiler. Defaults to '-'.
# See ERB documentation for suitable values.
class_attribute :erb_trim_mode
self.erb_trim_mode = "-"
class_attribute :erb_trim_mode, default: "-"
# Default implementation used.
class_attribute :erb_implementation
self.erb_implementation = Erubi
class_attribute :erb_implementation, default: Erubi
# Do not escape templates of these mime types.
class_attribute :escape_whitelist
self.escape_whitelist = ["text/plain"]
class_attribute :escape_whitelist, default: ["text/plain"]
ENCODING_TAG = Regexp.new("\\A(<%#{ENCODING_FLAG}-?%>)[ \\t]*")
@@ -3,9 +3,7 @@ module ViewPaths
extend ActiveSupport::Concern
included do
class_attribute :_view_paths
self._view_paths = ActionView::PathSet.new
_view_paths.freeze
class_attribute :_view_paths, default: ActionView::PathSet.new.freeze
end
delegate :template_exists?, :any_templates?, :view_paths, :formats, :formats=,
@@ -32,11 +32,8 @@ def queue_name_from_part(part_name) #:nodoc:
end
included do
class_attribute :queue_name, instance_accessor: false
class_attribute :queue_name_delimiter, instance_accessor: false
self.queue_name = default_queue_name
self.queue_name_delimiter = "_" # set default delimiter to '_'
class_attribute :queue_name, instance_accessor: false, default: default_queue_name
class_attribute :queue_name_delimiter, instance_accessor: false, default: "_"
end
# Returns the name of the queue the job will be run on.
@@ -27,9 +27,7 @@ def queue_with_priority(priority = nil, &block)
end
included do
class_attribute :priority, instance_accessor: false
self.priority = default_priority
class_attribute :priority, instance_accessor: false, default: default_priority
end
# Returns the priority that the job will be created with
@@ -68,9 +68,8 @@ module AttributeMethods
CALL_COMPILABLE_REGEXP = /\A[a-zA-Z_]\w*[!?]?\z/
included do
class_attribute :attribute_aliases, :attribute_method_matchers, instance_writer: false
self.attribute_aliases = {}
self.attribute_method_matchers = [ClassMethods::AttributeMethodMatcher.new]
class_attribute :attribute_aliases, instance_writer: false, default: {}
class_attribute :attribute_method_matchers, instance_writer: false, default: [ ClassMethods::AttributeMethodMatcher.new ]
end
module ClassMethods
@@ -10,8 +10,7 @@ module JSON
included do
extend ActiveModel::Naming
class_attribute :include_root_in_json, instance_writer: false
self.include_root_in_json = false
class_attribute :include_root_in_json, instance_writer: false, default: false
end
# Returns a hash representing the model. Some configuration can be
@@ -49,8 +49,7 @@ module Validations
private :validation_context=
define_callbacks :validate, scope: :name
class_attribute :_validators, instance_writer: false
self._validators = Hash.new { |h, k| h[k] = [] }
class_attribute :_validators, instance_writer: false, default: Hash.new { |h, k| h[k] = [] }
end
module ClassMethods
@@ -3,8 +3,7 @@ module AttributeDecorators # :nodoc:
extend ActiveSupport::Concern
included do
class_attribute :attribute_type_decorations, instance_accessor: false # :internal:
self.attribute_type_decorations = TypeDecorator.new
class_attribute :attribute_type_decorations, instance_accessor: false, default: TypeDecorator.new # :internal:
end
module ClassMethods # :nodoc:
@@ -14,8 +14,7 @@ module Dirty # :nodoc:
raise "You cannot include Dirty after Timestamp"
end
class_attribute :partial_writes, instance_writer: false
self.partial_writes = true
class_attribute :partial_writes, instance_writer: false, default: true
after_create { changes_internally_applied }
after_update { changes_internally_applied }
@@ -57,11 +57,8 @@ def map_avoiding_infinite_recursion(value)
mattr_accessor :time_zone_aware_attributes, instance_writer: false
self.time_zone_aware_attributes = false
class_attribute :skip_time_zone_conversion_for_attributes, instance_writer: false
self.skip_time_zone_conversion_for_attributes = []
class_attribute :time_zone_aware_types, instance_writer: false
self.time_zone_aware_types = [:datetime, :time]
class_attribute :skip_time_zone_conversion_for_attributes, instance_writer: false, default: []
class_attribute :time_zone_aware_types, instance_writer: false, default: [ :datetime, :time ]
end
module ClassMethods
@@ -6,8 +6,7 @@ module Attributes
extend ActiveSupport::Concern
included do
class_attribute :attributes_to_define_after_schema_loads, instance_accessor: false # :internal:
self.attributes_to_define_after_schema_loads = {}
class_attribute :attributes_to_define_after_schema_loads, instance_accessor: false, default: {} # :internal:
end
module ClassMethods
@@ -29,8 +29,7 @@ def update_table_definition(table_name, base) # :nodoc:
# to your application.rb file:
#
# ActiveRecord::ConnectionAdapters::Mysql2Adapter.emulate_booleans = false
class_attribute :emulate_booleans
self.emulate_booleans = true
class_attribute :emulate_booleans, default: true
NATIVE_DATABASE_TYPES = {
primary_key: "bigint auto_increment PRIMARY KEY",
@@ -95,8 +95,7 @@ module ActiveRecord
module Enum
def self.extended(base) # :nodoc:
base.class_attribute(:defined_enums, instance_writer: false)
base.defined_enums = {}
base.class_attribute(:defined_enums, instance_writer: false, default: {})
end
def inherited(base) # :nodoc:
@@ -878,20 +878,12 @@ def after_teardown # :nodoc:
included do
class_attribute :fixture_path, instance_writer: false
class_attribute :fixture_table_names
class_attribute :fixture_class_names
class_attribute :use_transactional_tests
class_attribute :use_instantiated_fixtures # true, false, or :no_instances
class_attribute :pre_loaded_fixtures
class_attribute :config
self.fixture_table_names = []
self.use_instantiated_fixtures = false
self.pre_loaded_fixtures = false
self.config = ActiveRecord::Base
self.fixture_class_names = {}
self.use_transactional_tests = true
class_attribute :fixture_table_names, default: []
class_attribute :fixture_class_names, default: {}
class_attribute :use_transactional_tests, default: true
class_attribute :use_instantiated_fixtures, default: false # true, false, or :no_instances
class_attribute :pre_loaded_fixtures, default: false
class_attribute :config, default: ActiveRecord::Base
end
module ClassMethods
@@ -38,8 +38,7 @@ module Inheritance
included do
# Determines whether to store the full constant name including namespace when using STI.
# This is true, by default.
class_attribute :store_full_sti_class, instance_writer: false
self.store_full_sti_class = true
class_attribute :store_full_sti_class, instance_writer: false, default: true
end
module ClassMethods
Oops, something went wrong.

0 comments on commit 1c275d8

Please sign in to comment.