From 56b7ba3ad086a101cc510de080d14010723045e0 Mon Sep 17 00:00:00 2001 From: Javier Aranda Date: Tue, 23 Jan 2024 00:24:08 +0100 Subject: [PATCH] No use database for holidays storage --- app/lib/holiday.rb | 19 +++++++ app/models/holiday.rb | 4 -- db/migrate/20240106173348_create_holidays.rb | 9 ---- db/schema.rb | 8 --- test/fixtures/holidays.yml | 53 -------------------- test/lib/holiday_test.rb | 15 ++++++ test/models/holiday_test.rb | 30 ----------- 7 files changed, 34 insertions(+), 104 deletions(-) create mode 100644 app/lib/holiday.rb delete mode 100644 app/models/holiday.rb delete mode 100644 db/migrate/20240106173348_create_holidays.rb delete mode 100644 test/fixtures/holidays.yml create mode 100644 test/lib/holiday_test.rb delete mode 100644 test/models/holiday_test.rb diff --git a/app/lib/holiday.rb b/app/lib/holiday.rb new file mode 100644 index 0000000..2e10936 --- /dev/null +++ b/app/lib/holiday.rb @@ -0,0 +1,19 @@ +class Holiday + def self.all + @all ||= new.all + end + + def all + (year2023 + year2024).map { |date| Date.parse(date) } + end + + private + + def year2023 + %w[2023-01-06 2023-05-01 2023-08-15 2023-10-12 2023-11-01 2023-12-06 2023-12-08 2023-12-25] + end + + def year2024 + %w[2024-01-01 2024-01-06 2024-05-01 2024-08-15 2024-10-12 2024-11-01 2024-12-06 2024-12-25] + end +end diff --git a/app/models/holiday.rb b/app/models/holiday.rb deleted file mode 100644 index eb8d94c..0000000 --- a/app/models/holiday.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Holiday < ApplicationRecord - validates :date, presence: true, uniqueness: true - validates :name, presence: true -end diff --git a/db/migrate/20240106173348_create_holidays.rb b/db/migrate/20240106173348_create_holidays.rb deleted file mode 100644 index a5e26a3..0000000 --- a/db/migrate/20240106173348_create_holidays.rb +++ /dev/null @@ -1,9 +0,0 @@ -class CreateHolidays < ActiveRecord::Migration[7.1] - def change - create_table :holidays, id: false do |t| - t.timestamps null: false - t.date :date, null: false, index: { unique: true } - t.string :name, null: false - end - end -end diff --git a/db/schema.rb b/db/schema.rb index 0f6d56f..0daf633 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -68,14 +68,6 @@ t.datetime "maxtime", null: false end - create_table "holidays", id: false, force: :cascade do |t| - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.date "date", null: false - t.string "name", null: false - t.index ["date"], name: "index_holidays_on_date", unique: true - end - create_table "pvpc", primary_key: "datetime", id: { type: :datetime, precision: 0 }, force: :cascade do |t| t.decimal "import", precision: 8, scale: 6, null: false t.decimal "export", precision: 8, scale: 6, null: false diff --git a/test/fixtures/holidays.yml b/test/fixtures/holidays.yml deleted file mode 100644 index ddd5381..0000000 --- a/test/fixtures/holidays.yml +++ /dev/null @@ -1,53 +0,0 @@ -# 2023 - -holiday1: - name: "Epifanía del Señor" - date: <%= Date.new(2023, 1, 6) %> -holiday2: - name: "Día del Trabajo" - date: <%= Date.new(2023, 5, 1) %> -holiday3: - name: "Asunción de la Virgen" - date: <%= Date.new(2023, 8, 15) %> -holiday4: - name: "Fiesta Nacional de España" - date: <%= Date.new(2023, 10, 12) %> -holiday5: - name: "Todos los Santos" - date: <%= Date.new(2023, 11, 1) %> -holiday6: - name: "Día de la Constitución" - date: <%= Date.new(2023, 12, 6) %> -holiday7: - name: "La Inmaculada Concepción" - date: <%= Date.new(2023, 12, 8) %> -holiday8: - name: "Navidad" - date: <%= Date.new(2023, 12, 25) %> - -# 2024 - -holiday9: - name: "Año Nuevo" - date: <%= Date.new(2024, 1, 1) %> -holiday10: - name: "Epifanía del Señor" - date: <%= Date.new(2024, 1, 6) %> -holiday11: - name: "Día del Trabajo" - date: <%= Date.new(2024, 5, 1) %> -holiday12: - name: "Asunción de la Virgen" - date: <%= Date.new(2024, 8, 15) %> -holiday13: - name: "Fiesta Nacional de España" - date: <%= Date.new(2024, 10, 12) %> -holiday14: - name: "Todos los Santos" - date: <%= Date.new(2024, 11, 1) %> -holiday15: - name: "Día de la Constitución" - date: <%= Date.new(2024, 12, 6) %> -holiday16: - name: "Navidad" - date: <%= Date.new(2024, 12, 25) %> diff --git a/test/lib/holiday_test.rb b/test/lib/holiday_test.rb new file mode 100644 index 0000000..bdbea22 --- /dev/null +++ b/test/lib/holiday_test.rb @@ -0,0 +1,15 @@ +require "test_helper" + +class HolidayTest < ActiveSupport::TestCase + test "2022-11-22 is not holiday" do + assert_not Holiday.all.include?(Date.parse("2022-11-22")) + end + + test "2023-01-06 is holiday" do + assert_includes Holiday.all, Date.parse("2023-01-06") + end + + test "2024-01-01 is holiday" do + assert_includes Holiday.all, Date.parse("2024-01-01") + end +end diff --git a/test/models/holiday_test.rb b/test/models/holiday_test.rb deleted file mode 100644 index ee1f798..0000000 --- a/test/models/holiday_test.rb +++ /dev/null @@ -1,30 +0,0 @@ -require "test_helper" - -class HolidayTest < ActiveSupport::TestCase - test "create without name" do - holiday = Holiday.new - - assert_not holiday.save - assert holiday.errors.added?(:name, :blank) - end - - test "create without date" do - holiday = Holiday.new - - assert_not holiday.save - assert holiday.errors.added?(:date, :blank) - end - - test "create with duplicate date" do - holiday = Holiday.new(name: "Año Nuevo", date: Date.new(2024, 1, 1)) - - assert_not holiday.save - assert holiday.errors.added?(:date, :taken, value: Date.new(2024, 1, 1)) - end - - test "create with valid attributes" do - holiday = Holiday.new(name: "Año Nuevo", date: Date.new(2025, 1, 1)) - - assert holiday.save - end -end