From 5e386be0c986a9b4e305d82d9791bbeb0750884a Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Thu, 28 Feb 2019 12:35:27 -0500 Subject: [PATCH 01/11] Complete Owner Assoc and Likes Migrations --- db/migrate/20190228172840_create_likes.rb | 9 +++++++++ db/migrate/20190228172909_add_user_to_dogs.rb | 5 +++++ db/schema.rb | 10 +++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20190228172840_create_likes.rb create mode 100644 db/migrate/20190228172909_add_user_to_dogs.rb diff --git a/db/migrate/20190228172840_create_likes.rb b/db/migrate/20190228172840_create_likes.rb new file mode 100644 index 00000000..82ba9e18 --- /dev/null +++ b/db/migrate/20190228172840_create_likes.rb @@ -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 diff --git a/db/migrate/20190228172909_add_user_to_dogs.rb b/db/migrate/20190228172909_add_user_to_dogs.rb new file mode 100644 index 00000000..6b393bbe --- /dev/null +++ b/db/migrate/20190228172909_add_user_to_dogs.rb @@ -0,0 +1,5 @@ +class AddUserToDogs < ActiveRecord::Migration[5.2] + def change + add_column :dogs, :user_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 462bd430..f1f11dd3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_06_07_114248) do +ActiveRecord::Schema.define(version: 2019_02_28_172909) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false @@ -40,6 +40,14 @@ t.text "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "user_id" + end + + create_table "likes", force: :cascade do |t| + t.integer "user_id" + t.integer "dog_id" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false end create_table "users", force: :cascade do |t| From 23420f212a41955fae9f34fead8fe9ea8863992e Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Mon, 4 Mar 2019 17:54:09 -0500 Subject: [PATCH 02/11] Complete multiple image uploading --- Gemfile | 3 ++- Gemfile.lock | 5 ++++- app/controllers/application_controller.rb | 5 +++++ app/controllers/dogs_controller.rb | 11 +++++++++-- app/models/dog.rb | 5 +++++ app/models/user.rb | 7 +++++++ app/views/dogs/_form.html.erb | 2 +- 7 files changed, 33 insertions(+), 5 deletions(-) diff --git a/Gemfile b/Gemfile index 3f1e6961..f8ebd5be 100644 --- a/Gemfile +++ b/Gemfile @@ -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' @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 16c3b7bc..563f3aa0 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -220,6 +220,7 @@ 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) @@ -227,6 +228,7 @@ PLATFORMS ruby DEPENDENCIES + bcrypt (~> 3.1.7) bootsnap (>= 1.1.0) byebug capybara @@ -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 diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d12..157d5ac8 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,7 @@ class ApplicationController < ActionController::Base + protect_from_forgery with: :exception + + helper_method :current_user, :user_signed_in? + + end diff --git a/app/controllers/dogs_controller.rb b/app/controllers/dogs_controller.rb index cb9eebc5..5cc76315 100644 --- a/app/controllers/dogs_controller.rb +++ b/app/controllers/dogs_controller.rb @@ -4,7 +4,7 @@ class DogsController < ApplicationController # GET /dogs # GET /dogs.json def index - @dogs = Dog.all + @dogs = Dog.paginate(:page => params[:page], :per_page => 5) end # GET /dogs/1 @@ -25,6 +25,7 @@ def edit # POST /dogs.json def create @dog = Dog.new(dog_params) + @dog.owner = current_user respond_to do |format| if @dog.save @@ -43,6 +44,12 @@ def create # PATCH/PUT /dogs/1.json def update respond_to do |format| + + unless @dog.owner.id == current_user.id + format.html { render :edit } + format.json { render json: @dog.errors, status: :unprocessable_entity } + end + if @dog.update(dog_params) @dog.images.attach(params[:dog][:image]) if params[:dog][:image].present? @@ -73,6 +80,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 diff --git a/app/models/dog.rb b/app/models/dog.rb index eb40bf6e..a91f02fd 100644 --- a/app/models/dog.rb +++ b/app/models/dog.rb @@ -1,3 +1,8 @@ class Dog < ApplicationRecord has_many_attached :images + + belongs_to :owner, + class_name: :User, + foreign_key: :user_id + end diff --git a/app/models/user.rb b/app/models/user.rb index b2091f9a..903b7481 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,11 @@ 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 + + end diff --git a/app/views/dogs/_form.html.erb b/app/views/dogs/_form.html.erb index 6dfc7dc9..3fd29f7e 100644 --- a/app/views/dogs/_form.html.erb +++ b/app/views/dogs/_form.html.erb @@ -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 %> From bcdc1fd0b38b61a1785402d470f21faa4f9fa68f Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Tue, 5 Mar 2019 12:08:02 -0500 Subject: [PATCH 03/11] Pass all specs --- app/controllers/dogs_controller.rb | 8 +++++--- app/models/dog.rb | 3 ++- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/app/controllers/dogs_controller.rb b/app/controllers/dogs_controller.rb index 5cc76315..2b7f0488 100644 --- a/app/controllers/dogs_controller.rb +++ b/app/controllers/dogs_controller.rb @@ -45,9 +45,11 @@ def create def update respond_to do |format| - unless @dog.owner.id == current_user.id - format.html { render :edit } - format.json { render json: @dog.errors, status: :unprocessable_entity } + if @dog.owner + unless @dog.owner.id == current_user.id + format.html { render :edit } + format.json { render json: @dog.errors, status: :unprocessable_entity } + end end if @dog.update(dog_params) diff --git a/app/models/dog.rb b/app/models/dog.rb index a91f02fd..dcc89729 100644 --- a/app/models/dog.rb +++ b/app/models/dog.rb @@ -3,6 +3,7 @@ class Dog < ApplicationRecord belongs_to :owner, class_name: :User, - foreign_key: :user_id + foreign_key: :user_id, + optional: true end From c98785b97944af22f54d3b99900ed56ae0d15a12 Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Tue, 5 Mar 2019 21:14:51 -0500 Subject: [PATCH 04/11] Complete Like and Unlike buttons and count --- app/controllers/dogs_controller.rb | 1 + app/controllers/likes_controller.rb | 20 ++++++++++++++++++++ app/models/dog.rb | 2 ++ app/models/like.rb | 6 ++++++ app/models/user.rb | 1 + app/views/dogs/show.html.erb | 16 ++++++++++++++++ app/views/layouts/application.html.erb | 2 +- config/routes.rb | 4 +++- 8 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 app/controllers/likes_controller.rb create mode 100644 app/models/like.rb diff --git a/app/controllers/dogs_controller.rb b/app/controllers/dogs_controller.rb index 2b7f0488..b59bbf22 100644 --- a/app/controllers/dogs_controller.rb +++ b/app/controllers/dogs_controller.rb @@ -10,6 +10,7 @@ def index # GET /dogs/1 # GET /dogs/1.json def show + @like = Like.find_by(user_id: current_user.id, dog_id: params[:id]) end # GET /dogs/new diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 00000000..fb6b0dc0 --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -0,0 +1,20 @@ +class LikesController < ApplicationController + before_action :authenticate_user! + +def create + @like = Like.new(user_id: current_user.id, dog_id: params[:dog_id]) + @like.save + @dog = Dog.find(params[:dog_id]) + redirect_to @dog + +end + +def destroy + @dog = Dog.find(params[:dog_id]) + @like = Like.find_by(user_id: current_user.id, dog_id: params[:dog_id]) + @like.delete + redirect_to @dog +end + + +end diff --git a/app/models/dog.rb b/app/models/dog.rb index dcc89729..401ba7b5 100644 --- a/app/models/dog.rb +++ b/app/models/dog.rb @@ -6,4 +6,6 @@ class Dog < ApplicationRecord foreign_key: :user_id, optional: true + has_many :likes + end diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 00000000..b8203e0a --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,6 @@ +class Like < ApplicationRecord + + belongs_to :user + belongs_to :dog + +end \ No newline at end of file diff --git a/app/models/user.rb b/app/models/user.rb index 903b7481..ff1a57c5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -9,5 +9,6 @@ class User < ApplicationRecord class_name: :Dog, foreign_key: :user_id + has_many :likes end diff --git a/app/views/dogs/show.html.erb b/app/views/dogs/show.html.erb index 562324d2..f07c382a 100644 --- a/app/views/dogs/show.html.erb +++ b/app/views/dogs/show.html.erb @@ -1,5 +1,21 @@

