Setup basic Revision History page

Sets up paper_trail tracking in important models

and add conference_id to versions for storing conference_id
of conference related objects  as metadata.This will help in querying
for versions related to conferences.

Fix issues with factories & event types initialization.Import paper_trail testing helpers
Fix bug: Creating an event creates a useless version with event update

Add revert and view changes features to revision history
This commit is contained in:
Nishanth Vijayan 2016-05-24 23:54:46 +05:30
parent 60877e9593
commit a45e537bac
49 changed files with 760 additions and 26 deletions

View file

@ -0,0 +1,101 @@
require 'spec_helper'
describe Admin::VersionsController do
let!(:conference) { create(:conference, short_title: 'exampletitle', description: 'Example Description') }
let(:admin) { create(:admin) }
with_versioning do
describe 'GET #revert' do
before :each do
sign_in admin
end
it 'reverts all changes for update actions' do
conference.update_attributes(short_title: 'testtitle', description: 'Some random text')
get :revert_object, id: PaperTrail::Version.last.id
conference.reload
expect(conference.short_title).to eq 'exampletitle'
expect(conference.description).to eq 'Example Description'
end
it 'shows correct flash on trying to revert create event of a deleted object' do
creation_version_id = conference.program.event_types.first.versions.first.id
conference.program.event_types.first.destroy
get :revert_object, id: creation_version_id
expect(flash[:error]).to match('The item is already in the state that you are trying to revert it back to')
end
it 'reverting deletion of object creates it again' do
conference.program.event_types.first.destroy
event_types_count = conference.program.event_types.count
get :revert_object, id: PaperTrail::Version.last.id
conference.reload
expect(PaperTrail::Version.last.event).to eq 'create'
expect(conference.program.event_types.count).to eq(event_types_count + 1)
end
it 'reverting creation of object deletes it ' do
create(:lodging, conference: conference)
get :revert_object, id: PaperTrail::Version.last.id
expect(PaperTrail::Version.last.event).to eq 'destroy'
expect(Lodging.count).to eq 0
end
it 'reverting creation of conference is not permitted' do
conference_count_before = Conference.count
get :revert_object, id: conference.versions.first.id
expect(flash[:alert]).to eq 'You are not authorized to access this page.'
expect(Conference.count).to eq(conference_count_before)
end
end
describe 'GET #revert_attribute' do
before :each do
sign_in admin
end
it 'reverts specified change for update actions' do
conference.update_attributes(short_title: 'testtitle', description: 'Some random text')
get :revert_attribute, id: PaperTrail::Version.last.id, attribute: 'short_title'
conference.reload
expect(conference.short_title).to eq 'exampletitle'
expect(conference.description).to eq 'Some random text'
end
it 'shows correct flash on trying to revert to the current state' do
conference.update_attributes(short_title: 'testtitle', description: 'Some random text')
conference.update_attributes(short_title: 'exampletitle')
get :revert_attribute, id: PaperTrail::Version.all[-2].id, attribute: 'short_title'
expect(flash[:error]).to match('The item is already in the state that you are trying to revert it back to')
expect(conference.short_title).to eq 'exampletitle'
end
it 'fails on trying to revert deleted object' do
conference.program.event_types.first.update_attributes(title: 'New Event Title')
conference.program.event_types.first.destroy
get :revert_attribute, id: PaperTrail::Version.all[-2].id, attribute: 'title'
conference.reload
expect(flash[:alert]).to eq 'You are not authorized to access this page.'
end
it 'fails on trying to revert creation event' do
create(:lodging, conference: conference)
get :revert_attribute, id: PaperTrail::Version.last.id, attribute: 'name'
expect(flash[:alert]).to eq 'You are not authorized to access this page.'
end
it 'revert fails when attribute is invalid' do
conference.update_attributes(short_title: 'testtitle', description: 'Some random text')
before_conference_title = conference.title
# Note: even though title is a valid attribute of conference, it was not updated in the change we are trying to revert
get :revert_attribute, id: PaperTrail::Version.last.id, attribute: 'title'
conference.reload
expect(conference.short_title).to eq 'testtitle'
expect(conference.description).to eq 'Some random text'
expect(conference.title).to eq(before_conference_title)
expect(flash[:error]).to match('Revert failed. Attribute missing or invalid')
end
end
end
end

View file

@ -17,7 +17,6 @@ FactoryGirl.define do
factory :event_full do
difficulty_level
track
after(:build) do |event|
event.commercials << build(:event_commercial, commercialable: event)
event.difficulty_level = build(:difficulty_level, program: event.program)

View file

@ -3,5 +3,6 @@ FactoryGirl.define do
name { Faker::Commerce.department(2, true) }
description { Faker::Lorem.sentence }
color { Faker::Color.hex_color }
program
end
end

View file

@ -102,6 +102,9 @@ feature 'Has correct abilities' do
visit admin_conference_commercials_path(conference1.short_title)
expect(current_path).to eq(admin_conference_commercials_path(conference1.short_title))
visit admin_revision_history_path
expect(current_path).to eq(admin_revision_history_path)
end
scenario 'when user is cfp' do
@ -176,6 +179,9 @@ feature 'Has correct abilities' do
visit admin_conference_commercials_path(conference2.short_title)
expect(current_path).to eq(root_path)
visit admin_revision_history_path
expect(current_path).to eq(root_path)
end
scenario 'when user is info desk' do
@ -250,5 +256,8 @@ feature 'Has correct abilities' do
visit admin_conference_commercials_path(conference3.short_title)
expect(current_path).to eq(root_path)
visit admin_revision_history_path
expect(current_path).to eq(root_path)
end
end

View file

@ -22,6 +22,10 @@ ActiveRecord::Migration.maintain_test_schema!
require 'capybara/poltergeist'
require 'phantomjs'
# Adds rspec helper provided by paper_trail
# makes it easier to control when PaperTrail is enabled during testing.
require 'paper_trail/frameworks/rspec'
# Requires supporting ruby files with custom matchers and macros, etc, in
# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are
# run as spec files by default. This means that files in spec/support that end