Skip to content
This repository has been archived by the owner on Mar 27, 2023. It is now read-only.

Commit

Permalink
Merge pull request #238 from SumOfUs/add-papertrail
Browse files Browse the repository at this point in the history
Adding paper_trail auditing to a number of models for change tracking.
  • Loading branch information
NealJMD committed Nov 25, 2015
2 parents dd38d48 + 8857b97 commit 5892862
Show file tree
Hide file tree
Showing 35 changed files with 221 additions and 8 deletions.
8 changes: 8 additions & 0 deletions app/admin/action.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ActiveAdmin.register Action do
actions :all, except: [:new, :edit]

sidebar 'Previous Versions', only: :show do
attributes_table_for action do
row :versions do
render '/versions/versions_link', model: action, model_name: 'action'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/campaign.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,12 @@

filter :name
filter :active

sidebar 'Previous Versions', only: :show do
attributes_table_for campaign do
row :versions do
render '/versions/versions_link', model: campaign, model_name: 'campaign'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
filter :description
filter :visible
filter :master

sidebar 'Previous Versions', only: :show do
attributes_table_for form do
row :versions do
render '/versions/versions_link', model: form, model_name: 'form'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/form_elements.rb → app/admin/form_element.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,12 @@
filter :name
filter :data_type
filter :form

sidebar 'Previous Versions', only: :show do
attributes_table_for form_element do
row :versions do
render '/versions/versions_link', model: form_element, model_name: 'form_element'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/image.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ActiveAdmin.register Image do
actions :all, except: [:new, :edit, :destroy]

sidebar 'Previous Versions', only: :show do
attributes_table_for image do
row :versions do
render '/versions/versions_link', model: image, model_name: 'image'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/language.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ActiveAdmin.register Language do
permit_params :code, :name

sidebar 'Previous Versions', only: :show do
attributes_table_for language do
row :versions do
render '/versions/versions_link', model: language, model_name: 'language'
end
end
end
end
10 changes: 9 additions & 1 deletion app/admin/link.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
ActiveAdmin.register Link do
permit_params :url, :title, :date, :source, :page_id
permit_params :url, :title, :date, :source, :link_id

sidebar 'Previous Versions', only: :show do
attributes_table_for link do
row :versions do
render '/versions/versions_link', model: link, model_name: 'link'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/liquid_layout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@
column :title
actions
end

sidebar 'Previous Versions', only: :show do
attributes_table_for liquid_layout do
row :versions do
render '/versions/versions_link', model: liquid_layout, model_name: 'liquid_layout'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/liquid_partial.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,12 @@
column :title
actions
end

sidebar 'Previous Versions', only: :show do
attributes_table_for liquid_partial do
row :versions do
render '/versions/versions_link', model: liquid_partial, model_name: 'liquid_partial'
end
end
end
end
10 changes: 9 additions & 1 deletion app/admin/member.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
ActiveAdmin.register Member do
permit_params :email, :country, :first_name, :last_name, :city, :postal, :title, :address1, :address2, :actionkit_user_id
permit_params :email, :country, :first_name, :last_name, :city, :postal, :title, :address1, :address2, :actionkit_member_id
actions :all, except: [:destroy]

sidebar 'Previous Versions', only: :show do
attributes_table_for member do
row :versions do
render '/versions/versions_link', model: member, model_name: 'member'
end
end
end
end
8 changes: 8 additions & 0 deletions app/admin/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,12 @@
end
actions
end

sidebar 'Previous Versions', only: :show do
attributes_table_for page do
row :versions do
render '/versions/versions_link', model: page, model_name: 'page'
end
end
end
end
10 changes: 9 additions & 1 deletion app/admin/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,15 @@
actions
end

filter :pages
filter :tags
filter :name
filter :actionkit_uri

sidebar 'Previous Versions', only: :show do
attributes_table_for tag do
row :versions do
render '/versions/versions_link', model: tag, model_name: 'tag'
end
end
end
end
7 changes: 7 additions & 0 deletions app/admin/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,11 @@
f.actions
end

sidebar 'Previous Versions', only: :show do
attributes_table_for user do
row :versions do
render '/versions/versions_link', model: user, model_name: 'user'
end
end
end
end
10 changes: 10 additions & 0 deletions app/assets/stylesheets/common.scss
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,13 @@ input.has-error {
}
}

.versions__changeset-removed {
background-color: #ffecec;
width:50%;
}

.versions__changeset-added {
background-color: #a6f3a6;
width:50%;
}

7 changes: 7 additions & 0 deletions app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class VersionsController < ApplicationController
before_action :authenticate_user!

def show
@versions = Versions::VersionFinder.find_versions(model: params[:model], id: params[:id])
end
end
1 change: 1 addition & 0 deletions app/models/action.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Action < ActiveRecord::Base
belongs_to :page
belongs_to :member
after_create :update_page_action_count
has_paper_trail on: [:update, :destroy]

def update_page_action_count
page.increment! :action_count
Expand Down
1 change: 1 addition & 0 deletions app/models/actionkit_page.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class ActionkitPage < ActiveRecord::Base
belongs_to :page
has_paper_trail

end
1 change: 1 addition & 0 deletions app/models/form.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Form < ActiveRecord::Base
has_paper_trail on: [:update, :destroy]
has_many :form_elements, -> { order(:position) }

