diff options
| -rw-r--r-- | app/models/calendar.rb | 112 | ||||
| -rw-r--r-- | app/models/calendar/date_value.rb | 32 | ||||
| -rw-r--r-- | app/models/calendar/period.rb | 63 | ||||
| -rw-r--r-- | config/environments/development.rb | 10 | ||||
| -rw-r--r-- | spec/models/calendar_spec.rb | 10 | 
5 files changed, 116 insertions, 111 deletions
| diff --git a/app/models/calendar.rb b/app/models/calendar.rb index 39e0d6d49..4025379fd 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -1,4 +1,7 @@  require 'range_ext' +require_relative 'calendar/date_value' +require_relative 'calendar/period' +  class Calendar < ActiveRecord::Base    belongs_to :organisation    has_many :time_tables @@ -30,69 +33,11 @@ class Calendar < ActiveRecord::Base      end    end -  class Period -    include ActiveAttr::Model - -    attribute :id, type: Integer -    attribute :begin, type: Date -    attribute :end, type: Date - -    validates_presence_of :begin, :end -    validate :check_end_greather_than_begin - -    def check_end_greather_than_begin -      if self.begin and self.end and self.begin > self.end -        errors.add(:end, :invalid) -      end -    end - -    def self.from_range(index, range) -      Period.new id: index, begin: range.begin, end: range.end -    end - -    def range -      if self.begin and self.end and self.begin <= self.end -        Range.new self.begin, self.end -      end -    end - -    def intersect?(*other) -      return false if range.nil? - -      other = other.flatten -      other = other.delete_if { |o| o.id == id } if id - -      other.any? do |period| -        if other_range = period.range -          (range & other_range).present? -        end -      end -    end - -    def cover? date -      range.cover? date -    end - -    # Stuff required for coocon -    def new_record? -      !persisted? -    end - -    def persisted? -      id.present? -    end - -    def mark_for_destruction -      self._destroy = true -    end - -    attribute :_destroy, type: Boolean -    alias_method :marked_for_destruction?, :_destroy -  end +  ### Calendar::Period    # Required by coocon    def build_period -    Period.new +    Calendar::Period.new    end    def periods @@ -100,11 +45,9 @@ class Calendar < ActiveRecord::Base    end    def init_periods -    if date_ranges -      date_ranges.each_with_index.map { |r, index| Period.from_range(index, r) } -    else -      [] -    end +    (date_ranges || []) +      .each_with_index +      .map( &Calendar::Period.method(:from_range) )    end    private :init_periods @@ -141,7 +84,7 @@ class Calendar < ActiveRecord::Base        ['begin', 'end'].map do |attr|          period_attribute[attr] = flatten_date_array(period_attribute, attr)        end -      period = Period.new(period_attribute.merge(id: index)) +      period = Calendar::Period.new(period_attribute.merge(id: index))        @periods << period unless period.marked_for_destruction?      end @@ -164,40 +107,11 @@ class Calendar < ActiveRecord::Base    private :clear_periods -### dates - -  class DateValue -    include ActiveAttr::Model - -    attribute :id, type: Integer -    attribute :value, type: Date - -    validates_presence_of :value - -    def self.from_date(index, date) -      DateValue.new id: index, value: date -    end - -    # Stuff required for coocon -    def new_record? -      !persisted? -    end - -    def persisted? -      id.present? -    end - -    def mark_for_destruction -      self._destroy = true -    end - -    attribute :_destroy, type: Boolean -    alias_method :marked_for_destruction?, :_destroy -  end +  ### Calendar::DateValue    # Required by coocon    def build_date_value -    DateValue.new +    Calendar::DateValue.new    end    def date_values @@ -206,7 +120,7 @@ class Calendar < ActiveRecord::Base    def init_date_values      if dates -      dates.each_with_index.map { |d, index| DateValue.from_date(index, d) } +      dates.each_with_index.map { |d, index| Calendar::DateValue.from_date(index, d) }      else        []      end @@ -244,7 +158,7 @@ class Calendar < ActiveRecord::Base      @date_values = []      attributes.each do |index, date_value_attribute|        date_value_attribute['value'] = flatten_date_array(date_value_attribute, 'value') -      date_value = DateValue.new(date_value_attribute.merge(id: index)) +      date_value = Calendar::DateValue.new(date_value_attribute.merge(id: index))        @date_values << date_value unless date_value.marked_for_destruction?      end diff --git a/app/models/calendar/date_value.rb b/app/models/calendar/date_value.rb new file mode 100644 index 000000000..6fe84d293 --- /dev/null +++ b/app/models/calendar/date_value.rb @@ -0,0 +1,32 @@ +class Calendar < ActiveRecord::Base +   +  class DateValue +    include ActiveAttr::Model + +    attribute :id, type: Integer +    attribute :value, type: Date + +    validates_presence_of :value + +    def self.from_date(index, date) +      DateValue.new id: index, value: date +    end + +    # Stuff required for coocon +    def new_record? +      !persisted? +    end + +    def persisted? +      id.present? +    end + +    def mark_for_destruction +      self._destroy = true +    end + +    attribute :_destroy, type: Boolean +    alias_method :marked_for_destruction?, :_destroy +  end + +end diff --git a/app/models/calendar/period.rb b/app/models/calendar/period.rb new file mode 100644 index 000000000..bfde242f3 --- /dev/null +++ b/app/models/calendar/period.rb @@ -0,0 +1,63 @@ +class Calendar < ActiveRecord::Base +   +  class Period +    include ActiveAttr::Model + +    attribute :id, type: Integer +    attribute :begin, type: Date +    attribute :end, type: Date + +    validates_presence_of :begin, :end +    validate :check_end_greather_than_begin + +    def check_end_greather_than_begin +      if self.begin and self.end and self.begin > self.end +        errors.add(:end, :invalid) +      end +    end + +    def self.from_range(range, index) +      last = range.exclude_end? ? range.end - 1.day : range.end +      Period.new id: index, begin: range.begin, end: last +    end + +    def range +      if self.begin and self.end and self.begin <= self.end +        Range.new self.begin, self.end +      end +    end + +    def intersect?(*other) +      return false if range.nil? + +      other = other.flatten +      other = other.delete_if { |o| o.id == id } if id + +      other.any? do |period| +        if other_range = period.range +          (range & other_range).present? +        end +      end +    end + +    def cover? date +      range.cover? date +    end + +    # Stuff required for coocon +    def new_record? +      !persisted? +    end + +    def persisted? +      id.present? +    end + +    def mark_for_destruction +      self._destroy = true +    end + +    attribute :_destroy, type: Boolean +    alias_method :marked_for_destruction?, :_destroy +  end +end diff --git a/config/environments/development.rb b/config/environments/development.rb index 0b4eab7d2..1d1c85785 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -60,13 +60,13 @@ Rails.application.configure do    config.reflex_api_url = "https://pprod.reflex.stif.info/ws/reflex/V1/service=getData"    config.codifligne_api_url = "https://pprod.codifligne.stif.info/rest/v1/lc/getlist" -  # config.chouette_authentication_settings = { -  #   type: "database" -  # }    config.chouette_authentication_settings = { -    type: "cas", -    cas_server: "http://stif-portail-dev.af83.priv/sessions" +    type: "database"    } +  # config.chouette_authentication_settings = { +  #   type: "cas", +  #   cas_server: "http://stif-portail-dev.af83.priv/sessions" +  # }    config.stif_portail_api =    {      key: "Ohphie1Voo6the5hohpi", diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb index 33d9676cd..222e6283f 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -109,15 +109,11 @@ RSpec.describe Calendar, :type => :model do      let(:calendar) { create(:calendar, date_ranges: []) }      it 'shoud fill date_ranges with date ranges' do -      expected_ranges = [ -        Range.new(Date.today, Date.tomorrow) -      ] -      expected_ranges.each_with_index do |range, index| -        calendar.date_ranges << Calendar::Period.from_range(index, range) -      end +      expected_range = Date.today..Date.tomorrow +      calendar.date_ranges << expected_range        calendar.valid? -      expect(calendar.date_ranges.map { |period| period.begin..period.end }).to eq(expected_ranges) +      expect(calendar.date_ranges.map { |period| period.begin..period.end }).to eq([expected_range])      end    end | 
