Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

アイデアとコメントにLikeボタンの追加 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions app/assets/javascripts/likes.js.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
$ ->
$('.btn_like_idea').click ->
$(@).attr('disabled', '')
likable_id = $(@).attr("name").match(/^like_button_idea([0-9]+)/)[1]
url = '/likes/ideas/' + likable_id + '.json'
$.getJSON url, (json) ->
if json.status
button_name = 'like_button_idea' + json.likable_id
$("button[name='" + button_name + "']").each ->
$(@).removeAttr('disabled')
$(@).find('.like_count').text(json.count)

$('.btn_like_comment').click ->
$(@).attr('disabled', '')
likable_id = $(@).attr("name").match(/^like_button_comment([0-9]+)/)[1]
url = '/likes/comments/' + likable_id + '.json'
$.getJSON url, (json) ->
if json.status
button_name = 'like_button_comment' + json.likable_id
$("button[name='" + button_name + "']").each ->
$(@).removeAttr('disabled')
$(@).find('.like_count').text(json.count)
18 changes: 18 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# -*- coding: utf-8 -*-
class LikesController < ApplicationController
before_action :authenticate, except: :show

def idea
@idea = Idea.find(params[:likable_id])
if current_user
@current_user_like = @idea.like(current_user)
end
end

def comment
@comment = Comment.find(params[:likable_id])
if current_user
@current_user_like = @comment.like(current_user)
end
end
end
2 changes: 2 additions & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Comment < ActiveRecord::Base
include Liking

belongs_to :idea
belongs_to :user

Expand Down
21 changes: 21 additions & 0 deletions app/models/concerns/liking.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module Liking

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

これもLikable でいい気がします!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

みんなにコメントもらったので、ingable もあるらしい

extend ActiveSupport::Concern

included do
has_many :likes, as: :likable

def like_count
self.likes.active.count
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

countするときに、select(:id)すると、SELECT(*)でとりにいかないので、ちょっと嬉しいかも

end

def like(user)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

例えば comment.like(user) だとコメントがユーザに対してイイねしてるっぽいので、 liked_by とかのほうがよさそう

user_like = self.likes.find_by(user: user)
if user_like
user_like.toggle!
else
user_like = self.likes.create(user: user)
end
user_like
end
end
end
2 changes: 2 additions & 0 deletions app/models/idea.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Idea < ActiveRecord::Base
include Liking

has_many :comments
belongs_to :user

Expand Down
11 changes: 11 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Like < ActiveRecord::Base
belongs_to :likable, polymorphic: true
belongs_to :user

scope :active, -> { where(deleted: false) }

def toggle!
self.deleted = self.deleted ? false : true
self.save!
end
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class User < ActiveRecord::Base
has_many :ideas
has_many :likes

def self.find_or_create_from_auth_hash(auth_hash)
provider = auth_hash[:provider]
Expand Down
10 changes: 10 additions & 0 deletions app/views/ideas/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
%h1
= @idea.title
= button_tag name: "like_button_idea#{@idea.id}", class: "btn_like_idea btn pull-right" do
%span.glyphicon.glyphicon-thumbs-up
%span.like_count
= @idea.like_count

%hr

Expand All @@ -12,6 +16,12 @@
= comment.body
- else
= comment.body

= button_tag name: "like_button_comment#{comment.id}", class: "btn_like_comment btn pull-right" do
%span.glyphicon.glyphicon-thumbs-up
%span.like_count
= comment.like_count

- if logged_in?
= form_for [@idea, @comment] do |f|
- if @comment.errors.any?
Expand Down
7 changes: 7 additions & 0 deletions app/views/likes/comment.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
json.count @comment.like_count
if @current_user_like
json.(@current_user_like, :likable_id, :id, :created_at, :updated_at)
json.status true
else
json.status false
end
7 changes: 7 additions & 0 deletions app/views/likes/idea.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
json.count @idea.like_count
if @current_user_like
json.(@current_user_like, :likable_id, :id, :created_at, :updated_at)
json.status true
else
json.status false
end
5 changes: 5 additions & 0 deletions app/views/welcome/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,8 @@
= idea.title
- else
= idea.title

= button_tag name: "like_button_idea#{idea.id}", class: "btn_like_idea btn pull-right" do
%span.glyphicon.glyphicon-thumbs-up
%span.like_count
= idea.like_count
5 changes: 5 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,10 @@
resources :comments, only: :create
end

scope :likes do
get 'ideas/:likable_id' => 'likes#idea'
get 'comments/:likable_id' => 'likes#comment'
end

match '*path' => 'application#error404', via: :all
end
14 changes: 14 additions & 0 deletions db/migrate/20140517034628_create_likes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateLikes < ActiveRecord::Migration
def change
create_table :likes do |t|
t.string :likable_type, null: false
t.integer :likable_id, null: false
t.integer :user_id, null: false
t.boolean :deleted, null: false, default: false

t.timestamps
end

add_index :likes, [:likable_type, :user_id]
end
end
13 changes: 12 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20140427143623) do
ActiveRecord::Schema.define(version: 20140517034628) do

create_table "comments", force: true do |t|
t.integer "idea_id"
Expand All @@ -33,6 +33,17 @@

add_index "ideas", ["user_id"], name: "index_ideas_on_user_id"

create_table "likes", force: true do |t|
t.string "likable_type", null: false
t.integer "likable_id", null: false
t.integer "user_id", null: false
t.boolean "deleted", default: false, null: false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "likes", ["likable_type", "user_id"], name: "index_likes_on_likable_type_and_user_id"

create_table "users", force: true do |t|
t.string "provider", null: false
t.string "uid", null: false
Expand Down
6 changes: 6 additions & 0 deletions spec/factories/likes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Read about factories at https://github.com/thoughtbot/factory_girl

FactoryGirl.define do
factory :like do
end
end
5 changes: 5 additions & 0 deletions spec/models/like_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe Like do
pending "add some examples to (or delete) #{__FILE__}"
end