diff --git a/app/models/answer.rb b/app/models/answer.rb index 901a2342..080ec498 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -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 diff --git a/app/models/smile.rb b/app/models/smile.rb new file mode 100644 index 00000000..eed8ffd8 --- /dev/null +++ b/app/models/smile.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index 4795c9ba..0f1e4f17 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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 diff --git a/db/migrate/20141130175749_create_smiles.rb b/db/migrate/20141130175749_create_smiles.rb new file mode 100644 index 00000000..c993b858 --- /dev/null +++ b/db/migrate/20141130175749_create_smiles.rb @@ -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 diff --git a/db/migrate/20141130180152_rename_columns_in_answers.rb b/db/migrate/20141130180152_rename_columns_in_answers.rb new file mode 100644 index 00000000..7798cc8f --- /dev/null +++ b/db/migrate/20141130180152_rename_columns_in_answers.rb @@ -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 diff --git a/db/schema.rb b/db/schema.rb index 83c90912..b0218608 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: 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