From 03e9806822bb6316df7eb73d003f98a3f4314d0d Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 8 Oct 2018 14:59:37 -0700 Subject: [PATCH 01/59] Initial Rails Setup --- .gitignore | 27 ++ .ruby-version | 1 + Gemfile | 81 +++++ Gemfile.lock | 277 ++++++++++++++++++ Guardfile | 9 + README.md | 122 +------- Rakefile | 6 + app/assets/config/manifest.js | 3 + app/assets/images/.keep | 0 app/assets/javascripts/application.js | 20 ++ app/assets/javascripts/cable.js | 13 + app/assets/javascripts/channels/.keep | 0 app/assets/stylesheets/application.scss | 18 ++ app/channels/application_cable/channel.rb | 4 + app/channels/application_cable/connection.rb | 4 + app/controllers/application_controller.rb | 2 + app/controllers/concerns/.keep | 0 app/helpers/application_helper.rb | 2 + app/jobs/application_job.rb | 2 + app/mailers/application_mailer.rb | 4 + app/models/application_record.rb | 3 + app/models/concerns/.keep | 0 app/views/layouts/application.html.erb | 15 + app/views/layouts/mailer.html.erb | 13 + app/views/layouts/mailer.text.erb | 1 + bin/bundle | 3 + bin/rails | 9 + bin/rake | 9 + bin/setup | 36 +++ bin/spring | 17 ++ bin/update | 31 ++ bin/yarn | 11 + config.ru | 5 + config/application.rb | 25 ++ config/boot.rb | 4 + config/cable.yml | 10 + config/credentials.yml.enc | 1 + config/database.yml | 85 ++++++ config/environment.rb | 5 + config/environments/development.rb | 61 ++++ config/environments/production.rb | 94 ++++++ config/environments/test.rb | 46 +++ .../application_controller_renderer.rb | 8 + config/initializers/assets.rb | 14 + config/initializers/backtrace_silencers.rb | 7 + .../initializers/content_security_policy.rb | 25 ++ config/initializers/cookies_serializer.rb | 5 + .../initializers/filter_parameter_logging.rb | 4 + config/initializers/inflections.rb | 16 + config/initializers/mime_types.rb | 4 + config/initializers/wrap_parameters.rb | 14 + config/locales/en.yml | 33 +++ config/puma.rb | 34 +++ config/routes.rb | 3 + config/spring.rb | 6 + config/storage.yml | 34 +++ db/seeds.rb | 7 + lib/assets/.keep | 0 lib/tasks/.keep | 0 log/.keep | 0 package.json | 5 + public/404.html | 67 +++++ public/422.html | 67 +++++ public/500.html | 66 +++++ public/apple-touch-icon-precomposed.png | 0 public/apple-touch-icon.png | 0 public/favicon.ico | 0 public/robots.txt | 1 + storage/.keep | 0 test/application_system_test_case.rb | 5 + test/controllers/.keep | 0 test/fixtures/.keep | 0 test/fixtures/files/.keep | 0 test/helpers/.keep | 0 test/integration/.keep | 0 test/mailers/.keep | 0 test/models/.keep | 0 test/system/.keep | 0 test/test_helper.rb | 24 ++ tmp/.keep | 0 vendor/.keep | 0 81 files changed, 1419 insertions(+), 109 deletions(-) create mode 100644 .gitignore create mode 100644 .ruby-version create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Guardfile create mode 100644 Rakefile create mode 100644 app/assets/config/manifest.js create mode 100644 app/assets/images/.keep create mode 100644 app/assets/javascripts/application.js create mode 100644 app/assets/javascripts/cable.js create mode 100644 app/assets/javascripts/channels/.keep create mode 100644 app/assets/stylesheets/application.scss create mode 100644 app/channels/application_cable/channel.rb create mode 100644 app/channels/application_cable/connection.rb create mode 100644 app/controllers/application_controller.rb create mode 100644 app/controllers/concerns/.keep create mode 100644 app/helpers/application_helper.rb create mode 100644 app/jobs/application_job.rb create mode 100644 app/mailers/application_mailer.rb create mode 100644 app/models/application_record.rb create mode 100644 app/models/concerns/.keep create mode 100644 app/views/layouts/application.html.erb create mode 100644 app/views/layouts/mailer.html.erb create mode 100644 app/views/layouts/mailer.text.erb create mode 100755 bin/bundle create mode 100755 bin/rails create mode 100755 bin/rake create mode 100755 bin/setup create mode 100755 bin/spring create mode 100755 bin/update create mode 100755 bin/yarn create mode 100644 config.ru create mode 100644 config/application.rb create mode 100644 config/boot.rb create mode 100644 config/cable.yml create mode 100644 config/credentials.yml.enc create mode 100644 config/database.yml create mode 100644 config/environment.rb create mode 100644 config/environments/development.rb create mode 100644 config/environments/production.rb create mode 100644 config/environments/test.rb create mode 100644 config/initializers/application_controller_renderer.rb create mode 100644 config/initializers/assets.rb create mode 100644 config/initializers/backtrace_silencers.rb create mode 100644 config/initializers/content_security_policy.rb create mode 100644 config/initializers/cookies_serializer.rb create mode 100644 config/initializers/filter_parameter_logging.rb create mode 100644 config/initializers/inflections.rb create mode 100644 config/initializers/mime_types.rb create mode 100644 config/initializers/wrap_parameters.rb create mode 100644 config/locales/en.yml create mode 100644 config/puma.rb create mode 100644 config/routes.rb create mode 100644 config/spring.rb create mode 100644 config/storage.yml create mode 100644 db/seeds.rb create mode 100644 lib/assets/.keep create mode 100644 lib/tasks/.keep create mode 100644 log/.keep create mode 100644 package.json create mode 100644 public/404.html create mode 100644 public/422.html create mode 100644 public/500.html create mode 100644 public/apple-touch-icon-precomposed.png create mode 100644 public/apple-touch-icon.png create mode 100644 public/favicon.ico create mode 100644 public/robots.txt create mode 100644 storage/.keep create mode 100644 test/application_system_test_case.rb create mode 100644 test/controllers/.keep create mode 100644 test/fixtures/.keep create mode 100644 test/fixtures/files/.keep create mode 100644 test/helpers/.keep create mode 100644 test/integration/.keep create mode 100644 test/mailers/.keep create mode 100644 test/models/.keep create mode 100644 test/system/.keep create mode 100644 test/test_helper.rb create mode 100644 tmp/.keep create mode 100644 vendor/.keep diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000000..18b43c9cd2 --- /dev/null +++ b/.gitignore @@ -0,0 +1,27 @@ +# See https://help.github.com/articles/ignoring-files for more about ignoring files. +# +# If you find yourself ignoring temporary files generated by your text editor +# or operating system, you probably want to add a global ignore instead: +# git config --global core.excludesfile '~/.gitignore_global' + +# Ignore bundler config. +/.bundle + +# Ignore all logfiles and tempfiles. +/log/* +/tmp/* +!/log/.keep +!/tmp/.keep + +# Ignore uploaded files in development +/storage/* +!/storage/.keep + +/node_modules +/yarn-error.log + +/public/assets +.byebug_history + +# Ignore master key for decrypting credentials and more. +/config/master.key diff --git a/.ruby-version b/.ruby-version new file mode 100644 index 0000000000..e54d447e8f --- /dev/null +++ b/.ruby-version @@ -0,0 +1 @@ +ruby-2.4.1 \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000000..8406aeb8bb --- /dev/null +++ b/Gemfile @@ -0,0 +1,81 @@ +source 'https://rubygems.org' +git_source(:github) { |repo| "https://github.com/#{repo}.git" } + +ruby '2.4.1' + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '~> 5.2.1' +# Use postgresql as the database for Active Record +gem 'pg', '>= 0.18', '< 2.0' +# Use Puma as the app server +gem 'puma', '~> 3.11' +# Use SCSS for stylesheets +gem 'sass-rails', '~> 5.0' +# Use Uglifier as compressor for JavaScript assets +gem 'uglifier', '>= 1.3.0' +# See https://github.com/rails/execjs#readme for more supported runtimes +# gem 'mini_racer', platforms: :ruby + +# Use CoffeeScript for .coffee assets and views +# gem 'coffee-rails', '~> 4.2' +# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks +gem 'turbolinks', '~> 5' +# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder +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' + +# Use ActiveStorage variant +# gem 'mini_magick', '~> 4.8' + +# Use Capistrano for deployment +# gem 'capistrano-rails', group: :development + +# Reduces boot times through caching; required in config/boot.rb +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] +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' + # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring + gem 'spring' + gem 'spring-watcher-listen', '~> 2.0.0' +end + +group :test do + # Adds support for Capybara system testing and selenium driver + gem 'capybara', '>= 2.15' + gem 'selenium-webdriver' + # Easy installation and use of chromedriver to run system tests with Chrome + gem 'chromedriver-helper' +end + +# Windows does not include zoneinfo files, so bundle the tzinfo-data gem +gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] + +gem 'jquery-rails' +gem 'jquery-turbolinks' +gem 'bootstrap', '~> 4.1.3' +group :development, :test do + gem 'pry-rails' +end + +group :development do + gem 'better_errors' + gem 'binding_of_caller' + gem 'guard' + gem 'guard-minitest' +end + +group :test do + gem 'minitest-rails' + gem 'minitest-reporters' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000000..fb84695226 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,277 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (5.2.1) + actionpack (= 5.2.1) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailer (5.2.1) + actionpack (= 5.2.1) + actionview (= 5.2.1) + activejob (= 5.2.1) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.2.1) + actionview (= 5.2.1) + activesupport (= 5.2.1) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.2.1) + activesupport (= 5.2.1) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.2.1) + activesupport (= 5.2.1) + globalid (>= 0.3.6) + activemodel (5.2.1) + activesupport (= 5.2.1) + activerecord (5.2.1) + activemodel (= 5.2.1) + activesupport (= 5.2.1) + arel (>= 9.0) + activestorage (5.2.1) + actionpack (= 5.2.1) + activerecord (= 5.2.1) + marcel (~> 0.3.1) + activesupport (5.2.1) + 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) + ansi (1.5.0) + archive-zip (0.11.0) + io-like (~> 0.3.0) + arel (9.0.0) + autoprefixer-rails (9.1.4) + execjs + better_errors (2.5.0) + coderay (>= 1.0.0) + erubi (>= 1.0.0) + rack (>= 0.9.0) + bindex (0.5.0) + binding_of_caller (0.8.0) + debug_inspector (>= 0.0.1) + bootsnap (1.3.2) + msgpack (~> 1.0) + bootstrap (4.1.3) + autoprefixer-rails (>= 6.0.3) + popper_js (>= 1.12.9, < 2) + sass (>= 3.5.2) + builder (3.2.3) + byebug (10.0.2) + capybara (3.9.0) + addressable + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + xpath (~> 3.1) + childprocess (0.9.0) + ffi (~> 1.0, >= 1.0.11) + chromedriver-helper (2.1.0) + archive-zip (~> 0.10) + nokogiri (~> 1.8) + coderay (1.1.2) + concurrent-ruby (1.0.5) + crass (1.0.4) + debug_inspector (0.0.3) + erubi (1.7.1) + execjs (2.7.0) + ffi (1.9.25) + formatador (0.2.5) + globalid (0.4.1) + activesupport (>= 4.2.0) + guard (2.14.2) + formatador (>= 0.2.4) + listen (>= 2.7, < 4.0) + lumberjack (>= 1.0.12, < 2.0) + nenv (~> 0.1) + notiffany (~> 0.0) + pry (>= 0.9.12) + shellany (~> 0.0) + thor (>= 0.18.1) + guard-compat (1.2.1) + guard-minitest (2.4.6) + guard-compat (~> 1.2) + minitest (>= 3.0) + i18n (1.1.0) + concurrent-ruby (~> 1.0) + io-like (0.3.0) + jbuilder (2.7.0) + activesupport (>= 4.2.0) + multi_json (>= 1.2) + jquery-rails (4.3.3) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + jquery-turbolinks (2.1.0) + railties (>= 3.1.0) + turbolinks + 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) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + lumberjack (1.0.13) + mail (2.7.0) + mini_mime (>= 0.1.1) + marcel (0.3.3) + mimemagic (~> 0.3.2) + method_source (0.9.0) + mimemagic (0.3.2) + mini_mime (1.0.1) + mini_portile2 (2.3.0) + minitest (5.11.3) + minitest-rails (3.0.0) + minitest (~> 5.8) + railties (~> 5.0) + minitest-reporters (1.3.5) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + msgpack (1.2.4) + multi_json (1.13.1) + nenv (0.3.0) + nio4r (2.3.1) + nokogiri (1.8.5) + mini_portile2 (~> 2.3.0) + notiffany (0.1.1) + nenv (~> 0.1) + shellany (~> 0.0) + pg (1.1.3) + popper_js (1.14.3) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.6) + pry (>= 0.10.4) + public_suffix (3.0.3) + puma (3.12.0) + rack (2.0.5) + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails (5.2.1) + actioncable (= 5.2.1) + actionmailer (= 5.2.1) + actionpack (= 5.2.1) + actionview (= 5.2.1) + activejob (= 5.2.1) + activemodel (= 5.2.1) + activerecord (= 5.2.1) + activestorage (= 5.2.1) + activesupport (= 5.2.1) + bundler (>= 1.3.0) + railties (= 5.2.1) + sprockets-rails (>= 2.0.0) + 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.1) + actionpack (= 5.2.1) + activesupport (= 5.2.1) + method_source + rake (>= 0.8.7) + thor (>= 0.19.0, < 2.0) + rake (12.3.1) + rb-fsevent (0.10.3) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + ruby-progressbar (1.10.0) + ruby_dep (1.5.0) + rubyzip (1.2.2) + sass (3.6.0) + 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 (~> 3.1) + sprockets (>= 2.8, < 4.0) + sprockets-rails (>= 2.0, < 4.0) + tilt (>= 1.1, < 3) + selenium-webdriver (3.14.1) + childprocess (~> 0.5) + rubyzip (~> 1.2, >= 1.2.2) + shellany (0.0.1) + spring (2.0.2) + activesupport (>= 4.2) + spring-watcher-listen (2.0.1) + listen (>= 2.7, < 4.0) + spring (>= 1.2, < 3.0) + sprockets (3.7.2) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + thor (0.20.0) + thread_safe (0.3.6) + tilt (2.0.8) + turbolinks (5.2.0) + turbolinks-source (~> 5.2) + turbolinks-source (5.2.0) + tzinfo (1.2.5) + thread_safe (~> 0.1) + uglifier (4.1.19) + execjs (>= 0.3.0, < 3) + web-console (3.7.0) + actionview (>= 5.0) + activemodel (>= 5.0) + bindex (>= 0.4.0) + railties (>= 5.0) + websocket-driver (0.7.0) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) + xpath (3.1.0) + nokogiri (~> 1.8) + +PLATFORMS + ruby + +DEPENDENCIES + better_errors + binding_of_caller + bootsnap (>= 1.1.0) + bootstrap (~> 4.1.3) + byebug + capybara (>= 2.15) + chromedriver-helper + guard + guard-minitest + jbuilder (~> 2.5) + jquery-rails + jquery-turbolinks + listen (>= 3.0.5, < 3.2) + minitest-rails + minitest-reporters + pg (>= 0.18, < 2.0) + pry-rails + puma (~> 3.11) + rails (~> 5.2.1) + sass-rails (~> 5.0) + selenium-webdriver + spring + spring-watcher-listen (~> 2.0.0) + turbolinks (~> 5) + tzinfo-data + uglifier (>= 1.3.0) + web-console (>= 3.3.0) + +RUBY VERSION + ruby 2.4.1p111 + +BUNDLED WITH + 1.16.4 diff --git a/Guardfile b/Guardfile new file mode 100644 index 0000000000..e34f706f4a --- /dev/null +++ b/Guardfile @@ -0,0 +1,9 @@ +guard :minitest, autorun: false, spring: true do + watch(%r{^app/(.+).rb$}) { |m| "test/#{m[1]}_test.rb" } + watch(%r{^app/controllers/application_controller.rb$}) { 'test/controllers' } + watch(%r{^app/controllers/(.+)_controller.rb$}) { |m| "test/integration/#{m[1]}_test.rb" } + watch(%r{^app/views/(.+)_mailer/.+}) { |m| "test/mailers/#{m[1]}_mailer_test.rb" } + watch(%r{^lib/(.+).rb$}) { |m| "test/lib/#{m[1]}_test.rb" } + watch(%r{^test/.+_test.rb$}) + watch(%r{^test/test_helper.rb$}) { 'test' } +end diff --git a/README.md b/README.md index 24ee1d9971..7db80e4ca1 100644 --- a/README.md +++ b/README.md @@ -1,120 +1,24 @@ -# Media Ranker +# README -## Introduction +This README would normally document whatever steps are necessary to get the +application up and running. -In this project, you will build a webapp where users can vote for their favorite pieces of media. +Things you may want to cover: -In contrast to previous projects, instead of implementing a pre-defined spec you will be imitating an existing site: http://media-ranker-2-0.herokuapp.com. Your job is to match the functionality and styling of this site as closely as possible. +* Ruby version -This is an individual, [stage 2](https://github.com/Ada-Developers-Academy/pedagogy/blob/master/rule-of-three.md) project. +* System dependencies -This project is due **Monday October 15th**. +* Configuration -## Learning Goals +* Database creation -The purpose of this assignment is to reinforce the following concepts: +* Database initialization -- Previous Rails learning, including MVC, RESTful routing, and the request cycle -- Testing Rails applications -- Building complex model logic -- Using `session` and `flash` to track data between requests -- DRYing up Rails code +* How to run the test suite -## Before You Begin +* Services (job queues, cache servers, search engines, etc.) -### Provided Files +* Deployment instructions -- `db/media_seeds.csv`: Some starter media to work with -- `app/assets/images/owl.jpg`: The owl picture from the site - -### Regarding the Word "Media" - -The Rails inflector considers "media" to be the plural of "medium", which is not really what we mean here. You may want to choose a different word to represent "a book, movie or album" internally. The instructor-proved example site uses the word "work". - -## Project Requirements - -### Core Requirements - -Regardless of how you choose to implement this project or how much of it gets done, you should exhibit - -- Squeaky-clean **git hygiene**, including - - A fresh branch for each new feature - - Regular commits - - Descriptive commit messages -- Fanatical devotion to **test-driven development** - - Pseudocode first, then write the tests, then write code to make them pass -- Steadfast adherence to **agile development practices** - - User stories should be listed and prioritized using a Trello board - - The finished application should be deployed to Heroku (deploy early, deploy often) -- Unrelenting use of **semantic HTML** - -### Baseline - -We will begin with some in-class work, exploring the site and pondering implementation details. Before you start writing _any_ code, you should: - -- Explore the existing Media Ranker site to become familiar with the necessary functionality -- Create a Trello board to manage user stories -- Create an ERD for the models - -Then, once you have a solid plan for how to structure your project: - -- Fork and clone the repo -- Use `rails new .` to generate a new Rails project in the cloned directory - - Verify that the changes we've made to Rails' defaults (postgres as the DB, spec-style testing) have been applied -- `git add .` and `git commit -m "Initial Rails setup"` - -### Wave 1 - -In this wave, you should build some functionality, and then build the tests for that functionality. We recommend doing the read and create operations first, then writing tests, then completing the update and delete operations. - -Mimic the site's basic functionality around Media, without worrying (yet) about Users or Votes: -- Build a main page, with a list of the media for each category, as well as a spotlight section for the top media overall (don't worry about the top 10 part right now) -- Build an index page with a list of all works for each category -- Allow users to add new works -- Build a details page for each piece of media -- Allow users to edit and delete works - -#### Testing -**Before** moving on to Wave 2 you need to have model tests for: -- Presence of required attributes -- Uniqueness of attributes -- Valid values for specific attributes - -### Wave 2 - -Mimic the site's functionality around Users and Voting: -- Allow users to "log in" to the site, and use the `session` to keep track of which user is currently logged in for a given browser -- Allow users to vote for media, and sort media by vote count whenever a list of media is displayed -- Don't allow a user to vote for the same media more than once -- Allow the users to see the top 10 for each media type on the main page - -#### Testing -**Before** moving on to Wave 3 you need to have: -- Tests from Wave 1 passing -- Presence and uniqueness validation tests for any new models -- Relationship tests - -Focus on testing voting logic since this is the most complex part of Wave 2. - -#### A note on logging in - -Passwords and security are tricky! We'll talk about that sort of thing a little in the coming weeks, but for now you don't need to provide any sort of security. The user gives you a username, and your site should just trust them. - -### Wave 3 -- Add a list of voting users to the details page for each media -- Add a page for each user, as well as a page showing a summary of all users - -### Optional Enhancement Ideas - -Use Bootstrap and CSS to style the site to match the example. The layout as well as the look and feel should match as close as possible. - -Once your test coverage is comprehensive, your HTML is semantic, your user stories have all been moved to the `Done` column and your application has been deployed to Heroku, you may consider the following enhancements. - -1. DRY up your code as much as you can! Techniques worth investigating: - - Helper methods - - Controller filters -1. Build category-specific pages for `index` and `new` (e.g. `/books` or `/movies/new`). These should be as DRY as possible. You might be interested in investigating _polymorphic routes_. -1. Add a [recommendation system](https://www.toptal.com/algorithms/predicting-likes-inside-a-simple-recommendation-engine) that suggests media to a user based on what they have previously voted for. - -## What we're looking for -You can find what instructors will be looking for in the [feedback](feedback.md) markdown document. +* ... diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000000..e85f913914 --- /dev/null +++ b/Rakefile @@ -0,0 +1,6 @@ +# Add your own tasks in files placed in lib/tasks ending in .rake, +# for example lib/tasks/capistrano.rake, and they will automatically be available to Rake. + +require_relative 'config/application' + +Rails.application.load_tasks diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js new file mode 100644 index 0000000000..b16e53d6d5 --- /dev/null +++ b/app/assets/config/manifest.js @@ -0,0 +1,3 @@ +//= link_tree ../images +//= link_directory ../javascripts .js +//= link_directory ../stylesheets .css diff --git a/app/assets/images/.keep b/app/assets/images/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js new file mode 100644 index 0000000000..4f73c21a7d --- /dev/null +++ b/app/assets/javascripts/application.js @@ -0,0 +1,20 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, or any plugin's +// vendor/assets/javascripts directory can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// compiled file. JavaScript code in this file should be added after the last require_* statement. +// +// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details +// about supported directives. + //= require jquery3 + //= require popper + //= require bootstrap-sprockets + +// +//= require rails-ujs +//= require activestorage +//= require turbolinks +//= require_tree . diff --git a/app/assets/javascripts/cable.js b/app/assets/javascripts/cable.js new file mode 100644 index 0000000000..739aa5f022 --- /dev/null +++ b/app/assets/javascripts/cable.js @@ -0,0 +1,13 @@ +// Action Cable provides the framework to deal with WebSockets in Rails. +// You can generate new channels where WebSocket features live using the `rails generate channel` command. +// +//= require action_cable +//= require_self +//= require_tree ./channels + +(function() { + this.App || (this.App = {}); + + App.cable = ActionCable.createConsumer(); + +}).call(this); diff --git a/app/assets/javascripts/channels/.keep b/app/assets/javascripts/channels/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/assets/stylesheets/application.scss b/app/assets/stylesheets/application.scss new file mode 100644 index 0000000000..8b1701e581 --- /dev/null +++ b/app/assets/stylesheets/application.scss @@ -0,0 +1,18 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, or any plugin's + * vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any other CSS/SCSS + * files in this directory. Styles in this file should be added after the last require_* statement. + * It is generally better to create a new file per style scope. + * + */ + +/* Custom bootstrap variables must be set or imported *before* bootstrap. */ +@import "bootstrap"; +/* Import scss content */ +@import "**/*"; diff --git a/app/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb new file mode 100644 index 0000000000..d672697283 --- /dev/null +++ b/app/channels/application_cable/channel.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Channel < ActionCable::Channel::Base + end +end diff --git a/app/channels/application_cable/connection.rb b/app/channels/application_cable/connection.rb new file mode 100644 index 0000000000..0ff5442f47 --- /dev/null +++ b/app/channels/application_cable/connection.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Connection < ActionCable::Connection::Base + end +end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 0000000000..09705d12ab --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::Base +end diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb new file mode 100644 index 0000000000..de6be7945c --- /dev/null +++ b/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb new file mode 100644 index 0000000000..a009ace51c --- /dev/null +++ b/app/jobs/application_job.rb @@ -0,0 +1,2 @@ +class ApplicationJob < ActiveJob::Base +end diff --git a/app/mailers/application_mailer.rb b/app/mailers/application_mailer.rb new file mode 100644 index 0000000000..286b2239d1 --- /dev/null +++ b/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: 'from@example.com' + layout 'mailer' +end diff --git a/app/models/application_record.rb b/app/models/application_record.rb new file mode 100644 index 0000000000..10a4cba84d --- /dev/null +++ b/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true +end diff --git a/app/models/concerns/.keep b/app/models/concerns/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb new file mode 100644 index 0000000000..f32268a271 --- /dev/null +++ b/app/views/layouts/application.html.erb @@ -0,0 +1,15 @@ + + + + MediaRanker + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %> + <%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %> + + + + <%= yield %> + + diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb new file mode 100644 index 0000000000..cbd34d2e9d --- /dev/null +++ b/app/views/layouts/mailer.html.erb @@ -0,0 +1,13 @@ + + + + + + + + + <%= yield %> + + diff --git a/app/views/layouts/mailer.text.erb b/app/views/layouts/mailer.text.erb new file mode 100644 index 0000000000..37f0bddbd7 --- /dev/null +++ b/app/views/layouts/mailer.text.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/bin/bundle b/bin/bundle new file mode 100755 index 0000000000..f19acf5b5c --- /dev/null +++ b/bin/bundle @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) +load Gem.bin_path('bundler', 'bundle') diff --git a/bin/rails b/bin/rails new file mode 100755 index 0000000000..5badb2fde0 --- /dev/null +++ b/bin/rails @@ -0,0 +1,9 @@ +#!/usr/bin/env ruby +begin + load File.expand_path('../spring', __FILE__) +rescue LoadError => e + raise unless e.message.include?('spring') +end +APP_PATH = File.expand_path('../config/application', __dir__) +require_relative '../config/boot' +require 'rails/commands' diff --git a/bin/rake b/bin/rake new file mode 100755 index 0000000000..d87d5f5781 --- /dev/null +++ b/bin/rake @@ -0,0 +1,9 @@ +#!/usr/bin/env ruby +begin + load File.expand_path('../spring', __FILE__) +rescue LoadError => e + raise unless e.message.include?('spring') +end +require_relative '../config/boot' +require 'rake' +Rake.application.run diff --git a/bin/setup b/bin/setup new file mode 100755 index 0000000000..94fd4d7977 --- /dev/null +++ b/bin/setup @@ -0,0 +1,36 @@ +#!/usr/bin/env ruby +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = File.expand_path('..', __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +chdir APP_ROOT do + # This script is a starting point to setup your application. + # Add necessary setup steps to this file. + + puts '== Installing dependencies ==' + system! 'gem install bundler --conservative' + system('bundle check') || system!('bundle install') + + # Install JavaScript dependencies if using Yarn + # system('bin/yarn') + + # puts "\n== Copying sample files ==" + # unless File.exist?('config/database.yml') + # cp 'config/database.yml.sample', 'config/database.yml' + # end + + puts "\n== Preparing database ==" + system! 'bin/rails db:setup' + + puts "\n== Removing old logs and tempfiles ==" + system! 'bin/rails log:clear tmp:clear' + + puts "\n== Restarting application server ==" + system! 'bin/rails restart' +end diff --git a/bin/spring b/bin/spring new file mode 100755 index 0000000000..fb2ec2ebb4 --- /dev/null +++ b/bin/spring @@ -0,0 +1,17 @@ +#!/usr/bin/env ruby + +# This file loads spring without using Bundler, in order to be fast. +# It gets overwritten when you run the `spring binstub` command. + +unless defined?(Spring) + require 'rubygems' + require 'bundler' + + lockfile = Bundler::LockfileParser.new(Bundler.default_lockfile.read) + spring = lockfile.specs.detect { |spec| spec.name == "spring" } + if spring + Gem.use_paths Gem.dir, Bundler.bundle_path.to_s, *Gem.path + gem 'spring', spring.version + require 'spring/binstub' + end +end diff --git a/bin/update b/bin/update new file mode 100755 index 0000000000..58bfaed518 --- /dev/null +++ b/bin/update @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = File.expand_path('..', __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +chdir APP_ROOT do + # This script is a way to update your development environment automatically. + # Add necessary update steps to this file. + + puts '== Installing dependencies ==' + system! 'gem install bundler --conservative' + system('bundle check') || system!('bundle install') + + # Install JavaScript dependencies if using Yarn + # system('bin/yarn') + + puts "\n== Updating database ==" + system! 'bin/rails db:migrate' + + puts "\n== Removing old logs and tempfiles ==" + system! 'bin/rails log:clear tmp:clear' + + puts "\n== Restarting application server ==" + system! 'bin/rails restart' +end diff --git a/bin/yarn b/bin/yarn new file mode 100755 index 0000000000..460dd565b4 --- /dev/null +++ b/bin/yarn @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +APP_ROOT = File.expand_path('..', __dir__) +Dir.chdir(APP_ROOT) do + begin + exec "yarnpkg", *ARGV + rescue Errno::ENOENT + $stderr.puts "Yarn executable was not detected in the system." + $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install" + exit 1 + end +end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000000..f7ba0b527b --- /dev/null +++ b/config.ru @@ -0,0 +1,5 @@ +# This file is used by Rack-based servers to start the application. + +require_relative 'config/environment' + +run Rails.application diff --git a/config/application.rb b/config/application.rb new file mode 100644 index 0000000000..08db79cb25 --- /dev/null +++ b/config/application.rb @@ -0,0 +1,25 @@ +require_relative 'boot' + +require 'rails/all' + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module MediaRanker + class Application < Rails::Application + config.generators do |g| + # Force new test files to be generated in the minitest-spec style + g.test_framework :minitest, spec: true + # Always use .js files, never .coffee + g.javascript_engine :js + end + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 5.2 + + # Settings in config/environments/* take precedence over those specified here. + # Application configuration can go into files in config/initializers + # -- all .rb files in that directory are automatically loaded after loading + # the framework and any gems in your application. + end +end diff --git a/config/boot.rb b/config/boot.rb new file mode 100644 index 0000000000..b9e460cef3 --- /dev/null +++ b/config/boot.rb @@ -0,0 +1,4 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. +require 'bootsnap/setup' # Speed up boot time by caching expensive operations. diff --git a/config/cable.yml b/config/cable.yml new file mode 100644 index 0000000000..0b286f676e --- /dev/null +++ b/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: async + +production: + adapter: redis + url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> + channel_prefix: MediaRanker_production diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc new file mode 100644 index 0000000000..91ec9100d1 --- /dev/null +++ b/config/credentials.yml.enc @@ -0,0 +1 @@ +TyRO5PYKdKYW/e0co4OMxixW7XAz1f1vzhwJtALJE+yiusXhjbqJn6LLD3dhQRVpZn9BrAeNPjrITzL8eYu/pEW/BVMGtp8DBAeQ7N0+uzAwaDAhoD9SPHU9iQTN8km/LWs4n8igUDjOsKkAFTuRelSowIYk6bE/Kvx6qV3F5gi9o6ZVG2cuhl1957d7OTY1TGB0AHqWckIPaJ25s4YM/mbXCZ9+tfU1Jn6eEvMyv26+MYT6iVLD21HaGcZyOB7/oXbZmm78aWH3wVSTmupCSamDpzzfPV1RQ8eb/10k7Dfu0OeFinpWh8SQXNmyzbdoW5BK30uT5wSdSVPUrmhJEG7BIYI3XNq1mWiizHpG4MLZbOYIt3yNwlMcjWjEUHdzzOdIT4Wo5tjm6WMkyTnBNWviIFgArYLt+XLL--K9njIDNLbcuLEl/f--iqevi+uyeN7TLvkf5N0uXw== \ No newline at end of file diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 0000000000..0bd2511123 --- /dev/null +++ b/config/database.yml @@ -0,0 +1,85 @@ +# PostgreSQL. Versions 9.1 and up are supported. +# +# Install the pg driver: +# gem install pg +# On OS X with Homebrew: +# gem install pg -- --with-pg-config=/usr/local/bin/pg_config +# On OS X with MacPorts: +# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config +# On Windows: +# gem install pg +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +# +# Configure Using Gemfile +# gem 'pg' +# +default: &default + adapter: postgresql + encoding: unicode + # For details on connection pooling, see Rails configuration guide + # http://guides.rubyonrails.org/configuring.html#database-pooling + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + +development: + <<: *default + database: MediaRanker_development + + # The specified database role being used to connect to postgres. + # To create additional roles in postgres see `$ createuser --help`. + # When left blank, postgres will use the default role. This is + # the same name as the operating system user that initialized the database. + #username: MediaRanker + + # The password associated with the postgres role (username). + #password: + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + + # The TCP port the server listens on. Defaults to 5432. + # If your server runs on a different port number, change accordingly. + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # Defaults to warning. + #min_messages: notice + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + <<: *default + database: MediaRanker_test + +# As with config/secrets.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password as a unix environment variable when you boot +# the app. Read http://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full rundown on how to provide these environment variables in a +# production deployment. +# +# On Heroku and other platform providers, you may have a full connection URL +# available as an environment variable. For example: +# +# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" +# +# You can use this database configuration with: +# +# production: +# url: <%= ENV['DATABASE_URL'] %> +# +production: + <<: *default + database: MediaRanker_production + username: MediaRanker + password: <%= ENV['MEDIARANKER_DATABASE_PASSWORD'] %> diff --git a/config/environment.rb b/config/environment.rb new file mode 100644 index 0000000000..426333bb46 --- /dev/null +++ b/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative 'application' + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/config/environments/development.rb b/config/environments/development.rb new file mode 100644 index 0000000000..1311e3e4ef --- /dev/null +++ b/config/environments/development.rb @@ -0,0 +1,61 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join('tmp', 'caching-dev.txt').exist? + config.action_controller.perform_caching = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options) + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + config.action_mailer.perform_caching = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + # Debug mode disables concatenation and preprocessing of assets. + # This option may cause significant delays in view rendering with a large + # number of complex assets. + config.assets.debug = true + + # Suppress logger output for asset requests. + config.assets.quiet = true + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true + + # Use an evented file watcher to asynchronously detect changes in source code, + # routes, locales, etc. This feature depends on the listen gem. + config.file_watcher = ActiveSupport::EventedFileUpdateChecker +end diff --git a/config/environments/production.rb b/config/environments/production.rb new file mode 100644 index 0000000000..bd39d9d752 --- /dev/null +++ b/config/environments/production.rb @@ -0,0 +1,94 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # Code is not reloaded between requests. + config.cache_classes = true + + # Eager load code on boot. This eager loads most of Rails and + # your application in memory, allowing both threaded web servers + # and those relying on copy on write to perform better. + # Rake tasks automatically ignore this option for performance. + config.eager_load = true + + # Full error reports are disabled and caching is turned on. + config.consider_all_requests_local = false + config.action_controller.perform_caching = true + + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # config.require_master_key = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? + + # Compress JavaScripts and CSS. + config.assets.js_compressor = :uglifier + # config.assets.css_compressor = :sass + + # Do not fallback to assets pipeline if a precompiled asset is missed. + config.assets.compile = false + + # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = 'http://assets.example.com' + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache + # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX + + # Store uploaded files on the local file system (see config/storage.yml for options) + config.active_storage.service = :local + + # Mount Action Cable outside main process or domain + # config.action_cable.mount_path = nil + # config.action_cable.url = 'wss://example.com/cable' + # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Use the lowest log level to ensure availability of diagnostic information + # when problems arise. + config.log_level = :debug + + # Prepend all log lines with the following tags. + config.log_tags = [ :request_id ] + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Use a real queuing backend for Active Job (and separate queues per environment) + # config.active_job.queue_adapter = :resque + # config.active_job.queue_name_prefix = "MediaRanker_#{Rails.env}" + + config.action_mailer.perform_caching = false + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners. + config.active_support.deprecation = :notify + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Use a different logger for distributed setups. + # require 'syslog/logger' + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + + if ENV["RAILS_LOG_TO_STDOUT"].present? + logger = ActiveSupport::Logger.new(STDOUT) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + end + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false +end diff --git a/config/environments/test.rb b/config/environments/test.rb new file mode 100644 index 0000000000..0a38fd3ce9 --- /dev/null +++ b/config/environments/test.rb @@ -0,0 +1,46 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory + config.active_storage.service = :test + + config.action_mailer.perform_caching = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/config/initializers/application_controller_renderer.rb b/config/initializers/application_controller_renderer.rb new file mode 100644 index 0000000000..89d2efab2b --- /dev/null +++ b/config/initializers/application_controller_renderer.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# ActiveSupport::Reloader.to_prepare do +# ApplicationController.renderer.defaults.merge!( +# http_host: 'example.org', +# https: false +# ) +# end diff --git a/config/initializers/assets.rb b/config/initializers/assets.rb new file mode 100644 index 0000000000..4b828e80cb --- /dev/null +++ b/config/initializers/assets.rb @@ -0,0 +1,14 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = '1.0' + +# Add additional assets to the asset load path. +# Rails.application.config.assets.paths << Emoji.images_path +# Add Yarn node_modules folder to the asset load path. +Rails.application.config.assets.paths << Rails.root.join('node_modules') + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in the app/assets +# folder are already added. +# Rails.application.config.assets.precompile += %w( admin.js admin.css ) diff --git a/config/initializers/backtrace_silencers.rb b/config/initializers/backtrace_silencers.rb new file mode 100644 index 0000000000..59385cdf37 --- /dev/null +++ b/config/initializers/backtrace_silencers.rb @@ -0,0 +1,7 @@ +# Be sure to restart your server when you modify this file. + +# You can add backtrace silencers for libraries that you're using but don't wish to see in your backtraces. +# Rails.backtrace_cleaner.add_silencer { |line| line =~ /my_noisy_library/ } + +# You can also remove all the silencers if you're trying to debug a problem that might stem from framework code. +# Rails.backtrace_cleaner.remove_silencers! diff --git a/config/initializers/content_security_policy.rb b/config/initializers/content_security_policy.rb new file mode 100644 index 0000000000..d3bcaa5ec8 --- /dev/null +++ b/config/initializers/content_security_policy.rb @@ -0,0 +1,25 @@ +# Be sure to restart your server when you modify this file. + +# Define an application-wide content security policy +# For further information see the following documentation +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy + +# Rails.application.config.content_security_policy do |policy| +# policy.default_src :self, :https +# policy.font_src :self, :https, :data +# policy.img_src :self, :https, :data +# policy.object_src :none +# policy.script_src :self, :https +# policy.style_src :self, :https + +# # Specify URI for violation reports +# # policy.report_uri "/csp-violation-report-endpoint" +# end + +# If you are using UJS then enable automatic nonce generation +# Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) } + +# Report CSP violations to a specified URI +# For further information see the following documentation: +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only +# Rails.application.config.content_security_policy_report_only = true diff --git a/config/initializers/cookies_serializer.rb b/config/initializers/cookies_serializer.rb new file mode 100644 index 0000000000..5a6a32d371 --- /dev/null +++ b/config/initializers/cookies_serializer.rb @@ -0,0 +1,5 @@ +# Be sure to restart your server when you modify this file. + +# Specify a serializer for the signed and encrypted cookie jars. +# Valid options are :json, :marshal, and :hybrid. +Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000000..4a994e1e7b --- /dev/null +++ b/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Configure sensitive parameters which will be filtered from the log file. +Rails.application.config.filter_parameters += [:password] diff --git a/config/initializers/inflections.rb b/config/initializers/inflections.rb new file mode 100644 index 0000000000..ac033bf9dc --- /dev/null +++ b/config/initializers/inflections.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Add new inflection rules using the following format. Inflections +# are locale specific, and you may define rules for as many different +# locales as you wish. All of these examples are active by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.plural /^(ox)$/i, '\1en' +# inflect.singular /^(ox)en/i, '\1' +# inflect.irregular 'person', 'people' +# inflect.uncountable %w( fish sheep ) +# end + +# These inflection rules are supported but not enabled by default: +# ActiveSupport::Inflector.inflections(:en) do |inflect| +# inflect.acronym 'RESTful' +# end diff --git a/config/initializers/mime_types.rb b/config/initializers/mime_types.rb new file mode 100644 index 0000000000..dc1899682b --- /dev/null +++ b/config/initializers/mime_types.rb @@ -0,0 +1,4 @@ +# Be sure to restart your server when you modify this file. + +# Add new mime types for use in respond_to blocks: +# Mime::Type.register "text/richtext", :rtf diff --git a/config/initializers/wrap_parameters.rb b/config/initializers/wrap_parameters.rb new file mode 100644 index 0000000000..bbfc3961bf --- /dev/null +++ b/config/initializers/wrap_parameters.rb @@ -0,0 +1,14 @@ +# Be sure to restart your server when you modify this file. + +# This file contains settings for ActionController::ParamsWrapper which +# is enabled by default. + +# Enable parameter wrapping for JSON. You can disable this by setting :format to an empty array. +ActiveSupport.on_load(:action_controller) do + wrap_parameters format: [:json] +end + +# To enable root element in JSON for ActiveRecord objects. +# ActiveSupport.on_load(:active_record) do +# self.include_root_in_json = true +# end diff --git a/config/locales/en.yml b/config/locales/en.yml new file mode 100644 index 0000000000..decc5a8573 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,33 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t 'hello' +# +# In views, this is aliased to just `t`: +# +# <%= t('hello') %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# The following keys must be escaped otherwise they will not be retrieved by +# the default I18n backend: +# +# true, false, on, off, yes, no +# +# Instead, surround them with single quotes. +# +# en: +# 'true': 'foo' +# +# To learn more, please read the Rails Internationalization guide +# available at http://guides.rubyonrails.org/i18n.html. + +en: + hello: "Hello world" diff --git a/config/puma.rb b/config/puma.rb new file mode 100644 index 0000000000..a5eccf816b --- /dev/null +++ b/config/puma.rb @@ -0,0 +1,34 @@ +# Puma can serve each request in a thread from an internal thread pool. +# The `threads` method setting takes two numbers: a minimum and maximum. +# Any libraries that use thread pools should be configured to match +# the maximum value specified for Puma. Default is set to 5 threads for minimum +# and maximum; this matches the default thread size of Active Record. +# +threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +threads threads_count, threads_count + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +# +port ENV.fetch("PORT") { 3000 } + +# Specifies the `environment` that Puma will run in. +# +environment ENV.fetch("RAILS_ENV") { "development" } + +# Specifies the number of `workers` to boot in clustered mode. +# Workers are forked webserver processes. If using threads and workers together +# the concurrency of the application would be max `threads` * `workers`. +# Workers do not work on JRuby or Windows (both of which do not support +# processes). +# +# workers ENV.fetch("WEB_CONCURRENCY") { 2 } + +# Use the `preload_app!` method when specifying a `workers` number. +# This directive tells Puma to first boot the application and load code +# before forking the application. This takes advantage of Copy On Write +# process behavior so workers use less memory. +# +# preload_app! + +# Allow puma to be restarted by `rails restart` command. +plugin :tmp_restart diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000000..787824f888 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,3 @@ +Rails.application.routes.draw do + # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html +end diff --git a/config/spring.rb b/config/spring.rb new file mode 100644 index 0000000000..9fa7863f99 --- /dev/null +++ b/config/spring.rb @@ -0,0 +1,6 @@ +%w[ + .ruby-version + .rbenv-vars + tmp/restart.txt + tmp/caching-dev.txt +].each { |path| Spring.watch(path) } diff --git a/config/storage.yml b/config/storage.yml new file mode 100644 index 0000000000..d32f76e8fb --- /dev/null +++ b/config/storage.yml @@ -0,0 +1,34 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) +# amazon: +# service: S3 +# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> +# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> +# region: us-east-1 +# bucket: your_own_bucket + +# Remember not to checkin your GCS keyfile to a repository +# google: +# service: GCS +# project: your_project +# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> +# bucket: your_own_bucket + +# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) +# microsoft: +# service: AzureStorage +# storage_account_name: your_account_name +# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> +# container: your_container_name + +# mirror: +# service: Mirror +# primary: local +# mirrors: [ amazon, google, microsoft ] diff --git a/db/seeds.rb b/db/seeds.rb new file mode 100644 index 0000000000..1beea2accd --- /dev/null +++ b/db/seeds.rb @@ -0,0 +1,7 @@ +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup). +# +# Examples: +# +# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) +# Character.create(name: 'Luke', movie: movies.first) diff --git a/lib/assets/.keep b/lib/assets/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/lib/tasks/.keep b/lib/tasks/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/log/.keep b/log/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/package.json b/package.json new file mode 100644 index 0000000000..625900f9d8 --- /dev/null +++ b/package.json @@ -0,0 +1,5 @@ +{ + "name": "MediaRanker", + "private": true, + "dependencies": {} +} diff --git a/public/404.html b/public/404.html new file mode 100644 index 0000000000..2be3af26fc --- /dev/null +++ b/public/404.html @@ -0,0 +1,67 @@ + + + + The page you were looking for doesn't exist (404) + + + + + + +
+
+

The page you were looking for doesn't exist.

+

You may have mistyped the address or the page may have moved.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/public/422.html b/public/422.html new file mode 100644 index 0000000000..c08eac0d1d --- /dev/null +++ b/public/422.html @@ -0,0 +1,67 @@ + + + + The change you wanted was rejected (422) + + + + + + +
+
+

The change you wanted was rejected.

+

Maybe you tried to change something you didn't have access to.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/public/500.html b/public/500.html new file mode 100644 index 0000000000..78a030af22 --- /dev/null +++ b/public/500.html @@ -0,0 +1,66 @@ + + + + We're sorry, but something went wrong (500) + + + + + + +
+
+

We're sorry, but something went wrong.

+
+

If you are the application owner check the logs for more information.

+
+ + diff --git a/public/apple-touch-icon-precomposed.png b/public/apple-touch-icon-precomposed.png new file mode 100644 index 0000000000..e69de29bb2 diff --git a/public/apple-touch-icon.png b/public/apple-touch-icon.png new file mode 100644 index 0000000000..e69de29bb2 diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 0000000000..e69de29bb2 diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 0000000000..37b576a4a0 --- /dev/null +++ b/public/robots.txt @@ -0,0 +1 @@ +# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/storage/.keep b/storage/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/application_system_test_case.rb b/test/application_system_test_case.rb new file mode 100644 index 0000000000..d19212abd5 --- /dev/null +++ b/test/application_system_test_case.rb @@ -0,0 +1,5 @@ +require "test_helper" + +class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + driven_by :selenium, using: :chrome, screen_size: [1400, 1400] +end diff --git a/test/controllers/.keep b/test/controllers/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/.keep b/test/fixtures/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/helpers/.keep b/test/helpers/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/integration/.keep b/test/integration/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/mailers/.keep b/test/mailers/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/models/.keep b/test/models/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/system/.keep b/test/system/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 0000000000..2b5172a7d6 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,24 @@ +ENV["RAILS_ENV"] = "test" +require File.expand_path("../../config/environment", __FILE__) +require "rails/test_help" +require "minitest/rails"require "minitest/reporters" # for Colorized output +# For colorful output! +Minitest::Reporters.use!( + Minitest::Reporters::SpecReporter.new, + ENV, + Minitest.backtrace_filter +) + + +# To add Capybara feature tests add `gem "minitest-rails-capybara"` +# to the test group in the Gemfile and uncomment the following: +# require "minitest/rails/capybara" + +# Uncomment for awesome colorful output +# require "minitest/pride" + +class ActiveSupport::TestCase + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. + fixtures :all + # Add more helper methods to be used by all tests here... +end diff --git a/tmp/.keep b/tmp/.keep new file mode 100644 index 0000000000..e69de29bb2 diff --git a/vendor/.keep b/vendor/.keep new file mode 100644 index 0000000000..e69de29bb2 From 324844b68e0f8c296e9af1934a203e1792b113eb Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 8 Oct 2018 16:41:22 -0700 Subject: [PATCH 02/59] created main page to list all media with main controller and main index page --- app/assets/javascripts/categories.js | 2 ++ app/assets/javascripts/main.js | 2 ++ app/assets/javascripts/users.js | 2 ++ app/assets/javascripts/votes.js | 2 ++ app/assets/javascripts/works.js | 2 ++ app/assets/stylesheets/main.scss | 3 +++ app/assets/stylesheets/works.scss | 3 +++ app/controllers/main_controller.rb | 4 ++++ app/controllers/works_controller.rb | 8 +++++++ app/helpers/categories_helper.rb | 2 ++ app/helpers/main_helper.rb | 2 ++ app/helpers/users_helper.rb | 2 ++ app/helpers/votes_helper.rb | 2 ++ app/helpers/works_helper.rb | 2 ++ app/models/work.rb | 2 ++ app/views/layouts/application.html.erb | 12 ++++++++++ app/views/main/index.html.erb | 15 +++++++++++++ app/views/works/index.html.erb | 4 ++++ config/routes.rb | 9 +++++++- db/migrate/20181008230039_create_works.rb | 12 ++++++++++ db/schema.rb | 27 +++++++++++++++++++++++ test/controllers/main_controller_test.rb | 9 ++++++++ test/controllers/works_controller_test.rb | 7 ++++++ test/fixtures/works.yml | 13 +++++++++++ test/models/work_test.rb | 9 ++++++++ 25 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/categories.js create mode 100644 app/assets/javascripts/main.js create mode 100644 app/assets/javascripts/users.js create mode 100644 app/assets/javascripts/votes.js create mode 100644 app/assets/javascripts/works.js create mode 100644 app/assets/stylesheets/main.scss create mode 100644 app/assets/stylesheets/works.scss create mode 100644 app/controllers/main_controller.rb create mode 100644 app/controllers/works_controller.rb create mode 100644 app/helpers/categories_helper.rb create mode 100644 app/helpers/main_helper.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/helpers/votes_helper.rb create mode 100644 app/helpers/works_helper.rb create mode 100644 app/models/work.rb create mode 100644 app/views/main/index.html.erb create mode 100644 app/views/works/index.html.erb create mode 100644 db/migrate/20181008230039_create_works.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/main_controller_test.rb create mode 100644 test/controllers/works_controller_test.rb create mode 100644 test/fixtures/works.yml create mode 100644 test/models/work_test.rb diff --git a/app/assets/javascripts/categories.js b/app/assets/javascripts/categories.js new file mode 100644 index 0000000000..dee720facd --- /dev/null +++ b/app/assets/javascripts/categories.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/javascripts/main.js b/app/assets/javascripts/main.js new file mode 100644 index 0000000000..dee720facd --- /dev/null +++ b/app/assets/javascripts/main.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/javascripts/users.js b/app/assets/javascripts/users.js new file mode 100644 index 0000000000..dee720facd --- /dev/null +++ b/app/assets/javascripts/users.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/javascripts/votes.js b/app/assets/javascripts/votes.js new file mode 100644 index 0000000000..dee720facd --- /dev/null +++ b/app/assets/javascripts/votes.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/javascripts/works.js b/app/assets/javascripts/works.js new file mode 100644 index 0000000000..dee720facd --- /dev/null +++ b/app/assets/javascripts/works.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/main.scss b/app/assets/stylesheets/main.scss new file mode 100644 index 0000000000..971b13c825 --- /dev/null +++ b/app/assets/stylesheets/main.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Main controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/works.scss b/app/assets/stylesheets/works.scss new file mode 100644 index 0000000000..5618452f3e --- /dev/null +++ b/app/assets/stylesheets/works.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Works 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/main_controller.rb b/app/controllers/main_controller.rb new file mode 100644 index 0000000000..88181c281f --- /dev/null +++ b/app/controllers/main_controller.rb @@ -0,0 +1,4 @@ +class MainController < ApplicationController + def index + end +end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb new file mode 100644 index 0000000000..7f1b21133e --- /dev/null +++ b/app/controllers/works_controller.rb @@ -0,0 +1,8 @@ +class WorksController < ApplicationController + def index + @works = Work.order(:name) + end + + def create + end +end diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb new file mode 100644 index 0000000000..e06f31554c --- /dev/null +++ b/app/helpers/categories_helper.rb @@ -0,0 +1,2 @@ +module CategoriesHelper +end diff --git a/app/helpers/main_helper.rb b/app/helpers/main_helper.rb new file mode 100644 index 0000000000..826effed96 --- /dev/null +++ b/app/helpers/main_helper.rb @@ -0,0 +1,2 @@ +module MainHelper +end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb new file mode 100644 index 0000000000..2310a240d7 --- /dev/null +++ b/app/helpers/users_helper.rb @@ -0,0 +1,2 @@ +module UsersHelper +end diff --git a/app/helpers/votes_helper.rb b/app/helpers/votes_helper.rb new file mode 100644 index 0000000000..5a82eed07d --- /dev/null +++ b/app/helpers/votes_helper.rb @@ -0,0 +1,2 @@ +module VotesHelper +end diff --git a/app/helpers/works_helper.rb b/app/helpers/works_helper.rb new file mode 100644 index 0000000000..ccb78c2b73 --- /dev/null +++ b/app/helpers/works_helper.rb @@ -0,0 +1,2 @@ +module WorksHelper +end diff --git a/app/models/work.rb b/app/models/work.rb new file mode 100644 index 0000000000..95322dd45e --- /dev/null +++ b/app/models/work.rb @@ -0,0 +1,2 @@ +class Work < ApplicationRecord +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f32268a271..5aef9f71ed 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,6 +10,18 @@ + + +
<%= yield %> +
+ diff --git a/app/views/main/index.html.erb b/app/views/main/index.html.erb new file mode 100644 index 0000000000..db11873acd --- /dev/null +++ b/app/views/main/index.html.erb @@ -0,0 +1,15 @@ +

Main#index

+

Find me in app/views/main/index.html.erb

+ +

Media Spotlight: top media right now

+ diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb new file mode 100644 index 0000000000..b1a40390d7 --- /dev/null +++ b/app/views/works/index.html.erb @@ -0,0 +1,4 @@ +

List of Works

+

Albums

+

Books

+

Movies

diff --git a/config/routes.rb b/config/routes.rb index 787824f888..14887ec714 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,10 @@ Rails.application.routes.draw do - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + root "main#index" + get 'main/index', as: 'main' + + + resources :works, only: [:index, :create] + + + end diff --git a/db/migrate/20181008230039_create_works.rb b/db/migrate/20181008230039_create_works.rb new file mode 100644 index 0000000000..73ac803dad --- /dev/null +++ b/db/migrate/20181008230039_create_works.rb @@ -0,0 +1,12 @@ +class CreateWorks < ActiveRecord::Migration[5.2] + def change + create_table :works do |t| + t.integer :votes + t.string :title + t.string :creator + t.string :description + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000000..0c4c83fef7 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,27 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2018_10_08_230039) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "works", force: :cascade do |t| + t.integer "votes" + t.string "title" + t.string "creator" + t.string "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/main_controller_test.rb b/test/controllers/main_controller_test.rb new file mode 100644 index 0000000000..32ded261bd --- /dev/null +++ b/test/controllers/main_controller_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe MainController do + it "should get index" do + get main_index_url + value(response).must_be :success? + end + +end diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb new file mode 100644 index 0000000000..d95073c22e --- /dev/null +++ b/test/controllers/works_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe WorksController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end diff --git a/test/fixtures/works.yml b/test/fixtures/works.yml new file mode 100644 index 0000000000..5008fe6181 --- /dev/null +++ b/test/fixtures/works.yml @@ -0,0 +1,13 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + votes: 1 + title: MyString + creator: MyString + description: MyString + +two: + votes: 1 + title: MyString + creator: MyString + description: MyString diff --git a/test/models/work_test.rb b/test/models/work_test.rb new file mode 100644 index 0000000000..f6fba7104d --- /dev/null +++ b/test/models/work_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe Work do + let(:work) { Work.new } + + it "must be valid" do + value(work).must_be :valid? + end +end From d15b1ebc55510f51cad1324ded375ca383685b4e Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 8 Oct 2018 16:57:11 -0700 Subject: [PATCH 03/59] added action_view.rb so that form_with works --- app/controllers/works_controller.rb | 8 ++++++++ config/initializers/action_view.rb | 1 + 2 files changed, 9 insertions(+) create mode 100644 config/initializers/action_view.rb diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 7f1b21133e..963ceb4346 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -3,6 +3,14 @@ def index @works = Work.order(:name) end + def show + end + + def new + end + def create end + + end diff --git a/config/initializers/action_view.rb b/config/initializers/action_view.rb new file mode 100644 index 0000000000..142d382f87 --- /dev/null +++ b/config/initializers/action_view.rb @@ -0,0 +1 @@ +Rails.application.config.action_view.form_with_generates_remote_forms = false From 4e79484c997c1dd141716b6b756044479eeee19a Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 8 Oct 2018 18:43:33 -0700 Subject: [PATCH 04/59] updated index show new and create methods in controller of work-feature --- app/controllers/works_controller.rb | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 963ceb4346..2fc016114a 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,15 +1,33 @@ class WorksController < ApplicationController def index - @works = Work.order(:name) + @works = Work.order(:title) end def show + @work = Work.find_by(id: params[:id].to_i) + + if @work.nil? + head :not_found + end end def new - end + @work = Work.new + end def create + @work = Work.new(work_params) + + if @work.save + redirect_to work_path(@work_id) + else + render :new + end + end + + private + def work_params + return params.require(:work).permit(:id, :title, :creator, :description, :votes) end From ed7e40da6c1a284b661866c7eceffcf35e641dfd Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 8 Oct 2018 18:57:19 -0700 Subject: [PATCH 05/59] created partial for works --- app/views/works/_form.html.erb | 30 ++++++++++++++++++++++++++++++ app/views/works/index.html.erb | 8 +++++--- 2 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 app/views/works/_form.html.erb diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb new file mode 100644 index 0000000000..8a5357e0cb --- /dev/null +++ b/app/views/works/_form.html.erb @@ -0,0 +1,30 @@ +
+
+

<%= action_name %>

+
+ +
+ <% if @work.errors.any? %> +
    + <% @work.errors.each do |column, message| %> +
  • + <%= column.capitalize %><%= message %> +
  • + <% end %> +
+ <% end %> + + <%= form_with model: @driver, class: "form" do |f|%> + <%# f.label :name %> + <%= f.text_field :name, placeholder: :name, class: "form__input"%> + + <%# f.label :vin %> + <%= f.text_field :vin, placeholder: :vin, class: "form__input"%> + + <%# f.label :image_url %> + <%= f.text_field :image_url, placeholder: :image_url, class: "form__input" %> + + <%= f.submit button_title, class: 'btn'%> + <% end %> +
+
diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index b1a40390d7..ce521fb06c 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,4 +1,6 @@

List of Works

-

Albums

-

Books

-

Movies

+
    +
  • Albums
  • +
  • Books
  • +
  • Movies
  • +
From d40027f651e25f9488696181a0a9ecfa10b4b03c Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 8 Oct 2018 19:10:00 -0700 Subject: [PATCH 06/59] created migration to add pubyear and category columns to works and removed votes column from works --- app/views/works/_form.html.erb | 8 ++++---- db/migrate/20181009015802_remove_votes_from_works.rb | 5 +++++ ...009020033_add_category_and_publicationyear_to_works.rb | 6 ++++++ db/schema.rb | 5 +++-- 4 files changed, 18 insertions(+), 6 deletions(-) create mode 100644 db/migrate/20181009015802_remove_votes_from_works.rb create mode 100644 db/migrate/20181009020033_add_category_and_publicationyear_to_works.rb diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb index 8a5357e0cb..0d5cb06ed4 100644 --- a/app/views/works/_form.html.erb +++ b/app/views/works/_form.html.erb @@ -14,14 +14,14 @@ <% end %> - <%= form_with model: @driver, class: "form" do |f|%> - <%# f.label :name %> + <%= form_with model: @work, class: "form" do |f|%> + <%= f.label :name %> <%= f.text_field :name, placeholder: :name, class: "form__input"%> - <%# f.label :vin %> + <%= f.label :vin %> <%= f.text_field :vin, placeholder: :vin, class: "form__input"%> - <%# f.label :image_url %> + <%= f.label :image_url %> <%= f.text_field :image_url, placeholder: :image_url, class: "form__input" %> <%= f.submit button_title, class: 'btn'%> diff --git a/db/migrate/20181009015802_remove_votes_from_works.rb b/db/migrate/20181009015802_remove_votes_from_works.rb new file mode 100644 index 0000000000..4b0b7da9ba --- /dev/null +++ b/db/migrate/20181009015802_remove_votes_from_works.rb @@ -0,0 +1,5 @@ +class RemoveVotesFromWorks < ActiveRecord::Migration[5.2] + def change + remove_column :works, :votes + end +end diff --git a/db/migrate/20181009020033_add_category_and_publicationyear_to_works.rb b/db/migrate/20181009020033_add_category_and_publicationyear_to_works.rb new file mode 100644 index 0000000000..c38a205720 --- /dev/null +++ b/db/migrate/20181009020033_add_category_and_publicationyear_to_works.rb @@ -0,0 +1,6 @@ +class AddCategoryAndPublicationyearToWorks < ActiveRecord::Migration[5.2] + def change + add_column :works, :category, :string + add_column :works, :publication_year, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 0c4c83fef7..abaf0f709c 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,18 +10,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2018_10_08_230039) do +ActiveRecord::Schema.define(version: 2018_10_09_020033) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" create_table "works", force: :cascade do |t| - t.integer "votes" t.string "title" t.string "creator" t.string "description" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.string "category" + t.integer "publication_year" end end From 8de401b9029fe45e1df6a59579e060080b99fe73 Mon Sep 17 00:00:00 2001 From: Kay Date: Mon, 8 Oct 2018 21:17:19 -0700 Subject: [PATCH 07/59] added validation in work model and new and show files in the view --- app/controllers/works_controller.rb | 4 +- app/models/work.rb | 1 + app/views/layouts/application.html.erb | 2 +- app/views/works/_form.html.erb | 18 +++++---- app/views/works/new.html.erb | 1 + app/views/works/show.html.erb | 53 ++++++++++++++++++++++++++ config/routes.rb | 2 +- 7 files changed, 70 insertions(+), 11 deletions(-) create mode 100644 app/views/works/new.html.erb create mode 100644 app/views/works/show.html.erb diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 2fc016114a..83d13df2f3 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -7,7 +7,7 @@ def show @work = Work.find_by(id: params[:id].to_i) if @work.nil? - head :not_found + head :not_found end end @@ -19,7 +19,7 @@ def create @work = Work.new(work_params) if @work.save - redirect_to work_path(@work_id) + redirect_to works_path(@work_id) else render :new end diff --git a/app/models/work.rb b/app/models/work.rb index 95322dd45e..fb279459d3 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,2 +1,3 @@ class Work < ApplicationRecord + validates :title, presence: true end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5aef9f71ed..f64f02a318 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -14,7 +14,7 @@ diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb index 0d5cb06ed4..1b5384ed76 100644 --- a/app/views/works/_form.html.erb +++ b/app/views/works/_form.html.erb @@ -8,21 +8,25 @@
    <% @work.errors.each do |column, message| %>
  • - <%= column.capitalize %><%= message %> + <%= column.capitalize %> <%= message %>
  • <% end %>
<% end %> <%= form_with model: @work, class: "form" do |f|%> - <%= f.label :name %> - <%= f.text_field :name, placeholder: :name, class: "form__input"%> + <%= f.label :category %> + <%= f.text_field :category, class: "form__input"%> + <%= f.select :category, [ ['Album', 1], ['Book', 2], ['Movie', 42] ] %> - <%= f.label :vin %> - <%= f.text_field :vin, placeholder: :vin, class: "form__input"%> + <%= f.label :title %> + <%= f.text_field :title, class: "form__input"%> - <%= f.label :image_url %> - <%= f.text_field :image_url, placeholder: :image_url, class: "form__input" %> + <%= f.label :creator %> + <%= f.text_field :creator, class: "form__input"%> + + <%= f.label :publication_year %> + <%= f.text_field :publication_year, class: "form__input" %> <%= f.submit button_title, class: 'btn'%> <% end %> diff --git a/app/views/works/new.html.erb b/app/views/works/new.html.erb new file mode 100644 index 0000000000..21d008067b --- /dev/null +++ b/app/views/works/new.html.erb @@ -0,0 +1 @@ +<%= render partial: "form", locals: { action_name: "Add a new work", button_title: 'Create Work' } %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb new file mode 100644 index 0000000000..ec4e3fe4bc --- /dev/null +++ b/app/views/works/show.html.erb @@ -0,0 +1,53 @@ +
+

Driver Details

+
+ +
+

ID
<%= @driver.id %>

+ +

VIN
<%= @driver.vin %>

+ +

Total Earnings
<%= @driver.total_earnings %>

+ +

Rating
<%= @driver.average_rating %>

+ + <%= button_to "Edit", edit_driver_path(@driver.id), class: "alt-button", method: :get %> + <%= button_to "Delete", driver_path(@driver.id), method: :delete, + data: { confirm: 'Are you sure?'}, class: "alt-button" %> +
+ + +
+ +
+
+

Trips

+
+ +
+ <% trips = @driver.list_trips %> + + + + + + + + + <% trips.each do |trip| %> + + + + + + + + <% end %> +
Trip IDPassenger IDDateCostRating
<%= link_to "#{trip.id}", trip_path(trip.id) %><%= link_to "#{trip.passenger_id}", passenger_path(trip.passenger_id) %><%= trip.date %><%= trip.display_cost %> + <% if trip.rating %> + <%= trip.rating %> + <% else %> + <%= "--" %> + <% end %> +
+
diff --git a/config/routes.rb b/config/routes.rb index 14887ec714..b06bed1c73 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ get 'main/index', as: 'main' - resources :works, only: [:index, :create] + resources :works, only: [:index, :create, :new] From 8bbace2bcecce58df832f10f6b9a07902e3091fd Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 9 Oct 2018 14:53:08 -0700 Subject: [PATCH 08/59] updated strong params in work so that the form works and saves pubyear and category --- app/controllers/works_controller.rb | 2 +- app/views/works/_form.html.erb | 3 +-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 83d13df2f3..aadcf05f2d 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -27,7 +27,7 @@ def create private def work_params - return params.require(:work).permit(:id, :title, :creator, :description, :votes) + return params.require(:work).permit(:id, :title, :creator, :description, :category, :publication_year) end diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb index 1b5384ed76..a2be86fed6 100644 --- a/app/views/works/_form.html.erb +++ b/app/views/works/_form.html.erb @@ -16,8 +16,7 @@ <%= form_with model: @work, class: "form" do |f|%> <%= f.label :category %> - <%= f.text_field :category, class: "form__input"%> - <%= f.select :category, [ ['Album', 1], ['Book', 2], ['Movie', 42] ] %> + <%= f.select :category, ['Album','Book','Movie'], class: 'form__input'%> <%= f.label :title %> <%= f.text_field :title, class: "form__input"%> From 1b2d4780c69c18e49abe4df5ae122174a0850f1a Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 9 Oct 2018 15:21:29 -0700 Subject: [PATCH 09/59] updated work show page --- app/views/works/show.html.erb | 51 ++++++++++++----------------------- 1 file changed, 17 insertions(+), 34 deletions(-) diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index ec4e3fe4bc..c2ad499011 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -1,52 +1,35 @@
-

Driver Details

+

@work.title

-
-

ID
<%= @driver.id %>

- -

VIN
<%= @driver.vin %>

+
    +
  • Created by: <%@work.creator%>
  • +
  • Published: <%@work.publication_year%>
  • +
  • <%@work.description%>
  • +
-

Total Earnings
<%= @driver.total_earnings %>

-

Rating
<%= @driver.average_rating %>

+ <%# button_to "Back to media ranks", root, class: "alt-button", method: :get %> + <%# button_to "Edit", edit_work_path(@work.id), class: "alt-button", method: :get %> + <%# button_to "Delete", work_path(@work.id), method: :delete, data: { confirm: 'Are you sure?'}, class: "alt-button" %> +

Total votes for this <%@work.category%>

- <%= button_to "Edit", edit_driver_path(@driver.id), class: "alt-button", method: :get %> - <%= button_to "Delete", driver_path(@driver.id), method: :delete, - data: { confirm: 'Are you sure?'}, class: "alt-button" %> -
-
-
-
-

Trips

-
+
User
+
Date
-
- <% trips = @driver.list_trips %> - - + - - - <% trips.each do |trip| %> + + <% @works.each do |work| %> - - - - - + + <% end %>
Trip IDPassenger IDUser DateCostRating
<%= link_to "#{trip.id}", trip_path(trip.id) %><%= link_to "#{trip.passenger_id}", passenger_path(trip.passenger_id) %><%= trip.date %><%= trip.display_cost %> - <% if trip.rating %> - <%= trip.rating %> - <% else %> - <%= "--" %> - <% end %> - <%= link_to "#{work.user}", work_path(work.id) %><%# link_to "#{work.vote.date}", work_path(work.id) %>
From 770ae5e57c703d52f10e50461f57cd13b7913e8c Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 9 Oct 2018 16:06:09 -0700 Subject: [PATCH 10/59] edited work form select drop down for category items were upcase when they should be downcase --- app/controllers/works_controller.rb | 5 +++++ app/models/work.rb | 14 +++++++++++++- app/views/works/_form.html.erb | 2 +- app/views/works/index.html.erb | 3 +++ 4 files changed, 22 insertions(+), 2 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index aadcf05f2d..d8e6f16ac0 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,6 +1,11 @@ class WorksController < ApplicationController def index @works = Work.order(:title) + + @albums = Work.albums + @books = Work.books + @movies = Work.movies + end def show diff --git a/app/models/work.rb b/app/models/work.rb index fb279459d3..6c8916ad91 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,3 +1,15 @@ class Work < ApplicationRecord - validates :title, presence: true + validates :title, presence: true + + def self.albums + return Work.find_by(category: 'album') + end + + def self.books + return Work.find_by(category: 'book') + end + + def self.movies + return Work.find_by(category: 'movie') + end end diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb index a2be86fed6..261009ac7c 100644 --- a/app/views/works/_form.html.erb +++ b/app/views/works/_form.html.erb @@ -16,7 +16,7 @@ <%= form_with model: @work, class: "form" do |f|%> <%= f.label :category %> - <%= f.select :category, ['Album','Book','Movie'], class: 'form__input'%> + <%= f.select :category, ['album','book','movie'], class: 'form__input'%> <%= f.label :title %> <%= f.text_field :title, class: "form__input"%> diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index ce521fb06c..c7600c8aee 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,6 +1,9 @@

