Created
March 2, 2015 05:38
-
-
Save yuroyoro/5d6f9ba7d94ac8c4b82e to your computer and use it in GitHub Desktop.
Rails4.2でのActiveRecordのcomplied attribute_methodを速くするpatch(だけどとても危険)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| diff --git activerecord/lib/active_record/attribute_methods/read.rb activerecord/lib/active_record/attribute_methods/read.rb | |
| index 20f0936..77bd11d 100644 | |
| --- activerecord/lib/active_record/attribute_methods/read.rb | |
| +++ activerecord/lib/active_record/attribute_methods/read.rb | |
| @@ -33,6 +33,17 @@ module ActiveRecord | |
| end | |
| }.new | |
| + TextReaderMethodCache = Class.new(AttributeMethodCache) { | |
| + def method_body(method_name, const_name) | |
| + <<-EOMETHOD | |
| + def #{method_name} | |
| + name = ::ActiveRecord::AttributeMethods::AttrNames::ATTR_#{const_name} | |
| + @attributes.fetch_original_value(name) | |
| + end | |
| + EOMETHOD | |
| + end | |
| + }.new | |
| + | |
| extend ActiveSupport::Concern | |
| module ClassMethods | |
| @@ -51,7 +62,7 @@ module ActiveRecord | |
| if Module.methods_transplantable? | |
| def define_method_attribute(name) | |
| - method = ReaderMethodCache[name] | |
| + method = (self.columns_hash[name].text? && !(self.serialized_attributes.key? name)) ? TextReaderMethodCache[name] : ReaderMethodCache[name] | |
| generated_attribute_methods.module_eval { define_method name, method } | |
| end | |
| else | |
| diff --git activerecord/lib/active_record/attribute_set.rb activerecord/lib/active_record/attribute_set.rb | |
| index 66fcaf6..4371eab 100644 | |
| --- activerecord/lib/active_record/attribute_set.rb | |
| +++ activerecord/lib/active_record/attribute_set.rb | |
| @@ -31,6 +31,10 @@ module ActiveRecord | |
| self[name].value { |n| yield n if block_given? } | |
| end | |
| + def fetch_original_value(name) | |
| + attributes.values[name] | |
| + end | |
| + | |
| def write_from_database(name, value) | |
| attributes[name] = self[name].with_value_from_database(value) | |
| end | |
| diff --git activerecord/lib/active_record/attribute_set/builder.rb activerecord/lib/active_record/attribute_set/builder.rb | |
| index 6f6b5ab..311e6c2 100644 | |
| --- activerecord/lib/active_record/attribute_set/builder.rb | |
| +++ activerecord/lib/active_record/attribute_set/builder.rb | |
| @@ -64,10 +64,14 @@ module ActiveRecord | |
| end | |
| end | |
| - protected | |
| attr_reader :types, :values, :additional_types, :delegate_hash | |
| + def type_of(name) | |
| + additional_types.fetch(name, types[name]) | |
| + end | |
| + | |
| + protected | |
| private | |
| def assign_default_value(name) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment