Module: ActiveAdmin::History

Extended by:
ActiveSupport::Concern
Defined in:
lib/activeadmin/history.rb,
lib/activeadmin/models/admin_action.rb,
lib/generators/active_admin/history/install_generator.rb

Defined Under Namespace

Modules: Generators Classes: AdminAction, Error

Class Method Summary collapse

Class Method Details

.included(dsl) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/activeadmin/history.rb', line 9

def self.included(dsl)
  dsl.controller do
    before_action :before_admin_action, only: [:create, :update, :destroy]
    after_action :after_admin_action, only: [:create, :update, :destroy]

    def before_admin_action
      @admin_user_id = current_admin_user.id
      @action = params[:action]
      @param_attributes = params.dig(controller_name.singularize.to_sym)
      @object_diff = nil

      if @action == 'update'
        @object_diff = diff(resource, resource.class.new(@param_attributes.to_unsafe_hash))
      end

      @admin_action = ::AdminAction.create(
        admin_user_id: @admin_user_id,
        action: @action,
        object_params: ActiveSupport::JSON.encode(@param_attributes),
        object_diff: ActiveSupport::JSON.encode(@object_diff),
      )
    end

    def after_admin_action
      @admin_action.update(
        object_id: resource.id,
        object_type: resource.class.to_s,
      )
    end

    private

    def diff(resource_a, resource_b)
      h = {}
      resource_a.attributes.each_pair do |key, value|
        if resource_a.respond_to?(key) && resource_b.respond_to?(key)
          unless %w[id created_at updated_at].include?(key)
            value_a = resource_a.send(key)
            value_b = resource_b.send(key)
            
            if value_a != value_b
              h[key] = [value_a, value_b]
            end
          end
        end  
      end    
      h
    end
  end
end