From ad7a4dcc8cf0c7c8b33c63b5d03b701a0f047beb Mon Sep 17 00:00:00 2001 From: Tim Cowlishaw Date: Mon, 12 Aug 2024 10:48:55 +0200 Subject: [PATCH] active flag on experiments derived from start and end times --- app/models/experiment.rb | 5 ++ ...108_remove_active_flag_from_experiments.rb | 5 ++ db/schema.rb | 3 +- spec/factories/experiment.rb | 1 - spec/models/experiment_spec.rb | 54 +++++++++++++++++++ 5 files changed, 65 insertions(+), 3 deletions(-) create mode 100644 db/migrate/20240812081108_remove_active_flag_from_experiments.rb create mode 100644 spec/models/experiment_spec.rb diff --git a/app/models/experiment.rb b/app/models/experiment.rb index 0d8ec197..83b4bed7 100644 --- a/app/models/experiment.rb +++ b/app/models/experiment.rb @@ -8,4 +8,9 @@ class Experiment < ApplicationRecord def self.ransackable_attributes(auth_object = nil) ["created_at", "description", "ends_at", "id", "is_test", "name", "owner_id", "starts_at", "status", "updated_at"] end + + + def active? + (!starts_at || Time.now >= starts_at) && (!ends_at || Time.now <= ends_at) + end end diff --git a/db/migrate/20240812081108_remove_active_flag_from_experiments.rb b/db/migrate/20240812081108_remove_active_flag_from_experiments.rb new file mode 100644 index 00000000..93303081 --- /dev/null +++ b/db/migrate/20240812081108_remove_active_flag_from_experiments.rb @@ -0,0 +1,5 @@ +class RemoveActiveFlagFromExperiments < ActiveRecord::Migration[6.1] + def change + remove_column :experiments, :active, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index cd3b4404..6c7c9344 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2024_07_18_054447) do +ActiveRecord::Schema.define(version: 2024_08_12_081108) do # These are extensions that must be enabled in order to support this database enable_extension "adminpack" @@ -140,7 +140,6 @@ t.string "name", null: false t.string "description" t.bigint "owner_id" - t.boolean "active", default: true, null: false t.boolean "is_test", default: false, null: false t.datetime "starts_at" t.datetime "ends_at" diff --git a/spec/factories/experiment.rb b/spec/factories/experiment.rb index 0d9b8d3b..c5c1c941 100644 --- a/spec/factories/experiment.rb +++ b/spec/factories/experiment.rb @@ -3,7 +3,6 @@ sequence("name") { |n| "experiment#{n}"} description { "my experiment" } association :owner, factory: :user - active { true } is_test { false } end end diff --git a/spec/models/experiment_spec.rb b/spec/models/experiment_spec.rb new file mode 100644 index 00000000..ee6c881c --- /dev/null +++ b/spec/models/experiment_spec.rb @@ -0,0 +1,54 @@ +require "rails_helper" + +RSpec.describe Experiment, type: :model do + + describe "#is_active?" do + context "when the experiment has neither start or end times" do + it "is active" do + experiment = build(:experiment, starts_at: nil, ends_at: nil) + expect(experiment).to be_active + end + end + + context "when the experiment has a start time but no end time" do + it "is inactive before the start time" do + experiment = build(:experiment, starts_at: Time.now + 1.hour, ends_at: nil) + expect(experiment).not_to be_active + end + + it "is active after the start time" do + experiment = build(:experiment, starts_at: Time.now - 1.hour, ends_at: nil) + expect(experiment).to be_active + end + end + + context "when the experiment has an end time but no start time" do + it "is active before the end time" do + experiment = build(:experiment, starts_at: nil, ends_at: Time.now + 1.hour) + expect(experiment).to be_active + end + + it "is inactive after the end time" do + experiment = build(:experiment, starts_at: nil, ends_at: Time.now - 1.hour) + expect(experiment).not_to be_active + end + end + + context "when the experiment has both start and end times" do + it "is inactive before the start time" do + experiment = build(:experiment, starts_at: Time.now + 1.hour, ends_at: Time.now + 2.hours) + expect(experiment).not_to be_active + end + + it "is active between the start and end times" do + experiment = build(:experiment, starts_at: Time.now - 1.hour, ends_at: Time.now + 1.hour) + expect(experiment).to be_active + end + + it "is inactive after the end time" do + experiment = build(:experiment, starts_at: Time.now - 2.hours, ends_at: Time.now - 1.hour) + expect(experiment).not_to be_active + end + end + end +end