Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

AR supporting new int4range and int8range data type on PostgreSQL >= 9.2. Fix #8528 pull request #8542

Merged
merged 1 commit into from Dec 17, 2012

Conversation

Projects
None yet
3 participants
Contributor

le0pard commented Dec 17, 2012

This is fix for #8528 pull request. I just add tests for all possible variants (with invalid data).

@rafaelfranca rafaelfranca commented on an outdated diff Dec 17, 2012

.../active_record/connection_adapters/postgresql/cast.rb
@@ -108,8 +107,16 @@ def string_to_intrange(string)
end
def intrange_to_string(object)
- if Range === object
- "[#{object.first},#{object.exclude_end? ? object.last : object.last.to_i + 1})"
+ if object.nil?
+ nil
+ elsif Range === object
+ if [object.first, object.last].all?{|el| Integer === el }
@rafaelfranca

rafaelfranca Dec 17, 2012

Owner

space after the all? and after the {

@rafaelfranca rafaelfranca commented on an outdated diff Dec 17, 2012

.../active_record/connection_adapters/postgresql/cast.rb
@@ -108,8 +107,16 @@ def string_to_intrange(string)
end
def intrange_to_string(object)
- if Range === object
- "[#{object.first},#{object.exclude_end? ? object.last : object.last.to_i + 1})"
+ if object.nil?
+ nil
+ elsif Range === object
+ if [object.first, object.last].all?{|el| Integer === el }
+ "[#{object.first.to_i},#{object.exclude_end? ? object.last.to_i : object.last.to_i + 1})"
+ elsif [object.first, object.last].all?{|el| NilClass === el }
Contributor

le0pard commented Dec 17, 2012

Spaces fixed

@rafaelfranca rafaelfranca commented on the diff Dec 17, 2012

...ecord/test/cases/adapters/postgresql/intrange_test.rb
+ x = IntRangeDataType.first
+ assert_equal(nil, x.int_range)
+ end
+
+ def test_save_empty_range
+ assert IntRangeDataType.create!(int_range: (nil..nil))
+ x = IntRangeDataType.first
+ assert_equal((nil..nil), x.int_range)
+ end
+
+ def test_save_invalid_data
+ assert_raises(ActiveRecord::StatementInvalid) do
+ IntRangeDataType.create!(int_range: "empty1")
+ end
+ end
+
@rafaelfranca

rafaelfranca Dec 17, 2012

Owner

Remove this blank line

@le0pard

le0pard Dec 17, 2012

Contributor

Done. Thanks!

@rafaelfranca rafaelfranca added a commit that referenced this pull request Dec 17, 2012

@rafaelfranca rafaelfranca Merge pull request #8542 from le0pard/fixed_ar_intrange
AR supporting new int4range and int8range data type on PostgreSQL >= 9.2. Fix #8528 pull request
504ed90

@rafaelfranca rafaelfranca merged commit 504ed90 into rails:master Dec 17, 2012

Owner

rafaelfranca commented Dec 17, 2012

Thanks

Contributor

slbug commented Dec 17, 2012

@rafaelfranca #7345 was here for ages, which adds support for all range types

Owner

rafaelfranca commented Dec 17, 2012

@slbug Sorry about that. I remember now why it was not merged. We need to improve the implementation of #7350 first

Contributor

slbug commented Dec 17, 2012

@rafaelfranca yes, but no one still can't say why other implementation does not work.

@slbug slbug referenced this pull request Dec 31, 2012

Merged

Postgresql range support #7345

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment