diff --git a/.generators b/.generators new file mode 100644 index 00000000..16189766 --- /dev/null +++ b/.generators @@ -0,0 +1,8 @@ + + diff --git a/.idea/.generators b/.idea/.generators new file mode 100644 index 00000000..16189766 --- /dev/null +++ b/.idea/.generators @@ -0,0 +1,8 @@ + + diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 00000000..73f69e09 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml +# Editor-based HTTP Client requests +/httpRequests/ diff --git a/.idea/.rakeTasks b/.idea/.rakeTasks new file mode 100644 index 00000000..c6865d9a --- /dev/null +++ b/.idea/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/.idea/dataSources.xml b/.idea/dataSources.xml new file mode 100644 index 00000000..dee134b0 --- /dev/null +++ b/.idea/dataSources.xml @@ -0,0 +1,11 @@ + + + + + sqlite.xerial + true + org.sqlite.JDBC + jdbc:sqlite:$PROJECT_DIR$/db/development.sqlite3 + + + \ No newline at end of file diff --git a/.idea/inspectionProfiles/Project_Default.xml b/.idea/inspectionProfiles/Project_Default.xml new file mode 100644 index 00000000..b0db9b0f --- /dev/null +++ b/.idea/inspectionProfiles/Project_Default.xml @@ -0,0 +1,6 @@ + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 00000000..275dd470 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 00000000..5d4cc7ed --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/project-challenge.iml b/.idea/project-challenge.iml new file mode 100644 index 00000000..0a9cb566 --- /dev/null +++ b/.idea/project-challenge.iml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 00000000..94a25f7f --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.rakeTasks b/.rakeTasks new file mode 100644 index 00000000..c6865d9a --- /dev/null +++ b/.rakeTasks @@ -0,0 +1,7 @@ + + diff --git a/Gemfile b/Gemfile index 3f1e6961..8f4bf791 100644 --- a/Gemfile +++ b/Gemfile @@ -41,23 +41,28 @@ gem 'bootsnap', '>= 1.1.0', require: false group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console - gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] - gem 'rspec-rails', '~> 3.7' - gem 'factory_bot_rails' + gem 'byebug', platforms: %i[mri mingw x64_mingw] gem 'capybara' + gem 'factory_bot_rails' gem 'pry' gem 'rails-controller-testing' + gem 'rspec-rails', '~> 3.7' + gem 'rubocop' + gem 'solargraph' + gem 'will_paginate', '~> 3.3' end group :development do # Access an interactive console on exception pages or by calling 'console' anywhere in the code. - gem 'web-console', '>= 3.3.0' gem 'listen', '>= 3.0.5', '< 3.2' + gem 'web-console', '>= 3.3.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring + gem 'rubocop' + gem 'solargraph' gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' + gem 'will_paginate', '~> 3.3' end - # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] +gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] diff --git a/Gemfile.lock b/Gemfile.lock index 16c3b7bc..4f540688 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,64 +1,66 @@ GEM remote: https://rubygems.org/ specs: - actioncable (5.2.0) - actionpack (= 5.2.0) + actioncable (5.2.4.4) + actionpack (= 5.2.4.4) nio4r (~> 2.0) websocket-driver (>= 0.6.1) - actionmailer (5.2.0) - actionpack (= 5.2.0) - actionview (= 5.2.0) - activejob (= 5.2.0) + actionmailer (5.2.4.4) + actionpack (= 5.2.4.4) + actionview (= 5.2.4.4) + activejob (= 5.2.4.4) mail (~> 2.5, >= 2.5.4) rails-dom-testing (~> 2.0) - actionpack (5.2.0) - actionview (= 5.2.0) - activesupport (= 5.2.0) - rack (~> 2.0) + actionpack (5.2.4.4) + actionview (= 5.2.4.4) + activesupport (= 5.2.4.4) + rack (~> 2.0, >= 2.0.8) rack-test (>= 0.6.3) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.2) - actionview (5.2.0) - activesupport (= 5.2.0) + actionview (5.2.4.4) + activesupport (= 5.2.4.4) builder (~> 3.1) erubi (~> 1.4) rails-dom-testing (~> 2.0) rails-html-sanitizer (~> 1.0, >= 1.0.3) - activejob (5.2.0) - activesupport (= 5.2.0) + activejob (5.2.4.4) + activesupport (= 5.2.4.4) globalid (>= 0.3.6) - activemodel (5.2.0) - activesupport (= 5.2.0) - activerecord (5.2.0) - activemodel (= 5.2.0) - activesupport (= 5.2.0) + activemodel (5.2.4.4) + activesupport (= 5.2.4.4) + activerecord (5.2.4.4) + activemodel (= 5.2.4.4) + activesupport (= 5.2.4.4) arel (>= 9.0) - activestorage (5.2.0) - actionpack (= 5.2.0) - activerecord (= 5.2.0) + activestorage (5.2.4.4) + actionpack (= 5.2.4.4) + activerecord (= 5.2.4.4) marcel (~> 0.3.1) - activesupport (5.2.0) + activesupport (5.2.4.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) - addressable (2.5.2) - public_suffix (>= 2.0.2, < 4.0) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) arel (9.0.0) - bcrypt (3.1.12) - bindex (0.5.0) - bootsnap (1.3.0) + ast (2.4.1) + bcrypt (3.1.16) + bindex (0.8.1) + bootsnap (1.5.0) msgpack (~> 1.0) - builder (3.2.3) - byebug (10.0.2) - capybara (2.18.0) + builder (3.2.4) + byebug (11.1.3) + capybara (3.32.2) addressable mini_mime (>= 0.1.3) - nokogiri (>= 1.3.3) - rack (>= 1.0.0) - rack-test (>= 0.5.4) - xpath (>= 2.0, < 4.0) - coderay (1.1.2) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (~> 1.5) + xpath (~> 3.2) + coderay (1.1.3) coffee-rails (4.2.2) coffee-script (>= 2.2.0) railties (>= 4.0.0) @@ -66,162 +68,196 @@ GEM coffee-script-source execjs coffee-script-source (1.12.2) - concurrent-ruby (1.0.5) - crass (1.0.4) - devise (4.4.3) + concurrent-ruby (1.1.7) + crass (1.0.6) + devise (4.7.3) bcrypt (~> 3.0) orm_adapter (~> 0.1) - railties (>= 4.1.0, < 6.0) + railties (>= 4.1.0) responders warden (~> 1.2.3) - diff-lcs (1.3) - erubi (1.7.1) + diff-lcs (1.4.4) + erubi (1.9.0) execjs (2.7.0) - factory_bot (1.0.0.alpha) - activesupport (>= 3.0.0) - factory_bot_rails (1.0.0.alpha) - factory_bot (~> 1.0.0.alpha) - railties (>= 3.0.0) - ffi (1.9.25) - globalid (0.4.1) + factory_bot (5.2.0) activesupport (>= 4.2.0) - i18n (1.0.1) - concurrent-ruby (~> 1.0) - jbuilder (2.7.0) + factory_bot_rails (5.2.0) + factory_bot (~> 5.2.0) + railties (>= 4.2.0) + ffi (1.13.1) + globalid (0.4.2) activesupport (>= 4.2.0) - multi_json (>= 1.2) + i18n (1.8.5) + concurrent-ruby (~> 1.0) + jbuilder (2.10.1) + activesupport (>= 5.0.0) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) ruby_dep (~> 1.2) - loofah (2.2.2) + loofah (2.7.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) - mail (2.7.0) + mail (2.7.1) mini_mime (>= 0.1.1) - marcel (0.3.2) + marcel (0.3.3) mimemagic (~> 0.3.2) - method_source (0.9.0) - mimemagic (0.3.2) - mini_mime (1.0.0) - mini_portile2 (2.3.0) - minitest (5.11.3) - msgpack (1.2.4) - multi_json (1.13.1) - nio4r (2.3.1) - nokogiri (1.8.2) - mini_portile2 (~> 2.3.0) + method_source (1.0.0) + mimemagic (0.3.5) + mini_mime (1.0.2) + mini_portile2 (2.4.0) + minitest (5.14.2) + msgpack (1.3.3) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + nio4r (2.5.4) + nokogiri (1.10.10) + mini_portile2 (~> 2.4.0) orm_adapter (0.5.0) - pry (0.11.3) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - public_suffix (3.0.2) - puma (3.11.4) - rack (2.0.5) - rack-test (1.0.0) + parallel (1.19.2) + parser (2.7.2.0) + ast (~> 2.4.1) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) + public_suffix (4.0.6) + puma (3.12.6) + rack (2.2.3) + rack-protection (2.1.0) + rack + rack-test (1.1.0) rack (>= 1.0, < 3) - rails (5.2.0) - actioncable (= 5.2.0) - actionmailer (= 5.2.0) - actionpack (= 5.2.0) - actionview (= 5.2.0) - activejob (= 5.2.0) - activemodel (= 5.2.0) - activerecord (= 5.2.0) - activestorage (= 5.2.0) - activesupport (= 5.2.0) + rails (5.2.4.4) + actioncable (= 5.2.4.4) + actionmailer (= 5.2.4.4) + actionpack (= 5.2.4.4) + actionview (= 5.2.4.4) + activejob (= 5.2.4.4) + activemodel (= 5.2.4.4) + activerecord (= 5.2.4.4) + activestorage (= 5.2.4.4) + activesupport (= 5.2.4.4) bundler (>= 1.3.0) - railties (= 5.2.0) + railties (= 5.2.4.4) sprockets-rails (>= 2.0.0) - rails-controller-testing (1.0.2) - actionpack (~> 5.x, >= 5.0.1) - actionview (~> 5.x, >= 5.0.1) - activesupport (~> 5.x) + rails-controller-testing (1.0.5) + actionpack (>= 5.0.1.rc1) + actionview (>= 5.0.1.rc1) + activesupport (>= 5.0.1.rc1) rails-dom-testing (2.0.3) activesupport (>= 4.2.0) nokogiri (>= 1.6) - rails-html-sanitizer (1.0.4) - loofah (~> 2.2, >= 2.2.2) - railties (5.2.0) - actionpack (= 5.2.0) - activesupport (= 5.2.0) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + railties (5.2.4.4) + actionpack (= 5.2.4.4) + activesupport (= 5.2.4.4) method_source rake (>= 0.8.7) - thor (>= 0.18.1, < 2.0) - rake (12.3.1) - rb-fsevent (0.10.3) - rb-inotify (0.9.10) - ffi (>= 0.5.0, < 2) - responders (2.4.0) - actionpack (>= 4.2.0, < 5.3) - railties (>= 4.2.0, < 5.3) - rspec-core (3.7.1) - rspec-support (~> 3.7.0) - rspec-expectations (3.7.0) + thor (>= 0.19.0, < 2.0) + rainbow (3.0.0) + rake (13.0.1) + rb-fsevent (0.10.4) + rb-inotify (0.10.1) + ffi (~> 1.0) + regexp_parser (1.8.2) + responders (3.0.1) + actionpack (>= 5.0) + railties (>= 5.0) + rexml (3.2.4) + rspec-core (3.9.3) + rspec-support (~> 3.9.3) + rspec-expectations (3.9.4) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-mocks (3.7.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.1) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-rails (3.7.2) + rspec-support (~> 3.9.0) + rspec-rails (3.9.1) actionpack (>= 3.0) activesupport (>= 3.0) railties (>= 3.0) - rspec-core (~> 3.7.0) - rspec-expectations (~> 3.7.0) - rspec-mocks (~> 3.7.0) - rspec-support (~> 3.7.0) - rspec-support (3.7.1) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.4) + rubocop (1.2.0) + parallel (~> 1.10) + parser (>= 2.7.1.5) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.8) + rexml + rubocop-ast (>= 1.0.1) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 2.0) + rubocop-ast (1.1.1) + parser (>= 2.7.1.5) + ruby-progressbar (1.10.1) + ruby2_keywords (0.0.2) ruby_dep (1.5.0) - sass (3.5.6) + sass (3.7.4) sass-listen (~> 4.0.0) sass-listen (4.0.0) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) - sass-rails (5.0.7) - railties (>= 4.0.0, < 6) + sass-rails (5.1.0) + railties (>= 5.2.0) sass (~> 3.1) sprockets (>= 2.8, < 4.0) sprockets-rails (>= 2.0, < 4.0) tilt (>= 1.1, < 3) - simple_form (4.0.1) + simple_form (5.0.3) actionpack (>= 5.0) activemodel (>= 5.0) - spring (2.0.2) - activesupport (>= 4.2) + sinatra (2.1.0) + mustermann (~> 1.0) + rack (~> 2.2) + rack-protection (= 2.1.0) + tilt (~> 2.0) + solargraph (0.17.4) + bundler (~> 1.14) + parser (~> 2.4) + sinatra (~> 2) + thor (~> 0.19, >= 0.19.4) + yard (~> 0.9) + spring (2.1.1) spring-watcher-listen (2.0.1) listen (>= 2.7, < 4.0) spring (>= 1.2, < 3.0) - sprockets (3.7.1) + sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) - sprockets-rails (3.2.1) + sprockets-rails (3.2.2) actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sqlite3 (1.3.13) - thor (0.20.0) + sqlite3 (1.4.2) + thor (0.20.3) thread_safe (0.3.6) - tilt (2.0.8) - turbolinks (5.1.1) - turbolinks-source (~> 5.1) - turbolinks-source (5.1.0) - tzinfo (1.2.5) + tilt (2.0.10) + turbolinks (5.2.1) + turbolinks-source (~> 5.2) + turbolinks-source (5.2.0) + tzinfo (1.2.8) thread_safe (~> 0.1) - uglifier (4.1.11) + uglifier (4.2.0) execjs (>= 0.3.0, < 3) - warden (1.2.7) - rack (>= 1.0) - web-console (3.6.2) + unicode-display_width (1.7.0) + warden (1.2.9) + rack (>= 2.0.9) + web-console (3.7.0) actionview (>= 5.0) activemodel (>= 5.0) bindex (>= 0.4.0) railties (>= 5.0) - websocket-driver (0.7.0) + websocket-driver (0.7.3) websocket-extensions (>= 0.1.0) - websocket-extensions (0.1.3) - xpath (3.0.0) + websocket-extensions (0.1.5) + will_paginate (3.3.0) + xpath (3.2.0) nokogiri (~> 1.8) + yard (0.9.25) PLATFORMS ruby @@ -240,8 +276,10 @@ DEPENDENCIES rails (~> 5.2.0) rails-controller-testing rspec-rails (~> 3.7) + rubocop sass-rails (~> 5.0) simple_form + solargraph spring spring-watcher-listen (~> 2.0.0) sqlite3 @@ -249,9 +287,10 @@ DEPENDENCIES tzinfo-data uglifier (>= 1.3.0) web-console (>= 3.3.0) + will_paginate (~> 3.3) RUBY VERSION ruby 2.4.4p296 BUNDLED WITH - 1.16.2 + 1.17.3 diff --git a/app/assets/javascripts/likes.coffee b/app/assets/javascripts/likes.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/likes.coffee @@ -0,0 +1,3 @@ +# 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/ diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index edb771d6..5efb175f 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -15,7 +15,12 @@ */ header { - align-items: center; - display: flex; - justify-content: space-between; + align-items: center; + display: flex; + justify-content: space-between; +} + +.dog-ad { + padding: 10px; + width: 30%; } diff --git a/app/assets/stylesheets/likes.scss b/app/assets/stylesheets/likes.scss new file mode 100644 index 00000000..dacd946d --- /dev/null +++ b/app/assets/stylesheets/likes.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Likes controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/dogs_controller.rb b/app/controllers/dogs_controller.rb index cb9eebc5..ceba6ce8 100644 --- a/app/controllers/dogs_controller.rb +++ b/app/controllers/dogs_controller.rb @@ -1,15 +1,19 @@ class DogsController < ApplicationController - before_action :set_dog, only: [:show, :edit, :update, :destroy] + before_action :authenticate_user!, :set_dog, only: %i[show edit update destroy] # GET /dogs # GET /dogs.json def index - @dogs = Dog.all + @dogs = Dog.paginate(page: params[:page], per_page: 5) end # GET /dogs/1 # GET /dogs/1.json def show + # @user = User.find(params[:id]) + # redirect_to root_path unless @user == current_user + # Fetches current user's likes for show view template + @like = Like.where(user_id: current_user.id).where(dog_id: @dog.id).first if current_user end # GET /dogs/new @@ -18,13 +22,13 @@ def new end # GET /dogs/1/edit - def edit - end + def edit; end # POST /dogs # POST /dogs.json def create @dog = Dog.new(dog_params) + @dog.user_id = current_user.id respond_to do |format| if @dog.save @@ -66,13 +70,15 @@ def destroy end private - # Use callbacks to share common setup or constraints between actions. - def set_dog - @dog = Dog.find(params[:id]) - end - # Never trust parameters from the scary internet, only allow the white list through. - def dog_params - params.require(:dog).permit(:name, :description, :images) - end + # Use callbacks to share common setup or constraints between actions. + def set_dog + @dog = Dog.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + # updated dog_params to handle multiple images + def dog_params + params.require(:dog).permit(:name, :description, images: []) + end end diff --git a/app/controllers/likes_controller.rb b/app/controllers/likes_controller.rb new file mode 100644 index 00000000..e306eaf9 --- /dev/null +++ b/app/controllers/likes_controller.rb @@ -0,0 +1,39 @@ +class LikesController < ApplicationController + before_action :authenticate_user!, :set_likes, only: %i[destroy] + + def new + @like = Like.new + end + + def create + @like = Like.new(like_params) + @like.user_id = current_user.id + @dog = Dog.find(params[:dog_id]) + + respond_to do |format| + if @like.save + format.html { redirect_to @dog, notice: 'Like was successfully created.' } + format.json { render :show, status: :created, location: @dog } + else + format.json { render json: @like.errors, status: :unprocessable_entity } + end + end + end + + def edit; end + + def update; end + + def destroy; end + + private + + def set_likes + @like = Like.find(params[:id]) + @dog = Dog.find(@like.dog.id) + end + + def like_params + params.permit(:user_id, :dog_id) + end +end diff --git a/app/helpers/likes_helper.rb b/app/helpers/likes_helper.rb new file mode 100644 index 00000000..a78a7596 --- /dev/null +++ b/app/helpers/likes_helper.rb @@ -0,0 +1,2 @@ +module LikesHelper +end diff --git a/app/models/dog.rb b/app/models/dog.rb index eb40bf6e..22df904a 100644 --- a/app/models/dog.rb +++ b/app/models/dog.rb @@ -1,3 +1,5 @@ class Dog < ApplicationRecord has_many_attached :images + belongs_to :user + has_many :likes, dependent: :destroy end diff --git a/app/models/like.rb b/app/models/like.rb new file mode 100644 index 00000000..dc452b29 --- /dev/null +++ b/app/models/like.rb @@ -0,0 +1,4 @@ +class Like < ApplicationRecord + belongs_to :user + belongs_to :dog +end diff --git a/app/models/user.rb b/app/models/user.rb index b2091f9a..f61fd7f5 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,4 +3,6 @@ class User < ApplicationRecord # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable, :recoverable, :rememberable, :trackable, :validatable + has_many :dogs, dependent: :destroy + has_many :likes, dependent: :destroy end diff --git a/app/views/dogs/_form.html.erb b/app/views/dogs/_form.html.erb index 6dfc7dc9..4bf89600 100644 --- a/app/views/dogs/_form.html.erb +++ b/app/views/dogs/_form.html.erb @@ -1,10 +1,12 @@ <%= simple_form_for @dog do |f| %> <%= f.input :name %> <%= f.input :description, as: :text %> - <%= f.input :image, as: :file %> + <%= f.file_field :image, as: :file, accept: "image/*", multiple: true%> <% if @dog.images.any? %> - <%= image_tag @dog.images.first %> + <% for dog_image in @dog.images %> + <%= image_tag dog_image %> + <% end %> <% end %> <%= f.button :submit %> diff --git a/app/views/dogs/_thumbnail.html.erb b/app/views/dogs/_thumbnail.html.erb index 4d6bb441..ea04bf57 100644 --- a/app/views/dogs/_thumbnail.html.erb +++ b/app/views/dogs/_thumbnail.html.erb @@ -1,6 +1,22 @@ - -
-