List of Works

  • Albums
  • + <% @albums.each do |album| %> + <%= album.title %> + <% end %>
  • Books
  • Movies
From 403699930d006710d7e0c77eb2a24535fe367386 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 9 Oct 2018 16:19:09 -0700 Subject: [PATCH 11/59] seeded database --- db/seeds.rb | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2accd..b318911720 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,30 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) + + +require 'csv' + +WORK_FILE = Rails.root.join('db', 'media_seeds.csv') +puts "Loading raw work data from #{WORK_FILE}" + +work_failures = [] +CSV.foreach(WORK_FILE, :headers => true) do |row| + work = Work.new + work.category = row['category'] + work.title = row['title'] + work.creator = row['creator'] + work.publication_year = row['publication_year'] + work.description = row['description'] + successful = work.save + + if !successful + work_failures << work + puts "Failed to save work: #{work.inspect}" + else + puts "Created work: #{work.inspect}" + end +end + +puts "Added #{Work.count} work records" +puts "#{work_failures.length} works failed to save" From bdb004bb4365362fbc227d29768680fe125ca682 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 9 Oct 2018 17:05:05 -0700 Subject: [PATCH 12/59] edited works index page so that list of works will be displayed --- app/controllers/works_controller.rb | 2 +- app/models/work.rb | 6 ++--- app/views/works/index.html.erb | 40 ++++++++++++++++++++++++----- 3 files changed, 38 insertions(+), 10 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index d8e6f16ac0..8394e67e18 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -5,7 +5,7 @@ def index @albums = Work.albums @books = Work.books @movies = Work.movies - + end def show diff --git a/app/models/work.rb b/app/models/work.rb index 6c8916ad91..c460a79483 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -2,14 +2,14 @@ class Work < ApplicationRecord validates :title, presence: true def self.albums - return Work.find_by(category: 'album') + return Work.where(category: 'album') end def self.books - return Work.find_by(category: 'book') + return Work.where(category: 'book') end def self.movies - return Work.find_by(category: 'movie') + return Work.where(category: 'movie') end end diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index c7600c8aee..18cf3efc64 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,9 +1,37 @@

