From 3094ae185bc667391f5f30a83afed92fa6a98733 Mon Sep 17 00:00:00 2001 From: Melissa Miller Date: Fri, 1 Jul 2016 15:55:39 -0400 Subject: [PATCH 1/6] Adds paranoia gem --- Gemfile | 1 + Gemfile.lock | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/Gemfile b/Gemfile index 3039c7a..1ddb5bf 100644 --- a/Gemfile +++ b/Gemfile @@ -36,6 +36,7 @@ gem 'paperclip', :git=> 'https://github.com/thoughtbot/paperclip', :ref => '523b gem 'dotenv-rails' gem 'gmaps4rails' gem 'font-awesome-rails' +gem 'paranoia', :github => 'rubysherpas/paranoia', :branch => 'rails4' # Use Capistrano for deployment # gem 'capistrano-rails', group: :development diff --git a/Gemfile.lock b/Gemfile.lock index 1db875e..a668167 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,3 +1,11 @@ +GIT + remote: git://github.com/rubysherpas/paranoia.git + revision: bd3383729c790bf09e488f84221eaaea27c6597e + branch: rails4 + specs: + paranoia (2.2.0.alpha) + activerecord (>= 4.0, < 5.1) + GIT remote: https://github.com/thoughtbot/paperclip revision: 523bd46c768226893f23889079a7aa9c73b57d68 @@ -262,6 +270,7 @@ DEPENDENCIES letter_opener listen (~> 3.0.5) paperclip! + paranoia! pg (~> 0.18) pry puma (~> 3.0) From 15c30e818c5d580abc1e1828643c0e06cd358d7f Mon Sep 17 00:00:00 2001 From: Melissa Miller Date: Fri, 1 Jul 2016 16:02:17 -0400 Subject: [PATCH 2/6] adds deleted at to users and includes paranoia in user model --- app/models/user.rb | 1 + .../20160701195916_add_deleted_at_to_users.rb | 6 ++++++ db/schema.rb | 14 +++----------- 3 files changed, 10 insertions(+), 11 deletions(-) create mode 100644 db/migrate/20160701195916_add_deleted_at_to_users.rb diff --git a/app/models/user.rb b/app/models/user.rb index 7f7bf5b..62bbc01 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,5 @@ class User < ApplicationRecord + acts_as_paranoid # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :invitable, :database_authenticatable, :registerable, diff --git a/db/migrate/20160701195916_add_deleted_at_to_users.rb b/db/migrate/20160701195916_add_deleted_at_to_users.rb new file mode 100644 index 0000000..5c20c86 --- /dev/null +++ b/db/migrate/20160701195916_add_deleted_at_to_users.rb @@ -0,0 +1,6 @@ +class AddDeletedAtToUsers < ActiveRecord::Migration[5.0] + def change + add_column :users, :deleted_at, :datetime + add_index :users, :deleted_at + end +end diff --git a/db/schema.rb b/db/schema.rb index 4609a8c..8d3d7b9 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,20 +11,11 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160618215216) do +ActiveRecord::Schema.define(version: 20160701195916) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" - create_table "images", force: :cascade do |t| - t.text "image_data" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "user_id" - t.integer "observation_id" - t.index ["observation_id"], name: "index_images_on_observation_id", using: :btree - end - create_table "observations", force: :cascade do |t| t.datetime "sighted_at" t.string "location" @@ -63,6 +54,8 @@ t.integer "invited_by_id" t.integer "invitations_count", default: 0 t.string "role" + t.datetime "deleted_at" + t.index ["deleted_at"], name: "index_users_on_deleted_at", using: :btree t.index ["email"], name: "index_users_on_email", unique: true, using: :btree t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true, using: :btree t.index ["invitations_count"], name: "index_users_on_invitations_count", using: :btree @@ -70,5 +63,4 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree end - add_foreign_key "images", "observations" end From 04aa1f56c0c29b4b4edf70ef786ae9a10f23b809 Mon Sep 17 00:00:00 2001 From: Melissa Miller Date: Sat, 2 Jul 2016 10:30:36 -0400 Subject: [PATCH 3/6] adds test for soft delete --- Gemfile.lock | 139 +++++++++++++++++++-------------------- spec/models/user_spec.rb | 37 ++++++++--- 2 files changed, 94 insertions(+), 82 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index a668167..10b434e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -21,51 +21,51 @@ GIT GEM remote: https://rubygems.org/ specs: - actioncable (5.0.0.rc1) - actionpack (= 5.0.0.rc1) + actioncable (5.0.0) + actionpack (= 5.0.0) nio4r (~> 1.2) websocket-driver (~> 0.6.1) - actionmailer (5.0.0.rc1) - actionpack (= 5.0.0.rc1) - actionview (= 5.0.0.rc1) - activejob (= 5.0.0.rc1) + actionmailer (5.0.0) + actionpack (= 5.0.0) + actionview (= 5.0.0) + activejob (= 5.0.0) mail (~> 2.5, >= 2.5.4) - rails-dom-testing (~> 1.0, >= 1.0.5) - actionpack (5.0.0.rc1) - actionview (= 5.0.0.rc1) - activesupport (= 5.0.0.rc1) - rack (~> 2.x) + rails-dom-testing (~> 2.0) + actionpack (5.0.0) + actionview (= 5.0.0) + activesupport (= 5.0.0) + rack (~> 2.0) rack-test (~> 0.6.3) - rails-dom-testing (~> 1.0, >= 1.0.5) + rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.0.0.rc1) - activesupport (= 5.0.0.rc1) + actionview (5.0.0) + activesupport (= 5.0.0) builder (~> 3.1) erubis (~> 2.7.0) - rails-dom-testing (~> 1.0, >= 1.0.5) + rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - activejob (5.0.0.rc1) - activesupport (= 5.0.0.rc1) + activejob (5.0.0) + activesupport (= 5.0.0) globalid (>= 0.3.6) - activemodel (5.0.0.rc1) - activesupport (= 5.0.0.rc1) - activerecord (5.0.0.rc1) - activemodel (= 5.0.0.rc1) - activesupport (= 5.0.0.rc1) + activemodel (5.0.0) + activesupport (= 5.0.0) + activerecord (5.0.0) + activemodel (= 5.0.0) + activesupport (= 5.0.0) arel (~> 7.0) - activesupport (5.0.0.rc1) + activesupport (5.0.0) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) minitest (~> 5.1) tzinfo (~> 1.1) addressable (2.4.0) arel (7.0.0) - aws-sdk (2.3.14) - aws-sdk-resources (= 2.3.14) - aws-sdk-core (2.3.14) + aws-sdk (2.3.19) + aws-sdk-resources (= 2.3.19) + aws-sdk-core (2.3.19) jmespath (~> 1.0) - aws-sdk-resources (2.3.14) - aws-sdk-core (= 2.3.14) + aws-sdk-resources (2.3.19) + aws-sdk-core (= 2.3.19) bcrypt (3.1.11) builder (3.2.2) byebug (9.0.5) @@ -84,7 +84,7 @@ GEM coffee-script-source (1.10.0) concurrent-ruby (1.0.2) debug_inspector (0.0.2) - devise (4.1.1) + devise (4.2.0) bcrypt (~> 3.0) orm_adapter (~> 0.1) railties (>= 4.1.0, < 5.1) @@ -116,8 +116,7 @@ GEM rails-dom-testing (>= 1, < 3) railties (>= 4.2.0) thor (>= 0.14, < 2.0) - json (1.8.3) - json_pure (1.8.3) + json_pure (2.0.1) launchy (2.4.3) addressable (~> 2.3) letter_opener (1.4.1) @@ -149,32 +148,28 @@ GEM method_source (~> 0.8.1) slop (~> 3.4) puma (3.4.0) - rack (2.0.0.rc1) - json + rack (2.0.1) rack-test (0.6.3) rack (>= 1.0) - rails (5.0.0.rc1) - actioncable (= 5.0.0.rc1) - actionmailer (= 5.0.0.rc1) - actionpack (= 5.0.0.rc1) - actionview (= 5.0.0.rc1) - activejob (= 5.0.0.rc1) - activemodel (= 5.0.0.rc1) - activerecord (= 5.0.0.rc1) - activesupport (= 5.0.0.rc1) + rails (5.0.0) + actioncable (= 5.0.0) + actionmailer (= 5.0.0) + actionpack (= 5.0.0) + actionview (= 5.0.0) + activejob (= 5.0.0) + activemodel (= 5.0.0) + activerecord (= 5.0.0) + activesupport (= 5.0.0) bundler (>= 1.3.0, < 2.0) - railties (= 5.0.0.rc1) + railties (= 5.0.0) sprockets-rails (>= 2.0.0) rails-controller-testing (0.1.1) actionpack (~> 5.x) actionview (~> 5.x) activesupport (~> 5.x) - rails-deprecated_sanitizer (1.0.3) - activesupport (>= 4.2.0.alpha) - rails-dom-testing (1.0.7) - activesupport (>= 4.2.0.beta, < 5.0) + rails-dom-testing (2.0.1) + activesupport (>= 4.2.0, < 6.0) nokogiri (~> 1.6.0) - rails-deprecated_sanitizer (>= 1.0.1) rails-html-sanitizer (1.0.3) loofah (~> 2.0) rails_12factor (0.0.3) @@ -182,9 +177,9 @@ GEM rails_stdout_logging rails_serve_static_assets (0.0.5) rails_stdout_logging (0.0.5) - railties (5.0.0.rc1) - actionpack (= 5.0.0.rc1) - activesupport (= 5.0.0.rc1) + railties (5.0.0) + actionpack (= 5.0.0) + activesupport (= 5.0.0) method_source rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) @@ -194,50 +189,50 @@ GEM ffi (>= 0.5.0) responders (2.2.0) railties (>= 4.2.0, < 5.1) - rollbar (2.11.5) + rollbar (2.12.0) multi_json - rspec-core (3.5.0.beta4) - rspec-support (= 3.5.0.beta4) - rspec-expectations (3.5.0.beta4) + rspec-core (3.5.0) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (= 3.5.0.beta4) - rspec-mocks (3.5.0.beta4) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (= 3.5.0.beta4) - rspec-rails (3.5.0.beta4) + rspec-support (~> 3.5.0) + rspec-rails (3.5.0) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (= 3.5.0.beta4) - rspec-expectations (= 3.5.0.beta4) - rspec-mocks (= 3.5.0.beta4) - rspec-support (= 3.5.0.beta4) - rspec-support (3.5.0.beta4) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) sass (3.4.22) - sass-rails (5.0.4) - railties (>= 4.0.0, < 5.0) + sass-rails (5.0.5) + railties (>= 4.0.0, < 6) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) slop (3.6.0) - spring (1.7.1) + spring (1.7.2) spring-watcher-listen (2.0.0) listen (>= 2.7, < 4.0) spring (~> 1.2) - sprockets (3.6.1) + sprockets (3.6.3) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.0.4) + sprockets-rails (3.1.1) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) thor (0.19.1) thread_safe (0.3.5) tilt (2.0.5) - turbolinks (5.0.0.beta2) - turbolinks-source - turbolinks-source (5.0.0.beta5) + turbolinks (5.0.0) + turbolinks-source (~> 5) + turbolinks-source (5.0.0) tzinfo (1.2.2) thread_safe (~> 0.1) uglifier (3.0.0) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 1f52c89..3821818 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -1,27 +1,44 @@ require 'rails_helper' RSpec.describe User, type: :model do - - it "can be valid" do - user = User.new( + let(:user) do + User.new( email: "spacejam@gmail.com", name: "Michael Jordan", role: "admin", password: "password", password_confirmation: "password" ) + end + + it "can be valid" do expect(user).to be_valid end it "can be an admin" do - user = User.new( - email: "spacejam@gmail.com", - name: "Michael Jordan", - role: "admin", - password: "password", - password_confirmation: "password" - ) expect(user.is_admin?).to be(true) end + describe "#destroy" do + let(:a_user) do + User.new( + email: "test@gmail.com", + name: "Michael Jordan", + role: "researcher", + password: "password", + password_confirmation: "password" + ) + end + + before do + a_user.save + end + + it "can be destroyed" do + a_user.destroy + expect(User.without_deleted).to be_empty + expect(User.only_deleted).not_to be_empty + end + + end end From ac65f1c55c9661927f50caa33b1e666f816c5c7f Mon Sep 17 00:00:00 2001 From: Melissa Miller Date: Sat, 2 Jul 2016 10:59:36 -0400 Subject: [PATCH 4/6] tests user for restore and for perma destroy --- spec/models/user_spec.rb | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 3821818..0f4fe1a 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -36,8 +36,18 @@ it "can be destroyed" do a_user.destroy - expect(User.without_deleted).to be_empty - expect(User.only_deleted).not_to be_empty + expect(a_user.paranoia_destroyed?).to eq(true) + end + + it "can be restored" do + a_user.restore + expect(a_user.paranoia_destroyed?).to eq(false) + end + + it "can be REALLY destroyed" do + a_user.really_destroy! + expect(a_user.destroyed?).to eq(true) + end end From fabb098662a8959dbb3f343eea78a314eaa3d7e8 Mon Sep 17 00:00:00 2001 From: Melissa Miller Date: Sat, 2 Jul 2016 11:41:18 -0400 Subject: [PATCH 5/6] migration run --- db/schema.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/db/schema.rb b/db/schema.rb index 8d3d7b9..29d3cf0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,4 +1,3 @@ -# encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. From d2dcc350e8726bb6d48ce0cf24c7e17bc0029b9a Mon Sep 17 00:00:00 2001 From: Melissa Miller Date: Wed, 6 Jul 2016 20:21:31 -0400 Subject: [PATCH 6/6] Fixes image schema issue --- db/schema.rb | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/db/schema.rb b/db/schema.rb index 29d3cf0..4609a8c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -1,3 +1,4 @@ +# encoding: UTF-8 # This file is auto-generated from the current state of the database. Instead # of editing this file, please use the migrations feature of Active Record to # incrementally modify your database, and then regenerate this schema definition. @@ -10,11 +11,20 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20160701195916) do +ActiveRecord::Schema.define(version: 20160618215216) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "images", force: :cascade do |t| + t.text "image_data" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "user_id" + t.integer "observation_id" + t.index ["observation_id"], name: "index_images_on_observation_id", using: :btree + end + create_table "observations", force: :cascade do |t| t.datetime "sighted_at" t.string "location" @@ -53,8 +63,6 @@ t.integer "invited_by_id" t.integer "invitations_count", default: 0 t.string "role" - t.datetime "deleted_at" - t.index ["deleted_at"], name: "index_users_on_deleted_at", using: :btree t.index ["email"], name: "index_users_on_email", unique: true, using: :btree t.index ["invitation_token"], name: "index_users_on_invitation_token", unique: true, using: :btree t.index ["invitations_count"], name: "index_users_on_invitations_count", using: :btree @@ -62,4 +70,5 @@ t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree end + add_foreign_key "images", "observations" end