From fcb00d388c8afe25cdc8bd0fa5c762840921a8c9 Mon Sep 17 00:00:00 2001 From: Ivan Antropov Date: Sat, 9 Nov 2013 10:16:19 +0700 Subject: [PATCH] Add :encode_with for proper YAML serialization --- lib/arel/nodes/sql_literal.rb | 4 ++++ test/nodes/test_sql_literal.rb | 8 ++++++++ 2 files changed, 12 insertions(+) diff --git a/lib/arel/nodes/sql_literal.rb b/lib/arel/nodes/sql_literal.rb index 1bae8c9366..b43288b29c 100644 --- a/lib/arel/nodes/sql_literal.rb +++ b/lib/arel/nodes/sql_literal.rb @@ -5,6 +5,10 @@ module Arel include Arel::Predications include Arel::AliasPredication include Arel::OrderPredications + + def encode_with(coder) + coder.scalar = self.to_s + end end class BindParam < SqlLiteral diff --git a/test/nodes/test_sql_literal.rb b/test/nodes/test_sql_literal.rb index 9deb8e5d8d..085c5dad6b 100644 --- a/test/nodes/test_sql_literal.rb +++ b/test/nodes/test_sql_literal.rb @@ -1,4 +1,5 @@ require 'helper' +require 'yaml' module Arel module Nodes @@ -56,6 +57,13 @@ module Arel @visitor.accept(node).must_be_like %{ (foo = 1 AND foo = 2) } end end + + describe 'serialization' do + it 'serializes into YAML' do + yaml_literal = SqlLiteral.new('foo').to_yaml + assert_equal('foo', YAML.load(yaml_literal)) + end + end end end end