Skip to content
This repository has been archived by the owner on Jul 13, 2021. It is now read-only.

Commit

Permalink
Added versioning of data
Browse files Browse the repository at this point in the history
  • Loading branch information
mosic committed May 14, 2014
1 parent f249ade commit ecab6eb
Show file tree
Hide file tree
Showing 17 changed files with 157 additions and 6 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ gem 'aws-sdk'

gem 'stringex'

gem 'paper_trail', '~> 3.0.1'

group :production do
gem 'rails_12factor'
# gem 'unicorn'
Expand Down
4 changes: 4 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ GEM
nokogiri (1.6.1-x86-mingw32)
mini_portile (~> 0.5.0)
orm_adapter (0.5.0)
paper_trail (3.0.1)
activerecord (>= 3.0, < 5.0)
activesupport (>= 3.0, < 5.0)
paperclip (4.1.1)
activemodel (>= 3.0.0)
activesupport (>= 3.0.0)
Expand Down Expand Up @@ -337,6 +340,7 @@ DEPENDENCIES
jquery-turbolinks
kaminari
launchy
paper_trail (~> 3.0.1)
paperclip
pg
rails (= 4.1.0)
Expand Down
15 changes: 15 additions & 0 deletions app/assets/javascripts/rails_admin/custom/ui.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
$(document).on 'rails_admin.dom_ready', ->
console.log 'loaded'

$('.history_approve_contribution').on 'click', (e) ->
version_id = $(this).attr('data-id')
console.log 'clicked'
$.ajax
type: "POST"
url: "/versions/"
data:
version_id: version_id
dataType: "json"
success: (msg) ->
alert "Data Saved: " + msg
return
4 changes: 4 additions & 0 deletions app/assets/stylesheets/rails_admin/custom/theming.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#history > tbody > tr > td:nth-child(3){
white-space: pre;
max-width: none;
}
6 changes: 6 additions & 0 deletions app/controllers/cameras_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class CamerasController < ApplicationController
before_action :set_camera, only: [:show, :edit, :update, :destroy]
after_action :rollback_to_previous_version, only: [:update]

# GET /cameras
# GET /cameras.json
Expand Down Expand Up @@ -92,6 +93,11 @@ def set_camera
@camera = Camera.find_by_camera_slug(params[:id])
end

def rollback_to_previous_version
@camera.versions.last.reify.save!
flash[:notice] = "Your changes will be reflected once an admin has reviewed them"
end

