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

うーん jquery と helperの組み合わせはみんなどう書いてるんだろう、、、 #8

Closed
wants to merge 3 commits into from
Closed
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/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,25 @@
//= require bootstrap
//= require turbolinks
//= require_tree .
$(document).ready(function(){
$(".like").on('click', function(ev){
console.log(this)
ev.preventDefault();

$.ajax(this.href)
.done(function(msg) {
if (msg.action === 'like') {
$(ev.target).parent().parent().children().toggleClass("hidden-button");
$(ev.target).parent().addClass("hidden-button");
$(".current-user-icon.idea-"+msg.idea_id).toggleClass("hidden-icon");
Copy link
Author

Choose a reason for hiding this comment

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

id で指定しちゃったほうがよい

} else {
$(ev.target).parent().parent().children().toggleClass("hidden-button");
$(ev.target).parent().addClass("hidden-button");
$(".current-user-icon.idea-"+msg.idea_id).addClass("hidden-icon");
}
})
.fail(function(msg) {
alert("しっぱいしました reloadしてね")
});
})
});
7 changes: 7 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,10 @@
*= require bootstrap
*= require_tree .
*/

.hidden-icon {
display: none;
}
.hidden-button {
display: none;
}
20 changes: 20 additions & 0 deletions app/controllers/ideas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

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

statusは201な気がしました

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
Copy link

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

The 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
36 changes: 36 additions & 0 deletions app/helpers/application_helper.rb
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
1 change: 1 addition & 0 deletions app/models/idea.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class Idea < ActiveRecord::Base
has_many :comments
has_many :likes
belongs_to :user

validates :title, length: { maximum: 100 }, presence: true
Expand Down
25 changes: 25 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
class Like < ActiveRecord::Base
default_scope -> {where(:live => true)}
Copy link
Member

Choose a reason for hiding this comment

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

deletedの方が多いかも?

belongs_to :user
belongs_to :idea

validate :idea, uniqueness: { scope: :user }

class << self
Copy link
Member

Choose a reason for hiding this comment

The 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
12 changes: 12 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,16 @@ def self.find_or_create_from_auth_hash(auth_hash)
user.image_url = image_url
end
end

def like(idea)
like = Like.unscoped.find_or_create_by(user_id: self.id, idea_id: idea.id)
if like and like.updated_at != like.created_at
like.relike
end
like
end

def like?(idea)
(Like.where(user_id: self.id, idea_id: idea.id).count >= 1)
end
end
2 changes: 2 additions & 0 deletions app/views/ideas/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
%h1
= @idea.title
= likes_list(@idea)
= like_button(@idea)

%hr

Expand Down
4 changes: 4 additions & 0 deletions app/views/welcome/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,7 @@
= idea.title
- else
= idea.title
%p
%span ↑
= likes_list(idea)
= like_button(idea)
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
get '/auth/:provider/callback' => 'sessions#create'
get '/auth/failure' => 'sessions#failure'
get '/logout' => 'sessions#destroy', as: :logout
get '/like' => 'ideas#like', as: :like
get '/unlike' => 'ideas#unlike', as: :unlike

resources :ideas, only: [:show, :create] do
resources :comments, only: :create
Expand Down
15 changes: 15 additions & 0 deletions db/migrate/20140520052025_create_likes.rb
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
14 changes: 13 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: 20140520052025) do

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

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

create_table "likes", force: true do |t|
t.integer "idea_id", null: false
t.integer "user_id", null: false
t.boolean "live", default: true, null: false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "likes", ["idea_id", "user_id"], name: "index_likes_on_idea_id_and_user_id", unique: true
add_index "likes", ["idea_id"], name: "index_likes_on_idea_id"
add_index "likes", ["user_id"], name: "index_likes_on_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
21 changes: 21 additions & 0 deletions spec/models/like_spec.rb
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