Module: EasyAdmin::ResourceVersions

Extended by:
ActiveSupport::Concern
Included in:
ResourcesController
Defined in:
app/concerns/easy_admin/resource_versions.rb

Constant Summary collapse

DEFAULT_VERSIONS_PER_PAGE =
10
MAX_VERSIONS_PER_PAGE =
50

Instance Method Summary collapse

Instance Method Details

#load_record_versions(page: 1, per_page: DEFAULT_VERSIONS_PER_PAGE) ⇒ Object

Load versions for a specific record with pagination and turbo compatibility



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/concerns/easy_admin/resource_versions.rb', line 13

def load_record_versions(page: 1, per_page: DEFAULT_VERSIONS_PER_PAGE)
  return OpenStruct.new(items: [], total_count: 0, has_more: false, current_page: 1) unless record_has_versions?(@record)

  per_page = [per_page.to_i, MAX_VERSIONS_PER_PAGE].min
  per_page = DEFAULT_VERSIONS_PER_PAGE if per_page <= 0
  page = [page.to_i, 1].max

  # Use pagy for consistent pagination across the app
  pagy, versions = pagy(@record.versions.reorder(id: :desc).includes(:item), 
                       items: per_page,
                       limit: per_page,
                       page: page,
                       overflow: :last_page)

  OpenStruct.new(
    items: versions,
    pagy: pagy,
    total_count: pagy.count,
    has_more: pagy.next.present?,
    current_page: pagy.page,
    per_page: pagy.vars[:items],
    total_pages: pagy.last
  )
end

#record_has_versions?(record) ⇒ Boolean

Check if a record has version support

Returns:

  • (Boolean)


39
40
41
42
43
44
# File 'app/concerns/easy_admin/resource_versions.rb', line 39

def record_has_versions?(record)
  return false unless record
  return false unless defined?(PaperTrail)
  
  record.respond_to?(:versions) && record.class.respond_to?(:paper_trail) rescue false
end

#revert_versionObject

Revert a record to a specific version with turbo response



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/concerns/easy_admin/resource_versions.rb', line 118

def revert_version
  version_id = params[:version_id]
  version = @record.versions.find_by(id: version_id)
  
  unless version
    respond_to do |format|
      format.turbo_stream do
        render turbo_stream: turbo_stream.update("notifications",
          EasyAdmin::NotificationComponent.new(
            type: :error,
            message: "Version not found",
            title: "Error"
          ).call
        )
      end
      format.html do
        redirect_to easy_admin.resource_path(@resource_class.route_key, @record),
                   alert: "Version not found"
      end
    end
    return
  end

  result = revert_to_version(@record, version_id, current_user: current_admin_user)
  
  respond_to do |format|
    if result[:success]
      format.turbo_stream do
        render turbo_stream: [
          turbo_stream.update("notifications",
            EasyAdmin::NotificationComponent.new(
              type: :success,
              message: result[:message],
              title: "Success"
            ).call
          ),
          # Refresh the versions section
          turbo_stream.replace("versions-section",
            EasyAdmin::Versions::HistoryComponent.new(
              record: @record.reload,
              resource_class: @resource_class
            ).call
          ),
          # Update the main record display if on show page
          turbo_stream.replace("record-content", 
            render_record_content(@record.reload)
          )
        ]
      end
      format.html do
        redirect_to easy_admin.resource_path(@resource_class.route_key, @record),
                   notice: result[:message]
      end
    else
      format.turbo_stream do
        render turbo_stream: turbo_stream.update("notifications",
          EasyAdmin::NotificationComponent.new(
            type: :error,
            message: result[:error],
            title: "Error"
          ).call
        )
      end
      format.html do
        redirect_to easy_admin.resource_path(@resource_class.route_key, @record),
                   alert: result[:error]
      end
    end
  end
end

#version_diffObject

Show detailed diff modal for a version



190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/concerns/easy_admin/resource_versions.rb', line 190

def version_diff
  version = @record.versions.find_by(id: params[:version_id])
  
  unless version
    respond_to do |format|
      format.html do
        render plain: EasyAdmin::Versions::ErrorComponent.new(
          message: "Version not found"
        ).call
      end
    end
    return
  end

  respond_to do |format|
    format.html do
      render plain: EasyAdmin::Versions::DiffModalComponent.new(
        version: version,
        record: @record,
        resource_class: @resource_class
      ).call
    end
  end
end

#version_statistics(record) ⇒ Object

Get version statistics for a record



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'app/concerns/easy_admin/resource_versions.rb', line 47

def version_statistics(record)
  return {} unless record_has_versions?(record)
  
  versions = record.versions
  
  {
    total_versions: versions.count,
    create_count: versions.where(event: 'create').count,
    update_count: versions.where(event: 'update').count,
    destroy_count: versions.where(event: 'destroy').count,
    first_version_at: versions.order(:created_at).first&.created_at,
    last_version_at: versions.order(:created_at).last&.created_at,
    unique_users: versions.where.not(whodunnit: nil).distinct.count(:whodunnit)
  }
end

#versionsObject

Turbo-compatible version loading action



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'app/concerns/easy_admin/resource_versions.rb', line 64

def versions
  result = load_record_versions(
    page: params[:page] || 1,
    per_page: params[:per_page] || DEFAULT_VERSIONS_PER_PAGE
  )

  respond_to do |format|
    format.html do
      if turbo_frame_request?
        # For lazy loading turbo frame (versions-timeline)
        render plain: EasyAdmin::Versions::TimelineComponent.new(
          versions: result.items,
          turbo_frame_id: "versions-timeline",
          resource_class: @resource_class,
          record: @record
        ).call
      else
        # Regular page load - redirect or render full page
        redirect_to easy_admin.resource_path(@resource_class.route_key, @record, anchor: "versions")
      end
    end
    
    format.turbo_stream do
      if params[:append] == "true"
        # Infinite scroll - append more versions
        render turbo_stream: [
          turbo_stream.append("versions-container", 
            EasyAdmin::Versions::TimelineComponent.new(
              versions: result.items
            ).call
          ),
          turbo_stream.replace("versions-pagination", 
            EasyAdmin::Versions::PaginationComponent.new(
              pagy: result.pagy,
              resource_class: @resource_class,
              record: @record
            ).call
          )
        ]
      else
        # Replace entire versions section
        render turbo_stream: turbo_stream.replace("versions-section",
          EasyAdmin::Versions::HistoryComponent.new(
            record: @record,
            resource_class: @resource_class,
            versions_result: result
          ).call
        )
      end
    end
  end
end