From d0ccb3dc85b767ef398895003fddfe5a8c19bedd Mon Sep 17 00:00:00 2001 From: Junv Zhao Date: Tue, 24 Nov 2015 00:23:26 +0800 Subject: [PATCH] #40 Add model soft delete --- Gemfile | 1 + Gemfile.lock | 3 +++ app/models/post.rb | 4 +++- app/models/share_instance.rb | 2 ++ app/models/user.rb | 1 + db/schema.rb | 10 +++++++++- db/seeds.rb | 1 + 7 files changed, 20 insertions(+), 2 deletions(-) diff --git a/Gemfile b/Gemfile index 77f8aba..01141fa 100644 --- a/Gemfile +++ b/Gemfile @@ -83,6 +83,7 @@ gem 'font-awesome-rails', '~> 4.4' #gem 'sprockets-es6' gem "sentry-raven" #, :github => "getsentry/raven-ruby" +gem "paranoia", "~> 2.0" group :development do gem 'capistrano' diff --git a/Gemfile.lock b/Gemfile.lock index 5e47226..bdb9d5d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -238,6 +238,8 @@ GEM mimemagic (= 0.3.0) paperclip-meta (1.2.0) paperclip (>= 3.0.2, < 5.0) + paranoia (2.1.4) + activerecord (~> 4.0) pg (0.18.3) polyamorous (1.2.0) activerecord (>= 3.0) @@ -431,6 +433,7 @@ DEPENDENCIES paperclip (~> 4.0) paperclip-meta paperclip-qiniu! + paranoia (~> 2.0) pg pry puma diff --git a/app/models/post.rb b/app/models/post.rb index fc411d3..208f8f9 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -3,13 +3,15 @@ class Post < ActiveRecord::Base has_many :comments, as: :commentable has_many :likes, as: :likable has_many :favorites, as: :favoritable - has_many :share_instances, as: :sharable + has_many :share_instances, as: :sharable, dependent: :destroy has_many :timeline_sources has_one :thumbnail validates_presence_of :url, :type + acts_as_paranoid + # default_scope { order(:created_at => :desc) } def first_sharer diff --git a/app/models/share_instance.rb b/app/models/share_instance.rb index f92aa9e..d6ca930 100644 --- a/app/models/share_instance.rb +++ b/app/models/share_instance.rb @@ -4,4 +4,6 @@ class ShareInstance < ActiveRecord::Base belongs_to :user delegate :name, to: :user, prefix: true + + acts_as_paranoid end diff --git a/app/models/user.rb b/app/models/user.rb index 460746b..26388e6 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -21,6 +21,7 @@ class User < ActiveRecord::Base has_one :avatar has_one :identity + acts_as_paranoid def followers user_ids = FollowRelation.where(star_id: self.id).pluck(:user_id) diff --git a/db/schema.rb b/db/schema.rb index 0930ffe..56a0468 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20151110094635) do +ActiveRecord::Schema.define(version: 20151123161926) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -144,8 +144,11 @@ ActiveRecord::Schema.define(version: 20151110094635) do t.integer "comments_count", default: 0 t.string "summary" t.integer "bookmarks_count", default: 0 + t.datetime "deleted_at" end + add_index "posts", ["deleted_at"], name: "index_posts_on_deleted_at", using: :btree + create_table "share_instances", force: :cascade do |t| t.integer "user_id" t.integer "sharable_id" @@ -153,8 +156,11 @@ ActiveRecord::Schema.define(version: 20151110094635) do t.datetime "created_at", null: false t.datetime "updated_at", null: false t.string "description" + t.datetime "deleted_at" end + add_index "share_instances", ["deleted_at"], name: "index_share_instances_on_deleted_at", using: :btree + create_table "thumbnails", force: :cascade do |t| t.datetime "created_at", null: false t.datetime "updated_at", null: false @@ -190,8 +196,10 @@ ActiveRecord::Schema.define(version: 20151110094635) do t.datetime "last_sign_in_at" t.inet "current_sign_in_ip" t.inet "last_sign_in_ip" + t.datetime "deleted_at" end + add_index "users", ["deleted_at"], name: "index_users_on_deleted_at", using: :btree add_index "users", ["email"], name: "index_users_on_email", unique: true, using: :btree add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e8..f938e82 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,4 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) +AdminUser.create!(email: 'admin@librme.com', password: 'password1', password_confirmation: 'password1')