I have a Rails app in which moderators can manage meetings and normal users can attend those meetings.
At some point, the admins should be able to "semi-freeze" the app. This is basically setting a period of time (let's call it a "Frozen Period") during which a few things happen:
edited during the frozen period.create new meetings and edit those meetings, as those new meetings won't affect the calculation.In principle I could create a FrozenPeriod model, but I don't actually need to store several frozen periods at a time, as they are discarded once they are over.
Therefore, I'm looking for a "global state" type approach. This should still be stored in the database, but maybe without an associated model.
Does this make sense? Do best practices include a way of having a "singleton model" stored in the database, for example?
Create Model to save this settings to, and create only 1 record in migration:
class CreateFrozenPeriod < ActiveRecord::Migration
def change
create_table :frozen_periods do |t|
t.boolean :active
end
end
# create 1 record to store settings to.
FrozenPeriod.create
end
Use this model as a singleton to set and get the value:
class FrozenPeriod
def self.active?
FrozenPeriod.first.active
end
def self.active= val
FrozenPeriod.first.update_column(:active, val)
end
end
You can also use Rails Settings Cached which do the same as above for you, and its good for extending models with settings.
I think that it will be good design to have a FreezePeriod model with the following attributes:
You can then write logic to check if there is currently an active freeze period.
Using this approach allows you to keep a history of freeze periods, and you can also schedule freezes in the future.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With