-
Notifications
You must be signed in to change notification settings - Fork 8
Writing AdminScript
alpaca-tc edited this page Dec 20, 2017
·
2 revisions
AdminScripts inherits from AdminScript::Base, live in your app/models/admin_script directory.
Once you define the model, view is automatically rendered.
AdminScript::Base includes ActiveModel::Base, ActiveModel::Validations and ActiveModel::Validations::Callbacks.
Available callbacks are (after/before)_initialize and (after/before)_validation.
# app/models/admin_script/expire_user_session.rb
module AdminScript
class ExpireUserSession < AdminScript::Base
self.description = 'Expire user session'
# Define type attribute to cast user input.
# the following defines `#id` and `#id=` as typecast method.
attribute :id, :integer
attr_reader :user
validates :id, :user, presence: true
before_validation :set_user
def perform
return false unless valid?
user.expire_session!
true
end
private
def set_user
@user = User.find_by(id: id) if id
end
end
end
admin_script.admin_scripts_path in your controller
admin_script.edit_admin_script_path(AdminScript::ExpireUserSession) in your controller