<%= dog.name %>

- <%= image_tag url_for(dog.images.first), class: "dog-photo", alt: "Photo of #{dog.name}" %> -
-
+ +
+ +
+

<%= dog.name %>

+ <%= image_tag url_for(dog.images.first), class: "dog-photo", alt: "Photo of #{dog.name}" %> +
+
+ + <% if dog.id % 2 == 0%> +
+
+

Collar for Sale!

+ <%= image_tag "ad.jpg", class: "collar-photo", alt: "new advert" %> +
+
+ <% end %> +
+ + + + diff --git a/app/views/dogs/edit.html.erb b/app/views/dogs/edit.html.erb index f35e7887..128fefd9 100644 --- a/app/views/dogs/edit.html.erb +++ b/app/views/dogs/edit.html.erb @@ -1,2 +1,4 @@

Edit Your Dog

<%= render partial: 'form' %> + + diff --git a/app/views/dogs/index.html.erb b/app/views/dogs/index.html.erb index 91ea5603..dd0bdeb2 100644 --- a/app/views/dogs/index.html.erb +++ b/app/views/dogs/index.html.erb @@ -1 +1,4 @@ + <%= render partial: 'thumbnail', collection: @dogs, as: :dog %> +<%= will_paginate @dogs, page_links: false %> + diff --git a/app/views/dogs/show.html.erb b/app/views/dogs/show.html.erb index 562324d2..42252c67 100644 --- a/app/views/dogs/show.html.erb +++ b/app/views/dogs/show.html.erb @@ -1,13 +1,26 @@

