diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-07-03 07:16:06 -0600 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-07-05 07:23:08 -0600 |
commit | 02579b56bbe0de17ce8657f453ad4f02acc4ab84 (patch) | |
tree | d3bde3bc313d1e49dbda7c221f2b396b093ab2b9 /activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb | |
parent | 082d1e38cb146cb73256643cbd2972f9028ac8f2 (diff) | |
download | rails-02579b56bbe0de17ce8657f453ad4f02acc4ab84.tar.gz rails-02579b56bbe0de17ce8657f453ad4f02acc4ab84.tar.bz2 rails-02579b56bbe0de17ce8657f453ad4f02acc4ab84.zip |
Use the type object for quoting PG Ranges
Diffstat (limited to 'activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb')
-rw-r--r-- | activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb | 52 |
1 files changed, 34 insertions, 18 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb index 991cdd0913..ae967d5167 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb @@ -10,28 +10,10 @@ module ActiveRecord @type = type end - def extract_bounds(value) - from, to = value[1..-2].split(',') - { - from: (value[1] == ',' || from == '-infinity') ? @subtype.infinity(negative: true) : from, - to: (value[-2] == ',' || to == 'infinity') ? @subtype.infinity : to, - exclude_start: (value[0] == '('), - exclude_end: (value[-1] == ')') - } - end - - def infinity?(value) - value.respond_to?(:infinite?) && value.infinite? - end - def type_cast_for_schema(value) value.inspect.gsub('Infinity', '::Float::INFINITY') end - def type_cast_single(value) - infinity?(value) ? value : @subtype.type_cast_from_database(value) - end - def cast_value(value) return if value == 'empty' return value if value.is_a?(::Range) @@ -53,6 +35,40 @@ This is not reliable and will be removed in the future. end ::Range.new(from, to, extracted[:exclude_end]) end + + def type_cast_for_database(value) + if value.is_a?(::Range) + from = type_cast_single_for_database(value.begin) + to = type_cast_single_for_database(value.end) + "[#{from},#{to}#{value.exclude_end? ? ')' : ']'}" + else + super + end + end + + private + + def type_cast_single(value) + infinity?(value) ? value : @subtype.type_cast_from_database(value) + end + + def type_cast_single_for_database(value) + infinity?(value) ? '' : @subtype.type_cast_for_database(value) + end + + def extract_bounds(value) + from, to = value[1..-2].split(',') + { + from: (value[1] == ',' || from == '-infinity') ? @subtype.infinity(negative: true) : from, + to: (value[-2] == ',' || to == 'infinity') ? @subtype.infinity : to, + exclude_start: (value[0] == '('), + exclude_end: (value[-1] == ')') + } + end + + def infinity?(value) + value.respond_to?(:infinite?) && value.infinite? + end end end end |