gshaw / timestamp_state_fields

Implements ActiveRecord state fields based on timestamp columns

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

TimestampStateFields

Treat timestamp columns as state fields in a model.

Usage

Call timestamp_state_fields on timestamp columns that represents state of your model, e.g.

class Order < ActiveRecord::Base
  include TimestampStateFields
  timestamp_state_fields :processed_at, :canceled_at
end

It will enable the following interactions on user model.

order = Order.new

# checks for presence of processed_at timestamp
order.processed?

# checks if canceled_at is nil
order.not_canceled?

# sets processed_at timestamp
order.mark_as_processed unless order.processed?

# unsets processed_at timestamp
order.mark_as_not_processed

# You can combine multiple scopes
Order.processed.not_canceled.count

# Use the is_processed accessors to show state in an form, typically in an
# admin section.
<%= f.check_box :is_processed, label: "Processed" %>

Installation

Add this line to your application's Gemfile:

gem 'timestamp_state_fields'

And then execute:

$ bundle install

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

About

Implements ActiveRecord state fields based on timestamp columns

License:MIT License


Languages

Language:Ruby 93.3%Language:CSS 2.9%Language:JavaScript 2.5%Language:HTML 1.3%