aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRyuta Kamizono <kamipo@gmail.com>2016-12-31 19:22:49 +0900
committerRyuta Kamizono <kamipo@gmail.com>2017-01-01 04:01:17 +0900
commitd8e5751a1d7ad027519463134fe1a861d02494fc (patch)
treecaf33980d24cbfddb3aeba787420d0f156d6219e
parentac0b28883b78999c5605d032eb42fcd842841e6b (diff)
downloadrails-d8e5751a1d7ad027519463134fe1a861d02494fc.tar.gz
rails-d8e5751a1d7ad027519463134fe1a861d02494fc.tar.bz2
rails-d8e5751a1d7ad027519463134fe1a861d02494fc.zip
Extract `casted_true`/`casted_false` for `Type::ImmutableString`
The only difference between `Type::ImmutableString` and its subclasses is the representation of the casted booleans. Prefer extracting `casted_true`/`casted_false` and override these by subclasses.
-rw-r--r--activemodel/lib/active_model/type/immutable_string.rb16
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb20
2 files changed, 18 insertions, 18 deletions
diff --git a/activemodel/lib/active_model/type/immutable_string.rb b/activemodel/lib/active_model/type/immutable_string.rb
index 58268540e5..c967d428ac 100644
--- a/activemodel/lib/active_model/type/immutable_string.rb
+++ b/activemodel/lib/active_model/type/immutable_string.rb
@@ -8,8 +8,8 @@ module ActiveModel
def serialize(value)
case value
when ::Numeric, ActiveSupport::Duration then value.to_s
- when true then "t"
- when false then "f"
+ when true then casted_true
+ when false then casted_false
else super
end
end
@@ -19,12 +19,20 @@ module ActiveModel
def cast_value(value)
result = \
case value
- when true then "t"
- when false then "f"
+ when true then casted_true
+ when false then casted_false
else value.to_s
end
result.freeze
end
+
+ def casted_true
+ "t".freeze
+ end
+
+ def casted_false
+ "f".freeze
+ end
end
end
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
index 1c3d10c15d..97bc5edd4b 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb
@@ -979,22 +979,14 @@ module ActiveRecord
end
class MysqlString < Type::String # :nodoc:
- def serialize(value)
- case value
- when true then MySQL::Quoting::QUOTED_TRUE
- when false then MySQL::Quoting::QUOTED_FALSE
- else super
- end
- end
-
private
- def cast_value(value)
- case value
- when true then MySQL::Quoting::QUOTED_TRUE
- when false then MySQL::Quoting::QUOTED_FALSE
- else super
- end
+ def casted_true
+ MySQL::Quoting::QUOTED_TRUE
+ end
+
+ def casted_false
+ MySQL::Quoting::QUOTED_FALSE
end
end