List of Works

    -
  • Albums
  • - <% @albums.each do |album| %> - <%= album.title %> - <% end %> -
  • Books
  • -
  • Movies
  • +
  • Albums
  • +
    + <% @albums.each do |album| %> + + + + + + <% end %> + + +
  • Books +
    + <% @books.each do |book| %> +
  • + + + + <% end %> + + + + +
  • Movies +
    + <% @movies.each do |movie| %> +
  • + + + + <% end %> + + +
    Title: <%= album.title %>Created by: <%= album.creator %>Published: <%= album.publication_year %>
    Title: <%= book.title %> Created by: <%= book.creator %>Published: <%= book.publication_year %>
    Title: <%= movie.title %>Created by: <%= movie.creator %>Published: <%= movie.publication_year %>
    From 08abebedc11f7963d94ef1cfb606ee5a3406fd5f Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 9 Oct 2018 20:29:22 -0700 Subject: [PATCH 13/59] created four tests for work model testing validations and custom methods --- app/models/work.rb | 3 + test/controllers/main_controller_test.rb | 14 ++--- test/fixtures/works.yml | 22 ++++---- test/models/work_test.rb | 71 +++++++++++++++++++++++- test/test_helper.rb | 3 +- 5 files changed, 94 insertions(+), 19 deletions(-) diff --git a/app/models/work.rb b/app/models/work.rb index c460a79483..0aa0be0eb5 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -12,4 +12,7 @@ def self.books def self.movies return Work.where(category: 'movie') end + +#make sure it returns an instance of Array +#make sure each category inside is end diff --git a/test/controllers/main_controller_test.rb b/test/controllers/main_controller_test.rb index 32ded261bd..134525a1ab 100644 --- a/test/controllers/main_controller_test.rb +++ b/test/controllers/main_controller_test.rb @@ -1,9 +1,9 @@ require "test_helper" -describe MainController do - it "should get index" do - get main_index_url - value(response).must_be :success? - end - -end +# describe MainController do +# it "should get index" do +# get main_index_url +# value(response).must_be :success? +# end +# +# end diff --git a/test/fixtures/works.yml b/test/fixtures/works.yml index 5008fe6181..b770ae8a93 100644 --- a/test/fixtures/works.yml +++ b/test/fixtures/works.yml @@ -1,13 +1,15 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -one: - votes: 1 - title: MyString - creator: MyString - description: MyString +harry_potter: + title: Harry Potter + publication_year: 1999 + creator: JK Rowling + description: A book about magic + category: book -two: - votes: 1 - title: MyString - creator: MyString - description: MyString +lord_of_the_rings: + title: Lord of The Rings + publication_year: 1954 + creator: JRR Tolkien + description: a book about magic + category: book diff --git a/test/models/work_test.rb b/test/models/work_test.rb index f6fba7104d..b327b0394f 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -1,9 +1,78 @@ require "test_helper" describe Work do - let(:work) { Work.new } + let(:work) { works(:harry_potter) } + it "must be valid" do value(work).must_be :valid? end + + it 'has required fields' do + fields = [:title, :publication_year, :creator, :description, :category] + + fields.each do |field| + expect(work).must_respond_to field + end + end + + + describe 'Validations' do + it 'must have a title' do + # arrange + work.title = nil + + #act + valid = work.save + + #assert + expect(valid).must_equal false + expect(work.errors.messages).must_include :title + end + end + + describe 'Custom Methods' do + #arrange + it 'returns an array of albums' do + albums_array = Work.albums #array of works that are albums + + #act /assert + albums_array.each do |work| + expect(work.category).must_equal 'album' + end + end + + it 'returns an array of books' do + books_array = Work.books #array of works that are albums + + #act /assert + books_array.each do |work| + expect(work.category).must_equal 'book' + end + end + + it 'returns an array of movies' do + movies_array = Work.movies #array of works that are albums + + #act /assert + movies_array.each do |work| + expect(work.category).must_equal 'movie' + end + end + + + end + + describe 'Relationships' do + end + + + # def self.movies + # return Work.where(category: 'movie') + # end + + #make sure it returns an instance of Array + #make sure each category inside is + + end diff --git a/test/test_helper.rb b/test/test_helper.rb index 2b5172a7d6..fb7dd505e2 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,7 +1,8 @@ ENV["RAILS_ENV"] = "test" require File.expand_path("../../config/environment", __FILE__) require "rails/test_help" -require "minitest/rails"require "minitest/reporters" # for Colorized output +require "minitest/rails" +require "minitest/reporters" # for Colorized output # For colorful output! Minitest::Reporters.use!( Minitest::Reporters::SpecReporter.new, From 86a5b824c813b52d36ad8e8f52d03617faba5347 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 10 Oct 2018 13:47:38 -0700 Subject: [PATCH 14/59] created update and destory methods to controller --- app/controllers/works_controller.rb | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 8394e67e18..cec168f8a3 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -30,6 +30,27 @@ def create end end + def update + @work = Work.find_by(id: params[:id].to_i) + if @work.update(work_params) + redirect_to work_path(@work.id) + else + render :edit + end + end + + def destroy + work = Work.find_by(id: params[:id].to_i) + if work.nil? + flash[:error] = "Work #{params[:id]} not found" + else + @deleted_work = work.destroy + flash[:success] = "#{work.title} deleted" + end + + redirect_to root_path + end + private def work_params return params.require(:work).permit(:id, :title, :creator, :description, :category, :publication_year) From 7a4b717685b7fb93e4488bae6511d35b2ad22ac0 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 10 Oct 2018 13:58:34 -0700 Subject: [PATCH 15/59] added views for edit and delete for works --- app/controllers/works_controller.rb | 2 +- app/views/works/destroy.html.erb | 1 + app/views/works/edit.html.erb | 2 ++ app/views/works/new.html.erb | 3 ++- 4 files changed, 6 insertions(+), 2 deletions(-) create mode 100644 app/views/works/destroy.html.erb create mode 100644 app/views/works/edit.html.erb diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index cec168f8a3..c45f08a21a 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -12,7 +12,7 @@ def show @work = Work.find_by(id: params[:id].to_i) if @work.nil? - head :not_found + head :notfound end end diff --git a/app/views/works/destroy.html.erb b/app/views/works/destroy.html.erb new file mode 100644 index 0000000000..5efe289eee --- /dev/null +++ b/app/views/works/destroy.html.erb @@ -0,0 +1 @@ +<%= "You have deleted #{@deleted_work.title}" %> diff --git a/app/views/works/edit.html.erb b/app/views/works/edit.html.erb new file mode 100644 index 0000000000..5280d3ef05 --- /dev/null +++ b/app/views/works/edit.html.erb @@ -0,0 +1,2 @@ +<%= render partial: "form",locals: + { action_name: 'Edit a work', button_title: 'Edit Work' } %> diff --git a/app/views/works/new.html.erb b/app/views/works/new.html.erb index 21d008067b..85652d7f92 100644 --- a/app/views/works/new.html.erb +++ b/app/views/works/new.html.erb @@ -1 +1,2 @@ -<%= render partial: "form", locals: { action_name: "Add a new work", button_title: 'Create Work' } %> +<%= render partial: "form", locals: + { action_name: "Add a new work", button_title: 'Create Work' } %> From a2cfab4219c2fef252849993cd9da674a51f0778 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 10 Oct 2018 14:01:08 -0700 Subject: [PATCH 16/59] added not found page to layouts --- app/views/layouts/notfound.html.erb | 1 + 1 file changed, 1 insertion(+) create mode 100644 app/views/layouts/notfound.html.erb diff --git a/app/views/layouts/notfound.html.erb b/app/views/layouts/notfound.html.erb new file mode 100644 index 0000000000..c37ab2ebaa --- /dev/null +++ b/app/views/layouts/notfound.html.erb @@ -0,0 +1 @@ +

    Not Found 404

    From 6f772ae6d9179a2a0cb3a5b32666ec6ad93849b5 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 10 Oct 2018 14:50:13 -0700 Subject: [PATCH 17/59] updated works index show and controller and routes, now there is an edit method which finds the work that needs to be edited --- app/controllers/works_controller.rb | 4 ++++ app/views/works/index.html.erb | 20 ++++++++++---------- app/views/works/show.html.erb | 24 +++++++++++------------- config/routes.rb | 2 +- 4 files changed, 26 insertions(+), 24 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index c45f08a21a..0934127581 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -30,6 +30,10 @@ def create end end + def edit + @work = Work.find_by(id: params[:id].to_i) + end + def update @work = Work.find_by(id: params[:id].to_i) if @work.update(work_params) diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 18cf3efc64..ea8eb925a1 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,6 +1,5 @@

    List of Works

    -
      -
    • Albums
    • +Albums
      <% @albums.each do |album| %> @@ -10,28 +9,29 @@ <% end %> - -
    • Books + +Books
      <% @books.each do |book| %> +
    • - + <% end %> - -
    • Movies + +Movies
      <% @movies.each do |movie| %>
    • - + + <% raise %> <% end %> - - +
      Published: <%= album.publication_year %>
      Title: <%= book.title %> Title: <%= link_to book.title, work_path(book.id) %> Created by: <%= book.creator %> Published: <%= book.publication_year %>
      Title: <%= movie.title %>Title: <%= link_to movie.title, work_path(movie.id) %>Created by: <%= movie.creator %> Published: <%= movie.publication_year %>
      diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index c2ad499011..301e8c0cde 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -1,24 +1,22 @@
      -

      @work.title

      +

      <%=@work.title%>

      • Created by: <%@work.creator%>
      • Published: <%@work.publication_year%>
      • -
      • <%@work.description%>
      • +
      • Description: <%@work.description%>
      - <%# button_to "Back to media ranks", root, class: "alt-button", method: :get %> - <%# button_to "Edit", edit_work_path(@work.id), class: "alt-button", method: :get %> - <%# button_to "Delete", work_path(@work.id), method: :delete, data: { confirm: 'Are you sure?'}, class: "alt-button" %> -

      Total votes for this <%@work.category%>

      - + <%= button_to "Back to media ranks", root_path, class: "alt-button", method: :get %> + <%= button_to "Edit", edit_work_path(@work.id), class: "alt-button", method: :get %> +
      upvote
      <%# insert button to upvote here%> + <%= button_to "Delete", work_path(@work.id), method: :delete, data: { confirm: 'Are you sure?'}, class: "alt-button" %> +

      <%= %> (insert work.total_votes here) votes for this <%= @work.category%>

      -
      User
      -
      Date
      @@ -26,11 +24,11 @@ - <% @works.each do |work| %> + <%# @works.each do |work| %> - - + + - <% end %> + <%# end %>
      Date
      <%= link_to "#{work.user}", work_path(work.id) %><%# link_to "#{work.vote.date}", work_path(work.id) %><%= link_to @work.title, work_path(@work.id) %><%# update this when i have votes link_to "#{work.vote.date}", work_path(work.id) %>
      diff --git a/config/routes.rb b/config/routes.rb index b06bed1c73..2b0a2d0d7e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ get 'main/index', as: 'main' - resources :works, only: [:index, :create, :new] + resources :works From b17af37a9a25c9aaf45cbc5e6c380a548a0c4c5f Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 10 Oct 2018 15:06:22 -0700 Subject: [PATCH 18/59] added flash functionality to the layouts/appplication.html.erb --- app/views/layouts/application.html.erb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index f64f02a318..5b5b876256 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,6 +10,21 @@ + +
      +
      + <% flash.each do |name, message| %> + <% if message.class == Array %> + <% message.each do |msg| %> +
      <%= "#{name}: #{msg}" %>
      + <% end %> + <% else %> +
      <%= message %>
      + <% end %> + <% end %> +
      +
      + - +
      <%= yield %> diff --git a/app/views/sessions/_form.html.erb b/app/views/sessions/_form.html.erb new file mode 100644 index 0000000000..53768d148d --- /dev/null +++ b/app/views/sessions/_form.html.erb @@ -0,0 +1,36 @@ + +
      +

      <%= action_name %>

      + + <%= form_with model: @user, class: "form-group" do |f| %> + <%= f.label "Username" %> + <%= f.text_field :name %> + <%= f.submit button_title, class: "btn btn-primary" %> + <% end %> + + +
      +

      A note about logging in

      +

      + There is no password field. In fact, there is no indication whatsoever + that you are who you say you are. There's nothing special about users - + username is just another piece of data that the user entered and we have to keep + track of. +

      + +

      + We'll learn more about security and authentication in the next couple weeks. + For now, don't worry about it beyond what you can see here. +

      +
      +
      diff --git a/app/views/sessions/new.html.erb b/app/views/sessions/new.html.erb index b39a3bc951..ae4d94f110 100644 --- a/app/views/sessions/new.html.erb +++ b/app/views/sessions/new.html.erb @@ -1,2 +1,7 @@ +<%= render partial: "form", + locals: { action_name: 'Log In', + button_title: 'Log In' } %> + +

      Sessions#new

      Find me in app/views/sessions/new.html.erb

      diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 0000000000..e69de29bb2 diff --git a/config/routes.rb b/config/routes.rb index 9a81d7f3b5..3d38f7fa3f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,10 @@ Rails.application.routes.draw do root "main#index" + post 'sessions/login', to: 'sessions#login', as: 'login' #or just user login? - get 'sessions/new', to: 'sessions#new' + get 'sessions/login', to: 'sessions#new' get 'sessions/destroy', to: 'sessions#destroy', as: 'logout' + get 'main/index', as: 'main' resources :works From b07e9e220f97d01e4a2ab9c4f10069552a7060d2 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 11 Oct 2018 15:50:31 -0700 Subject: [PATCH 28/59] edited sessions controller and view to allow users to log in the site be kept track of via session --- app/controllers/sessions_controller.rb | 3 +-- app/controllers/users_controller.rb | 3 ++- app/views/layouts/application.html.erb | 4 ++++ app/views/sessions/_form.html.erb | 2 +- config/routes.rb | 2 +- 5 files changed, 9 insertions(+), 5 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index af045f249a..f4e85d772e 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -6,13 +6,12 @@ def login if user.nil? #create new user user = User.create(name: params[:user][:name]) + end - else #log in the existing user session[:user_id] = user.id #where does :user_id come from? flash[:success] = "#{user.name} Successfully logged in!" redirect_to root_path - end end def new diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 0ead15bd7c..6b1b87c7f9 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,9 +1,10 @@ class UsersController < ApplicationController def index @users = User.order(:name) - + @current_user = User.find_by(id: session[:user_id]) #this finds the current user in the session so i can display it where i need + end def show diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4e2e69f51d..f13b6c85bd 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -47,7 +47,11 @@
    • <%= link_to "View all media", works_path %>
    • <%= link_to "Add a new work", new_work_path %>
    • <%= link_to "View all users", users_path %>
    • + <% if session[:user_id] %> +
    • <%= link_to "Log out", logout_path, method: :delete %>
    • + <% else %>
    • <%= link_to "Log In", login_path %>
    • + <% end %>
    diff --git a/app/views/sessions/_form.html.erb b/app/views/sessions/_form.html.erb index 53768d148d..f4c2b13348 100644 --- a/app/views/sessions/_form.html.erb +++ b/app/views/sessions/_form.html.erb @@ -2,7 +2,7 @@

    <%= action_name %>

    - <%= form_with model: @user, class: "form-group" do |f| %> + <%= form_with model: @user, url: login_path, method: :post, class: "form-group" do |f| %> <%= f.label "Username" %> <%= f.text_field :name %> <%= f.submit button_title, class: "btn btn-primary" %> diff --git a/config/routes.rb b/config/routes.rb index 3d38f7fa3f..3d747e7dda 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,7 +3,7 @@ post 'sessions/login', to: 'sessions#login', as: 'login' #or just user login? get 'sessions/login', to: 'sessions#new' - get 'sessions/destroy', to: 'sessions#destroy', as: 'logout' + delete 'sessions/destroy', to: 'sessions#destroy', as: 'logout' get 'main/index', as: 'main' From 417437ef60fd100166b48e02fe3bb3849ff802e9 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 11 Oct 2018 16:22:05 -0700 Subject: [PATCH 29/59] edited the index view for works --- app/views/users/index.html.erb | 20 ++++++ app/views/users/show.html.erb | 25 +++++++ app/views/works/index.html.erb | 125 ++++++++++++++++++++------------- 3 files changed, 123 insertions(+), 47 deletions(-) create mode 100644 app/views/users/show.html.erb diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index e69de29bb2..d4d4e46706 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -0,0 +1,20 @@ +

    List of Users

    + +
    + + + + <% @users.each do |user| %> + + + + + <% end %> + +
    Username
    <%= link_to user.name, user_path(user.id) %>
    + + + + + +Back to Media List diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000000..22c3986b7c --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,25 @@ +
    +

    User Summary: <%=@user.name%>

    +

    Joined site <%=@user.created_at%>

    + +
    +

    Votes

    + + + + + + + + + + + + +
    Media TitleCreated ByPublishedCategoryVoted On
    +
    + +See all Users +Back to Media List + +
    diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index f6af04bae3..43bdcace8f 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,54 +1,85 @@ -

    List of Works

    +
    +

    List of Works

    -
    - - - -

    Albums

    - <% @albums.each do |album| %> - - - - - - - - - <% end %> - -
    TitleCreated byPublished
    <%= link_to album.title, work_path(album.id) %> <%= album.creator %><%= album.publication_year %>
    +
    +

    Albums

    + + + + + + + + + + + <% @albums.each do |album| %> + + + + + + + + <% end %> + + +
    VotesTitleCreated ByPublishedUpvote
    888<%= link_to album.title, work_path(album.id) %><%= album.creator %><%= album.publication_year %>Upvote
    +
    +

    Books

    + + + + + + + + + + + <% @books.each do |book| %> + + + + + + + + <% end %> + + +
    VotesTitleCreated ByPublishedUpvote
    888<%= link_to book.title, work_path(book.id) %><%= book.creator %><%= book.publication_year %>Upvote
    - - -

    Books

    - <% @books.each do |book| %> - - - - +
    +

    Movies

    +
    TitleCreated byPublished
    + + + + + + + + + + <% @movies.each do |movie| %> + + + + + + + + <% end %> + + +
    VotesTitleCreated ByPublishedUpvote
    888<%= link_to movie.title, work_path(movie.id) %><%= movie.creator %><%= movie.publication_year %>Upvote
    - <%= link_to book.title, work_path(book.id) %> - <%= book.creator %> - <%= book.publication_year %> - <% end %> - +
    +View top media +Add a new work - - -

    Movies

    - <% @movies.each do |movie| %> - Title - Created by - Published - - - - - - - <% end %> - -
    <%= link_to movie.title, work_path(movie.id) %> <%= movie.creator %><%= movie.publication_year %>
    +
    From e429bd4944b2d502145fce7cf2d4f3158b3aa3ce Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 11 Oct 2018 16:26:57 -0700 Subject: [PATCH 30/59] added url login_path and post method to form --- app/views/sessions/_form.html.erb | 36 +++++++++++++++++++++++++++++++ app/views/users/index.html.erb | 20 +++++++++++++++++ app/views/users/show.html.erb | 25 +++++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 app/views/sessions/_form.html.erb create mode 100644 app/views/users/index.html.erb create mode 100644 app/views/users/show.html.erb diff --git a/app/views/sessions/_form.html.erb b/app/views/sessions/_form.html.erb new file mode 100644 index 0000000000..f4c2b13348 --- /dev/null +++ b/app/views/sessions/_form.html.erb @@ -0,0 +1,36 @@ + +
    +

    <%= action_name %>

    + + <%= form_with model: @user, url: login_path, method: :post, class: "form-group" do |f| %> + <%= f.label "Username" %> + <%= f.text_field :name %> + <%= f.submit button_title, class: "btn btn-primary" %> + <% end %> + + +
    +

    A note about logging in

    +

    + There is no password field. In fact, there is no indication whatsoever + that you are who you say you are. There's nothing special about users - + username is just another piece of data that the user entered and we have to keep + track of. +

    + +

    + We'll learn more about security and authentication in the next couple weeks. + For now, don't worry about it beyond what you can see here. +

    +
    +
    diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 0000000000..d4d4e46706 --- /dev/null +++ b/app/views/users/index.html.erb @@ -0,0 +1,20 @@ +

    List of Users

    + +
    + + + + <% @users.each do |user| %> + + + + + <% end %> + +
    Username
    <%= link_to user.name, user_path(user.id) %>
    + + + + + +Back to Media List diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb new file mode 100644 index 0000000000..22c3986b7c --- /dev/null +++ b/app/views/users/show.html.erb @@ -0,0 +1,25 @@ +
    +

    User Summary: <%=@user.name%>

    +

    Joined site <%=@user.created_at%>

    + +
    +

    Votes

    + + + + + + + + + + + + +
    Media TitleCreated ByPublishedCategoryVoted On
    +
    + +See all Users +Back to Media List + +
    From f7fc0da3af8def71a63ab5b61a73ee571ccbe683 Mon Sep 17 00:00:00 2001 From: Kay Date: Thu, 11 Oct 2018 17:01:32 -0700 Subject: [PATCH 31/59] updated work user vote models to include relationships --- app/models/user.rb | 2 ++ app/models/vote.rb | 2 ++ app/models/work.rb | 2 ++ 3 files changed, 6 insertions(+) diff --git a/app/models/user.rb b/app/models/user.rb index 379658a509..14d563bb3b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,2 +1,4 @@ class User < ApplicationRecord + has_many :works, through :votes + has_many :votes end diff --git a/app/models/vote.rb b/app/models/vote.rb index 4c58e4f3d5..b8b6f9f0aa 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,2 +1,4 @@ class Vote < ApplicationRecord + belongs_to :user + belongs_to :work end diff --git a/app/models/work.rb b/app/models/work.rb index d4345f89e8..4922ed9da7 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,6 +1,8 @@ class Work < ApplicationRecord validates :title, presence: true validates_inclusion_of :category, in: %w(book album movie) + has_many :users, through :votes + has_many :votes def self.albums From f79a1ae0034e65766c0a07f2ea68f9fe7643cc2f Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 11:26:26 -0700 Subject: [PATCH 32/59] minimally set up vote_test files and yml file and wrote pseudocode for #upvote in the work controller --- app/controllers/works_controller.rb | 12 +++++++ app/models/user.rb | 5 ++- app/models/vote.rb | 4 +++ app/models/work.rb | 2 +- test/controllers/sessions_controller_test.rb | 38 ++++++++++---------- test/fixtures/users.yml | 8 ++--- test/fixtures/votes.yml | 11 +++--- test/models/vote_test.rb | 2 +- 8 files changed, 52 insertions(+), 30 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 2926ba0d78..57b52eb8cc 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -55,6 +55,18 @@ def destroy redirect_to root_path end + def upvote + #how do i figure out if a user has voted on a piece? + #i need to put in a validation to make sure a vote can only be created on the same work by the same user once + # vote = Vote.new + #if vote.save + # upvote = Vote.new + # flash message success! + #else + # user has already voted for this work + # flash message could not upvote + end + private def work_params return params.require(:work).permit(:title, :creator, :description, :category, :publication_year) #this data is coming from the form diff --git a/app/models/user.rb b/app/models/user.rb index 14d563bb3b..191a916d3b 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,7 @@ class User < ApplicationRecord - has_many :works, through :votes + has_many :works, through: :votes has_many :votes + + validates :name, presence: true + end diff --git a/app/models/vote.rb b/app/models/vote.rb index b8b6f9f0aa..1563de6686 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,4 +1,8 @@ class Vote < ApplicationRecord belongs_to :user belongs_to :work + + #user must be logged in to vote + #each user can only upvote a work once + end diff --git a/app/models/work.rb b/app/models/work.rb index 4922ed9da7..07cea19624 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,7 +1,7 @@ class Work < ApplicationRecord validates :title, presence: true validates_inclusion_of :category, in: %w(book album movie) - has_many :users, through :votes + has_many :users, through: :votes has_many :votes diff --git a/test/controllers/sessions_controller_test.rb b/test/controllers/sessions_controller_test.rb index 1516a4a7c1..6153dc16d2 100644 --- a/test/controllers/sessions_controller_test.rb +++ b/test/controllers/sessions_controller_test.rb @@ -1,19 +1,19 @@ -require "test_helper" - -describe SessionsController do - it "should get login" do - get sessions_login_url - value(response).must_be :success? - end - - it "should get new" do - get sessions_new_url - value(response).must_be :success? - end - - it "should get destroy" do - get sessions_destroy_url - value(response).must_be :success? - end - -end +# require "test_helper" +# +# describe SessionsController do +# it "should get login" do +# get sessions_login_url +# value(response).must_be :success? +# end +# +# it "should get new" do +# get sessions_new_url +# value(response).must_be :success? +# end +# +# it "should get destroy" do +# get sessions_destroy_url +# value(response).must_be :success? +# end +# +# end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 56066c68af..c9b48e1630 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,7 +1,7 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -one: - name: MyString +kay: + name: Kay -two: - name: MyString +cody: + name: Cody diff --git a/test/fixtures/votes.yml b/test/fixtures/votes.yml index dc3ee79b5d..2159559b28 100644 --- a/test/fixtures/votes.yml +++ b/test/fixtures/votes.yml @@ -4,8 +4,11 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} +vote1: + user: Kay + work: book + +vote2: + user: Nelson + work: movie # column: value diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index fc15947bd3..f6569c61cc 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -1,7 +1,7 @@ require "test_helper" describe Vote do - let(:vote) { Vote.new } + let(:vote) { votes(:vote1) } it "must be valid" do value(vote).must_be :valid? From 806ee2cc95ea59e895eb5cee40e9eeea02b6e92b Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 11:44:46 -0700 Subject: [PATCH 33/59] updated work validation so the title must have a unique title within its category, and created vote validation so a user can only vote on a single work once --- app/models/vote.rb | 6 +++++- app/models/work.rb | 5 +++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/app/models/vote.rb b/app/models/vote.rb index 1563de6686..71fd508e60 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,8 +1,12 @@ class Vote < ApplicationRecord belongs_to :user belongs_to :work + #i need to put in a validation to make sure a + #vote can only be created on the same work by the same user once + #validates :user_id, uniqueness: true + validates :user_id, uniqueness: {scope: :work_id, message:"You can only vote once"} #user must be logged in to vote - #each user can only upvote a work once + #each user can only upvote a work once end diff --git a/app/models/work.rb b/app/models/work.rb index 07cea19624..471ab2562d 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,7 +1,8 @@ class Work < ApplicationRecord - validates :title, presence: true + # validates :title, presence: true + validates :title, uniqueness: { scope: :category } validates_inclusion_of :category, in: %w(book album movie) - has_many :users, through: :votes + has_many :users, through: :votes has_many :votes From 53fee8eaf26688d40db19303e833d364c843e2aa Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 12:14:14 -0700 Subject: [PATCH 34/59] fixed login issue, now a blank user wont be created --- app/controllers/sessions_controller.rb | 17 ++++++++++++++--- app/models/user.rb | 2 +- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index f4e85d772e..338233c634 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -5,13 +5,24 @@ def login if user.nil? #create new user - user = User.create(name: params[:user][:name]) - end + user = User.new(name: params[:user][:name]) + if user.save + #log in the existing user + session[:user_id] = user.id #where does :user_id come from? + flash[:success] = "#{user.name} Successfully logged in!" + # raise + redirect_to root_path + else + flash[:warning] = "no" + redirect_to root_path + end - #log in the existing user + else + raise session[:user_id] = user.id #where does :user_id come from? flash[:success] = "#{user.name} Successfully logged in!" redirect_to root_path + end end def new diff --git a/app/models/user.rb b/app/models/user.rb index 191a916d3b..6d97e373ee 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,6 @@ class User < ApplicationRecord has_many :works, through: :votes has_many :votes - validates :name, presence: true + validates :name, presence: true end From dbd124030ba6eb4a2cfbdf7826b9d7443d7f6fca Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 13:42:07 -0700 Subject: [PATCH 35/59] added validation test in work to require uniqe title per category --- app/models/work.rb | 2 +- test/models/user_test.rb | 14 +++++++------- test/models/vote_test.rb | 14 +++++++------- test/models/work_test.rb | 18 ++++++++++++++++-- 4 files changed, 31 insertions(+), 17 deletions(-) diff --git a/app/models/work.rb b/app/models/work.rb index 471ab2562d..8c347fdaa4 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,5 +1,5 @@ class Work < ApplicationRecord - # validates :title, presence: true + validates :title, presence: true validates :title, uniqueness: { scope: :category } validates_inclusion_of :category, in: %w(book album movie) has_many :users, through: :votes diff --git a/test/models/user_test.rb b/test/models/user_test.rb index cc862ac2d9..0768f790aa 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,9 +1,9 @@ require "test_helper" -describe User do - let(:user) { User.new } - - it "must be valid" do - value(user).must_be :valid? - end -end +# describe User do +# let(:user) { User.new } +# +# it "must be valid" do +# value(user).must_be :valid? +# end +# end diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index f6569c61cc..e61434ba9a 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -1,9 +1,9 @@ require "test_helper" -describe Vote do - let(:vote) { votes(:vote1) } - - it "must be valid" do - value(vote).must_be :valid? - end -end +# describe Vote do +# let(:vote) { votes(:vote1) } +# +# it "must be valid" do +# value(vote).must_be :valid? +# end +# end diff --git a/test/models/work_test.rb b/test/models/work_test.rb index 8826d7719c..c9c88a67d2 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -1,7 +1,9 @@ require "test_helper" - +require "pry" describe Work do let(:work) { works(:harry_potter) } + let(:work2) { works(:lord_of_the_rings) } + it "must be valid" do @@ -39,7 +41,19 @@ #assert expect(valid).must_equal false - end + end + + it 'title must be unique to its category' do + #arrange + work2.title = 'Harry Potter' + + #act + valid = work2.save + + #assert + expect(valid).must_equal false + end + end describe 'Custom Methods' do From b2279706b88d3844be7a99ecb8b7b8916f93bf89 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 14:17:46 -0700 Subject: [PATCH 36/59] edited work user vote test files to be more systematic and easy to refer to and created validations for vote --- app/models/vote.rb | 6 ++--- test/fixtures/users.yml | 4 +-- test/fixtures/votes.yml | 8 +++--- test/fixtures/works.yml | 4 +-- test/models/vote_test.rb | 55 +++++++++++++++++++++++++++++++++++----- test/models/work_test.rb | 4 +-- 6 files changed, 60 insertions(+), 21 deletions(-) diff --git a/app/models/vote.rb b/app/models/vote.rb index 71fd508e60..1a6651a6ad 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,12 +1,10 @@ class Vote < ApplicationRecord belongs_to :user belongs_to :work - #i need to put in a validation to make sure a - #vote can only be created on the same work by the same user once - #validates :user_id, uniqueness: true + #vote can only be created on the same work by the same user once aka #each user can only upvote a work once validates :user_id, uniqueness: {scope: :work_id, message:"You can only vote once"} + #validates :user_id, uniqueness: true <- this is what i had before just leave as reference #user must be logged in to vote - #each user can only upvote a work once end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index c9b48e1630..1d09ae85ce 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -1,7 +1,7 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -kay: +user1: name: Kay -cody: +user2: name: Cody diff --git a/test/fixtures/votes.yml b/test/fixtures/votes.yml index 2159559b28..58ac3c465c 100644 --- a/test/fixtures/votes.yml +++ b/test/fixtures/votes.yml @@ -5,10 +5,10 @@ # below each fixture, per the syntax in the comments below # vote1: - user: Kay - work: book + user: user1 + work: work1 vote2: - user: Nelson - work: movie + user: user2 + work: work2 # column: value diff --git a/test/fixtures/works.yml b/test/fixtures/works.yml index b770ae8a93..42ccdafb3e 100644 --- a/test/fixtures/works.yml +++ b/test/fixtures/works.yml @@ -1,13 +1,13 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html -harry_potter: +work1: title: Harry Potter publication_year: 1999 creator: JK Rowling description: A book about magic category: book -lord_of_the_rings: +work2: title: Lord of The Rings publication_year: 1954 creator: JRR Tolkien diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index e61434ba9a..e71293ad9b 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -1,9 +1,50 @@ require "test_helper" -# describe Vote do -# let(:vote) { votes(:vote1) } -# -# it "must be valid" do -# value(vote).must_be :valid? -# end -# end +describe Vote do + let(:vote) { votes(:vote1) } + + it "must not be valid when created without user_id and work_id" do + value(vote.valid?).must_equal true + end + + it 'has required fields' do + fields = [:user_id, :work_id] + + fields.each do |field| + expect(vote).must_respond_to field + end + end + + describe 'Validations' do + + it 'must not be valid when created without a work_id' do + #arrange + vote.work_id = nil + + #act + valid = vote.save + + #assert + expect(valid).must_equal false + end + + it 'must not be valid when created without a user_id' do + #arrange + vote.user_id = nil + + #act + valid = vote.save + + #assert + expect(valid).must_equal false + end + + end + +end + + +# belongs_to :user +# belongs_to :work +# #vote can only be created on the same work by the same user once aka #each user can only upvote a work once +# validates :user_id, uniqueness: {scope: :work_id, message:"You can only vote once"} diff --git a/test/models/work_test.rb b/test/models/work_test.rb index c9c88a67d2..7687fe8c16 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -1,8 +1,8 @@ require "test_helper" require "pry" describe Work do - let(:work) { works(:harry_potter) } - let(:work2) { works(:lord_of_the_rings) } + let(:work) { works(:work1) } + let(:work2) { works(:work2) } From 06e3ad5d0cb7edfe94f7bac4ea29a2b851d3497b Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 14:29:37 -0700 Subject: [PATCH 37/59] added validation for votes each user can only vote for a work once fixed bug --- test/models/vote_test.rb | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index e71293ad9b..17fd6edfe1 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -1,7 +1,9 @@ require "test_helper" - +require 'pry' describe Vote do let(:vote) { votes(:vote1) } + let(:vote2) { votes(:vote2) } + it "must not be valid when created without user_id and work_id" do value(vote.valid?).must_equal true @@ -39,12 +41,22 @@ expect(valid).must_equal false end - end + it 'must be associated with only one user_id (aka each user can only vote on a work once)' do + #arrange + vote.user_id = "user1" + + #act + vote.save + vote.user_id = "user1" + valid = vote.save + #assert + expect(valid).must_equal false + + end + + end end -# belongs_to :user -# belongs_to :work -# #vote can only be created on the same work by the same user once aka #each user can only upvote a work once # validates :user_id, uniqueness: {scope: :work_id, message:"You can only vote once"} From 33d94b4f3c38a9b28a68a13ac2a9aecc925361ec Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 14:37:31 -0700 Subject: [PATCH 38/59] wrote the code for upvote method --- app/controllers/works_controller.rb | 15 ++++++--------- config/routes.rb | 1 + 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 57b52eb8cc..ff0ce01e41 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -56,15 +56,12 @@ def destroy end def upvote - #how do i figure out if a user has voted on a piece? - #i need to put in a validation to make sure a vote can only be created on the same work by the same user once - # vote = Vote.new - #if vote.save - # upvote = Vote.new - # flash message success! - #else - # user has already voted for this work - # flash message could not upvote + vote = Vote.new + if vote.save + flash[:success] = "Successfully upvoted" + else + flash[:error] = "A problem occurred: Could not upvote" + end end private diff --git a/config/routes.rb b/config/routes.rb index 3d747e7dda..59129ed8b8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,6 +1,7 @@ Rails.application.routes.draw do root "main#index" + post 'works/:id/upvote', to: 'works#upvote', as: 'upvote' post 'sessions/login', to: 'sessions#login', as: 'login' #or just user login? get 'sessions/login', to: 'sessions#new' delete 'sessions/destroy', to: 'sessions#destroy', as: 'logout' From efed123ee120d231b9ddf16dd8f4afb3cfd62564 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 14:50:13 -0700 Subject: [PATCH 39/59] corrected upvote links in works index --- app/views/works/index.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 43bdcace8f..7f798b5124 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -20,7 +20,7 @@ <%= link_to album.title, work_path(album.id) %> <%= album.creator %> <%= album.publication_year %> - Upvote + <%= link_to "Upvote", work_path(album.id), method: :post, class:"btn btn-primary" %> <% end %> @@ -44,7 +44,7 @@ <%= link_to book.title, work_path(book.id) %> <%= book.creator %> <%= book.publication_year %> - Upvote + <%= link_to "Upvote", work_path(book.id), method: :post, class:"btn btn-primary" %> <% end %> @@ -70,7 +70,7 @@ <%= link_to movie.title, work_path(movie.id) %> <%= movie.creator %> <%= movie.publication_year %> - Upvote + <%= link_to "Upvote", work_path(movie.id), method: :post, class:"btn btn-primary" %> <% end %> From f188d45bdc66add7f33fab08b5a33df5764adb9b Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 15:49:20 -0700 Subject: [PATCH 40/59] fixed upvote routes --- app/controllers/sessions_controller.rb | 3 +-- app/controllers/users_controller.rb | 2 +- app/views/works/index.html.erb | 6 +++--- config/routes.rb | 6 ++---- 4 files changed, 7 insertions(+), 10 deletions(-) diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 338233c634..8b996b9e21 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -13,12 +13,11 @@ def login # raise redirect_to root_path else - flash[:warning] = "no" + flash[:warning] = "Not able to log in " redirect_to root_path end else - raise session[:user_id] = user.id #where does :user_id come from? flash[:success] = "#{user.name} Successfully logged in!" redirect_to root_path diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 6b1b87c7f9..07300886f6 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,7 +1,7 @@ class UsersController < ApplicationController def index @users = User.order(:name) - + @current_user = User.find_by(id: session[:user_id]) #this finds the current user in the session so i can display it where i need diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 7f798b5124..7e263f591c 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -20,7 +20,7 @@ <%= link_to album.title, work_path(album.id) %> <%= album.creator %> <%= album.publication_year %> - <%= link_to "Upvote", work_path(album.id), method: :post, class:"btn btn-primary" %> + <%= link_to "Upvote", work_votes_path(album.id), method: :post, class:"btn btn-primary" %> <% end %> @@ -44,7 +44,7 @@ <%= link_to book.title, work_path(book.id) %> <%= book.creator %> <%= book.publication_year %> - <%= link_to "Upvote", work_path(book.id), method: :post, class:"btn btn-primary" %> + <%= link_to "Upvote", work_votes_path(book.id), method: :post, class:"btn btn-primary" %> <% end %> @@ -67,7 +67,7 @@ 888 - <%= link_to movie.title, work_path(movie.id) %> + <%= link_to movie.title, work_votes_path(movie.id) %> <%= movie.creator %> <%= movie.publication_year %> <%= link_to "Upvote", work_path(movie.id), method: :post, class:"btn btn-primary" %> diff --git a/config/routes.rb b/config/routes.rb index 59129ed8b8..b22ca3db5b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,16 +1,14 @@ Rails.application.routes.draw do root "main#index" - post 'works/:id/upvote', to: 'works#upvote', as: 'upvote' + post 'works/:id/upvote', to: 'works#upvote', as: 'work_votes' post 'sessions/login', to: 'sessions#login', as: 'login' #or just user login? get 'sessions/login', to: 'sessions#new' delete 'sessions/destroy', to: 'sessions#destroy', as: 'logout' get 'main/index', as: 'main' - resources :works - - resources :users + resources :works, :users From f04c791e68b227a2abf87b24149535c8e485d532 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 18:49:10 -0700 Subject: [PATCH 41/59] save #upvote in works controller --- app/controllers/works_controller.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index ff0ce01e41..0a74085426 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -57,11 +57,16 @@ def destroy def upvote vote = Vote.new + work = Work.find_by(id: params[:id].to_i) + user = User.find_by(id: params[:id].to_i) + vote.work_id = work.id + vote.user_id = user.id + if vote.save flash[:success] = "Successfully upvoted" else flash[:error] = "A problem occurred: Could not upvote" - end + end end private From 69b28ee3a822de4304cea2497d2d323bab0a6710 Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 18:56:25 -0700 Subject: [PATCH 42/59] added vote validation test --- test/models/vote_test.rb | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index 17fd6edfe1..acd40cbe10 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -55,6 +55,20 @@ end + it 'must be associated with only one work_id ' do + #arrange + vote.work_id = "work1" + + #act + vote.save + vote.work_id = "work1" + valid = vote.save + + #assert + expect(valid).must_equal false + + end + end end From 228d4457956e6fae6d00901ca1b2244839159efe Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 19:04:49 -0700 Subject: [PATCH 43/59] updated view for works --- app/models/work.rb | 4 ++-- app/views/works/index.html.erb | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/models/work.rb b/app/models/work.rb index 8c347fdaa4..a09de8bffc 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -18,6 +18,6 @@ def self.movies return Work.where(category: 'movie') end -#make sure it returns an instance of Array -#make sure each category inside is + + end diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 7e263f591c..ed5dc67711 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -16,7 +16,7 @@ <% @albums.each do |album| %> - 888 + <%= album.votes.count %> <%= link_to album.title, work_path(album.id) %> <%= album.creator %> <%= album.publication_year %> @@ -40,7 +40,7 @@ <% @books.each do |book| %> - 888 + <%= book.votes.count %> <%= link_to book.title, work_path(book.id) %> <%= book.creator %> <%= book.publication_year %> @@ -66,7 +66,7 @@ <% @movies.each do |movie| %> - 888 + <%= movies.votes.count %> <%= link_to movie.title, work_votes_path(movie.id) %> <%= movie.creator %> <%= movie.publication_year %> From cabc35a07f3702a40ba68ad04832fa2dafd1d35f Mon Sep 17 00:00:00 2001 From: Kay Date: Fri, 12 Oct 2018 20:07:18 -0700 Subject: [PATCH 44/59] fixed bug so taht upvote can happen --- app/controllers/application_controller.rb | 8 ++++++++ app/controllers/users_controller.rb | 15 +++++++++++++-- app/controllers/works_controller.rb | 9 ++++++--- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 09705d12ab..8d8a4051ec 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,2 +1,10 @@ class ApplicationController < ActionController::Base + + # private + + # def find_user + # @current_user = User.find_by(id: session[:user_id]) + #this finds the current user in the session so i can display it where i need + # end + end diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 07300886f6..e2fba16239 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,8 +3,7 @@ def index @users = User.order(:name) @current_user = User.find_by(id: session[:user_id]) - #this finds the current user in the session so i can display it where i need - + end def show @@ -20,6 +19,13 @@ def new end def create + @user = User.new(user_params) + + if @user.save + redirect_to users_path(@user_id) + else + render :new + end end def edit @@ -32,5 +38,10 @@ def update def destroy end + private + def user_params + return params.require(:user).permit(:name) #this data is coming from the form + end + end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 0a74085426..6ebc1d1d47 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -6,6 +6,9 @@ def index @books = Work.books @movies = Work.movies + @current_user = User.find_by(id: session[:user_id]) + + end def show @@ -58,20 +61,20 @@ def destroy def upvote vote = Vote.new work = Work.find_by(id: params[:id].to_i) - user = User.find_by(id: params[:id].to_i) vote.work_id = work.id - vote.user_id = user.id + vote.user_id = User.find_by(id: session[:user_id]).id #change this up to @current_user variable later if vote.save flash[:success] = "Successfully upvoted" else flash[:error] = "A problem occurred: Could not upvote" end + redirect_to works_path end private def work_params - return params.require(:work).permit(:title, :creator, :description, :category, :publication_year) #this data is coming from the form + return params.require(:work).permit(:id, :title, :creator, :description, :category, :publication_year) #this data is coming from the form end From fcde1a438cf874582ef6460dfc1a6a98376e4a9f Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 13 Oct 2018 15:10:54 -0700 Subject: [PATCH 45/59] searched a way to order the results desc/asc by numer of items returned from Works child model Votes..pretty sure its working --- app/models/work.rb | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) diff --git a/app/models/work.rb b/app/models/work.rb index a09de8bffc..decfe0c895 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -7,7 +7,11 @@ class Work < ApplicationRecord def self.albums - return Work.where(category: 'album') + # Company.joins(:jobs).group("companies.id").order("count(companies.id) DESC") + # return Work.where(category: 'album') # Work is the table + # return Work.joins(:votes).group("works.id").order("count(works.id) DESC") + return Work.joins(:votes).group('works.id').order('count(works.id) DESC', 'title ASC') + #i want to return a list of albums that are sorted by vote count and title end def self.books @@ -18,6 +22,26 @@ def self.movies return Work.where(category: 'movie') end - + def sort_by_vote_count + #sort any list of work objects by title and votes + #first by votes + #second priority by title + return Work.order(votes: :desc, title: :asc) + #how can i call this method on the albums and books lists? + end + end + +# def sort_by_vote_count +# #this method sorts vote count in descending order & alphabetically +# #it needs to be in alphabetical order by vote count (by multiple conditions) +# # sort_by { |obj| block } → array +# # users.sort_by { |user| [user.age, user.name] } +# # ary.sort_by {|name, age| [age, name] } +# #list = self.sort #sorts alphabetically from a-z +# #then sort by vote number +# #check how many votes each work has +# #sort from greatest to smallest +# return list.sort_by {|work| [work.votes.count, work.title]} +# end From f75bd42b8940ccbb18121c7a459b0f15979ba54c Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 13 Oct 2018 16:28:11 -0700 Subject: [PATCH 46/59] added :counter_cache option to make it easier to find the number of votes belonging to works, did this by creating new migration to add votes_count column and added counter_cache option in votes belongs to relationship --- app/models/vote.rb | 7 ++++++- app/models/work.rb | 12 ++++++++++-- .../20181013231749_add_votes_count_to_work_model.rb | 5 +++++ db/schema.rb | 3 ++- 4 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20181013231749_add_votes_count_to_work_model.rb diff --git a/app/models/vote.rb b/app/models/vote.rb index 1a6651a6ad..aca9731607 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,10 +1,15 @@ class Vote < ApplicationRecord belongs_to :user - belongs_to :work + belongs_to :work, counter_cache: true #vote can only be created on the same work by the same user once aka #each user can only upvote a work once validates :user_id, uniqueness: {scope: :work_id, message:"You can only vote once"} #validates :user_id, uniqueness: true <- this is what i had before just leave as reference + + + + #user must be logged in to vote + end diff --git a/app/models/work.rb b/app/models/work.rb index decfe0c895..c776657f78 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -5,12 +5,20 @@ class Work < ApplicationRecord has_many :users, through: :votes has_many :votes + #this one is working to sort by title + default_scope {order(title: :asc)} + + + # , -> {group 'works.id'} default_scope {order(title: :desc)} + # default_scope where(color: 'red')`, please use `default_scope { where(color: 'red') } + # has_many :votes, -> {group 'works.id'}.order('count(works.id) DESC', 'title ASC') + def self.albums # Company.joins(:jobs).group("companies.id").order("count(companies.id) DESC") - # return Work.where(category: 'album') # Work is the table + return Work.where(category: 'album') # Work is the table # return Work.joins(:votes).group("works.id").order("count(works.id) DESC") - return Work.joins(:votes).group('works.id').order('count(works.id) DESC', 'title ASC') + # return Work.joins(:votes).group('works.id').order('count(works.id) DESC', 'title ASC') #i want to return a list of albums that are sorted by vote count and title end diff --git a/db/migrate/20181013231749_add_votes_count_to_work_model.rb b/db/migrate/20181013231749_add_votes_count_to_work_model.rb new file mode 100644 index 0000000000..0c05cd1296 --- /dev/null +++ b/db/migrate/20181013231749_add_votes_count_to_work_model.rb @@ -0,0 +1,5 @@ +class AddVotesCountToWorkModel < ActiveRecord::Migration[5.2] + def change + add_column :works, :votes_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index eb36255521..43a7439530 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_10_11_052623) do +ActiveRecord::Schema.define(version: 2018_10_13_231749) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -38,6 +38,7 @@ t.datetime "updated_at", null: false t.string "category" t.integer "publication_year" + t.integer "votes_count" end add_foreign_key "votes", "users" From 1a5878acb8b2d53e9793098b433d745e1cb48ab4 Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 13 Oct 2018 18:31:39 -0700 Subject: [PATCH 47/59] wrapped SQL strings in Arel.sql calls so that its not a dangerous query method --- app/models/vote.rb | 8 -------- app/models/work.rb | 41 +++-------------------------------------- 2 files changed, 3 insertions(+), 46 deletions(-) diff --git a/app/models/vote.rb b/app/models/vote.rb index aca9731607..1aa47770a2 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -4,12 +4,4 @@ class Vote < ApplicationRecord #vote can only be created on the same work by the same user once aka #each user can only upvote a work once validates :user_id, uniqueness: {scope: :work_id, message:"You can only vote once"} #validates :user_id, uniqueness: true <- this is what i had before just leave as reference - - - - - - #user must be logged in to vote - - end diff --git a/app/models/work.rb b/app/models/work.rb index c776657f78..f6cb62cfbc 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -5,51 +5,16 @@ class Work < ApplicationRecord has_many :users, through: :votes has_many :votes - #this one is working to sort by title - default_scope {order(title: :asc)} - - - # , -> {group 'works.id'} default_scope {order(title: :desc)} - # default_scope where(color: 'red')`, please use `default_scope { where(color: 'red') } - # has_many :votes, -> {group 'works.id'}.order('count(works.id) DESC', 'title ASC') - - def self.albums - # Company.joins(:jobs).group("companies.id").order("count(companies.id) DESC") - return Work.where(category: 'album') # Work is the table - # return Work.joins(:votes).group("works.id").order("count(works.id) DESC") - # return Work.joins(:votes).group('works.id').order('count(works.id) DESC', 'title ASC') - #i want to return a list of albums that are sorted by vote count and title + return Work.where(category: 'album').order(Arel.sql('votes_count IS NULL, votes_count DESC'), title: :asc) end def self.books - return Work.where(category: 'book') + return Work.where(category: 'book').order(Arel.sql('votes_count IS NULL, votes_count DESC'), title: :asc) end def self.movies - return Work.where(category: 'movie') + return Work.where(category: 'movie').order(Arel.sql('votes_count IS NULL, votes_count DESC'), title: :asc) end - def sort_by_vote_count - #sort any list of work objects by title and votes - #first by votes - #second priority by title - return Work.order(votes: :desc, title: :asc) - #how can i call this method on the albums and books lists? - end - - end - -# def sort_by_vote_count -# #this method sorts vote count in descending order & alphabetically -# #it needs to be in alphabetical order by vote count (by multiple conditions) -# # sort_by { |obj| block } → array -# # users.sort_by { |user| [user.age, user.name] } -# # ary.sort_by {|name, age| [age, name] } -# #list = self.sort #sorts alphabetically from a-z -# #then sort by vote number -# #check how many votes each work has -# #sort from greatest to smallest -# return list.sort_by {|work| [work.votes.count, work.title]} -# end From a90074520a3a99d8fc29d505f73d23571a2adc5a Mon Sep 17 00:00:00 2001 From: Kay Date: Sat, 13 Oct 2018 19:00:43 -0700 Subject: [PATCH 48/59] fixed link failure in work index --- app/views/works/index.html.erb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index ed5dc67711..7f0092c81a 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -66,11 +66,11 @@ <% @movies.each do |movie| %> - <%= movies.votes.count %> - <%= link_to movie.title, work_votes_path(movie.id) %> + <%= movie.votes.count %> + <%= link_to movie.title, work_path(movie.id) %> <%= movie.creator %> <%= movie.publication_year %> - <%= link_to "Upvote", work_path(movie.id), method: :post, class:"btn btn-primary" %> + <%= link_to "Upvote", work_votes_path(movie.id), method: :post, class:"btn btn-primary" %> <% end %> From 0db6ddf1f31bc2b25f0c5c60b032300afbac8c4d Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 14 Oct 2018 09:21:39 -0700 Subject: [PATCH 49/59] removed nested route and edited works show page adding link to each user who has voted on this work --- app/views/works/show.html.erb | 20 ++++++++++++-------- config/routes.rb | 4 ++++ 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 301e8c0cde..10d8374479 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -3,15 +3,15 @@
      -
    • Created by: <%@work.creator%>
    • -
    • Published: <%@work.publication_year%>
    • -
    • Description: <%@work.description%>
    • +
    • Created by: <%=@work.creator%>
    • +
    • Published: <%=@work.publication_year%>
    • +
    • Description: <%=@work.description%>
    <%= button_to "Back to media ranks", root_path, class: "alt-button", method: :get %> <%= button_to "Edit", edit_work_path(@work.id), class: "alt-button", method: :get %> -
    upvote
    <%# insert button to upvote here%> +
    <%# insert button to upvote here%> <%= button_to "Delete", work_path(@work.id), method: :delete, data: { confirm: 'Are you sure?'}, class: "alt-button" %>

    <%= %> (insert work.total_votes here) votes for this <%= @work.category%>

    @@ -24,11 +24,15 @@ Date - <%# @works.each do |work| %> +

    by <%# link_to "#{@book.author.name}", author_path(@book.author.id) %>

    +<%# find the users who voted on this work %> +<%# i'm trying to get to users thru work but must go thru votes first %> + + <% @work.votes.each do |vote| %> - <%= link_to @work.title, work_path(@work.id) %> - <%# update this when i have votes link_to "#{work.vote.date}", work_path(work.id) %> + <%= link_to vote.user.name, user_path(vote.user.id) %> + <%= vote.created_at.to_formatted_s(:long) %> - <%# end %> + <% end %>
