Delay Henka
'Henka' is Japanese for 'change'.
DelayHenka
is a Rails engine for managing delayed update of attributes. This is built to ensure that important attributes (such as price of a product, for instance) can be updated anytime but will not impact users until a specific time later in the day (maybe 2am in the morning, when it's safe to apply those changes).
Granted, there're other strategies with higher level of integrity (such as versioning the resources). This implementation is merely a low-cost solution that solves the problem to a reasonable extent.
Usage
In the including application,
`bundle exec rails delay_henka:install:migrations`
`bundle exec rails db:migrate`
mount DelayHenka::Engine, at: '/delay_henka'
DelayHenka.setup do |config|
config.base_view_controller = 'Backend::BaseController'
end
class SomeModel < ApplicationRecord
include DelayHenka::Model
end
class SomeController
def update
@product = SomeModel.find(params[:id])
delayed_changes = some_params.extract!(:discount_pct, :price)
if @product.update some_params.except(:discount_pct, :price)
result = DelayHenka::ScheduledChange.schedule(record: @product, changes: delayed_changes, by_id: current_user.id)
if result.ok?
redirect_to
else
flash.now[:error] = result.msg
render :edit
end
else
render :edit
end
end
end
- if @product.upcoming_changes.any?
= render 'delay_henka/web/admin/scheduled_changes/summary_table', scheduled_changes: @product.upcoming_changes
delay_henka.web_admin_scheduled_changes_path
apply_scheduled_changes:
cron: "0 1 * * *"
class: DelayHenka::ApplyChangesWorker
queue: default
States of ScheduledChange
- STAGED: initial state
- REPLACED: replaced by another record updating the same attribute
- COMPLETED: change is applied successfully
- ERRORED: change failed to be applied
Installation
Add this line to your application's Gemfile:
gem 'delay_henka'
And then execute:
$ bundle
Development
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Note that a mock AR class - Foo
- is created dynamically in rails_helper.rb for testing.
License
The gem is available as open source under the terms of the MIT License.