Skip to content
Open
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
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
gem 'will_paginate', '~> 3.1.0'

# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
Expand All @@ -25,7 +26,7 @@ gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
gem 'bcrypt', '~> 3.1.7'

# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
Expand Down
5 changes: 4 additions & 1 deletion Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -220,13 +220,15 @@ GEM
websocket-driver (0.7.0)
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.3)
will_paginate (3.1.6)
xpath (3.0.0)
nokogiri (~> 1.8)

PLATFORMS
ruby

DEPENDENCIES
bcrypt (~> 3.1.7)
bootsnap (>= 1.1.0)
byebug
capybara
Expand All @@ -249,9 +251,10 @@ DEPENDENCIES
tzinfo-data
uglifier (>= 1.3.0)
web-console (>= 3.3.0)
will_paginate (~> 3.1.0)

RUBY VERSION
ruby 2.4.4p296

BUNDLED WITH
1.16.2
2.0.1
87 changes: 87 additions & 0 deletions app/assets/stylesheets/application.css
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,90 @@ header {
display: flex;
justify-content: space-between;
}

body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
}

h1 {
text-shadow: 1px 1px 1px gray;
font-family: sans-serif;
}
.actions a {
margin: 0 20px 0 0;
border: solid 1px black;
border-radius: 2px;
padding: 10px;
}

a {
text-decoration: none;
padding-bottom: 10px;
}

.ad {
margin: 30px 0 0 0;
}

.like-button {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: pink;
border-radius: 20px;
height: 25px;
font-size: 12px;
font-weight: 100;
margin-bottom: 10px;
}

.unlike-button {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
background-color: lightgray;
height: 25px;
font-size: 12px;
font-weight: 500;
border-radius: 3px;
color: black;
}

.like-button:hover, .unlike-button:hover {
cursor: pointer;
box-shadow: 1px 1px 1px gray
}

.dog-name {
font-family: Gaegu;
font-weight: 500;
font-size: 36px;
margin: 30px 0 0 0;
text-shadow: 1px 1px 1px gray;
}

img {
height: 100px;
border: solid black 3px;
border-radius: 5px;
}

img:hover {
box-shadow: 3px 3px 6px gray
}
.index-count {
font-size: 12px;
color: gray;
font-style: italic;
font-weight: 400;
margin-top: 0

}
.like-count {
font-size: 12px;
color: gray;
font-style: italic;
font-weight: 400;
}

.description {
font-family: Gaegu;
font-weight: 200;
font-size: 18px;
}
5 changes: 5 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

helper_method :current_user, :user_signed_in?


end
50 changes: 48 additions & 2 deletions app/controllers/dogs_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,38 @@
require 'will_paginate/array'

class DogsController < ApplicationController
before_action :set_dog, only: [:show, :edit, :update, :destroy]

# GET /dogs
# GET /dogs.json
def index
@dogs = Dog.all
unliked_dogs = []
liked_dogs = []
liked_ids = Like.where('created_at > ?', 1.hours.ago).pluck(:dog_id)
dogs = liked_ids.map { |id| Dog.find(id) }
Dog.all.each do |dog|
if liked_ids.include?(dog.id)
liked_dogs << dog
else
unliked_dogs << dog
end
end
liked_dogs = dogs.sort_by { |dog| dog.likes.count }.reverse
all_dogs = liked_dogs.concat(unliked_dogs)
@dogs = all_dogs.paginate(:page => params[:page], :per_page => 5)

# @dogs = Dog.select('dogs.*, count(likes.id) as likes_count')
# .joins('LEFT OUTER JOIN likes on likes.post_id = dogs.id')
# .group_by('dogs.id')
# .order('likes_count desc')
# .where('created_at > ?', 1.hours.ago)
# .paginate(:page => params[:page], :per_page => 5)

# @dogs = Dog.left_joins(:likes)
# .group(:id)
# .order('COUNT(likes.id) DESC')
# .where('created_at > ?', 1.hours.ago)
# .paginate(:page => params[:page], :per_page => 5)
end

# GET /dogs/1
Expand All @@ -25,6 +53,7 @@ def edit
# POST /dogs.json
def create
@dog = Dog.new(dog_params)
@dog.owner = current_user

respond_to do |format|
if @dog.save
Expand All @@ -43,6 +72,15 @@ def create
# PATCH/PUT /dogs/1.json
def update
respond_to do |format|

if @dog.owner
if @dog.owner.id != current_user.id
flash[:notice] = "*You can't EDIT someone else's dog!*"
redirect_to @dog
end
return
end

if @dog.update(dog_params)
@dog.images.attach(params[:dog][:image]) if params[:dog][:image].present?

Expand All @@ -58,6 +96,14 @@ def update
# DELETE /dogs/1
# DELETE /dogs/1.json
def destroy
if @dog.owner
if @dog.owner.id != current_user.id
flash[:notice] = "*You can't just DELETE someone else's dog!*"
redirect_to @dog
return
end
end

@dog.destroy
respond_to do |format|
format.html { redirect_to dogs_url, notice: 'Dog was successfully destroyed.' }
Expand All @@ -73,6 +119,6 @@ def set_dog