diff --git a/config/routes.rb b/config/routes.rb index b22ca3db5b..81b4cdf153 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -10,6 +10,10 @@ resources :works, :users + # resources :works do + # resources :users, only: [:show] + # end + end From 09744185fe9a2756f1ca75957b359da46982cdcd Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 14 Oct 2018 09:30:10 -0700 Subject: [PATCH 50/59] edited the upvote button on work show so it is linked now --- app/views/works/show.html.erb | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 10d8374479..7033fbff18 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -9,11 +9,11 @@ - <%= button_to "Back to media ranks", root_path, class: "alt-button", method: :get %> - <%= button_to "Edit", edit_work_path(@work.id), class: "alt-button", method: :get %> -
<%# insert button to upvote here%> - <%= button_to "Delete", work_path(@work.id), method: :delete, data: { confirm: 'Are you sure?'}, class: "alt-button" %> -

<%= %> (insert work.total_votes here) votes for this <%= @work.category%>

+ <%= button_to "Back to media ranks", root_path, method: :get, class:"btn btn-primary" %> + <%= button_to "Edit", edit_work_path(@work.id), method: :get, class:"btn btn-primary" %> + <%= button_to "Upvote", work_votes_path(@work.id), method: :post, class:"btn btn-primary" %> + <%= button_to "Delete", work_path(@work.id), method: :delete, data: { confirm: 'Are you sure?'}, class: "btn btn-primary" %> +