# Never trust parameters from the scary internet, only allow the white list through.
def camera_params
params.require(:camera).permit(:model,
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/versions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class VersionsController < ApplicationController

def change
@version = PaperTrail::Version.find(params[:version_id]).next
if @version.reify
@version.reify.save!
else
@version.item.destroy
end
render :inline => "Success"
end

end
1 change: 1 addition & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ def avatar_url(user)
"http://gravatar.com/avatar/#{gravatar_id}.png?s=100"
end
end

1 change: 1 addition & 0 deletions app/models/camera.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class Camera < ActiveRecord::Base
has_many :documents, as: :owner
belongs_to :manufacturer
before_save :make_slug
has_paper_trail

accepts_nested_attributes_for :images, allow_destroy: true, reject_if: :all_blank

Expand Down
3 changes: 2 additions & 1 deletion app/models/manufacturer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
class Manufacturer < ActiveRecord::Base
has_one :image, as: :owner
has_many :cameras, dependent: :destroy
has_many :cameras
before_save :make_slug
has_paper_trail

accepts_nested_attributes_for :image, allow_destroy: true, reject_if: :all_blank

Expand Down
54 changes: 54 additions & 0 deletions app/views/rails_admin/main/history.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
- params = request.params.except(:action, :controller, :model_name)
- query = params[:query]
- filter = params[:filter]
- sort = params[:sort]
- sort_reverse = params[:sort_reverse]
- path_method = params[:id] ? "history_show_path" : "history_index_path"

= form_tag("", method: "get", class: "search pjax-form form-inline") do
.well
%input{name: "query", type: "search", value: query, placeholder: "#{t("admin.misc.filter")}", class: 'input-small'}
%button.btn.btn-primary{type: "submit", :'data-disable-with' => "<i class='icon-white icon-refresh'></i> ".html_safe + t("admin.misc.refresh")}
%i.icon-white.icon-refresh
= t("admin.misc.refresh")

%table#history.table.table-striped.table-condensed
%thead
%tr
- columns = []
- columns << { property_name: "created_at", css_class: "created_at",link_text: t('admin.table_headers.created_at') }
- columns << { property_name: "username", css_class: "username", link_text: t('admin.table_headers.username') }
- columns << { property_name: "item", css_class: "item", link_text: t('admin.table_headers.item') } if @general
- columns << { property_name: "changes", css_class: "changes", link_text: t('admin.table_headers.changes') }
- columns << { property_name: "approve", css_class: "approve", link_text: t('admin.table_headers.approve_change') }

- columns.each do |column|
- property_name = column[:property_name]
- selected = (sort == property_name)
- sort_direction = (sort_reverse ? "headerSortUp" : "headerSortDown" if selected)
- sort_location = send(path_method, params.except("sort_reverse").merge(model_name: @abstract_model.to_param, sort: property_name).merge(selected && sort_reverse != "true" ? {sort_reverse: "true"} : {}))
%th{class: "header pjax #{column[:css_class]} #{sort_direction if selected}", :'data-href' => sort_location}= column[:link_text]
%tbody
- @history.each_with_index do |object, index|
%tr
- unless object.created_at.nil?
%td= l(object.created_at, format: :long, default: l(object.created_at, format: :long, locale: :en))
%td= object.username
- if @general
- if o = @abstract_model.get(object.item)
- label = o.send(@abstract_model.config.object_label_method)
- if show_action = action(:show, @abstract_model, o)
%td= link_to(label, url_for(action: show_action.action_name, model_name: @abstract_model.to_param, id: o.id), class: 'pjax')
- else
%td= label
- else
%td= "#{@abstract_model.config.label} ##{object.item}"
%td= object.changes
- if index == 0
%td
- else
%td= link_to("##{object.id}", "#", :class => 'history_approve_contribution', :'data-id' => "#{object.id}")

- unless params[:all] || [email protected]_to?(:current_page)
= paginate(@history, theme: 'twitter-bootstrap', remote: true)
= link_to(t("admin.misc.show_all"), send(path_method, params.merge(all: true)), class: "show-all btn pjax") unless (tc = @history.total_count) <= @history.size || tc > 100
8 changes: 5 additions & 3 deletions config/initializers/rails_admin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require Rails.root.join('lib', 'rails_admin_approve_change.rb')

RailsAdmin.config do |config|

### Popular gems integration
Expand All @@ -12,7 +14,7 @@
# config.authorize_with :cancan

## == PaperTrail ==
# config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0
config.audit_with :paper_trail, 'User', 'PaperTrail::Version' # PaperTrail >= 3.0.0

### More at https://github.com/sferik/rails_admin/wiki/Base-configuration

Expand All @@ -28,7 +30,7 @@
show_in_app

## With an audit adapter, you can add:
# history_index
# history_show
history_index
history_show
end
end
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
get '/contact-us' => 'pages#contact'
get '/api-docs' => 'pages#api_docs'

post '/admin/history', to: 'cameras#history', as: :cameras_history
post "/versions", to: "versions#change", :as => "change_version"

devise_for :users

mount RailsAdmin::Engine => '/admin', as: 'rails_admin'
Expand Down
13 changes: 13 additions & 0 deletions db/migrate/20140512121824_create_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class CreateVersions < ActiveRecord::Migration
def change
create_table :versions do |t|
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
end
add_index :versions, [:item_type, :item_id]
end
end
5 changes: 5 additions & 0 deletions db/migrate/20140512121825_add_object_changes_to_versions.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddObjectChangesToVersions < ActiveRecord::Migration
def change
add_column :versions, :object_changes, :text
end
end
14 changes: 13 additions & 1 deletion 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: 20140507122914) do
ActiveRecord::Schema.define(version: 20140512121825) do

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

create_table "versions", force: true do |t|
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

end
15 changes: 15 additions & 0 deletions lib/rails_admin_approve_change.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module RailsAdmin
module Extensions
module PaperTrail
class VersionProxy
def id
@version.id
end

def changes
@changes = @version.changeset.to_a.collect {|c| c[0] + " = " + c[1][1].to_s}.join(", \n")
end
end
end
end
end
2 changes: 1 addition & 1 deletion public/api-docs/api/v1/cameras.json
Original file line number Diff line number Diff line change
Expand Up @@ -71,4 +71,4 @@
}
],
"resourcePath": "cameras"
}
}

0 comments on commit ecab6eb

Please sign in to comment.