<%= @dog.name %>

+ <% if @like %> +
+ + + +
+ <% else %> +
+ + +
+ <% end %> + + + +

<%= @dog.name %> has <%= @dog.likes.count %> likes! BARK!

<% @dog.images.each do |image| %> <%= image_tag url_for(image), alt: "Photo of #{@dog.name}" %> diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index d4bfefcd..71959b1b 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,7 +1,7 @@ - DogProfile + Paul's DogProfile <%= csrf_meta_tags %> <%= csp_meta_tag %> diff --git a/config/routes.rb b/config/routes.rb index 06b01adc..598038ca 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 From bceaf6a954c193627de4add6d89cdf66c2138062 Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Wed, 6 Mar 2019 10:22:43 -0500 Subject: [PATCH 05/11] Complete AD --- app/views/dogs/index.html.erb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/app/views/dogs/index.html.erb b/app/views/dogs/index.html.erb index 91ea5603..8dcc4aac 100644 --- a/app/views/dogs/index.html.erb +++ b/app/views/dogs/index.html.erb @@ -1 +1,11 @@ -<%= render partial: 'thumbnail', collection: @dogs, as: :dog %> + + +<% count = 0 %> +<% @dogs.each do |dog| %> + <%= render partial: 'thumbnail', object: dog, as: 'dog' %> + <% count += 1 %> + <% if count == 2 %> + + <% count = 0 %> + <% end %> +<% end %> From b2dd9033019b81f16ad6f4305efc305ab24bf36b Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Wed, 6 Mar 2019 11:52:49 -0500 Subject: [PATCH 06/11] Complete Pagination --- app/assets/stylesheets/application.css | 67 ++++++++++++++++++++++++++ app/views/dogs/index.html.erb | 6 +++ app/views/dogs/show.html.erb | 10 ++-- app/views/layouts/application.html.erb | 3 +- 4 files changed, 80 insertions(+), 6 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index edb771d6..9caeca31 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -19,3 +19,70 @@ 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; +} + +.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: 5px; + height: 25px; + font-size: 14px; + font-weight: 100; +} + +.unlike-button { + font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; + background-color: gray; + height: 25px; + font-size: 14px; + font-weight: 100; +} + +.dog-name { + font-family: Gaegu; + font-weight: 500; + font-size: 36px; + margin: 30px 0 0 0 +} + +img { + height: 100px; + border: solid black 3px; + border-radius: 5px; +} + +img:hover { + box-shadow: 3px 3px 6px gray +} + +.like-count { + font-size: 12px; + color: gray; + font-style: italic; + font-weight: 400; +} + +.description { + font-family: Gaegu; + font-weight: 200; + font-size: 18px; +} \ No newline at end of file diff --git a/app/views/dogs/index.html.erb b/app/views/dogs/index.html.erb index 8dcc4aac..88c92373 100644 --- a/app/views/dogs/index.html.erb +++ b/app/views/dogs/index.html.erb @@ -1,11 +1,17 @@ +<%= will_paginate @dogs %> <% count = 0 %> <% @dogs.each do |dog| %> <%= render partial: 'thumbnail', object: dog, as: 'dog' %> <% count += 1 %> <% if count == 2 %> +
+

