Skip to content

Writing AdminScript

alpaca-tc edited this page Dec 20, 2017 · 2 revisions

Writing AdminScript

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

Example of views.

GET /

admin_script.admin_scripts_path in your controller

index_example

GET /:script_name/edit

admin_script.edit_admin_script_path(AdminScript::ExpireUserSession) in your controller

edit_page_example
Clone this wiki locally