forked from design-beginners/debeg-idea
-
Notifications
You must be signed in to change notification settings - Fork 7
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
ken1flan
wants to merge
1
commit into
ginzarb:master
Choose a base branch
from
ken1flan:add_like_to_idea
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
module Liking | ||
extend ActiveSupport::Concern | ||
|
||
included do | ||
has_many :likes, as: :likable | ||
|
||
def like_count | ||
self.likes.active.count | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. countするときに、 |
||
end | ||
|
||
def like(user) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 例えば |
||
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
これもLikable でいい気がします!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
みんなにコメントもらったので、
ing
もable
もあるらしい