Skip to content
This repository has been archived by the owner on May 25, 2023. It is now read-only.

Commit

Permalink
Add upvotes of users on products.
Browse files Browse the repository at this point in the history
  • Loading branch information
ssaunier committed Apr 11, 2016
1 parent d70dddc commit c5549cd
Show file tree
Hide file tree
Showing 11 changed files with 101 additions and 12 deletions.
12 changes: 12 additions & 0 deletions app/assets/stylesheets/components/_product.scss
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
font-size: 13px;
line-height: 20px;
}
.product-upvoted, a.product-upvote.product-upvoted {
color: #5898f1;
}
.product-upvoted .product-arrow {
border-bottom-color: #5898f1;
}
a.product-upvote {
color: black;
}
.product-count {
margin: 2px 0 0;
}
Expand Down Expand Up @@ -83,6 +92,9 @@
color: #5898f1;
cursor: pointer;
}
a.product-upvote:hover, a.product-upvote:focus {
text-decoration: none;
}
.product:hover .product-upvote { transform: scale(1.2) }
.product:hover .product-controls-hidden a { color: #E6E6E6 }
.product-upvote:hover .product-arrow { border-bottom: 9px solid #5898f1 }
14 changes: 14 additions & 0 deletions app/controllers/upvotes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class UpvotesController < ApplicationController
def create
# Upvote?
product = Product.find(params[:product_id])
product.upvotes.create! user: current_user
redirect_to products_path
end

def destroy
upvote = Upvote.find(params[:id])
upvote.destroy
redirect_to products_path
end
end
2 changes: 2 additions & 0 deletions app/helpers/upvotes_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UpvotesHelper
end
1 change: 1 addition & 0 deletions app/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class Product < ApplicationRecord
has_attachment :photo

belongs_to :user
has_many :upvotes

validates :user, presence: true
validates :name, presence: true, uniqueness: true
Expand Down
6 changes: 6 additions & 0 deletions app/models/upvote.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Upvote < ApplicationRecord
belongs_to :user
belongs_to :product

validates :user, uniqueness: { scope: :product }
end
5 changes: 5 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class User < ApplicationRecord
has_many :products
has_many :upvotes
has_attachment :avatar

# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable

def voted_for?(product)
product.upvotes.where(user: self).any?
end
end
33 changes: 29 additions & 4 deletions app/views/products/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,35 @@
<% @products.each do |product| %>

<div class="product">
<div class='product-upvote'>
<div class="product-arrow"></div>
<div class='product-count'>0</div>
</div>

<% if user_signed_in? %>
<%# Upvote action %>
<% if current_user.voted_for?(product) %>
<%= link_to upvote_path(current_user.upvotes.where(product: product).first), method: :delete, class: 'product-upvote product-upvoted' do %>
<div class="product-arrow"></div>
<div class='product-count'>
<%= product.upvotes.size %>
</div>
<% end %>
<% else %>
<%= link_to upvotes_path(product_id: product.id), method: :post, class: 'product-upvote' do %>
<div class="product-arrow"></div>
<div class='product-count'>
<%= product.upvotes.size %>
</div>
<% end %>
<% end %>
<% else %>
<div class='product-upvote'>
<div class="product-arrow"></div>
<div class='product-count'>
<%= product.upvotes.size %>
</div>
</div>
<% end %>
<% if product.photo? %>
<%= cl_image_tag(product.photo.path, height: 117, width: 175, crop: :fill, class: 'product-image') %>
<% else %>
Expand Down
6 changes: 1 addition & 5 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,5 @@


resources :products

# For details on the D

# Serve websocket cable requests in-process
# mount ActionCable.server => '/cable'
resources :upvotes, only: [ :create, :destroy ]
end
10 changes: 10 additions & 0 deletions db/migrate/20160411144748_create_upvotes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreateUpvotes < ActiveRecord::Migration[5.0]
def change
create_table :upvotes do |t|
t.references :user, foreign_key: true
t.references :product, foreign_key: true

t.timestamps
end
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: 20160317172655) do
ActiveRecord::Schema.define(version: 20160411144748) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -44,6 +44,16 @@

add_index "products", ["user_id"], name: "index_products_on_user_id", using: :btree

create_table "upvotes", force: :cascade do |t|
t.integer "user_id"
t.integer "product_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

add_index "upvotes", ["product_id"], name: "index_upvotes_on_product_id", using: :btree
add_index "upvotes", ["user_id"], name: "index_upvotes_on_user_id", using: :btree

create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
Expand All @@ -63,4 +73,6 @@
add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree

add_foreign_key "products", "users"
add_foreign_key "upvotes", "products"
add_foreign_key "upvotes", "users"
end
10 changes: 8 additions & 2 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,13 @@
boris = User.create!(email: 'boris@lewagon.org', password: 'testtest')
seb = User.create!(email: 'seb@lewagon.org', password: 'testtest')

Product.create!(user: boris, name: "Kudoz", url: "http://getkudoz.com", tagline: "Tinder for job search", category: "tech")
kudoz = Product.create!(user: boris, name: "Kudoz", url: "http://getkudoz.com", tagline: "Tinder for job search", category: "tech")
Product.create!(user: boris, name: "uSlide", url: "http://uslide.io", tagline: "Youtube sucks for education", category: "education")
Product.create!(user: seb, name: "Medpics", url: "http://medpics.com", tagline: "Share your diagnostics", category: "tech")
Product.create!(user: seb, name: "Le Wagon", url: "http://lewagon.com", tagline: "We bring tech skills to creative people", category: "education")
le_wagon = Product.create!(user: seb, name: "Le Wagon", url: "http://lewagon.com", tagline: "We bring tech skills to creative people", category: "education")

# Upvotes
kudoz.upvotes.create! user: boris
kudoz.upvotes.create! user: seb

le_wagon.upvotes.create! user: seb

0 comments on commit c5549cd

Please sign in to comment.