You may be interested in:

+
<% count = 0 %> <% end %> <% end %> + +<%= will_paginate @dogs %> diff --git a/app/views/dogs/show.html.erb b/app/views/dogs/show.html.erb index f07c382a..21714f36 100644 --- a/app/views/dogs/show.html.erb +++ b/app/views/dogs/show.html.erb @@ -1,27 +1,27 @@
-

<%= @dog.name %>

+

<%= @dog.name %>

<% if @like %>
- +
<% else %>
- +
<% end %> -

<%= @dog.name %> has <%= @dog.likes.count %> likes! BARK!

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

<%= @dog.description %>

+

<%= @dog.description %>

<%= link_to "Edit #{@dog.name}'s Profile", edit_dog_path %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 71959b1b..5e470bf6 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -4,7 +4,8 @@ Paul's DogProfile <%= csrf_meta_tags %> <%= csp_meta_tag %> - + + <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track": "reload" %> <%= javascript_include_tag "application", "data-turbolinks-track": "reload" %> From d9e52cdfea0fa433133241eaba04317941488cd7 Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Wed, 6 Mar 2019 19:54:14 -0500 Subject: [PATCH 07/11] Order Index by Likes --- app/assets/stylesheets/application.css | 25 +++++++++++++++++++------ app/controllers/dogs_controller.rb | 20 ++++++++++++++++---- app/controllers/likes_controller.rb | 12 ++++++++---- app/models/dog.rb | 1 + app/models/like.rb | 1 + app/views/dogs/_thumbnail.html.erb | 1 + app/views/layouts/application.html.erb | 4 ++-- 7 files changed, 48 insertions(+), 16 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 9caeca31..4981fc9a 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -24,6 +24,10 @@ 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; @@ -43,9 +47,9 @@ padding-bottom: 10px; .like-button { font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; background-color: pink; - border-radius: 5px; + border-radius: 20px; height: 25px; - font-size: 14px; + font-size: 12px; font-weight: 100; } @@ -53,15 +57,17 @@ padding-bottom: 10px; font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif; background-color: gray; height: 25px; - font-size: 14px; - font-weight: 100; + font-size: 12px; + font-weight: 500; + border-radius: 3px; } .dog-name { font-family: Gaegu; font-weight: 500; font-size: 36px; - margin: 30px 0 0 0 + margin: 30px 0 0 0; + text-shadow: 1px 1px 1px gray; } img { @@ -73,7 +79,14 @@ img { 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; @@ -85,4 +98,4 @@ img:hover { font-family: Gaegu; font-weight: 200; font-size: 18px; -} \ No newline at end of file +} diff --git a/app/controllers/dogs_controller.rb b/app/controllers/dogs_controller.rb index b59bbf22..99c17552 100644 --- a/app/controllers/dogs_controller.rb +++ b/app/controllers/dogs_controller.rb @@ -4,7 +4,10 @@ class DogsController < ApplicationController # GET /dogs # GET /dogs.json def index - @dogs = Dog.paginate(:page => params[:page], :per_page => 5) + @dogs = Dog.left_joins(:likes) + .group(:id) + .order('COUNT(likes.id) DESC') + .paginate(:page => params[:page], :per_page => 5) end # GET /dogs/1 @@ -47,10 +50,11 @@ def update respond_to do |format| if @dog.owner - unless @dog.owner.id == current_user.id - format.html { render :edit } - format.json { render json: @dog.errors, status: :unprocessable_entity } + 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) @@ -68,6 +72,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.' } diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index fb6b0dc0..c06cd7aa 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -2,11 +2,15 @@ class LikesController < ApplicationController before_action :authenticate_user! def create - @like = Like.new(user_id: current_user.id, dog_id: params[:dog_id]) - @like.save @dog = Dog.find(params[:dog_id]) - redirect_to @dog - + if @dog.owner.id == current_user.id + flash[:notice] = "*You can't like your own dog!*" + 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 diff --git a/app/models/dog.rb b/app/models/dog.rb index 401ba7b5..529fc8e2 100644 --- a/app/models/dog.rb +++ b/app/models/dog.rb @@ -8,4 +8,5 @@ class Dog < ApplicationRecord has_many :likes + end diff --git a/app/models/like.rb b/app/models/like.rb index b8203e0a..90cee999 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -3,4 +3,5 @@ class Like < ApplicationRecord belongs_to :user belongs_to :dog + end \ No newline at end of file diff --git a/app/views/dogs/_thumbnail.html.erb b/app/views/dogs/_thumbnail.html.erb index 4d6bb441..b3a92087 100644 --- a/app/views/dogs/_thumbnail.html.erb +++ b/app/views/dogs/_thumbnail.html.erb @@ -1,6 +1,7 @@

