diff options
| author | Zog | 2018-01-24 07:28:57 +0100 |
|---|---|---|
| committer | cedricnjanga | 2018-02-06 11:06:08 -0800 |
| commit | 57d1749d53f0ad8fc26f67b42b2b207bf0ab06ad (patch) | |
| tree | 1254efe74696d76a2f612bdaca450304cb9efcd6 | |
| parent | a477a97347792540426aad79b1709dbb12aeb3c2 (diff) | |
| download | chouette-core-57d1749d53f0ad8fc26f67b42b2b207bf0ab06ad.tar.bz2 | |
Refs #5682; Add application_days field to calendars
| -rw-r--r-- | app/models/calendar.rb | 3 | ||||
| -rw-r--r-- | app/models/chouette/time_table.rb | 95 | ||||
| -rw-r--r-- | app/models/concerns/application_days_support.rb | 108 | ||||
| -rw-r--r-- | db/migrate/20180124061955_add_int_day_types_to_calendars.rb | 5 | ||||
| -rw-r--r-- | db/schema.rb | 5 | ||||
| -rw-r--r-- | spec/models/calendar_spec.rb | 3 | ||||
| -rw-r--r-- | spec/models/chouette/time_table_spec.rb | 5 |
7 files changed, 121 insertions, 103 deletions
diff --git a/app/models/calendar.rb b/app/models/calendar.rb index d93532908..40dfa1210 100644 --- a/app/models/calendar.rb +++ b/app/models/calendar.rb @@ -5,6 +5,7 @@ require_relative 'calendar/period' class Calendar < ActiveRecord::Base include DateSupport include PeriodSupport + include ApplicationDaysSupport has_paper_trail class_name: 'PublicVersion' belongs_to :organisation @@ -29,7 +30,7 @@ class Calendar < ActiveRecord::Base self.periods.each do |p| tt.periods << Chouette::TimeTablePeriod.new(period_start: p.begin, period_end: p.end) end - tt.int_day_types = 508 + tt.int_day_types = self.int_day_types end end diff --git a/app/models/chouette/time_table.rb b/app/models/chouette/time_table.rb index 07bf35444..1a1972113 100644 --- a/app/models/chouette/time_table.rb +++ b/app/models/chouette/time_table.rb @@ -4,11 +4,12 @@ module Chouette include ChecksumSupport include TimeTableRestrictions include ObjectidSupport + include ApplicationDaysSupport + # FIXME http://jira.codehaus.org/browse/JRUBY-6358 self.primary_key = "id" acts_as_taggable - attr_accessor :monday,:tuesday,:wednesday,:thursday,:friday,:saturday,:sunday attr_accessor :tag_search def self.ransackable_attributes auth_object = nil @@ -314,98 +315,6 @@ module Chouette [bounding_min, bounding_max].compact end - def display_day_types - %w(monday tuesday wednesday thursday friday saturday sunday).select{ |d| self.send(d) }.map{ |d| self.human_attribute_name(d).first(2)}.join(', ') - end - - def day_by_mask(flag) - int_day_types & flag == flag - end - - def self.day_by_mask(int_day_types,flag) - int_day_types & flag == flag - end - - - def valid_days - # Build an array with day of calendar week (1-7, Monday is 1). - [].tap do |valid_days| - valid_days << 1 if monday - valid_days << 2 if tuesday - valid_days << 3 if wednesday - valid_days << 4 if thursday - valid_days << 5 if friday - valid_days << 6 if saturday - valid_days << 7 if sunday - end - end - - def self.valid_days(int_day_types) - # Build an array with day of calendar week (1-7, Monday is 1). - [].tap do |valid_days| - valid_days << 1 if day_by_mask(int_day_types,4) - valid_days << 2 if day_by_mask(int_day_types,8) - valid_days << 3 if day_by_mask(int_day_types,16) - valid_days << 4 if day_by_mask(int_day_types,32) - valid_days << 5 if day_by_mask(int_day_types,64) - valid_days << 6 if day_by_mask(int_day_types,128) - valid_days << 7 if day_by_mask(int_day_types,256) - end - end - - def monday - day_by_mask(4) - end - def tuesday - day_by_mask(8) - end - def wednesday - day_by_mask(16) - end - def thursday - day_by_mask(32) - end - def friday - day_by_mask(64) - end - def saturday - day_by_mask(128) - end - def sunday - day_by_mask(256) - end - - def set_day(day,flag) - if day == '1' || day == true - self.int_day_types |= flag - else - self.int_day_types &= ~flag - end - shortcuts_update - end - - def monday=(day) - set_day(day,4) - end - def tuesday=(day) - set_day(day,8) - end - def wednesday=(day) - set_day(day,16) - end - def thursday=(day) - set_day(day,32) - end - def friday=(day) - set_day(day,64) - end - def saturday=(day) - set_day(day,128) - end - def sunday=(day) - set_day(day,256) - end - def effective_days_of_period(period,valid_days=self.valid_days) days = [] period.period_start.upto(period.period_end) do |date| diff --git a/app/models/concerns/application_days_support.rb b/app/models/concerns/application_days_support.rb new file mode 100644 index 000000000..927011acb --- /dev/null +++ b/app/models/concerns/application_days_support.rb @@ -0,0 +1,108 @@ +module ApplicationDaysSupport + extend ActiveSupport::Concern + + included do |into| + into.class_eval do + MONDAY = 4 + TUESDAY = 8 + WEDNESDAY = 16 + THURSDAY = 32 + FRIDAY = 64 + SATURDAY = 128 + SUNDAY = 256 + end + + # attr_accessor :monday,:tuesday,:wednesday,:thursday,:friday,:saturday,:sunday + end + + def display_day_types + %w(monday tuesday wednesday thursday friday saturday sunday).select{ |d| self.send(d) }.map{ |d| self.human_attribute_name(d).first(2)}.join(', ') + end + + def day_by_mask(flag) + int_day_types & flag == flag + end + + def self.day_by_mask(int_day_types,flag) + int_day_types & flag == flag + end + + def valid_days + # Build an array with day of calendar week (1-7, Monday is 1). + [].tap do |valid_days| + valid_days << 1 if monday + valid_days << 2 if tuesday + valid_days << 3 if wednesday + valid_days << 4 if thursday + valid_days << 5 if friday + valid_days << 6 if saturday + valid_days << 7 if sunday + end + end + + def self.valid_days(int_day_types) + # Build an array with day of calendar week (1-7, Monday is 1). + [].tap do |valid_days| + valid_days << 1 if day_by_mask(int_day_types,MONDAY) + valid_days << 2 if day_by_mask(int_day_types,TUESDAY) + valid_days << 3 if day_by_mask(int_day_types,WEDNESDAY) + valid_days << 4 if day_by_mask(int_day_types,THURSDAY) + valid_days << 5 if day_by_mask(int_day_types,FRIDAY) + valid_days << 6 if day_by_mask(int_day_types,SATURDAY) + valid_days << 7 if day_by_mask(int_day_types,SUNDAY) + end + end + + def monday + day_by_mask(MONDAY) + end + def tuesday + day_by_mask(TUESDAY) + end + def wednesday + day_by_mask(WEDNESDAY) + end + def thursday + day_by_mask(THURSDAY) + end + def friday + day_by_mask(FRIDAY) + end + def saturday + day_by_mask(SATURDAY) + end + def sunday + day_by_mask(SUNDAY) + end + + def set_day(day,flag) + if day == '1' || day == true + self.int_day_types |= flag + else + self.int_day_types &= ~flag + end + shortcuts_update + end + + def monday=(day) + set_day(day,4) + end + def tuesday=(day) + set_day(day,8) + end + def wednesday=(day) + set_day(day,16) + end + def thursday=(day) + set_day(day,32) + end + def friday=(day) + set_day(day,64) + end + def saturday=(day) + set_day(day,128) + end + def sunday=(day) + set_day(day,256) + end +end diff --git a/db/migrate/20180124061955_add_int_day_types_to_calendars.rb b/db/migrate/20180124061955_add_int_day_types_to_calendars.rb new file mode 100644 index 000000000..6384c7177 --- /dev/null +++ b/db/migrate/20180124061955_add_int_day_types_to_calendars.rb @@ -0,0 +1,5 @@ +class AddIntDayTypesToCalendars < ActiveRecord::Migration + def change + add_column :calendars, :int_day_types, :integer, default: 0 + end +end diff --git a/db/schema.rb b/db/schema.rb index 9be702fa3..614d9803e 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,11 +11,8 @@ # # It's strongly recommended that you check this file into your version control system. -<<<<<<< HEAD -ActiveRecord::Schema.define(version: 20180123174450) do -======= + ActiveRecord::Schema.define(version: 20180126134944) do ->>>>>>> Refs #5750 @1h; Add a "kind" attribute to StopAreas # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" diff --git a/spec/models/calendar_spec.rb b/spec/models/calendar_spec.rb index e71c2b081..4c65b9660 100644 --- a/spec/models/calendar_spec.rb +++ b/spec/models/calendar_spec.rb @@ -9,11 +9,12 @@ RSpec.describe Calendar, :type => :model do it { is_expected.to be_versioned } describe '#to_time_table' do - let(:calendar) { create(:calendar, date_ranges: [Date.today...(Date.today + 1.month)]) } + let(:calendar) { create(:calendar, int_day_types: Calendar::MONDAY | Calendar::SUNDAY, date_ranges: [Date.today...(Date.today + 1.month)]) } it 'should convert calendar to an instance of Chouette::TimeTable' do time_table = calendar.convert_to_time_table expect(time_table).to be_an_instance_of(Chouette::TimeTable) + expect(time_table.int_day_types).to eq calendar.int_day_types expect(time_table.periods[0].period_start).to eq(calendar.periods[0].begin) expect(time_table.periods[0].period_end).to eq(calendar.periods[0].end) expect(time_table.dates.map(&:date)).to match_array(calendar.dates) diff --git a/spec/models/chouette/time_table_spec.rb b/spec/models/chouette/time_table_spec.rb index d4a726740..28197984e 100644 --- a/spec/models/chouette/time_table_spec.rb +++ b/spec/models/chouette/time_table_spec.rb @@ -926,6 +926,7 @@ end end end end + describe "#validity_out_between?" do let(:empty_tm) {build(:time_table)} it "should be false if empty calendar" do @@ -1068,8 +1069,6 @@ end end end - - describe "#effective_days" do before do subject.periods.clear @@ -1094,8 +1093,6 @@ end end end - - describe "#optimize_overlapping_periods" do before do subject.periods.clear |
