mirror of
https://github.com/zhigang1992/tsemple.git
synced 2026-01-12 17:52:57 +08:00
Init Subscription model
This commit is contained in:
7
app/models/concerns/subscribable.rb
Normal file
7
app/models/concerns/subscribable.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
module Subscribable
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
has_many :subscriptions, as: 'subscribable', dependent: :delete_all
|
||||
end
|
||||
end
|
||||
8
app/models/subscription.rb
Normal file
8
app/models/subscription.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
class Subscription < ActiveRecord::Base
|
||||
enum status: [ :subscribed, :ignored ]
|
||||
|
||||
belongs_to :user
|
||||
belongs_to :subscribable, polymorphic: true, counter_cache: true
|
||||
|
||||
validates :user, :subscribable, presence: true
|
||||
end
|
||||
@@ -1,6 +1,7 @@
|
||||
class Topic < ActiveRecord::Base
|
||||
include Likeable
|
||||
include Trashable
|
||||
include Subscribable
|
||||
|
||||
belongs_to :user
|
||||
has_many :comments, as: 'commentable'
|
||||
|
||||
@@ -7,6 +7,7 @@ class CreateTopics < ActiveRecord::Migration
|
||||
t.float :hot, index: true, default: 0.0
|
||||
t.integer :comments_count, default: 0
|
||||
t.integer :likes_count, default: 0
|
||||
t.integer :subscriptions_count, default: 0
|
||||
t.boolean :trashed, default: false
|
||||
|
||||
t.timestamps
|
||||
|
||||
11
db/migrate/20140204164212_create_subscriptions.rb
Normal file
11
db/migrate/20140204164212_create_subscriptions.rb
Normal file
@@ -0,0 +1,11 @@
|
||||
class CreateSubscriptions < ActiveRecord::Migration
|
||||
def change
|
||||
create_table :subscriptions do |t|
|
||||
t.belongs_to :user, index: true
|
||||
t.belongs_to :subscribable, polymorphic: true, index: true
|
||||
t.integer :status, default: 0
|
||||
|
||||
t.timestamps
|
||||
end
|
||||
end
|
||||
end
|
||||
23
db/schema.rb
23
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: 20140204053314) do
|
||||
ActiveRecord::Schema.define(version: 20140204164212) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
@@ -61,14 +61,27 @@ ActiveRecord::Schema.define(version: 20140204053314) do
|
||||
|
||||
add_index "post_likes", ["user_id", "post_id"], name: "index_post_likes_on_user_id_and_post_id", unique: true, using: :btree
|
||||
|
||||
create_table "subscriptions", force: true do |t|
|
||||
t.integer "user_id"
|
||||
t.integer "subscribable_id"
|
||||
t.string "subscribable_type"
|
||||
t.integer "status", default: 0
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
add_index "subscriptions", ["subscribable_id", "subscribable_type"], name: "index_subscriptions_on_subscribable_id_and_subscribable_type", using: :btree
|
||||
add_index "subscriptions", ["user_id"], name: "index_subscriptions_on_user_id", using: :btree
|
||||
|
||||
create_table "topics", force: true do |t|
|
||||
t.integer "user_id"
|
||||
t.string "title"
|
||||
t.text "content"
|
||||
t.float "hot", default: 0.0
|
||||
t.integer "comments_count", default: 0
|
||||
t.integer "likes_count", default: 0
|
||||
t.boolean "trashed", default: false
|
||||
t.float "hot", default: 0.0
|
||||
t.integer "comments_count", default: 0
|
||||
t.integer "likes_count", default: 0
|
||||
t.integer "subscriptions_count", default: 0
|
||||
t.boolean "trashed", default: false
|
||||
t.datetime "created_at"
|
||||
t.datetime "updated_at"
|
||||
end
|
||||
|
||||
6
test/factories/subscriptions.rb
Normal file
6
test/factories/subscriptions.rb
Normal file
@@ -0,0 +1,6 @@
|
||||
FactoryGirl.define do
|
||||
factory :subscription do
|
||||
user
|
||||
association :subscribable, factory: 'topic'
|
||||
end
|
||||
end
|
||||
7
test/models/subscription_test.rb
Normal file
7
test/models/subscription_test.rb
Normal file
@@ -0,0 +1,7 @@
|
||||
require 'test_helper'
|
||||
|
||||
class SubscriptionTest < ActiveSupport::TestCase
|
||||
# test "the truth" do
|
||||
# assert true
|
||||
# end
|
||||
end
|
||||
Reference in New Issue
Block a user