mirror of
https://github.com/Growstuff/growstuff.git
synced 2025-12-27 11:37:49 -05:00
* feat(admin): add revert functionality to crops page This change adds a "Revert" button to the admin crops page, allowing crop wranglers to revert changes to a previous version. It introduces a new `Admin::VersionsController` with a `revert` action that uses `paper_trail`'s `reify` method to restore a previous version of a `Crop` object. The view is updated to include a "Revert" button, which is guarded by a `can?(:wrangle, Crop)` check to ensure only authorized users can see it. The controller also includes an authorization check to prevent unauthorized users from accessing the revert action directly. A feature spec is added to test the new functionality, including the authorization logic. * Consistent UX * Specs --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com> Co-authored-by: Daniel O'Connor <daniel.oconnor@gmail.com>
25 lines
709 B
Ruby
25 lines
709 B
Ruby
# frozen_string_literal: true
|
|
|
|
module Admin
|
|
class VersionsController < ApplicationController
|
|
before_action :authenticate_member!
|
|
before_action :authorize_admin!
|
|
|
|
def revert
|
|
@version = PaperTrail::Version.find(params[:id])
|
|
@object = @version.reify
|
|
if @object.save
|
|
redirect_to admin_crops_path, notice: "Reverted to version from #{@version.created_at.strftime('%B %d, %Y')}"
|
|
else
|
|
redirect_to admin_crops_path, alert: "Could not revert to version from #{@version.created_at.strftime('%B %d, %Y')}. Errors: #{@object.errors.full_messages.to_sentence}"
|
|
end
|
|
end
|
|
|
|
private
|
|
|
|
def authorize_admin!
|
|
authorize! :wrangle, Crop
|
|
end
|
|
end
|
|
end
|