<%= @work.votes_count%> votes for this <%= @work.category%>

@@ -24,10 +24,6 @@ Date -

by <%# link_to "#{@book.author.name}", author_path(@book.author.id) %>

-<%# find the users who voted on this work %> -<%# i'm trying to get to users thru work but must go thru votes first %> - <% @work.votes.each do |vote| %> <%= link_to vote.user.name, user_path(vote.user.id) %> From 094f14a1d124483995ffad329ccd66e7d30c11ff Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 14 Oct 2018 09:39:15 -0700 Subject: [PATCH 51/59] edited #upvote in works controller so that when upvote successful redirects back to the same back --- app/controllers/works_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 6ebc1d1d47..41407116c2 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -69,7 +69,7 @@ def upvote else flash[:error] = "A problem occurred: Could not upvote" end - redirect_to works_path + redirect_back fallback_location: root_path end private From b8dd145a4e9a7d7e33a11247e2df1f35ceb49ac4 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 14 Oct 2018 21:38:00 -0700 Subject: [PATCH 52/59] edited main page view to present top 10 of each media category --- app/controllers/main_controller.rb | 2 + app/views/main/index.html.erb | 99 ++++++++++++++++++++++++++---- 2 files changed, 88 insertions(+), 13 deletions(-) diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb index 88181c281f..16ce5d2191 100644 --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -1,4 +1,6 @@ class MainController < ApplicationController def index + @works = Work.order(:title) + end end diff --git a/app/views/main/index.html.erb b/app/views/main/index.html.erb index db11873acd..36f9837cc6 100644 --- a/app/views/main/index.html.erb +++ b/app/views/main/index.html.erb @@ -1,15 +1,88 @@ -

Main#index

-

Find me in app/views/main/index.html.erb

- -

Media Spotlight: top media right now

-
    -
  • - Top Movies -
  • -
  • - Top Books -
  • -
  • +
    + + +
    + +
    + +
    +

    Top Albums -

  • + +
      +
    • +

      + <% 10.times do |i|%> + <% if @works.albums[i] == nil %> + <%# break %> + <% else %> + <%= @works.albums[i].title %> + by <%= @works.albums[i].creator %> +
      + <% end %> + <%= @works.albums[i].votes.size %> votes +
      + <% end %> +

      +
    • +
    +
+ + + + + + + +
+

+ Top Books +

+
    +
  • +

    + <% 10.times do |i|%> + <% if @works.books[i] == nil %> + <% break %> + <% else %> + <%= @works.books[i].title %> + by <%= @works.books[i].creator %> +
    + <% end %> + <%= @works.books[i].votes.size %> votes +
    + <% end %> +

    +
  • +
+
+ + + + +
+

+ Top Movies +

+
    +
  • +

    + <% 10.times do |i|%> + <% if @works.movies[i] == nil %> + <% break %> + <% else %> + <%= @works.movies[i].title %> + by <%= @works.movies[i].creator %> +
    + <% end %> + <%= @works.movies[i].votes.size %> votes +
    + <% end %> +

    +
+
+ +
+ + From e4106b03fc4eb0992e831c2a033bb1a2a7d379f4 Mon Sep 17 00:00:00 2001 From: Kay Date: Sun, 14 Oct 2018 22:08:11 -0700 Subject: [PATCH 53/59] finished the view for user show and index pages --- app/views/users/index.html.erb | 32 ++++++++++++++++---------------- app/views/users/show.html.erb | 9 +++++++++ 2 files changed, 25 insertions(+), 16 deletions(-) diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index d4d4e46706..c28c913fa7 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,20 +1,20 @@ -

List of Users

- -
- - - +
+

List of Users

+
+ + + + + + + <% @users.each do |user| %> - - - - - <% end %> - + + + <% end %> +
Username
Username
<%= link_to user.name, user_path(user.id) %>
<%= link_to user.name, user_path(user.id) %>
- - - - Back to Media List + + diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 22c3986b7c..8ba7072e7f 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -7,6 +7,7 @@ + <% @user.votes.count.times do |i| %> @@ -15,6 +16,14 @@ + + + + + + + + <% end %>
Media Title Created By Published
<%= @user.votes[i].work.title %><%= @user.votes[i].work.creator %><%= @user.votes[i].work.publication_year %><%= @user.votes[i].work.category %><%= @user.votes[i].created_at %>
From 47e371f1b9ec332e508afa61b6252b30301ed6ff Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 16 Oct 2018 11:57:13 -0700 Subject: [PATCH 54/59] added Relationships describe block to vote test --- test/models/vote_test.rb | 45 +++++++++++++++++++++------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index acd40cbe10..ef3c76f71e 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -5,8 +5,8 @@ let(:vote2) { votes(:vote2) } - it "must not be valid when created without user_id and work_id" do - value(vote.valid?).must_equal true + it "must be valid" do + value(vote).must_be :valid? end it 'has required fields' do @@ -41,32 +41,35 @@ expect(valid).must_equal false end - it 'must be associated with only one user_id (aka each user can only vote on a work once)' do - #arrange - vote.user_id = "user1" + describe 'Relationships' do - #act - vote.save - vote.user_id = "user1" - valid = vote.save + it 'must be associated with only one user_id (aka each user can only vote on a work once)' do + #arrange + vote.user_id = "user1" - #assert - expect(valid).must_equal false + #act + vote.save + vote.user_id = "user1" + valid = vote.save - end + #assert + expect(valid).must_equal false - it 'must be associated with only one work_id ' do - #arrange - vote.work_id = "work1" + end - #act - vote.save - vote.work_id = "work1" - valid = vote.save + it 'must be associated with only one work_id ' do + #arrange + vote.work_id = "work1" + + #act + vote.save + vote.work_id = "work1" + valid = vote.save - #assert - expect(valid).must_equal false + #assert + expect(valid).must_equal false + end end end From 36ccdd4819a2fd4d498d59421f4dd00b88026404 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 16 Oct 2018 12:21:13 -0700 Subject: [PATCH 55/59] added relationship tests for user --- test/models/user_test.rb | 47 ++++++++++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 7 deletions(-) diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 0768f790aa..c67a5c8f0a 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,9 +1,42 @@ require "test_helper" -# describe User do -# let(:user) { User.new } -# -# it "must be valid" do -# value(user).must_be :valid? -# end -# end +describe User do + let(:user) { users(:user1) } + + it "must be valid" do + value(user).must_be :valid? + end + + + describe 'Validations' do + + it 'must not be valid when created without a name' do + #arrange + user = User.new + + #act + valid = user.save + + #assert + expect(valid).must_equal false + end + end + + describe 'Relationships' do + it 'has a list of votes' do + user.must_respond_to :votes + + user.votes.each do |vote| + vote.must_be_kind_of Vote + end + end + + it 'has a list of works through votes' do + user.votes.each do |vote| + vote.must_respond_to :work_id + end + end + + + end +end From f4d982db3e12b7ee575627c47b08e6a6d0c076dc Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 16 Oct 2018 23:17:48 -0700 Subject: [PATCH 56/59] added reltionship testing for works --- test/fixtures/votes.yml | 17 ++++++++++++++++- test/fixtures/works.yml | 32 ++++++++++++++++++++++++++++++-- test/models/vote_test.rb | 2 +- test/models/work_test.rb | 22 +++++++++++++++++++++- 4 files changed, 68 insertions(+), 5 deletions(-) diff --git a/test/fixtures/votes.yml b/test/fixtures/votes.yml index 58ac3c465c..18057b65b5 100644 --- a/test/fixtures/votes.yml +++ b/test/fixtures/votes.yml @@ -11,4 +11,19 @@ vote1: vote2: user: user2 work: work2 -# column: value + +vote3: + user: user3 + work: work1 + +vote4: + user: user4 + work: work1 + +vote5: + user: user4 + work: work3 + +vote6: + user: user4 + work: work3 diff --git a/test/fixtures/works.yml b/test/fixtures/works.yml index 42ccdafb3e..70e5bba308 100644 --- a/test/fixtures/works.yml +++ b/test/fixtures/works.yml @@ -8,8 +8,36 @@ work1: category: book work2: - title: Lord of The Rings + title: book2 + publication_year: 1999 + creator: JK Rowling + description: A book about magic the second + category: book + +work3: + title: album1 publication_year: 1954 creator: JRR Tolkien description: a book about magic - category: book + category: album + +work4: + title: album2 + publication_year: 1954 + creator: JRR Tolkien + description: a book about magic and more + category: album + +work5: + title: movie1 + publication_year: 1954 + creator: JRR Tolkien + description: a book about magic and more + category: movie + +work6: + title: movie2 + publication_year: 1954 + creator: JRR Tolkien + description: a book about magic and more + category: movie diff --git a/test/models/vote_test.rb b/test/models/vote_test.rb index ef3c76f71e..2627e63099 100644 --- a/test/models/vote_test.rb +++ b/test/models/vote_test.rb @@ -60,7 +60,7 @@ it 'must be associated with only one work_id ' do #arrange vote.work_id = "work1" - + #act vote.save vote.work_id = "work1" diff --git a/test/models/work_test.rb b/test/models/work_test.rb index 7687fe8c16..88be4f9a91 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -51,7 +51,7 @@ valid = work2.save #assert - expect(valid).must_equal false + expect(work2.save).must_equal false end end @@ -67,6 +67,26 @@ end end + it 'returns an array of albums ordered by vote count in descending order' do + albums_array = Work.albums #array of works that are albums + work1 = albums_array[0] + work2 = albums_array[1] + + #act /assert + expect(work1.votes.count > work2.votes.count).must_equal true + end + + it 'returns an array of albums ordered by title in ascending order' do + albums_array = Work.albums #array of works that are albums + albums_array2 = Work.albums.sort + + compare = albums_array.zip(albums_array2).map {|album, album2| album == album2} + #returns an array of 'true' values if album and album2 are identical + + #assert + expect(compare.all?).must_equal true + end + it 'returns an array of books' do books_array = Work.books #array of works that are albums From 15a9979272ea89b2beb2cb28ffa72389eb05970e Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 16 Oct 2018 23:21:59 -0700 Subject: [PATCH 57/59] updated relationshps tests in works --- test/models/work_test.rb | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/test/models/work_test.rb b/test/models/work_test.rb index 88be4f9a91..b0a84c57b8 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -83,7 +83,7 @@ compare = albums_array.zip(albums_array2).map {|album, album2| album == album2} #returns an array of 'true' values if album and album2 are identical - #assert + #assert expect(compare.all?).must_equal true end @@ -109,6 +109,19 @@ end describe 'Relationships' do + it 'has a list of votes' do + work.must_respond_to :votes + + work.votes.each do |vote| + vote.must_be_kind_of Vote + end + end + + it 'has a list of users through votes' do + work.votes.each do |vote| + vote.must_respond_to :user_id + end + end end From 040a81f222f579046ec7403058cde4bfb84db6f5 Mon Sep 17 00:00:00 2001 From: Kay Date: Tue, 16 Oct 2018 23:23:13 -0700 Subject: [PATCH 58/59] deleted unneeded messages --- test/models/work_test.rb | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test/models/work_test.rb b/test/models/work_test.rb index b0a84c57b8..ce818572dc 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -125,12 +125,6 @@ end - # def self.movies - # return Work.where(category: 'movie') - # end - - #make sure it returns an instance of Array - #make sure each category inside is end From 1be552012594b49a08a96430647d61cd00af5e21 Mon Sep 17 00:00:00 2001 From: Kay Date: Wed, 17 Oct 2018 09:01:25 -0700 Subject: [PATCH 59/59] added one more test to works --- test/fixtures/users.yml | 12 ++++++++++++ test/fixtures/votes.yml | 18 +++++++++++++++++- test/models/work_test.rb | 18 ++++++++++++++++++ 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml index 1d09ae85ce..05c76b4202 100644 --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -5,3 +5,15 @@ user1: user2: name: Cody + +user3: + name: Cake + +user4: + name: Cheddar + +user5: + name: Ham + +user6: + name: Cheese diff --git a/test/fixtures/votes.yml b/test/fixtures/votes.yml index 18057b65b5..9bafe87bd9 100644 --- a/test/fixtures/votes.yml +++ b/test/fixtures/votes.yml @@ -26,4 +26,20 @@ vote5: vote6: user: user4 - work: work3 + work: work5 + +vote7: + user: user1 + work: work5 + +vote8: + user: user5 + work: work6 + +vote9: + user: user6 + work: work6 + +vote10: + user: user3 + user: work5 diff --git a/test/models/work_test.rb b/test/models/work_test.rb index ce818572dc..764a633d33 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -96,6 +96,14 @@ end end + it 'returns an array of books ordered by vote count in descending order' do + books_array = Work.books #array of works that are albums + book1 = books_array[0] + book2 = books_array[1] + #act /assert + expect(book1.votes.count > book2.votes.count).must_equal true + end + it 'returns an array of movies' do movies_array = Work.movies #array of works that are albums @@ -105,6 +113,16 @@ end end + # it 'returns an array of movies ordered by vote count in descending order' do + # movies_array = Work.movies #array of works that are albums + # movie1 = movies_array[0] + # movie2 = movies_array[1] + # binding.pry + # + # #act /assert + # expect(movie1.votes.count > movie2.votes.count).must_equal true + # end + end