aboutsummaryrefslogtreecommitdiffstats
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/business_calendar.rb16
-rw-r--r--app/models/calendar.rb9
-rw-r--r--app/models/chouette/purchase_window.rb43
-rw-r--r--app/models/concerns/date_support.rb (renamed from app/models/concerns/calendar_support.rb)84
-rw-r--r--app/models/concerns/period_support.rb80
-rw-r--r--app/models/referential.rb4
6 files changed, 139 insertions, 97 deletions
diff --git a/app/models/business_calendar.rb b/app/models/business_calendar.rb
deleted file mode 100644
index 9ada43b6f..000000000
--- a/app/models/business_calendar.rb
+++ /dev/null
@@ -1,16 +0,0 @@
-require 'range_ext'
-require_relative 'calendar/date_value'
-require_relative 'calendar/period'
-
-class BusinessCalendar < ActiveRecord::Base
- include CalendarSupport
- extend Enumerize
- enumerize :color, in: %w(#9B9B9B #FFA070 #C67300 #7F551B #41CCE3 #09B09C #3655D7 #6321A0 #E796C6 #DD2DAA)
-
- scope :contains_date, ->(date) { where('date ? = any (dates) OR date ? <@ any (date_ranges)', date, date) }
-
- def self.ransackable_scopes(auth_object = nil)
- [:contains_date]
- end
-
-end
diff --git a/app/models/calendar.rb b/app/models/calendar.rb
index 5df2bb1da..34ed51374 100644
--- a/app/models/calendar.rb
+++ b/app/models/calendar.rb
@@ -3,7 +3,14 @@ require_relative 'calendar/date_value'
require_relative 'calendar/period'
class Calendar < ActiveRecord::Base
- include CalendarSupport
+ include DateSupport
+ include PeriodSupport
+
+ has_paper_trail
+ belongs_to :organisation
+
+ validates_presence_of :name, :short_name, :organisation
+ validates_uniqueness_of :short_name
has_many :time_tables
diff --git a/app/models/chouette/purchase_window.rb b/app/models/chouette/purchase_window.rb
new file mode 100644
index 000000000..a9bbe2003
--- /dev/null
+++ b/app/models/chouette/purchase_window.rb
@@ -0,0 +1,43 @@
+require 'range_ext'
+require_relative '../calendar/period'
+
+module Chouette
+ class PurchaseWindow < Chouette::TridentActiveRecord
+ # include ChecksumSupport
+ include ObjectidSupport
+ include PeriodSupport
+ extend Enumerize
+ enumerize :color, in: %w(#9B9B9B #FFA070 #C67300 #7F551B #41CCE3 #09B09C #3655D7 #6321A0 #E796C6 #DD2DAA)
+
+ has_paper_trail
+ belongs_to :referential
+
+ validates_presence_of :name, :referential
+
+ scope :overlapping, -> (period_range) do
+ where("(periods.begin <= :end AND periods.end >= :begin) OR (dates BETWEEN :begin AND :end)", {begin: period_range.begin, end: period_range.end})
+ end
+
+ def local_id
+ "IBOO-#{self.referential.id}-#{self.id}"
+ end
+
+ def bounding_dates
+ periods_min_date..periods_max_date if periods_min_date && periods_max_date
+ end
+
+ def periods_max_date
+ return nil if self.periods.empty?
+ self.periods.max.end
+ end
+
+ def periods_min_date
+ return nil if self.periods.empty?
+ self.periods.min.begin
+ end
+
+ # def checksum_attributes
+ # end
+
+ end
+end \ No newline at end of file
diff --git a/app/models/concerns/calendar_support.rb b/app/models/concerns/date_support.rb
index 73bb201ec..fbfe19af1 100644
--- a/app/models/concerns/calendar_support.rb
+++ b/app/models/concerns/date_support.rb
@@ -1,90 +1,14 @@
-module CalendarSupport
+module DateSupport
extend ActiveSupport::Concern
included do
- has_paper_trail
- belongs_to :organisation
+ after_initialize :init_dates
- validates_presence_of :name, :short_name, :organisation
- validates_uniqueness_of :short_name
- after_initialize :init_dates_and_date_ranges
-
-
- def init_dates_and_date_ranges
+ def init_dates
self.dates ||= []
- self.date_ranges ||= []
- end
- ### Calendar::Period
- # Required by coocon
- def build_period
- Calendar::Period.new
- end
-
- def periods
- @periods ||= init_periods
- end
-
- def init_periods
- (date_ranges || [])
- .each_with_index
- .map( &Calendar::Period.method(:from_range) )
- end
- private :init_periods
-
- validate :validate_periods
-
- def validate_periods
- periods_are_valid = periods.all?(&:valid?)
-
- periods.each do |period|
- if period.intersect?(periods)
- period.errors.add(:base, I18n.t('calendars.errors.overlapped_periods'))
- periods_are_valid = false
- end
- end
-
- unless periods_are_valid
- errors.add(:periods, :invalid)
- end
end
- def flatten_date_array attributes, key
- date_int = %w(1 2 3).map {|e| attributes["#{key}(#{e}i)"].to_i }
- Date.new(*date_int)
- end
-
- def periods_attributes=(attributes = {})
- @periods = []
- attributes.each do |index, period_attribute|
- # Convert date_select to date
- ['begin', 'end'].map do |attr|
- period_attribute[attr] = flatten_date_array(period_attribute, attr)
- end
- period = Calendar::Period.new(period_attribute.merge(id: index))
- @periods << period unless period.marked_for_destruction?
- end
-
- date_ranges_will_change!
- end
-
- before_validation :fill_date_ranges
-
- def fill_date_ranges
- if @periods
- self.date_ranges = @periods.map(&:range).compact.sort_by(&:begin)
- end
- end
-
- after_save :clear_periods
-
- def clear_periods
- @periods = nil
- end
-
- private :clear_periods
-
### Calendar::DateValue
-
# Required by coocon
def build_date_value
Calendar::DateValue.new
@@ -153,4 +77,4 @@ module CalendarSupport
private :clear_date_values
end
-end
+end \ No newline at end of file
diff --git a/app/models/concerns/period_support.rb b/app/models/concerns/period_support.rb
new file mode 100644
index 000000000..f512c4e89
--- /dev/null
+++ b/app/models/concerns/period_support.rb
@@ -0,0 +1,80 @@
+module PeriodSupport
+ extend ActiveSupport::Concern
+
+ included do
+ after_initialize :init_date_ranges
+
+ def init_date_ranges
+ self.date_ranges ||= []
+ end
+
+ ### Calendar::Period
+ # Required by coocon
+ def build_period
+ Calendar::Period.new
+ end
+
+ def periods
+ @periods ||= init_periods
+ end
+
+ def init_periods
+ (date_ranges || [])
+ .each_with_index
+ .map( &Calendar::Period.method(:from_range) )
+ end
+ private :init_periods
+
+ validate :validate_periods
+
+ def validate_periods
+ periods_are_valid = periods.all?(&:valid?)
+
+ periods.each do |period|
+ if period.intersect?(periods)
+ period.errors.add(:base, I18n.t('calendars.errors.overlapped_periods'))
+ periods_are_valid = false
+ end
+ end
+
+ unless periods_are_valid
+ errors.add(:periods, :invalid)
+ end
+ end
+
+ def flatten_date_array attributes, key
+ date_int = %w(1 2 3).map {|e| attributes["#{key}(#{e}i)"].to_i }
+ Date.new(*date_int)
+ end
+
+ def periods_attributes=(attributes = {})
+ @periods = []
+ attributes.each do |index, period_attribute|
+ # Convert date_select to date
+ ['begin', 'end'].map do |attr|
+ period_attribute[attr] = flatten_date_array(period_attribute, attr)
+ end
+ period = Calendar::Period.new(period_attribute.merge(id: index))
+ @periods << period unless period.marked_for_destruction?
+ end
+
+ date_ranges_will_change!
+ end
+
+ before_validation :fill_date_ranges
+
+ def fill_date_ranges
+ if @periods
+ self.date_ranges = @periods.map(&:range).compact.sort_by(&:begin)
+ end
+ end
+
+ after_save :clear_periods
+
+ def clear_periods
+ @periods = nil
+ end
+
+ private :clear_periods
+ end
+end \ No newline at end of file
diff --git a/app/models/referential.rb b/app/models/referential.rb
index 851a33653..122af65a1 100644
--- a/app/models/referential.rb
+++ b/app/models/referential.rb
@@ -128,6 +128,10 @@ class Referential < ActiveRecord::Base
Chouette::RoutingConstraintZone.all
end
+ def purchase_windows
+ Chouette::PurchaseWindow.all
+ end
+
before_validation :define_default_attributes
def define_default_attributes