diff options
| author | Alban Peignier | 2016-11-19 19:39:40 +0100 |
|---|---|---|
| committer | Alban Peignier | 2016-11-19 19:59:24 +0100 |
| commit | 5e24b492252fbfbd266e5266108174b1f1babab5 (patch) | |
| tree | dd1c147e6a298220bac890bf30c14530c3838d48 | |
| parent | 5fa2817d34bdfbd19c3217bcb552912e279df2ba (diff) | |
| download | chouette-core-5e24b492252fbfbd266e5266108174b1f1babab5.tar.bz2 | |
Unnormalize daterange read from PostgreSQL. Removes exclude_end which makes complex to read human range end. Refs #1782
| -rw-r--r-- | config/initializers/postgresql_adapter_patch.rb | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/config/initializers/postgresql_adapter_patch.rb b/config/initializers/postgresql_adapter_patch.rb index 6284c80ed..2e4d73f85 100644 --- a/config/initializers/postgresql_adapter_patch.rb +++ b/config/initializers/postgresql_adapter_patch.rb @@ -35,3 +35,20 @@ class ActiveRecord::ConnectionAdapters::PostgreSQLColumn end end + +module ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::OID + class DateRange < Range + # Unnormalize daterange + # [2016-11-19,2016-12-26) -> 2016-11-19..2016-12-25 + def type_cast(value) + result = super value + + if result.respond_to?(:exclude_end?) && result.exclude_end? + ::Range.new(result.begin, result.end - 1, false) + else + result + end + end + end + register_type 'daterange', DateRange.new(:date) +end |