# Never trust parameters from the scary internet, only allow the white list through.
def dog_params
params.require(:dog).permit(:name, :description, :images)
params.require(:dog).permit(:name, :description, images: [] )
end
end
33 changes: 33 additions & 0 deletions app/controllers/likes_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class LikesController < ApplicationController
before_action :authenticate_user!

def create
@like = Like.find_by(user_id: current_user.id, dog_id: params[:dog_id])
@dog = Dog.find(params[:dog_id])
if @dog.owner.id == current_user.id
flash[:notice] = "*You can't like your own dog!*"
redirect_to @dog
elsif @like
flash[:notice] = "*You can't like the same dog twice!*"
redirect_to @dog
else
@like = Like.new(user_id: current_user.id, dog_id: params[:dog_id])
@like.save
redirect_to @dog
end
end

def destroy
@dog = Dog.find(params[:dog_id])
@like = Like.find_by(user_id: current_user.id, dog_id: params[:dog_id])
if !@like
flash[:notice] = "*Why would you want to UNlike a dog you haven't even liked?!... No.*"
redirect_to @dog
else
@like.delete
redirect_to @dog
end
end


end
9 changes: 9 additions & 0 deletions app/models/dog.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
class Dog < ApplicationRecord
has_many_attached :images

belongs_to :owner,
class_name: :User,
foreign_key: :user_id,
optional: true

has_many :likes


end
6 changes: 6 additions & 0 deletions app/models/like.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class Like < ApplicationRecord

belongs_to :user
belongs_to :dog

end
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,12 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable


has_many :dogs,
class_name: :Dog,
foreign_key: :user_id

has_many :likes

end
2 changes: 1 addition & 1 deletion app/views/dogs/_form.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<%= simple_form_for @dog do |f| %>
<%= f.input :name %>
<%= f.input :description, as: :text %>
<%= f.input :image, as: :file %>
<%= f.input :images, :input_html => { :multiple => true }%>

<% if @dog.images.any? %>
<%= image_tag @dog.images.first %>
Expand Down
1 change: 1 addition & 0 deletions app/views/dogs/_thumbnail.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<a href="<%= url_for(dog) %>">
<article>
<h2 class="dog-name"><%= dog.name %></h2>
<p class="index-count">Likes: <%= dog.likes.count %></p>
<%= image_tag url_for(dog.images.first), class: "dog-photo", alt: "Photo of #{dog.name}" %>
</article>
</a>
18 changes: 17 additions & 1 deletion app/views/dogs/index.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,17 @@
<%= render partial: 'thumbnail', collection: @dogs, as: :dog %>
<!-- <%= render partial: 'thumbnail', collection: @dogs, as: :dog %> -->
<%= will_paginate @dogs %>

<% count = 0 %>
<% @dogs.each do |dog| %>
<%= render partial: 'thumbnail', object: dog, as: 'dog' %>
<% count += 1 %>
<% if count == 2 %>
<div class="ad">
<h3>You may be interested in:</h2>
<img src="../../assets/ad.jpg">
</div>
<% count = 0 %>
<% end %>
<% end %>

<%= will_paginate @dogs %>
19 changes: 17 additions & 2 deletions app/views/dogs/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
<section>
<h2><%= @dog.name %></h2>
<h2 class="dog-name"><%= @dog.name %></h2>

<form action="<%= dog_likes_url(@dog) %>" method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<input class="like-button"type="submit" value="Like this Pup!">
</form>

<form action="<%= dog_like_url(@dog) %>" method="post">
<input type="hidden" name="authenticity_token" value="<%= form_authenticity_token %>">
<input type="hidden" name="_method" value="delete">
<input class="unlike-button" type="submit" value="Unlike this Pup (you monster...) ">
</form>



<p class="like-count"><%= @dog.name %> has <%= @dog.likes.count %> likes! BARK!</p>

<% @dog.images.each do |image| %>
<%= image_tag url_for(image), alt: "Photo of #{@dog.name}" %>
<% end %>

<p><%= @dog.description %></p>
<p class="description"><%= @dog.description %></p>

<%= link_to "Edit #{@dog.name}'s Profile", edit_dog_path %>
<br>
Expand Down
3 changes: 2 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!DOCTYPE html>
<html>
<head>
<title>DogProfile</title>
<title>PDC DogProfile</title>
<%= csrf_meta_tags %>
<%= csp_meta_tag %>

<link href="https://fonts.googleapis.com/css?family=Gaegu" rel="stylesheet">
<%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track": "reload" %>
<%= javascript_include_tag "application", "data-turbolinks-track": "reload" %>
</head>
Expand Down
4 changes: 3 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Rails.application.routes.draw do
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
devise_for :users
resources :dogs
resources :dogs do
resources :likes, only: [:create, :destroy]
end
root to: "dogs#index"
end
9 changes: 9 additions & 0 deletions db/migrate/20190228172840_create_likes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateLikes < ActiveRecord::Migration[5.2]
def change
create_table :likes do |t|
t.integer :user_id
t.integer :dog_id
t.timestamps
end
end
end
Loading