Gem Logisticed - 轻松记录操作人员和操作时间

SuMingXuan · 2021年01月27日 · 最后由 aline57963 回复于 2021年02月04日 · 635 次阅读

很多系统的数据变更状态都可能会记录变更的操作人以及操作时间。写这个的原因是没有找到相关功能的 gem,于是自己写了这个 gem。也希望对部分有需求的开发者们有帮助

补充更新: audited 更侧重于对历史数据的收集, logisticed 更侧重于对精准数据的变更记录,侧重点不一样。

介绍

Logisticed 可以让你在 Controller 中无感知的记录 操作人操作时间 (其实也是模仿 audited 去实现的)

Installation

Add this line to your application's Gemfile:

gem 'logisticed'

Then, from your Rails app directory, create the logistics table:

$ rake logisticed_migration:install:migrations
$ rails db:migrate

Usage

你只需要告诉 logisticed 需要监听什么字段,并且被修改为什么值的时候就行了。

class Page < ActiveRecord::Base
  logisticed :status, values: [:active, :archived]
end

# 他将会为你提供 `active_at`、 `active_by`、 `archived_at`、 `archived_by` 这几个方法,为你提供某个状态最近的操作历史

如果在 model 中定义了枚举类型的字段,也可以在定义枚举的下面直接添加 logisticed,他会自动为你监听枚举的所有值,同时 logisticed 支持 onlyexcept 这两个参数

class Page < ActiveRecord::Base
  enum status: [:draft, :active, :archived]
  logisticed :status, only: [:active, :archived]
end

现在就已经可以监听所有的操作人和操作时间等信息了

class PagesController < ApplicationController
  def create
    current_user # => #<User name: 'sss'>
    @page = Page.first # => #<Page status: 'draft'>
    @page.active!
    @page.active_at # => 2021-01-22 17:15:13 +0800
    @page.active_by # => #<User name: 'sss'>
  end
end

当然你也可以使用 @page.logistics 得到 @page 这条记录的所有变更流程,也可以使用 @page.active_logistics 得到状态变为 active 的所有变更流程

除此之外你也可以使用 as_user 制定某个用户成为操作人员

class PagesController < ApplicationController
  def create
    current_user # => #<User name: 'sss'>
    user = User.last # => #<User name: 'smx'>
    Logisticed::Logistic.as_user(user) do
      @page = Page.first # => #<Page status: 'draft'>
      @page.active!
      @page.active_at # => 2021-01-22 17:15:13 +0800
      @page.active_by # => #<User name: 'smx'>
    end
  end
end

setting

# config/initializers/logisticed.rb

Logisticed.config do |config|
  config.current_user_method                = :authenticated_user
  # if your table primary_key type is uuid
  config.logisticed_source_id_column_type   = :uuid
  config.logisticed_operator_id_column_type = :uuid
end

项目地址

https://github.com/SuMingXuan/logisticed

需要 登录 后方可回复, 如果你还没有账号请 注册新账号