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

イイネ!機能を追加 #6

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: ruby
rvm:
- 2.1.1
- 2.1.2
deploy:
provider: heroku
api_key:
Expand Down
14 changes: 7 additions & 7 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
ruby '2.1.1'
ruby '2.1.2'

source 'https://rubygems.org'
source 'https://rails-assets.org'

gem 'rails', '4.1.0'
gem 'rails', '4.1.1'
gem 'sass-rails', '~> 4.0.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.0.0'
Expand All @@ -26,14 +26,14 @@ group :development, :test do
gem 'pry-byebug'
gem "binding_of_caller"
gem 'meta_request'
gem 'rspec-rails', '~> 3.0.0.beta', github: 'rspec/rspec-rails'
gem 'rspec-core', '~> 3.0.0.beta', github: 'rspec/rspec-core'
gem 'rspec-expectations', '~> 3.0.0.beta', github: 'rspec/rspec-expectations'
gem 'rspec-mocks', '~> 3.0.0.beta', github: 'rspec/rspec-mocks'
gem 'rspec-support', '~> 3.0.0.beta', github: 'rspec/rspec-support'
gem 'rspec-rails', '~> 3.0.0.rc1'
gem 'factory_girl_rails', '~> 4.4.1'
end

group :development do
gem 'quiet_assets'
end

group :test do
gem 'shoulda-matchers', '~> 2.6.0'
gem 'capybara'
Expand Down
128 changes: 49 additions & 79 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,71 +1,28 @@
GIT
remote: git://github.com/rspec/rspec-core.git
revision: 6b67068d1ae0525d15c02f3a1b00218e5b36b4db
specs:
rspec-core (3.0.0.beta2)
rspec-support (= 3.0.0.beta2)

GIT
remote: git://github.com/rspec/rspec-expectations.git
revision: 1603a39f45d6683b562ed91a5c766d83e2f3cb20
specs:
rspec-expectations (3.0.0.beta2)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (= 3.0.0.beta2)

GIT
remote: git://github.com/rspec/rspec-mocks.git
revision: d2be1035a098b89d591d4573becae086452baf8c
specs:
rspec-mocks (3.0.0.beta2)
rspec-support (= 3.0.0.beta2)

