-
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
うーん jquery と helperの組み合わせはみんなどう書いてるんだろう、、、 #8
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,10 @@ | |
*= require bootstrap | ||
*= require_tree . | ||
*/ | ||
|
||
.hidden-icon { | ||
display: none; | ||
} | ||
.hidden-button { | ||
display: none; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,4 +18,24 @@ def create | |
render 'welcome/index' | ||
end | ||
end | ||
|
||
def like | ||
idea = Idea.find(params[:idea_id]) | ||
if current_user.like(idea) | ||
render :json => {action: 'like', idea_id: idea.id}, :status => 200 | ||
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. statusは |
||
else | ||
render :text => 'ng', :status => 500 | ||
end | ||
end | ||
|
||
def unlike | ||
idea = Idea.find(params[:idea_id]) | ||
like = Like.where(idea_id: idea.id, user_id: current_user.id).first | ||
if like and like.unlike | ||
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. 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. これは!ためになりました m()m |
||
render :json => {action: 'unlike', idea_id: idea.id}, :status => 200 | ||
else | ||
render :text => 'ng', :status => 500 | ||
end | ||
end | ||
|
||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,38 @@ | ||
module ApplicationHelper | ||
def like_button(idea) | ||
return nil unless logged_in? | ||
like_flag = current_user.like?(idea) | ||
|
||
buttons = content_tag(:button, class: "#{(like_flag) ? '' : 'hidden-button'}") do | ||
link_to "unlike", unlike_path(idea_id: idea.id), class: "like" | ||
end | ||
buttons << content_tag(:button, class: "#{(like_flag) ? 'hidden-button' : ''}") do | ||
link_to "like", like_path(idea_id: idea.id), class: "like" | ||
end | ||
content_tag :div do | ||
buttons | ||
end | ||
end | ||
|
||
def likes_list(idea) | ||
users = idea.likes.map(&:user) | ||
if logged_in? | ||
users.reject!{|u| u == current_user} | ||
like_now = current_user.like?(idea) | ||
end | ||
users_icon(users, like_now, "idea-#{idea.id}") | ||
end | ||
|
||
def users_icon(users, current_user_show, klass = "") | ||
content_tag :div, :class => 'user-icon' do | ||
html = "" | ||
users.each do |user| | ||
html << image_tag(user.image_url, width: 30, height: 30, alt: user.nickname, title: user.nickname) | ||
end | ||
if logged_in? | ||
html << image_tag(current_user.image_url, width: 30, height: 30, alt: current_user.nickname, title: current_user.nickname, class: "#{klass} current-user-icon #{(current_user_show) ? '' : 'hidden-icon'}") | ||
end | ||
html.html_safe | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class Like < ActiveRecord::Base | ||
default_scope -> {where(:live => true)} | ||
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.
|
||
belongs_to :user | ||
belongs_to :idea | ||
|
||
validate :idea, uniqueness: { scope: :user } | ||
|
||
class << self | ||
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. クラスメソッドの定義の仕方って、特異メソッド形式と特異クラス形式どちらが多いでしょうか? |
||
def create_with_user_and_idea(user, idea) | ||
like = self.new(user_id: user.id, idea_id: idea.id) | ||
like.save | ||
like | ||
end | ||
end | ||
|
||
def unlike | ||
self.live = false | ||
self.save | ||
end | ||
|
||
def relike | ||
self.live = true | ||
self.save | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
%h1 | ||
= @idea.title | ||
= likes_list(@idea) | ||
= like_button(@idea) | ||
|
||
%hr | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,3 +29,7 @@ | |
= idea.title | ||
- else | ||
= idea.title | ||
%p | ||
%span ↑ | ||
= likes_list(idea) | ||
= like_button(idea) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
class CreateLikes < ActiveRecord::Migration | ||
def change | ||
create_table :likes do |t| | ||
|
||
t.references :idea, :null => false | ||
t.references :user, :null => false | ||
t.boolean :live, :null => false, :default => true | ||
t.timestamps | ||
end | ||
|
||
add_index :likes, :idea_id | ||
add_index :likes, :user_id | ||
add_index :likes, [:idea_id, :user_id], :unique => true | ||
end | ||
end |
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
require 'spec_helper' | ||
|
||
describe Like do | ||
|
||
describe "relations" do | ||
it { should belong_to(:user) } | ||
it { should belong_to(:idea) } | ||
end | ||
|
||
describe "#create_with_user_and_idea" do | ||
before do | ||
@user = build(:user) | ||
@idea = build(:idea) | ||
end | ||
|
||
it "return created instance" do | ||
expect(Like.create_with_user_and_idea(@user, @idea).class).to eq(Like) | ||
end | ||
end | ||
|
||
end |
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.
id で指定しちゃったほうがよい