Last active
March 13, 2022 10:33
-
-
Save igorkasyanchuk/615f22516e2a4de359eda7a1135df0b4 to your computer and use it in GitHub Desktop.
diff using paper trail and diff gem
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Gemfile | |
gem 'paper_trail' | |
gem 'diffy' | |
gem 'activerecord-diff' | |
# view | |
= title "Logs" | |
h1 Logs | |
br | |
<style>#{Diffy::CSS}</style> | |
table.table.table-striped.table-hover.table-centered.table-condensed | |
thead | |
tr | |
th ID | |
th User | |
th Event | |
th Parent Type | |
th Parent ID | |
th Datetime | |
th Changes | |
tbody | |
- @versions.each do |version| | |
- user = User.find_by_id(version.whodunnit) | |
- item = version.item | |
tr | |
td= version.id | |
td= user.try(:full_name) | |
td= version.event | |
td= version.item_type.downcase | |
td= version.item_id | |
td= l version.created_at, format: :long | |
td | |
- version.changeset.each do |k, (old, new)| | |
strong= k | |
p= diff old, new | |
# controller | |
def logs | |
@versions = PaperTrail::Version.order('id desc').page(params[:page]).per(20) | |
end | |
# helper | |
def diff(content1, content2) | |
changes = Diffy::Diff.new(content1, content2, | |
include_plus_and_minus_in_html: true, | |
include_diff_info: true) | |
changes.to_s.present? ? changes.to_s(:html).html_safe : 'No Changes' | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment