added smile model

This commit is contained in:
nilsding 2014-11-30 19:43:22 +01:00
parent 44cea60021
commit 027f2629b1
6 changed files with 63 additions and 9 deletions

View File

@ -1,5 +1,6 @@
class Answer < ActiveRecord::Base class Answer < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :question belongs_to :question
has_many :comments has_many :comments, dependent: :destroy
has_many :smiles, dependent: :destroy
end end

6
app/models/smile.rb Normal file
View File

@ -0,0 +1,6 @@
class Smile < ActiveRecord::Base
belongs_to :user
belongs_to :answer
validates :user_id, presence: true
validates :answer_id, presence: true
end

View File

@ -70,4 +70,20 @@ class User < ActiveRecord::Base
def following?(target_user) def following?(target_user)
friends.include? target_user friends.include? target_user
end end
# smiles an answer
# @param answer [Answer] the answer to smile
def smile(answer)
Smile.create(user: self, answer: answer)
increment! :smiled_count
answer.increment! :smile_count
end
# unsmile an answer
# @param answer [Answer] the answer to unsmile
def unsmile(answer)
Smile.find_by(user: self, answer: answer).destroy
decrement! :smiled_count
answer.decrement! :smile_count
end
end end

View File

@ -0,0 +1,14 @@
class CreateSmiles < ActiveRecord::Migration
def change
create_table :smiles do |t|
t.integer :user_id
t.integer :answer_id
t.timestamps
end
add_index :smiles, :user_id
add_index :smiles, :answer_id
add_index :smiles, [:user_id, :answer_id], unique: true
end
end

View File

@ -0,0 +1,7 @@
class RenameColumnsInAnswers < ActiveRecord::Migration
def change
rename_column :answers, :comments, :comment_count
remove_column :answers, :likes
change_column :answers, :comment_count, :integer, default: 0, null: false
end
end

View File

@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20141130130221) do ActiveRecord::Schema.define(version: 20141130180152) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
@ -19,8 +19,7 @@ ActiveRecord::Schema.define(version: 20141130130221) do
create_table "answers", force: true do |t| create_table "answers", force: true do |t|
t.string "content" t.string "content"
t.integer "question_id" t.integer "question_id"
t.integer "comments" t.integer "comment_count", default: 0, null: false
t.integer "likes"
t.integer "user_id" t.integer "user_id"
t.datetime "created_at" t.datetime "created_at"
t.datetime "updated_at" t.datetime "updated_at"
@ -70,6 +69,17 @@ ActiveRecord::Schema.define(version: 20141130130221) do
add_index "relationships", ["source_id"], name: "index_relationships_on_source_id", using: :btree add_index "relationships", ["source_id"], name: "index_relationships_on_source_id", using: :btree
add_index "relationships", ["target_id"], name: "index_relationships_on_target_id", using: :btree add_index "relationships", ["target_id"], name: "index_relationships_on_target_id", using: :btree
create_table "smiles", force: true do |t|
t.integer "user_id"
t.integer "answer_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "smiles", ["answer_id"], name: "index_smiles_on_answer_id", using: :btree
add_index "smiles", ["user_id", "answer_id"], name: "index_smiles_on_user_id_and_answer_id", unique: true, using: :btree
add_index "smiles", ["user_id"], name: "index_smiles_on_user_id", using: :btree
create_table "users", force: true do |t| create_table "users", force: true do |t|
t.string "email", default: "", null: false t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false t.string "encrypted_password", default: "", null: false