Simple Date Flag Support for Rails
This is a date flag method for ActiveRecord models and works with Rails 3.x or newer.
A date flag is a DATETIME type column that behaves as a boolean. This helps when tracking things such as if article is published or if a user is banned while preserving information about when that event did or should occur.
Dates can be in the past or in the future and the meaning of the flag is subjective.
This can be installed as a Ruby gem by adding the following to your
Inside your model if you have a column
This will introduce methods
banned? for quick testing and
immediately setting this flag and saving the record. There's also a
scope established for that model.
There are several options that can customize the name of the methods:
:namedefines the base name for this flag and defaults to the name of the column minus the trailing
:scopecan be set to a a symbol or string, in which case that's the name of the generated scope, to
falseto skip creating the scope.
:inversecan be set with a symbol or string which will create a scope of this name that behaves in the opposite manner to the main scope.
:actioncan be set with a symbol or string that defines the name used when generating accessor methods.
:inverse_actioncan be set with a symbol or string which will create a method to unset the flag with the supplied name. If
:inverseis set, that name will be used by default.
These might be used together like this:
:was_published_at, name: :published, scope: :published, inverse: :unpublished, inverse_action: :unpublish!, action: :publish
This results in instance methods
publish! but the scopes are named
unpublished. The flag itself is also available via the
(C) 2009-2016 Scott Tadman, The Working Group Inc.