scope :masters, -> { where(master: true) }
Expand Down
1 change: 1 addition & 0 deletions app/models/form_element.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class FormElement < ActiveRecord::Base
belongs_to :form
has_paper_trail

before_validation :set_position, on: :create
before_validation :set_name, on: :create
Expand Down
1 change: 1 addition & 0 deletions app/models/image.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Image < ActiveRecord::Base
has_paper_trail

has_attached_file :content,
styles: {
Expand Down
2 changes: 1 addition & 1 deletion app/models/language.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Language < ActiveRecord::Base

has_paper_trail on: [:update, :destroy]
has_many :pages

validates :code, :name, presence: true
Expand Down
1 change: 1 addition & 0 deletions app/models/link.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Link < ActiveRecord::Base
belongs_to :page
has_paper_trail on: [:update, :destroy]

validates :url, :title, presence: true, allow_blank: false
validates_associated :page
Expand Down
1 change: 1 addition & 0 deletions app/models/liquid_layout.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class LiquidLayout < ActiveRecord::Base
include HasLiquidPartials
has_paper_trail

has_many :pages
validates :title, presence: true, allow_blank: false
Expand Down
1 change: 1 addition & 0 deletions app/models/liquid_partial.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class LiquidPartial < ActiveRecord::Base
include HasLiquidPartials
has_paper_trail

validates :title, presence: true, allow_blank: false
validates :content, presence: true, allow_blank: false
Expand Down
1 change: 1 addition & 0 deletions app/models/member.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Member < ActiveRecord::Base
has_paper_trail on: [:update, :destroy]

def self.find_from_request(akid: nil, id: nil)
if akid.present?
Expand Down
1 change: 1 addition & 0 deletions app/models/tag.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Tag < ActiveRecord::Base
validates :name, :actionkit_uri, presence: true, uniqueness: true
has_paper_trail on: [:update, :destroy]

has_many :pages_tags, dependent: :destroy
has_many :pages, through: :pages_tags
Expand Down
15 changes: 15 additions & 0 deletions app/views/versions/_changeset.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
span.changeset
- if changes.respond_to? :each_key
- changes.each_key do |key|
- if key != 'updated_at'
table.table-striped.table
thead
th colspan=2 = key.titleize
tr
td From
td To
tr
td.versions__changeset-removed
= changes[key][0]
td.versions__changeset-added
= changes[key][1]
1 change: 1 addition & 0 deletions app/views/versions/_versions_link.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
= link_to "There are #{model.versions.length} total versions of this #{model_name}. Click here to view.", controller: '/versions', action: 'show', model: model_name, id: model.id
21 changes: 21 additions & 0 deletions app/views/versions/show.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
.row
.col-md-12
h2 = t('versions.shown_below', {count: @versions.length, model_name: params[:model]})

table.table-striped.table
thead
tr
th Version ID
th Version created at
th Differences from previous version
th Who made the change?
tbody
- @versions.each do |version|
tr
td=version.index + 1
td=version.created_at
td=version.event == 'create' ? 'Created' : render('versions/changeset', changes: version.changeset)
td
- if version.whodunnit
= User.find(version.whodunnit).email

3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,6 @@ en:
index:
title: "ActionKit Requests"

versions:
shown_below: "There are %{count} versions of this %{model_name}, shown below."

3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
post '/tags/add', to: 'tags#add_tag_to_page'
delete '/tags/remove', to: 'tags#remove_tag_from_page'

# Resource Versioning
get '/versions/show/:model/:id', to: 'versions#show'

resources :ak_logs

resource :action_kit, controller: 'action_kit' do
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20151124153734_add_object_changes_to_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class AddObjectChangesToVersions < ActiveRecord::Migration

# The largest text column available in all supported RDBMS.
# See `create_versions.rb` for details.
TEXT_BYTES = 1_073_741_823

def change
add_column :versions, :object_changes, :text, limit: TEXT_BYTES
end
end
9 changes: 5 additions & 4 deletions db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20151120143713) do
ActiveRecord::Schema.define(version: 20151124153734) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -299,12 +299,13 @@
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

create_table "versions", force: :cascade do |t|
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "item_type", null: false
t.integer "item_id", null: false
t.string "event", null: false
t.string "whodunnit"
t.text "object"
t.datetime "created_at"
t.text "object_changes"
end

add_index "versions", ["item_type", "item_id"], name: "index_versions_on_item_type_and_item_id", using: :btree
Expand Down
28 changes: 28 additions & 0 deletions lib/versions/version_finder.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Versions
class VersionFinder
class << self
def find_versions(model:, id:)
versioned_models[model.to_sym].find(id.to_i).versions
end

def versioned_models
{
action: Action,
actionkit_page: ActionkitPage,
campaign: Campaign,
form: Form,
form_element: FormElement,
image: Image,
language: Language,
link: Link,
liquid_layout: LiquidLayout,
liquid_partial: LiquidPartial,
member: Member,
page: Page,
tag: Tag,
user: User
}
end
end
end
end
1 change: 1 addition & 0 deletions spec/rails_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
require 'support/omni_auth_helper'
require 'support/capybara'
require 'webmock/rspec'
require 'paper_trail/frameworks/rspec'

# Add additional requires below this line. Rails is not loaded until this point!

Expand Down

0 comments on commit 5892862

Please sign in to comment.