diff options
| author | Robert | 2017-06-20 17:03:56 +0200 | 
|---|---|---|
| committer | Robert | 2017-06-20 17:03:56 +0200 | 
| commit | f42ea52d0f7f6fd6cab7f9179f93e189dd90bdca (patch) | |
| tree | b90c3dad73117122ae322ea54c235d1ffc2cb465 | |
| parent | aceeb812800c9429631f9c21d3f1b063ee634fef (diff) | |
| download | chouette-core-f42ea52d0f7f6fd6cab7f9179f93e189dd90bdca.tar.bz2 | |
Refs: #3595@3h; calendars/new Date validation
| -rw-r--r-- | app/controllers/calendars_controller.rb | 1 | ||||
| -rw-r--r-- | app/models/calendar.rb | 97 | ||||
| -rw-r--r-- | app/models/calendar/date_value.rb | 38 | ||||
| -rw-r--r-- | app/models/calendar/period.rb | 60 | ||||
| -rw-r--r-- | config/environments/development.rb | 10 | ||||
| -rw-r--r-- | spec/controllers/autocomplete_calendars_controller_spec.rb | 2 | ||||
| -rw-r--r-- | spec/controllers/calendars_controller_spec.rb | 31 | ||||
| -rw-r--r-- | spec/features/calendars_spec.rb | 18 | 
8 files changed, 152 insertions, 105 deletions
| diff --git a/app/controllers/calendars_controller.rb b/app/controllers/calendars_controller.rb index 86d567882..a2bfe9b63 100644 --- a/app/controllers/calendars_controller.rb +++ b/app/controllers/calendars_controller.rb @@ -5,6 +5,7 @@ class CalendarsController < BreadcrumbController    respond_to :html    respond_to :js, only: :index +    private    def calendar_params      permitted_params = [:id, :name, :short_name, periods_attributes: [:id, :begin, :end, :_destroy], date_values_attributes: [:id, :value, :_destroy]] diff --git a/app/models/calendar.rb b/app/models/calendar.rb index 39e0d6d49..215b13bb7 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -30,66 +30,6 @@ 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 -    # Required by coocon    def build_period      Period.new @@ -132,6 +72,8 @@ class Calendar < ActiveRecord::Base    def flatten_date_array attributes, key      date_int = %w(1 2 3).map {|e| attributes["#{key}(#{e}i)"].to_i }      Date.new(*date_int) +  rescue +    nil    end    def periods_attributes=(attributes = {}) @@ -166,35 +108,6 @@ class Calendar < ActiveRecord::Base  ### 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 -    # Required by coocon    def build_date_value      DateValue.new @@ -216,11 +129,7 @@ class Calendar < ActiveRecord::Base    validate :validate_date_values    def validate_date_values -    date_values_are_valid = true - -    unless date_values.all?(&:valid?) -      date_values_are_valid = false -    end +    date_values_are_valid = date_values.all?(&:valid?)      date_values.each do |date_value|        if date_values.count { |d| d.value == date_value.value } > 1 diff --git a/app/models/calendar/date_value.rb b/app/models/calendar/date_value.rb new file mode 100644 index 000000000..9dfa7e5f3 --- /dev/null +++ b/app/models/calendar/date_value.rb @@ -0,0 +1,38 @@ +class Calendar::DateValue +  include ActiveAttr::Model + +  attribute :id, type: Integer +  attribute :value, type: Date + +  validate :validate +   +  def initialize(atts={}) +    super( atts.slice(:id, :value) ) +    # date stored for error message +    @date = atts.slice(*%w{value(3i) value(2i) value(1i)}).values.join("/")  +  end + +  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 + +  def validate +    errors.add(:value, "Illegal Date #{@date}") if value.blank? +  end + +  attribute :_destroy, type: Boolean +  alias_method :marked_for_destruction?, :_destroy +end diff --git a/app/models/calendar/period.rb b/app/models/calendar/period.rb new file mode 100644 index 000000000..0f1902e59 --- /dev/null +++ b/app/models/calendar/period.rb @@ -0,0 +1,60 @@ +class Calendar::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 + 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/controllers/autocomplete_calendars_controller_spec.rb b/spec/controllers/autocomplete_calendars_controller_spec.rb index 3ff75fadf..812cd92f9 100644 --- a/spec/controllers/autocomplete_calendars_controller_spec.rb +++ b/spec/controllers/autocomplete_calendars_controller_spec.rb @@ -1,5 +1,3 @@ -require 'rails_helper' -  RSpec.describe AutocompleteCalendarsController, type: :controller do  end diff --git a/spec/controllers/calendars_controller_spec.rb b/spec/controllers/calendars_controller_spec.rb new file mode 100644 index 000000000..ab3123192 --- /dev/null +++ b/spec/controllers/calendars_controller_spec.rb @@ -0,0 +1,31 @@ +RSpec.describe CalendarsController, type: :controller do +  login_user +  describe 'POST /create' do + +    context 'legal date' do +      let( :params ){ { +        "calendar"=>{"name"=>"cal", "short_name"=>"cal", "shared"=>"false",  +                     "date_values_attributes"=>{"1497892917360"=>{"value(3i)"=>"19", "value(2i)"=>"6", "value(1i)"=>"2017", "_destroy"=>""}}} +      } } + +      it 'creates the calendar and redirects to show' do +        expect{ post :create, params }.to change{Calendar.count}.by 1 +        expect( response ).to redirect_to( calendar_path( Calendar.last ) ) +      end +    end + +    context 'illegal date' do +      let( :params ){ { +        "calendar"=>{"name"=>"cal", "short_name"=>"cal", "shared"=>"false",  +                     "date_values_attributes"=>{"1497892917360"=>{"value(3i)"=>"31", "value(2i)"=>"6", "value(1i)"=>"2017", "_destroy"=>""}}} +      } } + +      it 'does not create the calendar and redircets to new' do +        post :create, params +        expect{ post :create, params }.not_to change{Calendar.count} +        expect( response ).to redirect_to( new_calendar_path ) +      end +    end + +  end  +end diff --git a/spec/features/calendars_spec.rb b/spec/features/calendars_spec.rb index e15624295..8c38e7820 100644 --- a/spec/features/calendars_spec.rb +++ b/spec/features/calendars_spec.rb @@ -1,7 +1,4 @@ -# -*- coding: utf-8 -*- -require 'spec_helper' - -describe 'Calendars', type: :feature do +RSpec.describe 'Calendars', type: :feature do    login_user    let!(:calendars) { Array.new(2) { create :calendar, organisation_id: 1 } } @@ -69,4 +66,17 @@ describe 'Calendars', type: :feature do        expect(page).to have_content(calendars.first.name)      end    end + +  describe 'create', :wip do +    before do +      visit new_calendar_path + +    end +    it 'with correct date' do +      require 'pry' +      binding.pry + +    end + +  end  end | 