GIT
remote: git://github.com/rspec/rspec-rails.git
revision: 3d63b16bc6d8f8cf6d605e1d62e940f8441d0cd7
specs:
rspec-rails (3.0.0.beta2)
actionpack (>= 3.0)
activemodel (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-collection_matchers
rspec-core (= 3.0.0.beta2)
rspec-expectations (= 3.0.0.beta2)
rspec-mocks (= 3.0.0.beta2)
rspec-support (= 3.0.0.beta2)

GIT
remote: git://github.com/rspec/rspec-support.git
revision: 2d12b1e2368054cf9997fa2b2e41329769aa812d
specs:
rspec-support (3.0.0.beta2)

GEM
remote: https://rubygems.org/
remote: https://rails-assets.org/
specs:
actionmailer (4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
actionmailer (4.1.1)
actionpack (= 4.1.1)
actionview (= 4.1.1)
mail (~> 2.5.4)
actionpack (4.1.0)
actionview (= 4.1.0)
activesupport (= 4.1.0)
actionpack (4.1.1)
actionview (= 4.1.1)
activesupport (= 4.1.1)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
actionview (4.1.0)
activesupport (= 4.1.0)
actionview (4.1.1)
activesupport (= 4.1.1)
builder (~> 3.1)
erubis (~> 2.7.0)
activemodel (4.1.0)
activesupport (= 4.1.0)
activemodel (4.1.1)
activesupport (= 4.1.1)
builder (~> 3.1)
activerecord (4.1.0)
activemodel (= 4.1.0)
activesupport (= 4.1.0)
activerecord (4.1.1)
activemodel (= 4.1.1)
activesupport (= 4.1.1)
arel (~> 5.0.0)
activesupport (4.1.0)
activesupport (4.1.1)
i18n (~> 0.6, >= 0.6.9)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
Expand Down Expand Up @@ -148,8 +105,8 @@ GEM
method_source (0.8.2)
mime-types (1.25.1)
mini_portile (0.5.3)
minitest (5.3.3)
multi_json (1.9.2)
minitest (5.3.4)
multi_json (1.10.1)
newrelic_rpm (3.8.0.218)
nokogiri (1.6.1)
mini_portile (~> 0.5.0)
Expand Down Expand Up @@ -180,20 +137,22 @@ GEM
pry-remote (0.1.8)
pry (~> 0.9)
slop (~> 3.0)
quiet_assets (1.0.2)
railties (>= 3.1, < 5.0)
rack (1.5.2)
rack-contrib (1.1.0)
rack (>= 0.9.1)
rack-test (0.6.2)
rack (>= 1.0)
rails (4.1.0)
actionmailer (= 4.1.0)
actionpack (= 4.1.0)
actionview (= 4.1.0)
activemodel (= 4.1.0)
activerecord (= 4.1.0)
activesupport (= 4.1.0)
rails (4.1.1)
actionmailer (= 4.1.1)
actionpack (= 4.1.1)
actionview (= 4.1.1)
activemodel (= 4.1.1)
activerecord (= 4.1.1)
activesupport (= 4.1.1)
bundler (>= 1.3.0, < 2.0)
railties (= 4.1.0)
railties (= 4.1.1)
sprockets-rails (~> 2.0)
rails-assets-bootstrap (3.1.1)
rails-assets-jquery (>= 1.9.0)
Expand All @@ -203,15 +162,29 @@ GEM
rails_stdout_logging
rails_serve_static_assets (0.0.2)
rails_stdout_logging (0.0.3)
railties (4.1.0)
actionpack (= 4.1.0)
activesupport (= 4.1.0)
railties (4.1.1)
actionpack (= 4.1.1)
activesupport (= 4.1.1)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.3.1)
rake (10.3.2)
ref (1.0.5)
rspec-collection_matchers (0.0.3)
rspec-expectations (>= 2.99.0.beta1)
rspec-core (3.0.0.rc1)
rspec-support (= 3.0.0.rc1)
rspec-expectations (3.0.0.rc1)
diff-lcs (>= 1.2.0, < 2.0)
rspec-support (= 3.0.0.rc1)
rspec-mocks (3.0.0.rc1)
rspec-support (= 3.0.0.rc1)
rspec-rails (3.0.0.rc1)
actionpack (>= 3.0)
activesupport (>= 3.0)
railties (>= 3.0)
rspec-core (= 3.0.0.rc1)
rspec-expectations (= 3.0.0.rc1)
rspec-mocks (= 3.0.0.rc1)
rspec-support (= 3.0.0.rc1)
rspec-support (3.0.0.rc1)
ruby_parser (3.1.3)
sexp_processor (~> 4.1)
sass (3.2.19)
Expand Down Expand Up @@ -278,14 +251,11 @@ DEPENDENCIES
pry
pry-byebug
pry-remote
rails (= 4.1.0)
quiet_assets
rails (= 4.1.1)
rails-assets-bootstrap
rails_12factor
rspec-core (~> 3.0.0.beta)!
rspec-expectations (~> 3.0.0.beta)!
rspec-mocks (~> 3.0.0.beta)!
rspec-rails (~> 3.0.0.beta)!
rspec-support (~> 3.0.0.beta)!
rspec-rails (~> 3.0.0.rc1)
sass-rails (~> 4.0.0)
shoulda-matchers (~> 2.6.0)
sqlite3
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# デザインビギナーズのアイデア帳

[![Build Status](https://travis-ci.org/design-beginners/debeg-idea.svg?branch=master)](https://travis-ci.org/design-beginners/debeg-idea)
[![Code Climate](https://codeclimate.com/github/design-beginners/debeg-idea.png)](https://codeclimate.com/github/design-beginners/debeg-idea)

## Setup for development

Expand Down
5 changes: 5 additions & 0 deletions app/assets/javascripts/ideas.js.coffee
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
#

$(document).on 'ajax:success', '.js-like', (xhr, data, status) ->
location.reload()

2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def authenticate
redirect_to root_path, alert: 'ログインしてください'
end

def error404(e)
def error404

Choose a reason for hiding this comment

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

これかw デバック中に困ってましたw

render 'error404', status: 404, formats: [:html]
end

Expand Down
17 changes: 17 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class LikesController < ApplicationController
before_action :authenticate

def create
idea = Idea.find(params[:idea_id])
comment_id = params[:comment_id]

likable = if comment_id

Choose a reason for hiding this comment

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

これは、likableが増えるたびにってやつですね。なんかやり方がありそうに思います。

target = likable_type.クラスに変えるメソッド.find(params[:id])

idea.comments.find(comment_id)
else
idea
end
likable.likes.create!(user: current_user)

head 201
end
end
2 changes: 2 additions & 0 deletions app/helpers/likes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module LikesHelper
end
1 change: 1 addition & 0 deletions app/models/comment.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Comment < ActiveRecord::Base
has_many :likes, as: :likable
belongs_to :idea
belongs_to :user

Expand Down
1 change: 1 addition & 0 deletions app/models/idea.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class Idea < ActiveRecord::Base
has_many :likes, as: :likable
has_many :comments
belongs_to :user

Expand Down
4 changes: 4 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Like < ActiveRecord::Base
belongs_to :user
belongs_to :likable, polymorphic: true
end
7 changes: 7 additions & 0 deletions app/views/ideas/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@
= comment.body
- else
= comment.body
.pull-right
イイネ!: #{comment.likes.count}
- if logged_in? && comment.likes.exists?(user: current_user)
= link_to 'イイネ!済み', '#', class: 'btn btn-primary btn-sm disabled'
- elsif logged_in?
= link_to 'イイネ!', idea_comment_likes_path(@idea, comment), method: :post, remote: true, class: 'btn btn-primary btn-sm js-like'

- if logged_in?
= form_for [@idea, @comment] do |f|
- if @comment.errors.any?
Expand Down
6 changes: 6 additions & 0 deletions app/views/welcome/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,9 @@
= idea.title
- else
= idea.title
.pull-right
イイネ!: #{idea.likes.count}
- if logged_in? && idea.likes.exists?(user: current_user)
= link_to 'イイネ!済み', '#', class: 'btn btn-primary btn-sm disabled'
- elsif logged_in?
= link_to 'イイネ!', idea_likes_path(idea), method: :post, remote: true, class: 'btn btn-primary btn-sm js-like'
8 changes: 6 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
get '/auth/failure' => 'sessions#failure'
get '/logout' => 'sessions#destroy', as: :logout

resources :ideas, only: [:show, :create] do
resources :comments, only: :create
concern :likable do
resources :likes, only: :create
end

resources :ideas, concerns: :likable, only: [:show, :create] do
resources :comments, concerns: :likable, only: :create
end

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

add_index :likes, [:likable_id, :likable_type]
add_index :likes, [:user_id, :likable_id, :likable_type], unique: true
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: 20140507063631) 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.integer "user_id", null: false
t.integer "likable_id", null: false
t.string "likable_type", null: false
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "likes", ["likable_id", "likable_type"], name: "index_likes_on_likable_id_and_likable_type"
add_index "likes", ["user_id", "likable_id", "likable_type"], name: "index_likes_on_user_id_and_likable_id_and_likable_type", unique: true

create_table "users", force: true do |t|
t.string "provider", null: false
t.string "uid", null: false
Expand Down
5 changes: 5 additions & 0 deletions spec/controllers/likes_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'spec_helper'

describe LikesController do

end
Loading