### Humanize Changeset Method Example (Ruby) Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This Ruby example illustrates the functionality of the `humanize_changeset` method, which converts the raw PaperTrail changeset hash into a human-readable string. It shows the input format (a hash of attribute changes) and the expected output format, describing each modification clearly. ```ruby # lib/administrate/field/paper_trail.rb # Transforms PaperTrail changeset hash into human-readable string # Input: {"name" => ["John", "Jane"], "email" => ["old@example.com", "new@example.com"]} # Output: "Name changed from 'John' to 'Jane'. Email changed from 'old@example.com' to 'new@example.com'. " # Example of the actual output when viewing a record # Given a user record with these changes: user = User.find(1) user.versions.last.changeset # => {"name"=>["John Doe", "Jane Doe"], "role"=>["user", "admin"]} # The humanize_changeset method processes this as: field = Administrate::Field::PaperTrail.new(:changeset, user.versions, :show) field.humanize_changeset(user.versions.last.changeset) # => "Name changed from 'John Doe' to 'Jane Doe'. Role changed from 'user' to 'admin'. " ``` -------------------------------- ### Add Gemfile Dependency for Administrate Field PaperTrail Source: https://github.com/daanvanvugt/administrate-field-paper_trail/blob/master/README.md This snippet shows how to add the administrate-field-paper_trail gem to your project's Gemfile. Ensure PaperTrail is set up before adding this dependency. After adding the gem, run `bundle install` to install it. ```ruby gem 'administrate-field-paper_trail' ``` -------------------------------- ### Configure PaperTrail Exclusions in Administrate Dashboards Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt These Ruby examples show how to configure the PaperTrail field in Administrate dashboards to exclude specific attributes from being displayed in the change logs. This is useful for hiding sensitive or irrelevant data like IDs, timestamps, or internal notes. The `excluded_attributes` option is passed to `Field::PaperTrail.with_options`. ```ruby # app/dashboards/order_dashboard.rb class OrderDashboard < Administrate::BaseDashboard ATTRIBUTE_TYPES = { id: Field::Number, order_number: Field::String, status: Field::String, total_amount: Field::Number.with_options(decimals: 2), # Only show status and customer-facing changes changeset: Field::PaperTrail.with_options( excluded_attributes: %w[id created_at updated_at internal_notes processed_at] ), } SHOW_PAGE_ATTRIBUTES = [:id, :order_number, :status, :total_amount, :changeset] end # app/dashboards/admin_user_dashboard.rb class AdminUserDashboard < Administrate::BaseDashboard ATTRIBUTE_TYPES = { id: Field::Number, email: Field::String, name: Field::String, permissions: Field::String, # Show all changes including sensitive fields for admin audit changeset: Field::PaperTrail.with_options( excluded_attributes: %w[id encrypted_password] ), } SHOW_PAGE_ATTRIBUTES = [:id, :email, :name, :permissions, :changeset] end ``` -------------------------------- ### Basic Dashboard Configuration (Ruby) Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This Ruby code illustrates the basic configuration of a UserDashboard using Administrate. It defines the `ATTRIBUTE_TYPES`, including the custom `Field::PaperTrail` type for the 'changeset' attribute, and specifies which attributes to display on the show page. ```ruby # app/dashboards/user_dashboard.rb require "administrate/base_dashboard" class UserDashboard < Administrate::BaseDashboard ATTRIBUTE_TYPES = { id: Field::Number, name: Field::String, email: Field::String, role: Field::String, changeset: Field::PaperTrail, created_at: Field::DateTime, updated_at: Field::DateTime, } SHOW_PAGE_ATTRIBUTES = [ :id, :name, :email, :role, :changeset, :created_at, :updated_at, ] # Other dashboard configuration... end ``` -------------------------------- ### Show Page Partial Rendering (ERB) Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This ERB code represents the `_show.html.erb` partial used by Administrate to render the PaperTrail changeset information on the show page. It's automatically rendered and demonstrates how version history, including changesets, is displayed to the administrator. ```erb <% # Given: # user.versions => [ # # # #["John", "Jane"], "email"=>["j@example.com", "jane@example.com"]}> ``` -------------------------------- ### Configure PaperTrail in Model (Ruby) Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This Ruby code demonstrates how to enable PaperTrail's versioning for a specific model (e.g., User) by adding the `has_paper_trail` declaration. This is essential for PaperTrail to track changes to records of this model. ```ruby # app/models/user.rb class User < ApplicationRecord has_paper_trail end ``` -------------------------------- ### Render PaperTrail Versions in Administrate Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This Ruby code snippet demonstrates how to render PaperTrail version history within an Administrate dashboard. It iterates through resource versions, displaying the event, user (whodunnit), timestamp, and humanized changes. It handles cases where no versions are present. ```erb <% versions = field.resource.versions %> <% if versions.present? %> <% versions.each do |version| %> <% end %>
Action Updated By Updated At Changes
<%= version.event %> <%= version.whodunnit %> <%= version.created_at %> <%= field.humanize_changeset(version.changeset) %>
<% else %> - <% end %> ``` -------------------------------- ### Configure Administrate Dashboard with PaperTrail Changeset Source: https://github.com/daanvanvugt/administrate-field-paper_trail/blob/master/README.md This code demonstrates how to configure your Administrate dashboard to display record changes using the PaperTrail field. It shows how to define the `changeset` attribute type and optionally exclude specific attributes like `created_at` and `updated_at` from the displayed changes. This configuration is typically placed within your dashboard's `ATTRIBUTE_TYPES` hash. ```ruby ATTRIBUTE_TYPES = { changeset: Field::PaperTrail.with_options(excluded_attributes: %w[created_at updated_at]) } ``` -------------------------------- ### Display PaperTrail Changeset in Administrate Show Page Source: https://github.com/daanvanvugt/administrate-field-paper_trail/blob/master/README.md This snippet illustrates how to make the PaperTrail changes visible on the Administrate show page. By including `changeset` in the `SHOW_PAGE_ATTRIBUTES` array, you enable the display of historical changes for each record. This functionality currently only supports the show page view. ```ruby SHOW_PAGE_ATTRIBUTES = %i[ changeset ] ``` -------------------------------- ### Dashboard Config with Excluded Attributes (Ruby) Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This Ruby snippet shows advanced configuration for a ProductDashboard, demonstrating how to exclude specific attributes (like 'id', 'created_at', 'updated_at', 'stock_quantity') from the PaperTrail changeset display using `Field::PaperTrail.with_options(excluded_attributes: [...])`. This helps declutter the audit trail view. ```ruby # app/dashboards/product_dashboard.rb require "administrate/base_dashboard" class ProductDashboard < Administrate::BaseDashboard ATTRIBUTE_TYPES = { id: Field::Number, name: Field::String, price: Field::Number.with_options(decimals: 2), description: Field::Text, sku: Field::String, stock_quantity: Field::Number, # Exclude timestamps and internal fields from version display changeset: Field::PaperTrail.with_options( excluded_attributes: %w[created_at updated_at id stock_quantity] ), created_at: Field::DateTime, updated_at: Field::DateTime, } SHOW_PAGE_ATTRIBUTES = [ :id, :name, :sku, :price, :description, :changeset, :created_at, ] end ``` -------------------------------- ### Test Administrate PaperTrail Field Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This RSpec code tests the `Administrate::Field::PaperTrail` class. It verifies the `to_partial_path` method for rendering the correct partial, and the `excluded_attributes` and `humanize_changeset` methods for correctly handling attribute exclusions and formatting change data. ```ruby # spec/lib/administrate/field/paper_trail_spec.rb require "administrate/field/paper_trail" describe Administrate::Field::PaperTrail do describe "#to_partial_path" do it "returns a partial based on the page being rendered" do page = :show field = Administrate::Field::PaperTrail.new(:changeset, "/a.jpg", page) path = field.to_partial_path expect(path).to eq("/fields/paper_trail/#{page}") end end describe "#excluded_attributes" do it "returns default excluded attributes" do field = Administrate::Field::PaperTrail.new(:changeset, [], :show) expect(field.excluded_attributes).to eq(%w[id created_at updated_at]) end it "returns custom excluded attributes when provided" do field = Administrate::Field::PaperTrail.with_options( excluded_attributes: %w[secret_field internal_id] ).new(:changeset, [], :show) expect(field.excluded_attributes).to eq(%w[secret_field internal_id]) end end describe "#humanize_changeset" do it "formats changeset into readable text" do field = Administrate::Field::PaperTrail.new(:changeset, [], :show) changeset = { "name" => ["Old Name", "New Name"], "email" => ["old@test.com", "new@test.com"] } result = field.humanize_changeset(changeset) expect(result).to include("Name changed from 'Old Name' to 'New Name'") expect(result).to include("Email changed from 'old@test.com' to 'new@test.com'") end it "excludes specified attributes" do field = Administrate::Field::PaperTrail.with_options( excluded_attributes: %w[email] ).new(:changeset, [], :show) changeset = { "name" => ["Old", "New"], "email" => ["old@test.com", "new@test.com"] } result = field.humanize_changeset(changeset) expect(result).to include("Name changed") expect(result).not_to include("Email changed") end end end ``` -------------------------------- ### Custom Excluded Attributes Method (Ruby) Source: https://context7.com/daanvanvugt/administrate-field-paper_trail/llms.txt This Ruby code defines how to extend the `excluded_attributes` method within a custom PaperTrail field class. It demonstrates overriding the default exclusions to add more attributes (e.g., 'encrypted_password', 'reset_token') that should not be shown in the audit trail. ```ruby # lib/administrate/field/paper_trail.rb # Returns array of attributes to exclude from changeset display # Default: ["id", "created_at", "updated_at"] # Usage example in custom field subclass module Administrate module Field class CustomPaperTrail < Administrate::Field::PaperTrail def excluded_attributes # Override to add more default exclusions super + %w[encrypted_password reset_token] end end end end # In dashboard ATTRIBUTE_TYPES = { changeset: Field::CustomPaperTrail, } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.