<%= dog.name %>

+

Likes: <%= dog.likes.count %>

<%= image_tag url_for(dog.images.first), class: "dog-photo", alt: "Photo of #{dog.name}" %>
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5e470bf6..25a6a169 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,10 +1,10 @@ - Paul's DogProfile + PDC DogProfile <%= csrf_meta_tags %> <%= csp_meta_tag %> - + <%= stylesheet_link_tag "application", media: "all", "data-turbolinks-track": "reload" %> <%= javascript_include_tag "application", "data-turbolinks-track": "reload" %> From 122eb10630108e9841b4f293a7aba65bbf41bf9e Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Wed, 6 Mar 2019 21:25:08 -0500 Subject: [PATCH 08/11] Complete Sort by hourly likes --- app/controllers/dogs_controller.rb | 33 ++++++++++++++++++++++++++---- app/models/like.rb | 1 - app/views/dogs/index.html.erb | 1 - 3 files changed, 29 insertions(+), 6 deletions(-) diff --git a/app/controllers/dogs_controller.rb b/app/controllers/dogs_controller.rb index 99c17552..151f0436 100644 --- a/app/controllers/dogs_controller.rb +++ b/app/controllers/dogs_controller.rb @@ -1,13 +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.left_joins(:likes) - .group(:id) - .order('COUNT(likes.id) DESC') - .paginate(:page => params[:page], :per_page => 5) + 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 diff --git a/app/models/like.rb b/app/models/like.rb index 90cee999..202b8943 100644 --- a/app/models/like.rb +++ b/app/models/like.rb @@ -2,6 +2,5 @@ class Like < ApplicationRecord belongs_to :user belongs_to :dog - end \ No newline at end of file diff --git a/app/views/dogs/index.html.erb b/app/views/dogs/index.html.erb index 88c92373..d1e420fb 100644 --- a/app/views/dogs/index.html.erb +++ b/app/views/dogs/index.html.erb @@ -14,4 +14,3 @@ <% end %> <% end %> -<%= will_paginate @dogs %> From 4225256588ef3e8c13c001178725014c53307734 Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Wed, 6 Mar 2019 21:27:00 -0500 Subject: [PATCH 09/11] Add bottom pg navigation --- app/views/dogs/index.html.erb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/views/dogs/index.html.erb b/app/views/dogs/index.html.erb index d1e420fb..0c69ecdf 100644 --- a/app/views/dogs/index.html.erb +++ b/app/views/dogs/index.html.erb @@ -14,3 +14,4 @@ <% end %> <% end %> +<%= will_paginate @dogs %> \ No newline at end of file From 55f0e3cce7df079ad265d6609b696374c3096203 Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Wed, 6 Mar 2019 21:53:52 -0500 Subject: [PATCH 10/11] Refactor Like buttons to pass specs --- app/assets/stylesheets/application.css | 4 +++- app/controllers/dogs_controller.rb | 1 - app/controllers/likes_controller.rb | 13 +++++++++++-- app/views/dogs/show.html.erb | 23 +++++++++++------------ 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index 4981fc9a..b7a2e156 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -51,15 +51,17 @@ padding-bottom: 10px; 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: gray; + background-color: lightgray; height: 25px; font-size: 12px; font-weight: 500; border-radius: 3px; + color: black; } .dog-name { diff --git a/app/controllers/dogs_controller.rb b/app/controllers/dogs_controller.rb index 151f0436..1ce5e76c 100644 --- a/app/controllers/dogs_controller.rb +++ b/app/controllers/dogs_controller.rb @@ -38,7 +38,6 @@ def index # GET /dogs/1 # GET /dogs/1.json def show - @like = Like.find_by(user_id: current_user.id, dog_id: params[:id]) end # GET /dogs/new diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb index c06cd7aa..c42c479a 100644 --- a/app/controllers/likes_controller.rb +++ b/app/controllers/likes_controller.rb @@ -2,10 +2,14 @@ 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 @@ -16,8 +20,13 @@ def create def destroy @dog = Dog.find(params[:dog_id]) @like = Like.find_by(user_id: current_user.id, dog_id: params[:dog_id]) - @like.delete - redirect_to @dog + 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 diff --git a/app/views/dogs/show.html.erb b/app/views/dogs/show.html.erb index 21714f36..c8f18475 100644 --- a/app/views/dogs/show.html.erb +++ b/app/views/dogs/show.html.erb @@ -1,17 +1,16 @@

<%= @dog.name %>

- <% if @like %> -
- - - -
- <% else %> -
- - -
- <% end %> + +
+ + +
+ +
+ + + +
From 13a5239f385c80cef30a90e09933d3c7aab255f3 Mon Sep 17 00:00:00 2001 From: Paul Cloeter Date: Wed, 6 Mar 2019 21:56:40 -0500 Subject: [PATCH 11/11] Add button CSS --- app/assets/stylesheets/application.css | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index b7a2e156..abafd4ba 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -64,6 +64,11 @@ padding-bottom: 10px; color: black; } +.like-button:hover, .unlike-button:hover { + cursor: pointer; + box-shadow: 1px 1px 1px gray +} + .dog-name { font-family: Gaegu; font-weight: 500;