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
belongs_to :user
belongs_to :question
has_many :comments
has_many :comments, dependent: :destroy
has_many :smiles, dependent: :destroy
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

@ -4,9 +4,9 @@ class User < ActiveRecord::Base
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable,
:validatable, :authentication_keys => [:login]
# attr_accessor :login
has_many :questions, dependent: :destroy
has_many :answers, dependent: :destroy
has_many :comments, dependent: :destroy
@ -21,7 +21,7 @@ class User < ActiveRecord::Base
has_many :followers, through: :passive_relationships, source: :source
SCREEN_NAME_REGEX = /\A[a-zA-Z0-9_]{1,16}\z/
validates :screen_name, presence: true, format: { with: SCREEN_NAME_REGEX }, uniqueness: { case_sensitive: false }
@ -33,7 +33,7 @@ class User < ActiveRecord::Base
def login
@login || self.screen_name || self.email
end
def self.find_first_by_auth_conditions(warden_conditions)
conditions = warden_conditions.dup
if login = conditions.delete(:login)
@ -70,4 +70,20 @@ class User < ActiveRecord::Base
def following?(target_user)
friends.include? target_user
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

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.
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
enable_extension "plpgsql"
@ -19,12 +19,11 @@ ActiveRecord::Schema.define(version: 20141130130221) do
create_table "answers", force: true do |t|
t.string "content"
t.integer "question_id"
t.integer "comments"
t.integer "likes"
t.integer "comment_count", default: 0, null: false
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "smile_count", default: 0, null: false
t.integer "smile_count", default: 0, null: false
end
add_index "answers", ["user_id", "created_at"], name: "index_answers_on_user_id_and_created_at", using: :btree
@ -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", ["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|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false