<%= @dog.name %>

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

<%= @dog.description %>

- <%= link_to "Edit #{@dog.name}'s Profile", edit_dog_path %> -
- <%= link_to "Delete #{@dog.name}'s Profile", dog_path, method: :delete, data: { confirm: 'Are you sure?' } %> +
+ <%= "#{@dog.likes.count} likes" %> +
+ <% if current_user && current_user.id != @dog.user_id %> + <% if !@like %> + + <% end %> + <% end %> +
+ +
+
+ <%= link_to "Edit #{@dog.name}'s Profile", edit_dog_path %> +
+ <%= link_to "Delete #{@dog.name}'s Profile", dog_path, method: :delete, data: { confirm: 'Are you sure?' } %> +
+ diff --git a/config/routes.rb b/config/routes.rb index 06b01adc..36ba9abc 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 - root to: "dogs#index" + resources :dogs do + resources :likes, only: %i[create] + end + root to: 'dogs#index' end diff --git a/db/migrate/20201108205017_add_user_to_create_dogs.rb b/db/migrate/20201108205017_add_user_to_create_dogs.rb new file mode 100644 index 00000000..c080aecb --- /dev/null +++ b/db/migrate/20201108205017_add_user_to_create_dogs.rb @@ -0,0 +1,5 @@ +class AddUserToCreateDogs < ActiveRecord::Migration[5.2] + def change + add_reference :dogs, :user, foreign_key: true + end +end diff --git a/db/migrate/20201109223410_create_likes.rb b/db/migrate/20201109223410_create_likes.rb new file mode 100644 index 00000000..60f6dcea --- /dev/null +++ b/db/migrate/20201109223410_create_likes.rb @@ -0,0 +1,10 @@ +class CreateLikes < ActiveRecord::Migration[5.2] + def change + create_table :likes do |t| + t.references :user, foreign_key: true + t.references :dog, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 462bd430..f3ba44a3 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: 2020_11_09_223410) do create_table "active_storage_attachments", force: :cascade do |t| t.string "name", null: false @@ -40,6 +40,17 @@ t.text "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "user_id" + t.index ["user_id"], name: "index_dogs_on_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 + t.index ["dog_id"], name: "index_likes_on_dog_id" + t.index ["user_id"], name: "index_likes_on_user_id" end create_table "users", force: :cascade do |t| diff --git a/db/seeds.rb b/db/seeds.rb index d3e6fde3..6ae9fb92 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -17,7 +17,7 @@ }, { name: 'Jax', - description: '', + description: '' }, { name: 'Jiro', @@ -31,7 +31,7 @@ name: 'Bijou', description: 'Bijou is the fluffiest of them all' }, - { + { name: 'Britta', description: 'Britta has two different colored eyes' }, @@ -46,16 +46,16 @@ { name: 'Tonks', description: 'Tonks loves to run' - }, + } ] dogs.each do |dog| - dog = Dog.find_or_create_by(name: dog[:name], description: dog[:description]) - directory_name = File.join(Rails.root, 'db', 'seed', "#{dog[:name].downcase}", "*") + dog = Dog.find_or_create_by(name: dog[:name], description: dog[:description], user: User.all.sample) + directory_name = File.join(Rails.root, 'db', 'seed', dog[:name].downcase.to_s, '*') Dir.glob(directory_name).each do |filename| - if !dog.images.any?{|i| i.filename == filename} - dog.images.attach(io: File.open(filename), filename: filename.split("/").pop) + unless dog.images.any? { |i| i.filename == filename } + dog.images.attach(io: File.open(filename), filename: filename.split('/').pop) sleep 1 end end diff --git a/spec/controllers/likes_controller_spec.rb b/spec/controllers/likes_controller_spec.rb new file mode 100644 index 00000000..6c4e73de --- /dev/null +++ b/spec/controllers/likes_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe LikesController, type: :controller do + +end diff --git a/spec/factories/likes.rb b/spec/factories/likes.rb new file mode 100644 index 00000000..2f6fd75c --- /dev/null +++ b/spec/factories/likes.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :like do + like_count { "MyString" } + user { nil } + dog { nil } + end +end diff --git a/spec/helpers/likes_helper_spec.rb b/spec/helpers/likes_helper_spec.rb new file mode 100644 index 00000000..bc196878 --- /dev/null +++ b/spec/helpers/likes_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the LikesHelper. For example: +# +# describe LikesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe LikesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/like_spec.rb b/spec/models/like_spec.rb new file mode 100644 index 00000000..76ea93df --- /dev/null +++ b/spec/models/like_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Like, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index 4e322aeb..22901f46 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -1,14 +1,13 @@ # This file is copied to spec/ when you run 'rails generate rspec:install' require 'spec_helper' ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) +require File.expand_path('../config/environment', __dir__) # Prevent database truncation if the environment is production -abort("The Rails environment is running in production mode!") if Rails.env.production? +abort('The Rails environment is running in production mode!') if Rails.env.production? require 'rspec/rails' # Add additional requires below this line. Rails is not loaded until this point! require 'support/factory_bot' - # Requires supporting ruby files with custom matchers and macros, etc, in # spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are # run as spec files by default. This means that files in spec/support that end