From fd0d046c9720fcba2d7cf7c618fe34b41d04a09f Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 10 Oct 2017 14:23:43 -0700 Subject: [PATCH 01/26] Initial Rails Setup --- .gitignore | 19 ++ Gemfile | 66 ++++++ Gemfile.lock | 222 ++++++++++++++++++ README.md | 104 +------- Rakefile | 6 + app/assets/config/manifest.js | 3 + app/assets/images/.keep | 0 app/assets/javascripts/application.js | 15 ++ app/assets/javascripts/cable.js | 13 + app/assets/javascripts/channels/.keep | 0 app/assets/stylesheets/application.css | 15 ++ app/channels/application_cable/channel.rb | 4 + app/channels/application_cable/connection.rb | 4 + app/controllers/application_controller.rb | 3 + 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 | 14 ++ 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 | 38 +++ bin/spring | 17 ++ bin/update | 29 +++ bin/yarn | 11 + config.ru | 5 + config/application.rb | 25 ++ config/boot.rb | 3 + config/cable.yml | 10 + config/database.yml | 85 +++++++ config/environment.rb | 5 + config/environments/development.rb | 54 +++++ config/environments/production.rb | 91 +++++++ config/environments/test.rb | 42 ++++ .../application_controller_renderer.rb | 8 + config/initializers/assets.rb | 14 ++ config/initializers/backtrace_silencers.rb | 7 + 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 | 56 +++++ config/routes.rb | 3 + config/secrets.yml | 32 +++ config/spring.rb | 6 + 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 + 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 | 26 ++ tmp/.keep | 0 vendor/.keep | 0 76 files changed, 1304 insertions(+), 91 deletions(-) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock 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.css 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/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/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/secrets.yml create mode 100644 config/spring.rb 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 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..82701fedc8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,19 @@ +# 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 + +/node_modules +/yarn-error.log + +.byebug_history diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000000..88aee9dc96 --- /dev/null +++ b/Gemfile @@ -0,0 +1,66 @@ +source 'https://rubygems.org' + +git_source(:github) do |repo_name| + repo_name = "#{repo_name}/#{repo_name}" unless repo_name.include?("/") + "https://github.com/#{repo_name}.git" +end + + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '~> 5.1.4' +# Use postgresql as the database for Active Record +gem 'pg', '~> 0.18' +# Use Puma as the app server +gem 'puma', '~> 3.7' +# 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 'therubyracer', 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', '~> 3.0' +# Use ActiveModel has_secure_password +# gem 'bcrypt', '~> 3.1.7' + +# Use Capistrano for deployment +# gem 'capistrano-rails', group: :development + +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] + # Adds support for Capybara system testing and selenium driver + gem 'capybara', '~> 2.13' + gem 'selenium-webdriver' +end + +group :development do + # Access an IRB console on exception pages or by using <%= 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 + +# Windows does not include zoneinfo files, so bundle the tzinfo-data gem +gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] + +gem 'jquery-turbolinks' +group :development do + gem 'better_errors' + gem 'pry-rails' + gem 'binding_of_caller' +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..7314ff8d29 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,222 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (5.1.4) + actionpack (= 5.1.4) + nio4r (~> 2.0) + websocket-driver (~> 0.6.1) + actionmailer (5.1.4) + actionpack (= 5.1.4) + actionview (= 5.1.4) + activejob (= 5.1.4) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.1.4) + actionview (= 5.1.4) + activesupport (= 5.1.4) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.1.4) + activesupport (= 5.1.4) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.1.4) + activesupport (= 5.1.4) + globalid (>= 0.3.6) + activemodel (5.1.4) + activesupport (= 5.1.4) + activerecord (5.1.4) + activemodel (= 5.1.4) + activesupport (= 5.1.4) + arel (~> 8.0) + activesupport (5.1.4) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (~> 0.7) + minitest (~> 5.1) + tzinfo (~> 1.1) + addressable (2.5.2) + public_suffix (>= 2.0.2, < 4.0) + ansi (1.5.0) + arel (8.0.0) + better_errors (2.3.0) + coderay (>= 1.0.0) + erubi (>= 1.0.0) + rack (>= 0.9.0) + bindex (0.5.0) + binding_of_caller (0.7.2) + debug_inspector (>= 0.0.1) + builder (3.2.3) + byebug (9.1.0) + capybara (2.15.4) + addressable + mini_mime (>= 0.1.3) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) + childprocess (0.8.0) + ffi (~> 1.0, >= 1.0.11) + coderay (1.1.2) + concurrent-ruby (1.0.5) + crass (1.0.2) + debug_inspector (0.0.3) + erubi (1.7.0) + execjs (2.7.0) + ffi (1.9.18) + globalid (0.4.0) + activesupport (>= 4.2.0) + i18n (0.8.6) + jbuilder (2.7.0) + activesupport (>= 4.2.0) + multi_json (>= 1.2) + 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.1.1) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.6.6) + mime-types (>= 1.16, < 4) + method_source (0.9.0) + mime-types (3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2016.0521) + mini_mime (0.1.4) + mini_portile2 (2.3.0) + minitest (5.10.3) + minitest-rails (3.0.0) + minitest (~> 5.8) + railties (~> 5.0) + minitest-reporters (1.1.18) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + multi_json (1.12.2) + nio4r (2.1.0) + nokogiri (1.8.1) + mini_portile2 (~> 2.3.0) + pg (0.21.0) + pry (0.11.1) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.6) + pry (>= 0.10.4) + public_suffix (3.0.0) + puma (3.10.0) + rack (2.0.3) + rack-test (0.7.0) + rack (>= 1.0, < 3) + rails (5.1.4) + actioncable (= 5.1.4) + actionmailer (= 5.1.4) + actionpack (= 5.1.4) + actionview (= 5.1.4) + activejob (= 5.1.4) + activemodel (= 5.1.4) + activerecord (= 5.1.4) + activesupport (= 5.1.4) + bundler (>= 1.3.0) + railties (= 5.1.4) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.0.3) + loofah (~> 2.0) + railties (5.1.4) + actionpack (= 5.1.4) + activesupport (= 5.1.4) + method_source + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (12.1.0) + rb-fsevent (0.10.2) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + ruby-progressbar (1.9.0) + ruby_dep (1.5.0) + rubyzip (1.2.1) + sass (3.5.2) + 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.6) + 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.6.0) + childprocess (~> 0.5) + rubyzip (~> 1.0) + 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.1) + 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.0.1) + turbolinks-source (~> 5) + turbolinks-source (5.0.3) + tzinfo (1.2.3) + thread_safe (~> 0.1) + uglifier (3.2.0) + execjs (>= 0.3.0, < 3) + web-console (3.5.1) + actionview (>= 5.0) + activemodel (>= 5.0) + bindex (>= 0.4.0) + railties (>= 5.0) + websocket-driver (0.6.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.2) + xpath (2.1.0) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + better_errors + binding_of_caller + byebug + capybara (~> 2.13) + jbuilder (~> 2.5) + jquery-turbolinks + listen (>= 3.0.5, < 3.2) + minitest-rails + minitest-reporters + pg (~> 0.18) + pry-rails + puma (~> 3.7) + rails (~> 5.1.4) + 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) + +BUNDLED WITH + 1.16.0.pre.2 diff --git a/README.md b/README.md index 6d792f05ce..7db80e4ca1 100644 --- a/README.md +++ b/README.md @@ -1,102 +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 -## Learning Goals +* Configuration -The purpose of this assignment is to reinforce the following concepts: +* Database creation -- 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 -- Working with a CSS framework -- Styling a page based on an existing design +* Database initialization -## Before You Begin +* How to run the test suite -### Provided Files +* Services (job queues, cache servers, search engines, etc.) -- `db/media_seeds.csv`: Some starter media to work with -- `app/assets/images/owl.jpg`: The owl picture from the site +* Deployment instructions -### 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 - -Mimic the site's basic functionality around Media, without worrying (yet) about Users or Votes: -- Build a main page, with a list of the top 10 media of each category, as well as a spotlight section for the top media overall -- 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 -- Make sure any models and controllers you've built so far are well-tested - -### 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 -- Add a list of voting users to the details page for each media -- Don't allow a user to vote for the same media more than once -- Add a page for each user, as well as a page showing a summary of all users -- Make sure any models and controllers you've built so far are well-tested - -#### 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 - -Use Foundation 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. - -### Optional Enhancement Ideas - -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. +* ... 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..46b20359fa --- /dev/null +++ b/app/assets/javascripts/application.js @@ -0,0 +1,15 @@ +// 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 rails-ujs +//= 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.css b/app/assets/stylesheets/application.css new file mode 100644 index 0000000000..d05ea0f511 --- /dev/null +++ b/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * 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. + * + *= require_tree . + *= require_self + */ 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..1c07694e9d --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,3 @@ +class ApplicationController < ActionController::Base + protect_from_forgery with: :exception +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..6af511b830 --- /dev/null +++ b/app/views/layouts/application.html.erb @@ -0,0 +1,14 @@ + + + + MediaRanker + <%= csrf_meta_tags %> + + <%= 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..66e9889e8b --- /dev/null +++ b/bin/bundle @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) +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..78c4e861dc --- /dev/null +++ b/bin/setup @@ -0,0 +1,38 @@ +#!/usr/bin/env ruby +require 'pathname' +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) + +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..a8e4462f20 --- /dev/null +++ b/bin/update @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +require 'pathname' +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) + +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') + + 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..c2bacef836 --- /dev/null +++ b/bin/yarn @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +VENDOR_PATH = File.expand_path('..', __dir__) +Dir.chdir(VENDOR_PATH) do + begin + exec "yarnpkg #{ARGV.join(" ")}" + 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..e73bab908f --- /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.1 + + # Settings in config/environments/* take precedence over those specified here. + # Application configuration should go into files in config/initializers + # -- all .rb files in that directory are automatically loaded. + end +end diff --git a/config/boot.rb b/config/boot.rb new file mode 100644 index 0000000000..30f5120df6 --- /dev/null +++ b/config/boot.rb @@ -0,0 +1,3 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. diff --git a/config/cable.yml b/config/cable.yml new file mode 100644 index 0000000000..3a27f13933 --- /dev/null +++ b/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: async + +production: + adapter: redis + url: redis://localhost:6379/1 + channel_prefix: MediaRanker_production 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..5187e22186 --- /dev/null +++ b/config/environments/development.rb @@ -0,0 +1,54 @@ +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. + 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.seconds.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # 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 + + # 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..d242dac65c --- /dev/null +++ b/config/environments/production.rb @@ -0,0 +1,91 @@ +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 + + # Attempt to read encrypted secrets from `config/secrets.yml.enc`. + # Requires an encryption key in `ENV["RAILS_MASTER_KEY"]` or + # `config/secrets.yml.key`. + config.read_encrypted_secrets = 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 + + # 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..8e5cbde533 --- /dev/null +++ b/config/environments/test.rb @@ -0,0 +1,42 @@ +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.seconds.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 + 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/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..1e19380dcb --- /dev/null +++ b/config/puma.rb @@ -0,0 +1,56 @@ +# 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. If you use this option +# you need to make sure to reconnect any threads in the `on_worker_boot` +# block. +# +# preload_app! + +# If you are preloading your application and using Active Record, it's +# recommended that you close any connections to the database before workers +# are forked to prevent connection leakage. +# +# before_fork do +# ActiveRecord::Base.connection_pool.disconnect! if defined?(ActiveRecord) +# end + +# The code in the `on_worker_boot` will be called if you are using +# clustered mode by specifying a number of `workers`. After each worker +# process is booted, this block will be run. If you are using the `preload_app!` +# option, you will want to use this block to reconnect to any threads +# or connections that may have been created at application boot, as Ruby +# cannot share connections between processes. +# +# on_worker_boot do +# ActiveRecord::Base.establish_connection if defined?(ActiveRecord) +# end +# + +# 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/secrets.yml b/config/secrets.yml new file mode 100644 index 0000000000..73b7bc54da --- /dev/null +++ b/config/secrets.yml @@ -0,0 +1,32 @@ +# Be sure to restart your server when you modify this file. + +# Your secret key is used for verifying the integrity of signed cookies. +# If you change this key, all old signed cookies will become invalid! + +# Make sure the secret is at least 30 characters and all random, +# no regular words or you'll be exposed to dictionary attacks. +# You can use `rails secret` to generate a secure secret key. + +# Make sure the secrets in this file are kept private +# if you're sharing your code publicly. + +# Shared secrets are available across all environments. + +# shared: +# api_key: a1B2c3D4e5F6 + +# Environmental secrets are only available for that specific environment. + +development: + secret_key_base: 814e58d1b9e3686bb222af837028a065ab8e6c5c6ca01db2c2f5b2c08f77cbba7d62d2d0aa69d6ee8c71caa3ed4513e8d167570affcd9befd0c5822119e11507 + +test: + secret_key_base: f30cc8b54a4a88ae11ce2e61c8bf45e992449fa6e17b250a0f4adf9cc1fcfeacdfa30dd96e334975a6421c33c1ba5e153c74fd9d12e5c9675487202048871355 + +# Do not keep production secrets in the unencrypted secrets file. +# Instead, either read values from the environment. +# Or, use `bin/rails secrets:setup` to configure encrypted secrets +# and move the `production:` environment over there. + +production: + secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> diff --git a/config/spring.rb b/config/spring.rb new file mode 100644 index 0000000000..c9119b40c0 --- /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/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/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..10594a3248 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,26 @@ +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 03cb2acf9526ce92280c24b8a6ea8ae66f7f3667 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 10 Oct 2017 15:23:19 -0700 Subject: [PATCH 02/26] Set up Routes --- app/assets/javascripts/users.js | 2 ++ app/assets/javascripts/works.js | 2 ++ app/assets/stylesheets/users.scss | 3 +++ app/assets/stylesheets/works.scss | 3 +++ app/controllers/users_controller.rb | 4 ++++ app/controllers/works_controller.rb | 4 ++++ app/helpers/users_helper.rb | 2 ++ app/helpers/works_helper.rb | 2 ++ app/views/users/index.html.erb | 2 ++ app/views/works/index.html.erb | 2 ++ config/routes.rb | 17 +++++++++++++++++ test/controllers/users_controller_test.rb | 9 +++++++++ test/controllers/works_controller_test.rb | 9 +++++++++ 13 files changed, 61 insertions(+) create mode 100644 app/assets/javascripts/users.js create mode 100644 app/assets/javascripts/works.js create mode 100644 app/assets/stylesheets/users.scss create mode 100644 app/assets/stylesheets/works.scss create mode 100644 app/controllers/users_controller.rb create mode 100644 app/controllers/works_controller.rb create mode 100644 app/helpers/users_helper.rb create mode 100644 app/helpers/works_helper.rb create mode 100644 app/views/users/index.html.erb create mode 100644 app/views/works/index.html.erb create mode 100644 test/controllers/users_controller_test.rb create mode 100644 test/controllers/works_controller_test.rb 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/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/users.scss b/app/assets/stylesheets/users.scss new file mode 100644 index 0000000000..31a2eacb84 --- /dev/null +++ b/app/assets/stylesheets/users.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Users 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/users_controller.rb b/app/controllers/users_controller.rb new file mode 100644 index 0000000000..3004a55609 --- /dev/null +++ b/app/controllers/users_controller.rb @@ -0,0 +1,4 @@ +class UsersController < 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..0895e0bd10 --- /dev/null +++ b/app/controllers/works_controller.rb @@ -0,0 +1,4 @@ +class WorksController < ApplicationController + def index + end +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/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/views/users/index.html.erb b/app/views/users/index.html.erb new file mode 100644 index 0000000000..51968c88d2 --- /dev/null +++ b/app/views/users/index.html.erb @@ -0,0 +1,2 @@ +

Users#index

+

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

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

Works#index

+

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

diff --git a/config/routes.rb b/config/routes.rb index 787824f888..18f02f7228 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,20 @@ Rails.application.routes.draw do + get '/users/index' + + get '/users/new', to: 'users#new', as: 'new_user' + + get '/users/:id/edit', to: 'users#edit', as: 'edit_user' + + post '/users', to: 'users#create', as: 'users' + + patch '/users/:id', to: 'users#update' + + get '/users/:id', to: 'users#show', as: 'user' + + delete '/users/:id', to: 'users#destroy' + + + get 'works/index' + # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb new file mode 100644 index 0000000000..3523fbeeb8 --- /dev/null +++ b/test/controllers/users_controller_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe UsersController do + it "should get index" do + get users_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..6d781c6906 --- /dev/null +++ b/test/controllers/works_controller_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe WorksController do + it "should get index" do + get works_index_url + value(response).must_be :success? + end + +end From 1d23e4169936a8a883c96116519033e80dfb925f Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 10 Oct 2017 16:29:18 -0700 Subject: [PATCH 03/26] seed database --- app/models/work.rb | 2 ++ config/routes.rb | 12 +++++++++ db/migrate/20171010223806_create_works.rb | 13 ++++++++++ db/schema.rb | 28 +++++++++++++++++++++ db/{media_seeds.csv => seed_data/works.csv} | 2 +- db/seeds.rb | 22 ++++++++++++++++ test/fixtures/works.yml | 15 +++++++++++ test/models/work_test.rb | 9 +++++++ 8 files changed, 102 insertions(+), 1 deletion(-) create mode 100644 app/models/work.rb create mode 100644 db/migrate/20171010223806_create_works.rb create mode 100644 db/schema.rb rename db/{media_seeds.csv => seed_data/works.csv} (93%) create mode 100644 test/fixtures/works.yml create mode 100644 test/models/work_test.rb 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/config/routes.rb b/config/routes.rb index 18f02f7228..700b9d6e84 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,5 +16,17 @@ get 'works/index' + get '/works/new', to: 'works#new', as: 'new_work' + + get '/works/:id/edit', to: 'works#edit', as: 'edit_work' + + post '/works', to: 'works#create', as: 'works' + + patch '/works/:id', to: 'works#update' + + get 'works/:id', to: 'works#show', as: 'work' + + delete '/works/:id', to: 'works#destory' + # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20171010223806_create_works.rb b/db/migrate/20171010223806_create_works.rb new file mode 100644 index 0000000000..58fba27f1a --- /dev/null +++ b/db/migrate/20171010223806_create_works.rb @@ -0,0 +1,13 @@ +class CreateWorks < ActiveRecord::Migration[5.1] + def change + create_table :works do |t| + t.string :category + t.string :title + t.string :creator + t.integer :publication_year + t.string :description + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000000..696118647a --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,28 @@ +# 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: 20171010223806) 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.string "category" + t.string "title" + t.string "creator" + t.integer "publication_year" + t.string "description" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/db/media_seeds.csv b/db/seed_data/works.csv similarity index 93% rename from db/media_seeds.csv rename to db/seed_data/works.csv index b5101408d0..eb9ef1e985 100644 --- a/db/media_seeds.csv +++ b/db/seed_data/works.csv @@ -24,4 +24,4 @@ book,Hidden Figures,Margot Lee Shetterly,2016,Good book that came before the mov book,Practical Object-Oriented Design in Ruby,Sandi Metz,2012,Good programming book book,Kindred,Octavia E. Butler,1979,Good sci-fi book book,Parable of the Sower,Octavia E. Butler,1993, -book,A Wizard of Earthsea,Ursula K. Leguin,1968, \ No newline at end of file +book,A Wizard of Earthsea,Ursula K. Leguin,1968, diff --git a/db/seeds.rb b/db/seeds.rb index 1beea2accd..58933b7aa3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,25 @@ # # 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', 'seed_data', 'works.csv') +puts "Loading raw works 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'] + puts "Created work: #{work.inspect}" + successful = work.save + if !successful + work_failures << work + end +end + +puts "Added #{Work.count} work records" +puts "#{work_failures.length} works failed to save" diff --git a/test/fixtures/works.yml b/test/fixtures/works.yml new file mode 100644 index 0000000000..6772d84de5 --- /dev/null +++ b/test/fixtures/works.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + category: MyString + title: MyString + creator: MyString + publication_year: 1 + description: MyString + +two: + category: MyString + title: MyString + creator: MyString + publication_year: 1 + 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 e63a5725de74cab9b84242a78649cafef4aeb9f1 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 10 Oct 2017 16:44:41 -0700 Subject: [PATCH 04/26] Create Show file and fill in links on Index Page --- app/controllers/users_controller.rb | 1 + app/controllers/works_controller.rb | 5 +++++ app/views/works/index.html.erb | 5 +++++ app/views/works/show.html.erb | 1 + 4 files changed, 12 insertions(+) create mode 100644 app/views/works/show.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 3004a55609..4eae25837c 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,4 +1,5 @@ class UsersController < ApplicationController def index + end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 0895e0bd10..a38a08f8f8 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,4 +1,9 @@ class WorksController < ApplicationController def index + @works = Work.all + end + + def show + @work = Work.find(params[:id]) end end diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index e519642270..cb358ce943 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,2 +1,7 @@

Works#index

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

+ +<% @works.each do |work| %> +

<%= link_to work.title, work %>

+ +<% end %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb new file mode 100644 index 0000000000..e7c9ecfdfa --- /dev/null +++ b/app/views/works/show.html.erb @@ -0,0 +1 @@ +

This is the Show Page

From bcd80d9d770056f8ce815452e41214bd8f3aafdc Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 10 Oct 2017 20:48:11 -0700 Subject: [PATCH 05/26] add forms, controller methods --- app/controllers/works_controller.rb | 17 +++++++++++++++ app/views/layouts/application.html.erb | 8 +++++++ app/views/works/_form.html.erb | 29 ++++++++++++++++++++++++++ app/views/works/edit.html.erb | 1 + app/views/works/index.html.erb | 5 +++-- app/views/works/new.html.erb | 3 +++ app/views/works/show.html.erb | 7 +++++++ config/routes.rb | 2 +- 8 files changed, 69 insertions(+), 3 deletions(-) create mode 100644 app/views/works/_form.html.erb create mode 100644 app/views/works/edit.html.erb create mode 100644 app/views/works/new.html.erb diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index a38a08f8f8..00274fb17a 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -6,4 +6,21 @@ def index def show @work = Work.find(params[:id]) end + + def new + @work = Work.new + end + + def create + @work = Work.new(work_params) + @work.save + redirect_to works_index_path + end + + private + + def work_params + return params.require(:work).permit(:category, :title, :creator, :publication_year, :description) + end + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6af511b830..250c038ec6 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -9,6 +9,14 @@ +
+ +
+ + <%= yield %> diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb new file mode 100644 index 0000000000..b21d7d1f2f --- /dev/null +++ b/app/views/works/_form.html.erb @@ -0,0 +1,29 @@ +<%= form_for @work do |f| %> +
    +
  • +<%= f.label :category %> +<%= f.text_field :category, class: "form-field"%> +
  • +
  • +<%= f.label :title %> +<%= f.text_field :title, class: "form-field"%> +
  • +
  • +<%= f.label :creator %> +<%= f.text_field :creator, class: "form-field"%> +
  • +
  • +<%= f.label :publication_year %> +<%= f.text_field :publication_year, class: "form-field"%> +
  • +
  • +<%= f.label :description %> +<%= f.text_field :description, class: "form-field"%> +
  • + +
  • +<%= f.submit class: "submit"%> +
  • + +
+<% end %> diff --git a/app/views/works/edit.html.erb b/app/views/works/edit.html.erb new file mode 100644 index 0000000000..efb813c092 --- /dev/null +++ b/app/views/works/edit.html.erb @@ -0,0 +1 @@ +

This is the edit page

diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index cb358ce943..e47e3992d8 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,5 +1,6 @@ -

Works#index

-

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

+

Welcome to Media Ranker

+ + <% @works.each do |work| %>

<%= link_to work.title, work %>

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

This is the New Page

+ +<%= render partial: 'form' %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index e7c9ecfdfa..d5711b0233 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -1 +1,8 @@

This is the Show Page

+ + +<%= @work.category %> +<%= @work.title %> +<%= @work.creator %> +<%= @work.publication_year %> +<%= @work.description %> diff --git a/config/routes.rb b/config/routes.rb index 700b9d6e84..0435dcaddb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -26,7 +26,7 @@ get 'works/:id', to: 'works#show', as: 'work' - delete '/works/:id', to: 'works#destory' + delete '/works/:id', to: 'works#destroy' # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end From 93a5ca56962973b06a9df8fd6b5a8202cc4d44a6 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Tue, 10 Oct 2017 21:11:45 -0700 Subject: [PATCH 06/26] Format Works Index Page --- app/controllers/works_controller.rb | 17 +++++++++++++ app/views/works/edit.html.erb | 2 ++ app/views/works/index.html.erb | 39 +++++++++++++++++++++++++++-- 3 files changed, 56 insertions(+), 2 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 00274fb17a..bf4ab809c0 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -17,6 +17,23 @@ def create redirect_to works_index_path end + def edit + @work = Work.find(params[:id]) + end + + def update + @work = Work.find(params[:id]) + @work.update_attributes(work_params) + @work.save + redirect_to works_index_path + end + + def destroy + @work = Work.find(params[:id]) + @work.destroy + redirect_to works_index_path + end + private def work_params diff --git a/app/views/works/edit.html.erb b/app/views/works/edit.html.erb index efb813c092..da514f249a 100644 --- a/app/views/works/edit.html.erb +++ b/app/views/works/edit.html.erb @@ -1 +1,3 @@

This is the edit page

+ +<%= render partial: 'form' %> diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index e47e3992d8..1dbc98151e 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,8 +1,43 @@

Welcome to Media Ranker

+ + + + + + + + <% @works.each do |work| %> -

<%= link_to work.title, work %>

- + + + + + + + <% end %> + +
+ Category + + Title + + Creator + + Publication Year + + Description +
+ <%= work.category %> + + <%= link_to work.title, work %> + + <%= work.creator %> + + <%= work.publication_year %> + + <%= work.description %> +
From 485ba9ebcadd7d74ddb4b04868ec166cd60fcce3 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Wed, 11 Oct 2017 15:51:47 -0700 Subject: [PATCH 07/26] add votes controller and users migration --- app/assets/javascripts/votes.js | 2 ++ app/assets/stylesheets/votes.scss | 3 +++ app/controllers/users_controller.rb | 6 +++++- app/controllers/votes_controller.rb | 4 ++++ app/helpers/votes_helper.rb | 2 ++ app/models/user.rb | 2 ++ app/views/layouts/application.html.erb | 2 ++ app/views/users/index.html.erb | 22 ++++++++++++++++++++-- app/views/votes/index.html.erb | 17 +++++++++++++++++ config/routes.rb | 2 ++ db/migrate/20171011224635_create_users.rb | 9 +++++++++ db/schema.rb | 8 +++++++- test/controllers/votes_controller_test.rb | 9 +++++++++ test/fixtures/users.yml | 7 +++++++ test/models/user_test.rb | 9 +++++++++ 15 files changed, 100 insertions(+), 4 deletions(-) create mode 100644 app/assets/javascripts/votes.js create mode 100644 app/assets/stylesheets/votes.scss create mode 100644 app/controllers/votes_controller.rb create mode 100644 app/helpers/votes_helper.rb create mode 100644 app/models/user.rb create mode 100644 app/views/votes/index.html.erb create mode 100644 db/migrate/20171011224635_create_users.rb create mode 100644 test/controllers/votes_controller_test.rb create mode 100644 test/fixtures/users.yml create mode 100644 test/models/user_test.rb 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/stylesheets/votes.scss b/app/assets/stylesheets/votes.scss new file mode 100644 index 0000000000..9a6720f80e --- /dev/null +++ b/app/assets/stylesheets/votes.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Votes 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/users_controller.rb b/app/controllers/users_controller.rb index 4eae25837c..9699deeeed 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,5 +1,9 @@ class UsersController < ApplicationController def index - + # @users = Users.all end + + # def show + # @user = User.find(params[:id]) + # end end diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb new file mode 100644 index 0000000000..b4ee6a7464 --- /dev/null +++ b/app/controllers/votes_controller.rb @@ -0,0 +1,4 @@ +class VotesController < ApplicationController + def index + end +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/models/user.rb b/app/models/user.rb new file mode 100644 index 0000000000..379658a509 --- /dev/null +++ b/app/models/user.rb @@ -0,0 +1,2 @@ +class User < ApplicationRecord +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 250c038ec6..b71de463ff 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,8 +11,10 @@
diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 51968c88d2..322df38d92 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -1,2 +1,20 @@ -

Users#index

-

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

+

All of our Beautiful Users

+ + + + + + + + + + + + +
+ UserName + + Votes + + Join Date +
diff --git a/app/views/votes/index.html.erb b/app/views/votes/index.html.erb new file mode 100644 index 0000000000..61f51a20e0 --- /dev/null +++ b/app/views/votes/index.html.erb @@ -0,0 +1,17 @@ +

Votes#index

+

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

+ +<%# + diff --git a/config/routes.rb b/config/routes.rb index 0435dcaddb..2108f184a9 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ Rails.application.routes.draw do + get 'votes/index' + get '/users/index' get '/users/new', to: 'users#new', as: 'new_user' diff --git a/db/migrate/20171011224635_create_users.rb b/db/migrate/20171011224635_create_users.rb new file mode 100644 index 0000000000..0a365dee1d --- /dev/null +++ b/db/migrate/20171011224635_create_users.rb @@ -0,0 +1,9 @@ +class CreateUsers < ActiveRecord::Migration[5.1] + def change + create_table :users do |t| + t.string :username + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 696118647a..5c2015d9d3 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,11 +10,17 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20171010223806) do +ActiveRecord::Schema.define(version: 20171011224635) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" + create_table "users", force: :cascade do |t| + t.string "username" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "works", force: :cascade do |t| t.string "category" t.string "title" diff --git a/test/controllers/votes_controller_test.rb b/test/controllers/votes_controller_test.rb new file mode 100644 index 0000000000..4d5465f1d0 --- /dev/null +++ b/test/controllers/votes_controller_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe VotesController do + it "should get index" do + get votes_index_url + value(response).must_be :success? + end + +end diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml new file mode 100644 index 0000000000..952febd97d --- /dev/null +++ b/test/fixtures/users.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +one: + username: MyString + +two: + username: MyString diff --git a/test/models/user_test.rb b/test/models/user_test.rb new file mode 100644 index 0000000000..cc862ac2d9 --- /dev/null +++ b/test/models/user_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe User do + let(:user) { User.new } + + it "must be valid" do + value(user).must_be :valid? + end +end From 109e1de515abf096d2fbb2323d7c6c6e15c93181 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Thu, 12 Oct 2017 07:29:21 -0700 Subject: [PATCH 08/26] Add Links Between Pages, Drop Down Menus --- app/controllers/users_controller.rb | 8 ++--- app/controllers/works_controller.rb | 2 +- app/views/users/index.html.erb | 14 +++++++++ app/views/votes/index.html.erb | 21 +++++-------- app/views/works/_form.html.erb | 48 ++++++++++++++--------------- app/views/works/edit.html.erb | 3 +- app/views/works/index.html.erb | 9 ++++++ app/views/works/show.html.erb | 10 ++++++ 8 files changed, 71 insertions(+), 44 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 9699deeeed..7ee3956acd 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -1,9 +1,9 @@ class UsersController < ApplicationController def index - # @users = Users.all + @users = User.all end - # def show - # @user = User.find(params[:id]) - # end + def show + @user = User.find(params[:id]) + end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index bf4ab809c0..80ce8a9e42 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,6 +1,6 @@ class WorksController < ApplicationController def index - @works = Work.all + @works = Work.all.order(:category, :title) end def show diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 322df38d92..da71915fe7 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -16,5 +16,19 @@ +<% @users.each do |user| %> + + + <%= user.username%> + + + Votes + + + <%= user.created_at %> + + +<% end %> --> + diff --git a/app/views/votes/index.html.erb b/app/views/votes/index.html.erb index 61f51a20e0..58ccd4cd2d 100644 --- a/app/views/votes/index.html.erb +++ b/app/views/votes/index.html.erb @@ -1,17 +1,10 @@

Votes#index

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

-<%# - +

+ Need View Top Media Page with Spotlight view and top ten in each category +

+ +

+ /works/id/upvote +

diff --git a/app/views/works/_form.html.erb b/app/views/works/_form.html.erb index b21d7d1f2f..f80bc2f8be 100644 --- a/app/views/works/_form.html.erb +++ b/app/views/works/_form.html.erb @@ -1,29 +1,29 @@ <%= form_for @work do |f| %>
  • -<%= f.label :category %> -<%= f.text_field :category, class: "form-field"%> -
  • -
  • -<%= f.label :title %> -<%= f.text_field :title, class: "form-field"%> -
  • -
  • -<%= f.label :creator %> -<%= f.text_field :creator, class: "form-field"%> -
  • -
  • -<%= f.label :publication_year %> -<%= f.text_field :publication_year, class: "form-field"%> -
  • -
  • -<%= f.label :description %> -<%= f.text_field :description, class: "form-field"%> -
  • + <%= f.select :category, ['Album', 'Book', + 'Movie'] %> + +
  • + <%= f.label :title %> + <%= f.text_field :title, class: "form-field"%> +
  • +
  • + <%= f.label :creator %> + <%= f.text_field :creator, class: "form-field"%> +
  • +
  • + <%= f.label :publication_year %> + <%= f.text_field :publication_year, class: "form-field"%> +
  • +
  • + <%= f.label :description %> + <%= f.text_field :description, class: "form-field"%> +
  • -
  • -<%= f.submit class: "submit"%> -
  • +
  • + <%= f.submit class: "submit"%> +
  • -
-<% end %> + + <% end %> diff --git a/app/views/works/edit.html.erb b/app/views/works/edit.html.erb index da514f249a..fe9b12a999 100644 --- a/app/views/works/edit.html.erb +++ b/app/views/works/edit.html.erb @@ -1,3 +1,4 @@

This is the edit page

-<%= render partial: 'form' %> +<%= render partial: 'form', + locals: { hide_placeholder_text: false, button_text: "Update This Work"}%> diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 1dbc98151e..6fb230939c 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -3,6 +3,9 @@ + @@ -22,6 +25,9 @@ <% @works.each do |work| %> + @@ -37,6 +43,9 @@ + <% end %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index d5711b0233..befd32980c 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -1,5 +1,15 @@

This is the Show Page

+

+ User who created it +

+ +

+ Link to Edit, Link to Upvote, Link to Delete +

+ +<%= link_to "Edit Work", edit_work_path %> +<%= link_to "Delete Work", work_path(@work.id), method: :delete, data: {:confirm => "Are you sure you want to delete this Work?"} %> <%= @work.category %> <%= @work.title %> From de1483dfbd99f765d417e67ba6470cb631a812fc Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 11:14:27 -0700 Subject: [PATCH 09/26] update foundation file --- .DS_Store | Bin 0 -> 6148 bytes app/.DS_Store | Bin 0 -> 6148 bytes app/assets/.DS_Store | Bin 0 -> 6148 bytes app/assets/stylesheets/.DS_Store | Bin 0 -> 6148 bytes app/assets/stylesheets/foundation.css | 4001 +++++++++++++++++++++ app/models/user.rb | 1 + app/models/vote.rb | 5 + app/models/work.rb | 1 + db/migrate/20171014175921_create_votes.rb | 10 + db/schema.rb | 9 +- test/fixtures/votes.yml | 9 + test/models/vote_test.rb | 9 + 12 files changed, 4044 insertions(+), 1 deletion(-) create mode 100644 .DS_Store create mode 100644 app/.DS_Store create mode 100644 app/assets/.DS_Store create mode 100644 app/assets/stylesheets/.DS_Store create mode 100644 app/assets/stylesheets/foundation.css create mode 100644 app/models/vote.rb create mode 100644 db/migrate/20171014175921_create_votes.rb create mode 100644 test/fixtures/votes.yml create mode 100644 test/models/vote_test.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c4d1abfe5bd07eca70f562dad070efd6a3549f29 GIT binary patch literal 6148 zcmeHKF=_)r43umahBPi+?ic)n#W*kU2V!GL6KoSm>Z|guJS{VlFkH@oG-<*Jq}iR5 zcDX4|Co}Wy+i7FAGP5O|Xdety<8%7NX2lSlXS|+YF6ZamO|tJ3texAzu6BLjkLqW3 zvg0@I{uyGWfE17dQa}nwf!`~Do=samBq~Y)DIf)I3h?{T;KW`yB*v!$Aw~e;0(2PG zF-rg&31BZA62ZVcOMzMHHDY*{gKw4B3x~ukH;)yi)bn$;+|VHuw{qIiGME)=`2G?HCyC7#p_Z dbtGk8;~LL<;gA^R;G-OZ^DcPh-Y*b2?I_NE3}u-r4nf zc6C#n&t~SUukDlB!p!DyqWy4~8u#fFyQ>J%5;_V%N*xhInXQa}nw0VyB_E?1yx==||=su)s03f#B?{5~`|u@?@B@#(-2 zEdX$aa2V#%O8^@Kz+N~cA_DWI0+Z_1VtCRKZJZ?w4MY?%U z)F=g{z*vF%TrODupW$cv|1n7`DIf)IN&%m(H|rH%scP%wX>7}U5-xmU;y7NML#7f2D46vBbn38YiybNOg_^C2K47cShGH!$;d zXXmZZD>NDr(eC^0S!63B8@QpIEG*5=%@_8N83n>|$4(CNfw+_7qIx}H+!m~({K`1v zAAW|f{rI_mkK5Pj%u)d=Kn17(6`%rFDqy`AwrK+ysQ?wA0{;rw_o2WIYvL5>pAG~c z0e}tCZdm&)0W6jP*2F0g8JGqY7*x#>LxYZd$-0_21qNL-hY!ssYfdQYPsjbm%SCG- zBNd3c-&q>@-0V;4;3TV?0eTP@d-nw`>>$L^`3b&dw+ze}{Ab2|l jdOOC(+VPVYMP0Eq&THZn=yc?r4&={(=|ZCdw^rZ}AFviK literal 0 HcmV?d00001 diff --git a/app/assets/stylesheets/.DS_Store b/app/assets/stylesheets/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..5008ddfcf53c02e82d7eee2e57c38e5672ef89f6 GIT binary patch literal 6148 zcmeH~Jr2S!425mzP>H1@V-^m;4Wg<&0T*E43hX&L&p$$qDprKhvt+--jT7}7np#A3 zem<@ulZcFPQ@L2!n>{z**++&mCkOWA81W14cNZlEfg7;MkzE(HCqgga^y>{tEnwC%0;vJ&^%eQ zLs35+`xjp>T0 <% end %> - + diff --git a/config/routes.rb b/config/routes.rb index 0f16681c59..aab58f185b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -9,8 +9,9 @@ resources :users resources :works - resources :votes + resources :votes + get 'upvote', to: 'votes#new', as: 'upvote' post 'upvote', to: 'votes#create' diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 3523fbeeb8..439b02a897 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,9 +1,9 @@ require "test_helper" - -describe UsersController do - it "should get index" do - get users_index_url - value(response).must_be :success? - end - -end +# +# describe UsersController do +# it "should get index" do +# get users_index_url +# value(response).must_be :success? +# end +# +# end diff --git a/test/controllers/votes_controller_test.rb b/test/controllers/votes_controller_test.rb index 4d5465f1d0..5ee394fb8d 100644 --- a/test/controllers/votes_controller_test.rb +++ b/test/controllers/votes_controller_test.rb @@ -1,9 +1,9 @@ require "test_helper" - -describe VotesController do - it "should get index" do - get votes_index_url - value(response).must_be :success? - end - -end +# +# describe VotesController do +# it "should get index" do +# get votes_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 index 6d781c6906..c8bb41bb6e 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -1,9 +1,220 @@ require "test_helper" describe WorksController do - it "should get index" do - get works_index_url - value(response).must_be :success? - end + describe "index" do + it "returns a success status for all works" do + get works_path + must_respond_with :success + end -end + it "returns a success status when there are no works" do + Work.destroy_all + get works_path + must_respond_with :success + end + + end #index tests + + describe "new" do + it "gets a new form" do + get new_work_path + must_respond_with :success + end + + # it "works without an author id" do + # get new_book_path + # must_respond_with :success + # end + end #new tests + + + + describe "create" do + it "adds the work to the DB and redirects when the work data is valid" do + # Arrange + work_data = { + work: { + title: "Test work" + } + } + # Test data should result in a valid work, otherwise + # the test is broken + Work.new(work_data[:work]).must_be :valid? + + start_work_count = Work.count + + # Act + post work_path, params: work_data + + # Assert + must_respond_with :redirect + must_redirect_to works_path + + Work.count.must_equal start_work_count + 1 + end #adds work test + + it "sends bad_request when the work data is bogus" do + # Arrange + invalid_work_data = { + work: { + # NO TITLE!!! + creator: "Fake Creator" + } + } + # Double check the data is truly invalid + Work.new(invalid_work_data[:work]).wont_be :valid? + + start_work_count = Work.count + + # Act + post works_path, params: invalid_work_data + + # Assert + must_respond_with :bad_request + # Vanilla rails doesn't provide any way to do this + # assert_template :new + Work.count.must_equal start_work_count + end #bad request + end #create + +end#all tests + + +# describe "index" do + +# + +# +# it "returns a success status when given a valid author_id" do +# get author_books_path(Author.first) +# must_respond_with :success +# end +# +# it "returns something when given a bogus author_id" do +# bad_author_id = Author.last.id + 1 +# get author_books_path(bad_author_id) +# must_respond_with :not_found +# end +# end +# + +# describe "show" do +# it "returns success when given a valid work ID" do +# # Arrange +# book_id = Book.first.id +# +# # Act +# get book_path(book_id) +# +# # Assert +# must_respond_with :success +# end +# +# it "returns not_found when given an invalid book ID" do +# invalid_book_id = Book.last.id + 1 +# get book_path(invalid_book_id) +# must_respond_with :not_found +# end +# end +# +# describe "edit" do +# it "returns success when given a valid book ID" do +# # Arrange +# book_id = Book.first.id +# +# # Act +# get edit_book_path(book_id) +# +# # Assert +# must_respond_with :success +# end +# +# it "returns not_found when given an invalid book ID" do +# invalid_book_id = Book.last.id + 1 +# get edit_book_path(invalid_book_id) +# must_respond_with :not_found +# end +# end +# +# describe "update" do +# it "returns success if the book ID is valid and the change is valid" do +# book = Book.first +# book_data = { +# book: { +# title: "Changed title", +# author_id: book.author_id +# } +# } +# book.update_attributes(book_data[:book]) +# book.must_be :valid? +# +# patch book_path(book), params: book_data +# +# must_respond_with :redirect +# must_redirect_to book_path(book) +# +# # Check that the change went through +# book.reload +# book.title.must_equal book_data[:book][:title] +# end +# +# it "returns not_found if the book ID is invalid" do +# invalid_book_id = Book.last.id + 1 +# book_data = { +# book: { +# title: "Changed title", +# author_id: Author.first.id +# } +# } +# +# patch book_path(invalid_book_id), params: book_data +# +# must_respond_with :not_found +# end +# +# it "returns bad_request if the change is invalid" do +# book = Book.first +# invalid_book_data = { +# book: { +# title: "" +# } +# } +# # Check that the update is actually invalid +# book.update_attributes(invalid_book_data[:book]) +# book.wont_be :valid? +# +# patch book_path(book), params: invalid_book_data +# +# must_respond_with :bad_request +# +# book.reload +# book.title.wont_equal invalid_book_data[:book][:title] +# end +# end +# +# describe "destroy" do +# it "returns success and destroys the book when given a valid book ID" do +# # Arrange +# book_id = Book.first.id +# +# # Act +# delete book_path(book_id) +# +# # Assert +# must_respond_with :redirect +# must_redirect_to books_path +# Book.find_by(id: book_id).must_be_nil +# end +# +# it "returns not_found when given an invalid book ID" do +# invalid_book_id = Book.last.id + 1 +# +# start_book_count = Book.count +# +# delete book_path(invalid_book_id) +# +# must_respond_with :not_found +# Book.count.must_equal start_book_count +# end +# end +# end 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 fc15947bd3..6f51ad6e4f 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) { Vote.new } - - it "must be valid" do - value(vote).must_be :valid? - end -end +# +# describe Vote do +# let(:vote) { Vote.new } +# +# 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 f6fba7104d..48f3da9e64 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -1,9 +1,9 @@ require "test_helper" -describe Work do - let(:work) { Work.new } - - it "must be valid" do - value(work).must_be :valid? - end -end +# describe Work do +# let(:work) { Work.new } +# +# it "must be valid" do +# value(work).must_be :valid? +# end +# end From b28d3c3be5b62fc17cccc22a7ec17c9b885c2fae Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 13:06:44 -0700 Subject: [PATCH 20/26] Update controller methods --- app/controllers/works_controller.rb | 109 ++++--- test/controllers/works_controller_test.rb | 379 ++++++++++------------ 2 files changed, 239 insertions(+), 249 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 01b318f4c7..48caf0af25 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,5 +1,5 @@ class WorksController < ApplicationController - before_action :get_work , only: [:show, :edit, :update, :destroy] + # before_action :get_work , only: [:update, :destroy] def get_work @work = Work.find(params[:id]) @@ -11,6 +11,7 @@ def index def show # @work = Work.find(params[:id]) + find_work_by_params_id end def new @@ -19,70 +20,90 @@ def new def create @work = Work.new(work_params) + if @work.save flash[:status] = :success flash[:message] = "Successfully created work #{@work.id}" redirect_to works_path + return else flash.now[:status] = :failure flash.now[:message] = "Failed to create new work" + flash.now[:details] = @work.errors.messages render :new, status: :bad_request end end - # @book = Book.new(book_params) - - # if @book.save - # flash[:status] = :success - # flash[:message] = "Successfully created book #{@book.id}" - # redirect_to books_path - # else - # Tell the user what went wrong - # flash.now[:status] = :failure - # flash.now[:message] = "Failed to create book" - # flash.now[:details] = @work.errors.messages - # render :new, status: :bad_request - # end -# end - - - - def edit # @work = Work.find(params[:id]) + find_work_by_params_id end def update # @work = Work.find(params[:id]) - @work.update_attributes(work_params) - @work.save - redirect_to works_path + if find_work_by_params_id + @work.update_attributes(work_params) + if @work.save + redirect_to work_path(@work) + return + else + render :edit, status: :bad_request + return + end + end end def destroy - # @work = Work.find(params[:id]) - @work.destroy - redirect_to works_path + if find_work_by_params_id + @work.destroy + redirect_to works_path + return + else + head :not_found + return + end end + # @work = Work.find(params[:id]) + # unless @work + # head :not_found + # enda + # @work.destroy + # redirect_to works_path - def topten - end +def topten +end - # def upvote - # @work = Work.find(params[:id]) - # @work.votes.create - # redirect_to new_vote_path - # - # # render :topten - # #want to create a vote, which includes this id as the work id and the logged in user id as the user-id - # # @work.votes.create - # # redirect_to works_index_path - # # get 'works/:id/upvote', to: 'works#upvote', as: 'upvote_work' - # # - # - # end +private + +def work_params + return params.require(:work).permit(:category, :title, :creator, :publication_year, :description) +end + +def find_work_by_params_id + @work = Work.find_by(id: params[:id]) + unless @work + head :not_found + end + return @work +end + +end + +# def upvote +# @work = Work.find(params[:id]) +# @work.votes.create +# redirect_to new_vote_path +# +# # render :topten +# #want to create a vote, which includes this id as the work id and the logged in user id as the user-id +# # @work.votes.create +# # redirect_to works_index_path +# # get 'works/:id/upvote', to: 'works#upvote', as: 'upvote_work' +# # +# +# end # def upvote # @startup = Startup.find(params[:id]) @@ -90,11 +111,3 @@ def topten # upvote.save # redirect_to startups_path # end - - private - - def work_params - return params.require(:work).permit(:category, :title, :creator, :publication_year, :description) - end - -end diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index c8bb41bb6e..ee9d829c28 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -15,206 +15,183 @@ end #index tests - describe "new" do - it "gets a new form" do - get new_work_path - must_respond_with :success + describe "new" do + it "gets a new form" do + get new_work_path + must_respond_with :success + end + end #new tests + + + + describe "create" do + it "adds the work to the DB and redirects when the work data is valid" do + # Arrange + work_data = { + work: { + title: "Test work" + } + } + # Test data should result in a valid work, otherwise + # the test is broken + Work.new(work_data[:work]).must_be :valid? + + start_work_count = Work.count + + # Act + post works_path, params: work_data + + # Assert + must_respond_with :redirect + must_redirect_to works_path + + Work.count.must_equal start_work_count + 1 + end #adds work test + + it "sends bad_request when the work data is bogus" do + # Arrange + invalid_work_data = { + work: { + # NO TITLE!!! + creator: "Fake Creator" + } + } + # Double check the data is truly invalid + Work.new(invalid_work_data[:work]).wont_be :valid? + + start_work_count = Work.count + + # Act + post works_path, params: invalid_work_data + + # Assert + must_respond_with :bad_request + # Vanilla rails doesn't provide any way to do this + # assert_template :new + Work.count.must_equal start_work_count + end #bad request + end #create + + + + describe "show" do + it "returns success when given a valid work ID" do + # Arrange + work_id = Work.first.id + + # Act + get work_path(work_id) + + # Assert + must_respond_with :success + end + + it "returns not_found when given an invalid book ID" do + invalid_work_id = Work.last.id + 1 + get work_path(invalid_work_id) + must_respond_with :not_found + end + end + + describe "edit" do + it "returns success when given a valid work ID" do + # Arrange + work_id = Work.first.id + + # Act + get edit_work_path(work_id) + + # Assert + must_respond_with :success + end + + it "returns not_found when given an invalid work ID" do + invalid_work_id = Work.last.id + 1 + get edit_work_path(invalid_work_id) + must_respond_with :not_found + end + end + + + describe "update" do + it "returns success if the work ID is valid and the change is valid" do + work = Work.first + work_data = { + work: { + title: "New title", + creator: "New Creator" + } + } + work.update_attributes(work_data[:work]) + work.must_be :valid? + + patch work_path(work), params: work_data + + must_respond_with :redirect + must_redirect_to work_path + + # Check that the change went through + work.reload + work.title.must_equal work_data[:work][:title] + end + + it "returns not_found if the book ID is invalid" do + invalid_work_id = Work.last.id + 1 + work_data = { + work: { + title: "New title", + creator: "New Creator" + } + } + + patch work_path(invalid_work_id), params: work_data + + must_respond_with :not_found + end + + it "returns bad_request if the change is invalid" do + work = Work.first + invalid_work_data = { + work: { + title: "" + } + } + # Check that the update is actually invalid + work.update_attributes(invalid_work_data[:work]) + work.wont_be :valid? + + patch work_path(work), params: invalid_work_data + + must_respond_with :bad_request + + work.reload + work.title.wont_equal invalid_work_data[:work][:title] + end + end + + describe "destroy" do + it "returns success and destroys the book when given a valid book ID" do + # Arrange + work_id = Work.first.id + + # Act + delete work_path(work_id) + + # Assert + must_respond_with :redirect + must_redirect_to works_path + Work.find_by(id: work_id).must_be_nil end - # it "works without an author id" do - # get new_book_path - # must_respond_with :success - # end - end #new tests - - - - describe "create" do - it "adds the work to the DB and redirects when the work data is valid" do - # Arrange - work_data = { - work: { - title: "Test work" - } - } - # Test data should result in a valid work, otherwise - # the test is broken - Work.new(work_data[:work]).must_be :valid? - - start_work_count = Work.count - - # Act - post work_path, params: work_data - - # Assert - must_respond_with :redirect - must_redirect_to works_path - - Work.count.must_equal start_work_count + 1 - end #adds work test - - it "sends bad_request when the work data is bogus" do - # Arrange - invalid_work_data = { - work: { - # NO TITLE!!! - creator: "Fake Creator" - } - } - # Double check the data is truly invalid - Work.new(invalid_work_data[:work]).wont_be :valid? - - start_work_count = Work.count - - # Act - post works_path, params: invalid_work_data - - # Assert - must_respond_with :bad_request - # Vanilla rails doesn't provide any way to do this - # assert_template :new - Work.count.must_equal start_work_count - end #bad request - end #create - -end#all tests - - -# describe "index" do - -# - -# -# it "returns a success status when given a valid author_id" do -# get author_books_path(Author.first) -# must_respond_with :success -# end -# -# it "returns something when given a bogus author_id" do -# bad_author_id = Author.last.id + 1 -# get author_books_path(bad_author_id) -# must_respond_with :not_found -# end -# end -# - -# describe "show" do -# it "returns success when given a valid work ID" do -# # Arrange -# book_id = Book.first.id -# -# # Act -# get book_path(book_id) -# -# # Assert -# must_respond_with :success -# end -# -# it "returns not_found when given an invalid book ID" do -# invalid_book_id = Book.last.id + 1 -# get book_path(invalid_book_id) -# must_respond_with :not_found -# end -# end -# -# describe "edit" do -# it "returns success when given a valid book ID" do -# # Arrange -# book_id = Book.first.id -# -# # Act -# get edit_book_path(book_id) -# -# # Assert -# must_respond_with :success -# end -# -# it "returns not_found when given an invalid book ID" do -# invalid_book_id = Book.last.id + 1 -# get edit_book_path(invalid_book_id) -# must_respond_with :not_found -# end -# end -# -# describe "update" do -# it "returns success if the book ID is valid and the change is valid" do -# book = Book.first -# book_data = { -# book: { -# title: "Changed title", -# author_id: book.author_id -# } -# } -# book.update_attributes(book_data[:book]) -# book.must_be :valid? -# -# patch book_path(book), params: book_data -# -# must_respond_with :redirect -# must_redirect_to book_path(book) -# -# # Check that the change went through -# book.reload -# book.title.must_equal book_data[:book][:title] -# end -# -# it "returns not_found if the book ID is invalid" do -# invalid_book_id = Book.last.id + 1 -# book_data = { -# book: { -# title: "Changed title", -# author_id: Author.first.id -# } -# } -# -# patch book_path(invalid_book_id), params: book_data -# -# must_respond_with :not_found -# end -# -# it "returns bad_request if the change is invalid" do -# book = Book.first -# invalid_book_data = { -# book: { -# title: "" -# } -# } -# # Check that the update is actually invalid -# book.update_attributes(invalid_book_data[:book]) -# book.wont_be :valid? -# -# patch book_path(book), params: invalid_book_data -# -# must_respond_with :bad_request -# -# book.reload -# book.title.wont_equal invalid_book_data[:book][:title] -# end -# end -# -# describe "destroy" do -# it "returns success and destroys the book when given a valid book ID" do -# # Arrange -# book_id = Book.first.id -# -# # Act -# delete book_path(book_id) -# -# # Assert -# must_respond_with :redirect -# must_redirect_to books_path -# Book.find_by(id: book_id).must_be_nil -# end -# -# it "returns not_found when given an invalid book ID" do -# invalid_book_id = Book.last.id + 1 -# -# start_book_count = Book.count -# -# delete book_path(invalid_book_id) -# -# must_respond_with :not_found -# Book.count.must_equal start_book_count -# end -# end -# end + it "returns not_found when given an invalid book ID" do + invalid_work_id = Work.last.id + 1 + + start_work_count = Work.count + + delete work_path(invalid_work_id) + + must_respond_with :not_found + Work.count.must_equal start_work_count + end + end + end From 25ca9752b8a26a10a577b5ee18292cd1970a7aba Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 14:20:24 -0700 Subject: [PATCH 21/26] Add more model tests, voting is maybe, maybe, maybe working? --- app/controllers/votes_controller.rb | 16 +- app/models/work.rb | 3 +- app/views/works/index.html.erb | 8 +- test/controllers/works_controller_test.rb | 394 +++++++++++----------- test/models/work_test.rb | 81 ++++- 5 files changed, 291 insertions(+), 211 deletions(-) diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index db2bfa86e4..73b2ed0f2b 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -8,10 +8,10 @@ def new def create # user_id = session[:logged_in_user] - @vote = Vote.new + @vote = Vote.new(vote_params) - @vote.user_id = session[:logged_in_user] - @vote.work_id = params[:work_id] + # @vote.user_id = session[:logged_in_user] + # @vote.work_id = params[:work_id] @vote.save redirect_to works_path end @@ -21,11 +21,11 @@ def create # session[:logged_in_user] = @user.id # redirect_to works_path - # private - # - # def vote_params - # return params.require(:vote).permit(:user_id, :work_id) - # end + private + + def vote_params + return params.require(:vote).permit(:user_id, :work_id) + end end diff --git a/app/models/work.rb b/app/models/work.rb index 21a9d2be3b..abfce8ec51 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -4,8 +4,7 @@ class Work < ApplicationRecord # must provide a title validates :title, presence: {message: "Title is Required"} validates :title, uniqueness: {message: "Someone Else has Already Added that Work"} - - +validates :description, length: { maximum: 200, message: "The description is too long" } def testing(input) diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index b1157a1607..c5805afa48 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -33,12 +33,13 @@ <%# @votes.where(work_id: work.id).count %> -<%# @votes = Vote.all %> +<%# @votes = Vote.all work.testing(work.id)%> <% @works.each do |work| %> diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index ee9d829c28..e3e3f04102 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -1,197 +1,197 @@ -require "test_helper" - -describe WorksController do - describe "index" do - it "returns a success status for all works" do - get works_path - must_respond_with :success - end - - it "returns a success status when there are no works" do - Work.destroy_all - get works_path - must_respond_with :success - end - - end #index tests - - describe "new" do - it "gets a new form" do - get new_work_path - must_respond_with :success - end - end #new tests - - - - describe "create" do - it "adds the work to the DB and redirects when the work data is valid" do - # Arrange - work_data = { - work: { - title: "Test work" - } - } - # Test data should result in a valid work, otherwise - # the test is broken - Work.new(work_data[:work]).must_be :valid? - - start_work_count = Work.count - - # Act - post works_path, params: work_data - - # Assert - must_respond_with :redirect - must_redirect_to works_path - - Work.count.must_equal start_work_count + 1 - end #adds work test - - it "sends bad_request when the work data is bogus" do - # Arrange - invalid_work_data = { - work: { - # NO TITLE!!! - creator: "Fake Creator" - } - } - # Double check the data is truly invalid - Work.new(invalid_work_data[:work]).wont_be :valid? - - start_work_count = Work.count - - # Act - post works_path, params: invalid_work_data - - # Assert - must_respond_with :bad_request - # Vanilla rails doesn't provide any way to do this - # assert_template :new - Work.count.must_equal start_work_count - end #bad request - end #create - - - - describe "show" do - it "returns success when given a valid work ID" do - # Arrange - work_id = Work.first.id - - # Act - get work_path(work_id) - - # Assert - must_respond_with :success - end - - it "returns not_found when given an invalid book ID" do - invalid_work_id = Work.last.id + 1 - get work_path(invalid_work_id) - must_respond_with :not_found - end - end - - describe "edit" do - it "returns success when given a valid work ID" do - # Arrange - work_id = Work.first.id - - # Act - get edit_work_path(work_id) - - # Assert - must_respond_with :success - end - - it "returns not_found when given an invalid work ID" do - invalid_work_id = Work.last.id + 1 - get edit_work_path(invalid_work_id) - must_respond_with :not_found - end - end - - - describe "update" do - it "returns success if the work ID is valid and the change is valid" do - work = Work.first - work_data = { - work: { - title: "New title", - creator: "New Creator" - } - } - work.update_attributes(work_data[:work]) - work.must_be :valid? - - patch work_path(work), params: work_data - - must_respond_with :redirect - must_redirect_to work_path - - # Check that the change went through - work.reload - work.title.must_equal work_data[:work][:title] - end - - it "returns not_found if the book ID is invalid" do - invalid_work_id = Work.last.id + 1 - work_data = { - work: { - title: "New title", - creator: "New Creator" - } - } - - patch work_path(invalid_work_id), params: work_data - - must_respond_with :not_found - end - - it "returns bad_request if the change is invalid" do - work = Work.first - invalid_work_data = { - work: { - title: "" - } - } - # Check that the update is actually invalid - work.update_attributes(invalid_work_data[:work]) - work.wont_be :valid? - - patch work_path(work), params: invalid_work_data - - must_respond_with :bad_request - - work.reload - work.title.wont_equal invalid_work_data[:work][:title] - end - end - - describe "destroy" do - it "returns success and destroys the book when given a valid book ID" do - # Arrange - work_id = Work.first.id - - # Act - delete work_path(work_id) - - # Assert - must_respond_with :redirect - must_redirect_to works_path - Work.find_by(id: work_id).must_be_nil - end - - it "returns not_found when given an invalid book ID" do - invalid_work_id = Work.last.id + 1 - - start_work_count = Work.count - - delete work_path(invalid_work_id) - - must_respond_with :not_found - Work.count.must_equal start_work_count - end - end - end +# require "test_helper" +# +# describe WorksController do +# describe "index" do +# it "returns a success status for all works" do +# get works_path +# must_respond_with :success +# end +# +# it "returns a success status when there are no works" do +# Work.destroy_all +# get works_path +# must_respond_with :success +# end +# +# end #index tests +# +# describe "new" do +# it "gets a new form" do +# get new_work_path +# must_respond_with :success +# end +# end #new tests +# +# +# +# describe "create" do +# it "adds the work to the DB and redirects when the work data is valid" do +# # Arrange +# work_data = { +# work: { +# title: "Test work" +# } +# } +# # Test data should result in a valid work, otherwise +# # the test is broken +# Work.new(work_data[:work]).must_be :valid? +# +# start_work_count = Work.count +# +# # Act +# post works_path, params: work_data +# +# # Assert +# must_respond_with :redirect +# must_redirect_to works_path +# +# Work.count.must_equal start_work_count + 1 +# end #adds work test +# +# it "sends bad_request when the work data is bogus" do +# # Arrange +# invalid_work_data = { +# work: { +# # NO TITLE!!! +# creator: "Fake Creator" +# } +# } +# # Double check the data is truly invalid +# Work.new(invalid_work_data[:work]).wont_be :valid? +# +# start_work_count = Work.count +# +# # Act +# post works_path, params: invalid_work_data +# +# # Assert +# must_respond_with :bad_request +# # Vanilla rails doesn't provide any way to do this +# # assert_template :new +# Work.count.must_equal start_work_count +# end #bad request +# end #create +# +# +# +# describe "show" do +# it "returns success when given a valid work ID" do +# # Arrange +# work_id = Work.first.id +# +# # Act +# get work_path(work_id) +# +# # Assert +# must_respond_with :success +# end +# +# it "returns not_found when given an invalid book ID" do +# invalid_work_id = Work.last.id + 1 +# get work_path(invalid_work_id) +# must_respond_with :not_found +# end +# end +# +# describe "edit" do +# it "returns success when given a valid work ID" do +# # Arrange +# work_id = Work.first.id +# +# # Act +# get edit_work_path(work_id) +# +# # Assert +# must_respond_with :success +# end +# +# it "returns not_found when given an invalid work ID" do +# invalid_work_id = Work.last.id + 1 +# get edit_work_path(invalid_work_id) +# must_respond_with :not_found +# end +# end +# +# +# describe "update" do +# it "returns success if the work ID is valid and the change is valid" do +# work = Work.first +# work_data = { +# work: { +# title: "New title", +# creator: "New Creator" +# } +# } +# work.update_attributes(work_data[:work]) +# work.must_be :valid? +# +# patch work_path(work), params: work_data +# +# must_respond_with :redirect +# must_redirect_to work_path +# +# # Check that the change went through +# work.reload +# work.title.must_equal work_data[:work][:title] +# end +# +# it "returns not_found if the book ID is invalid" do +# invalid_work_id = Work.last.id + 1 +# work_data = { +# work: { +# title: "New title", +# creator: "New Creator" +# } +# } +# +# patch work_path(invalid_work_id), params: work_data +# +# must_respond_with :not_found +# end +# +# it "returns bad_request if the change is invalid" do +# work = Work.first +# invalid_work_data = { +# work: { +# title: "" +# } +# } +# # Check that the update is actually invalid +# work.update_attributes(invalid_work_data[:work]) +# work.wont_be :valid? +# +# patch work_path(work), params: invalid_work_data +# +# must_respond_with :bad_request +# +# work.reload +# work.title.wont_equal invalid_work_data[:work][:title] +# end +# end +# +# describe "destroy" do +# it "returns success and destroys the book when given a valid book ID" do +# # Arrange +# work_id = Work.first.id +# +# # Act +# delete work_path(work_id) +# +# # Assert +# must_respond_with :redirect +# must_redirect_to works_path +# Work.find_by(id: work_id).must_be_nil +# end +# +# it "returns not_found when given an invalid book ID" do +# invalid_work_id = Work.last.id + 1 +# +# start_work_count = Work.count +# +# delete work_path(invalid_work_id) +# +# must_respond_with :not_found +# Work.count.must_equal start_work_count +# end +# end +# end diff --git a/test/models/work_test.rb b/test/models/work_test.rb index 48f3da9e64..a1a63df284 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -6,4 +6,83 @@ # it "must be valid" do # value(work).must_be :valid? # end -# end +# params.require(:work).permit(:category, :title, :creator, :publication_year, :description) + +describe Work do + let :work{ Work.first } + describe "validations" do + it "can be created with all fields" do + b = Work.new(category: "Album", title: "testing", creator: "Fake Creator", publication_year: 1999, description: "A Good Album") + b.must_be :valid? + end # validations + + it "requires a title" do + b = Work.new + is_valid = b.valid? + is_valid.must_equal false + b.errors.messages.must_include :title + end #require title + + it "requires a unique title" do + title = "test book" + b1 = Work.create!(title: title) + b2 = Work.new(title: title) + + b2.wont_be :valid? + end #uniqueness + end #work do + + describe "description" do + it "rejects descriptions longer than 200 chars" do + desc = "a" * 201 + b = Work.new(title: "Fake Title", description: desc) + + # b.wont_be :valid? + is_valid = b.valid? + is_valid.must_equal false + + b.errors.messages.must_include :description + end + + it "allows descriptions <= 200 chars" do + descriptions = [ + "a" * 199, + "a" * 10, + "a" * 0 + ] + + descriptions.each do |desc| + b = Work.new(title: "test", description: desc) + b.must_be :valid? + end + end + end + + + + # describe "relations" do + # it "has an author" do + # b = books(:poodr) + # a = authors(:metz) + # + # binding.pry + # + # b.must_respond_to :author + # b.author.must_equal a + # b.author_id.must_equal a.id + # end +# +# it "has a collection of genres" do +# b = Book.new +# b.must_respond_to :genres +# b.genres.must_be :empty? +# +# g = Genre.create!(name: "test genre") +# b.genres << g +# b.genres.must_include g +# end +# end +# +# describe "age" do +# end +end #all tests From ec08769cdb0448141c22ebb6e641b821b73fca6e Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 14:46:56 -0700 Subject: [PATCH 22/26] More user tests, update error messages --- app/controllers/users_controller.rb | 28 +-- app/models/user.rb | 3 +- test/controllers/users_controller_test.rb | 204 +++++++++++++++++++++- test/models/user_test.rb | 92 ++++++++++ test/models/work_test.rb | 150 ++++++++-------- 5 files changed, 382 insertions(+), 95 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 418ecb3205..da74c430e4 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -9,15 +9,22 @@ def new def create @user = User.new(user_params) + @user.save session[:logged_in_user] = @user.id - redirect_to works_path - # if @user.save - # flash[:success] = "Welcome New User!" - # redirect_to works__path - # else - # render :new - # end + + if @user.save + flash[:status] = :success + flash[:message] = "Welcome New User!" + redirect_to users_path + return + else + flash.now[:status] = :failure + flash.now[:message] = "Failed to create new user" + flash.now[:details] = @user.errors.messages + render :new, status: :bad_request + # render :new + end end def login_form @@ -45,13 +52,6 @@ def show - # votes_index GET /votes/index(.:format) votes#index - # user_votes POST /users/:user_id/votes(.:format) votes#create - # new_user_vote GET /users/:user_id/votes/new(.:format) votes#new - - - - private def user_params diff --git a/app/models/user.rb b/app/models/user.rb index 5ccdac341e..09f912a354 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,6 +2,7 @@ class User < ApplicationRecord has_many :votes, dependent: :destroy validates :username, presence: {message: "%{value} must be given"} + validates :username, uniqueness: {message: "Username already exists/"} def list_votes(user) @votes = Vote.all @@ -14,7 +15,7 @@ def list_votes(user) return user_works end - + diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 439b02a897..bea5a74873 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,9 +1,203 @@ require "test_helper" + +describe UsersController do + describe "index" do + it "returns a success status for all works" do + get users_path + must_respond_with :success + end #success1 + + it "returns a success status when there are no users" do + User.destroy_all + get users_path + must_respond_with :success + end #success2 + end#index tests + + + describe "new" do + it "gets a new form" do + get new_user_path + must_respond_with :success + end #get form + end #new tests + + + describe "create" do + it "adds the User to the DB and redirects when the user data is valid" do + # Arrange + user_data = { + user: { + username: "Test Name" + } + } + # Test data should result in a valid work, otherwise + # the test is broken + User.new(user_data[:user]).must_be :valid? + + start_user_count = User.count + + # Act + post users_path, params: user_data + + # Assert + must_respond_with :redirect + must_redirect_to users_path + + User.count.must_equal start_user_count + 1 + end #adds work test + + # it "sends bad_request when the work data is bogus" do + # # Arrange + # invalid_work_data = { + # work: { + # # NO TITLE!!! + # creator: "Fake Creator" + # } + # } + # # Double check the data is truly invalid + # Work.new(invalid_work_data[:work]).wont_be :valid? + # + # start_work_count = Work.count + # + # # Act + # post works_path, params: invalid_work_data + # + # # Assert + # must_respond_with :bad_request + # # Vanilla rails doesn't provide any way to do this + # # assert_template :new + # Work.count.must_equal start_work_count + # end #bad request + # end #create + # + # + # + + end #create tests + + + +end #all tests + +# describe "show" do +# it "returns success when given a valid work ID" do +# # Arrange +# work_id = Work.first.id # -# describe UsersController do -# it "should get index" do -# get users_index_url -# value(response).must_be :success? +# # Act +# get work_path(work_id) +# +# # Assert +# must_respond_with :success +# end +# +# it "returns not_found when given an invalid book ID" do +# invalid_work_id = Work.last.id + 1 +# get work_path(invalid_work_id) +# must_respond_with :not_found +# end +# end +# +# describe "edit" do +# it "returns success when given a valid work ID" do +# # Arrange +# work_id = Work.first.id +# +# # Act +# get edit_work_path(work_id) +# +# # Assert +# must_respond_with :success +# end +# +# it "returns not_found when given an invalid work ID" do +# invalid_work_id = Work.last.id + 1 +# get edit_work_path(invalid_work_id) +# must_respond_with :not_found +# end # end # -# end +# +# describe "update" do +# it "returns success if the work ID is valid and the change is valid" do +# work = Work.first +# work_data = { +# work: { +# title: "New title", +# creator: "New Creator" +# } +# } +# work.update_attributes(work_data[:work]) +# work.must_be :valid? +# +# patch work_path(work), params: work_data +# +# must_respond_with :redirect +# must_redirect_to work_path +# +# # Check that the change went through +# work.reload +# work.title.must_equal work_data[:work][:title] +# end +# +# it "returns not_found if the book ID is invalid" do +# invalid_work_id = Work.last.id + 1 +# work_data = { +# work: { +# title: "New title", +# creator: "New Creator" +# } +# } +# +# patch work_path(invalid_work_id), params: work_data +# +# must_respond_with :not_found +# end +# +# it "returns bad_request if the change is invalid" do +# work = Work.first +# invalid_work_data = { +# work: { +# title: "" +# } +# } +# # Check that the update is actually invalid +# work.update_attributes(invalid_work_data[:work]) +# work.wont_be :valid? +# +# patch work_path(work), params: invalid_work_data +# +# must_respond_with :bad_request +# +# work.reload +# work.title.wont_equal invalid_work_data[:work][:title] +# end +# end +# +# describe "destroy" do +# it "returns success and destroys the book when given a valid book ID" do +# # Arrange +# work_id = Work.first.id +# +# # Act +# delete work_path(work_id) +# +# # Assert +# must_respond_with :redirect +# must_redirect_to works_path +# Work.find_by(id: work_id).must_be_nil +# end +# +# it "returns not_found when given an invalid book ID" do +# invalid_work_id = Work.last.id + 1 +# +# start_work_count = Work.count +# +# delete work_path(invalid_work_id) +# +# must_respond_with :not_found +# Work.count.must_equal start_work_count +# end +# end +# end diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 0768f790aa..7679b75ba7 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -7,3 +7,95 @@ # value(user).must_be :valid? # end # end + +# Write at least one test for each custom method on a model +# Write at least one test for each model relationship on a model +# Write at least two tests for each validation on a model +# Write at least two tests for each scope on a model (we'll talk about scopes next week) + +# describe Work do +# let(:work) { Work.new } +# +# it "must be valid" do +# value(work).must_be :valid? +# end +# params.require(:work).permit(:category, :title, :creator, :publication_year, :description) +# +# describe Work do +# let :work{ Work.first } +# describe "validations" do +# it "can be created with all fields" do +# b = Work.new(category: "Album", title: "testing", creator: "Fake Creator", publication_year: 1999, description: "A Good Album") +# b.must_be :valid? +# end # validations +# +# it "requires a title" do +# b = Work.new +# is_valid = b.valid? +# is_valid.must_equal false +# b.errors.messages.must_include :title +# end #require title +# +# it "requires a unique title" do +# title = "test book" +# b1 = Work.create!(title: title) +# b2 = Work.new(title: title) +# +# b2.wont_be :valid? +# end #uniqueness +# end #work do +# +# describe "description" do +# it "rejects descriptions longer than 200 chars" do +# desc = "a" * 201 +# b = Work.new(title: "Fake Title", description: desc) +# +# # b.wont_be :valid? +# is_valid = b.valid? +# is_valid.must_equal false +# +# b.errors.messages.must_include :description +# end +# +# it "allows descriptions <= 200 chars" do +# descriptions = [ +# "a" * 199, +# "a" * 10, +# "a" * 0 +# ] +# +# descriptions.each do |desc| +# b = Work.new(title: "test", description: desc) +# b.must_be :valid? +# end +# end +# end +# +# +# +# # describe "relations" do +# # it "has an author" do +# # b = books(:poodr) +# # a = authors(:metz) +# # +# # binding.pry +# # +# # b.must_respond_to :author +# # b.author.must_equal a +# # b.author_id.must_equal a.id +# # end +# # +# # it "has a collection of genres" do +# # b = Book.new +# # b.must_respond_to :genres +# # b.genres.must_be :empty? +# # +# # g = Genre.create!(name: "test genre") +# # b.genres << g +# # b.genres.must_include g +# # end +# # end +# # +# # describe "age" do +# # end +# end #all tests diff --git a/test/models/work_test.rb b/test/models/work_test.rb index a1a63df284..2ad325a070 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -7,82 +7,82 @@ # value(work).must_be :valid? # end # params.require(:work).permit(:category, :title, :creator, :publication_year, :description) - -describe Work do - let :work{ Work.first } - describe "validations" do - it "can be created with all fields" do - b = Work.new(category: "Album", title: "testing", creator: "Fake Creator", publication_year: 1999, description: "A Good Album") - b.must_be :valid? - end # validations - - it "requires a title" do - b = Work.new - is_valid = b.valid? - is_valid.must_equal false - b.errors.messages.must_include :title - end #require title - - it "requires a unique title" do - title = "test book" - b1 = Work.create!(title: title) - b2 = Work.new(title: title) - - b2.wont_be :valid? - end #uniqueness - end #work do - - describe "description" do - it "rejects descriptions longer than 200 chars" do - desc = "a" * 201 - b = Work.new(title: "Fake Title", description: desc) - - # b.wont_be :valid? - is_valid = b.valid? - is_valid.must_equal false - - b.errors.messages.must_include :description - end - - it "allows descriptions <= 200 chars" do - descriptions = [ - "a" * 199, - "a" * 10, - "a" * 0 - ] - - descriptions.each do |desc| - b = Work.new(title: "test", description: desc) - b.must_be :valid? - end - end - end - - - - # describe "relations" do - # it "has an author" do - # b = books(:poodr) - # a = authors(:metz) - # - # binding.pry - # - # b.must_respond_to :author - # b.author.must_equal a - # b.author_id.must_equal a.id - # end # -# it "has a collection of genres" do -# b = Book.new -# b.must_respond_to :genres -# b.genres.must_be :empty? +# describe Work do +# let :work{ Work.first } +# describe "validations" do +# it "can be created with all fields" do +# b = Work.new(category: "Album", title: "testing", creator: "Fake Creator", publication_year: 1999, description: "A Good Album") +# b.must_be :valid? +# end # validations +# +# it "requires a title" do +# b = Work.new +# is_valid = b.valid? +# is_valid.must_equal false +# b.errors.messages.must_include :title +# end #require title +# +# it "requires a unique title" do +# title = "test book" +# b1 = Work.create!(title: title) +# b2 = Work.new(title: title) +# +# b2.wont_be :valid? +# end #uniqueness +# end #work do +# +# describe "description" do +# it "rejects descriptions longer than 200 chars" do +# desc = "a" * 201 +# b = Work.new(title: "Fake Title", description: desc) +# +# # b.wont_be :valid? +# is_valid = b.valid? +# is_valid.must_equal false # -# g = Genre.create!(name: "test genre") -# b.genres << g -# b.genres.must_include g +# b.errors.messages.must_include :description +# end +# +# it "allows descriptions <= 200 chars" do +# descriptions = [ +# "a" * 199, +# "a" * 10, +# "a" * 0 +# ] +# +# descriptions.each do |desc| +# b = Work.new(title: "test", description: desc) +# b.must_be :valid? +# end +# end # end -# end # -# describe "age" do -# end -end #all tests +# +# +# # describe "relations" do +# # it "has an author" do +# # b = books(:poodr) +# # a = authors(:metz) +# # +# # binding.pry +# # +# # b.must_respond_to :author +# # b.author.must_equal a +# # b.author_id.must_equal a.id +# # end +# # +# # it "has a collection of genres" do +# # b = Book.new +# # b.must_respond_to :genres +# # b.genres.must_be :empty? +# # +# # g = Genre.create!(name: "test genre") +# # b.genres << g +# # b.genres.must_include g +# # end +# # end +# # +# # describe "age" do +# # end +# end #all tests From a113f1a082b9b55c2bd52de55b21f26ac76633aa Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 16:02:10 -0700 Subject: [PATCH 23/26] More user tests, fix some customer methods --- app/controllers/users_controller.rb | 12 +- app/models/user.rb | 6 +- app/models/work.rb | 26 ++- app/views/works/index.html.erb | 5 +- app/views/works/new.html.erb | 3 +- app/views/works/show.html.erb | 2 + test/controllers/users_controller_test.rb | 193 +++++++++++----------- 7 files changed, 132 insertions(+), 115 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index da74c430e4..12ea5270d8 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -40,14 +40,20 @@ def login session[:logged_in_user] = user.id redirect_to works_path else - flash.now[:status] = :failure - flash.now[:message] = "No such user exists." + flash.now[:status] = :not_found + flash.now[:message] = "No such user exists. Please sign up as a new user." redirect_to new_user_path end end + + def show - @user = User.find(params[:id]) + @user = User.find_by(id: params[:id]) + unless @user + head :not_found + end + return @user end diff --git a/app/models/user.rb b/app/models/user.rb index 09f912a354..8f253c1141 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,8 +1,10 @@ class User < ApplicationRecord has_many :votes, dependent: :destroy - validates :username, presence: {message: "%{value} must be given"} - validates :username, uniqueness: {message: "Username already exists/"} + validates :username, presence: {message: "Username is required."} + validates :username, uniqueness: {message: "Username already exists."} + + def list_votes(user) @votes = Vote.all diff --git a/app/models/work.rb b/app/models/work.rb index abfce8ec51..8aecfe5a77 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -7,10 +7,15 @@ class Work < ApplicationRecord validates :description, length: { maximum: 200, message: "The description is too long" } -def testing(input) +# def testing(input) +# @votes = Vote.all +# total = @votes.where(work_id: input).count +# return total +# end + +def count @votes = Vote.all - total = @votes.where(work_id: input).count - return total + return @votes.where(work_id: id).count end def list_users(work) @@ -24,11 +29,14 @@ def list_users(work) return work_voters end -# def score -# @work = Work.find(params[:id]) -# @votes = Vote.all -# @votes.where(work_id: @work.id).count -# return scores -# end +def users_voted + @votes = Vote.all + @users = User.all + votes = @votes.where(work_id: id) + votes.each do |vote| + work_voters << @users.find(vote.user_id).username + end + return work_voters +end end diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index c5805afa48..5c8bcdb5f9 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -38,8 +38,8 @@ <% @works.each do |work| %> -<%# @votes.where(work_id: work.id).count -%> -<%# @votes = Vote.all work.testing(work.id)%> <% @works.each do |work| %> <% end %> - <%# @vote = Vote.new %> - <%#= form_for @vote url {action: create} - - <%# orm_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'}). %> - <%#{}= form_for(:vote, url: {:controller => votes, action: create} do |f| %> - <%#= form_tag("/upvote", method: :post) do %> - <%#= text_field :user_id, work.id %> - <%#= submit_tag "Vote" %> - - - - <%#= link_to "Vote", new_work_vote_path(work.id) %> - diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index cdb152c1fd..886e4fb993 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -6,7 +6,6 @@ <% end %> -
  • Category: <%= @work.category %>
  • Creator: <%= @work.creator %>
  • diff --git a/app/views/works/topten.html.erb b/app/views/works/topten.html.erb index c070e74aa8..be22c25857 100644 --- a/app/views/works/topten.html.erb +++ b/app/views/works/topten.html.erb @@ -7,6 +7,6 @@

    -

    Logged in user is <%= session[:logged_in_user] %>

    + diff --git a/config/routes.rb b/config/routes.rb index aab58f185b..5f43bca610 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,57 +6,22 @@ get 'login', to: 'users#login_form', as: 'login' post 'login', to: 'users#login' + get 'topten', to: 'works#topten', as: 'topten' + resources :users resources :works resources :votes - - get 'upvote', to: 'votes#new', as: 'upvote' - post 'upvote', to: 'votes#create' - end -# <% if user_signed_in? && current_user.likes?(post) %> -# <%= link_to "Unlike", post_like_path(post), method: :delete, remote: true %> -# <% else %> -# <%= link_to "Like", post_like_path(post), method: :post, remote: true %> -# <% end %> -# -# <%= post.likes.count %> -# -# # <% post.likes.each do |like| %> -# # <%= image_tag like.user.avatar_url, width: 20 %> -# # <% end %> -# <% @posts.each do |post| %> -#

    <%= link_to post.title, post %>

    -#
    <%= simple_format post.body %>
    -#

    <%= render partial: "likes", locals: {post: post} %>

    -#
    -# <% end %> - # get '/votes/new', to: 'votes#new', as: 'new_vote' - # - # get '/votes', to: 'votes#create', as: 'votes' - - - - # get 'works/:id/upvote', to: 'works#upvote', as: 'upvote_work' - # get '/works/topten', to: 'works#topten', as: 'topten' - # - # get '/votes/new', to: 'votes#new', as: 'new_vote' - # post '/votes', to: 'votes#create', as: 'votes' -# resources :works -# post '/work/:id/voted_for', to: 'work#voted_for', as: 'vote_counted' -# resources :authors, only: [:index, :new, :create] do -# resources :books, only: [:index, :new] -# end # get '/works/:id/upvote', to: 'works#upvote, ' diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index 569be42dbf..e4ead8ac02 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,202 +1,115 @@ -require "test_helper" - -describe UsersController do - describe "index" do - it "returns a success status for all works" do - get users_path - must_respond_with :success - end #success1 - - it "returns a success status when there are no users" do - User.destroy_all - get users_path - must_respond_with :success - end #success2 - end#index tests - - - describe "new" do - it "gets a new form" do - get new_user_path - must_respond_with :success - end #get form - end #new tests - - - describe "create" do - it "adds the User to the DB and redirects when the user data is valid" do - # Arrange - user_data = { - user: { - username: "Test Name" - } - } - # Test data should result in a valid work, otherwise - # the test is broken - User.new(user_data[:user]).must_be :valid? - - start_user_count = User.count - - # Act - post users_path, params: user_data - - # Assert - must_respond_with :redirect - must_redirect_to users_path - - User.count.must_equal start_user_count + 1 - end #adds work test - - it "sends bad_request when the user data is bogus" do - # Arrange - invalid_user_data = { - user: { - # NO TITLE!!! - username: "" - } - } - # Double check the data is truly invalid - User.new(invalid_user_data[:user]).wont_be :valid? - - start_user_count = User.count - - # Act - post users_path, params: invalid_user_data - - # Assert - must_respond_with :bad_request - # Vanilla rails doesn't provide any way to do this - # assert_template :new - User.count.must_equal start_user_count - end #bad request - end #create tests - - describe "show" do - it "returns success when given a valid User ID" do - # Arrange - user_id = User.first.id - - # Act - get user_path(user_id) - - # Assert - must_respond_with :success - end #success - - - it "returns not_found when given an invalid user ID" do - invalid_user_id = User.last.id + 1 - get user_path(invalid_user_id) - must_respond_with :not_found - end #invalid - - - - - end #showtests - describe "log in" do - it "returns success when given a valid username" do - # Arrange - user_id = User.first.id - # Act - get login_path(user_id) - - # Assert - must_respond_with :success - end - - it "renders new form if no username is entered" do - invalid_user_id = User.first.id + 1 - get login_path(invalid_user_id) - must_respond_with :not_found - end - end - - - -end #all tests - - -# -# describe "update" do -# it "returns success if the work ID is valid and the change is valid" do -# work = Work.first -# work_data = { -# work: { -# title: "New title", -# creator: "New Creator" +# require "test_helper" +# +# describe UsersController do +# describe "index" do +# it "returns a success status for all works" do +# get users_path +# must_respond_with :success +# end #success1 +# +# it "returns a success status when there are no users" do +# User.destroy_all +# get users_path +# must_respond_with :success +# end #success2 +# end#index tests +# +# +# describe "new" do +# it "gets a new form" do +# get new_user_path +# must_respond_with :success +# end #get form +# end #new tests +# +# +# describe "create" do +# it "adds the User to the DB and redirects when the user data is valid" do +# # Arrange +# user_data = { +# user: { +# username: "Test Name" # } # } -# work.update_attributes(work_data[:work]) -# work.must_be :valid? +# # Test data should result in a valid work, otherwise +# # the test is broken +# User.new(user_data[:user]).must_be :valid? # -# patch work_path(work), params: work_data +# start_user_count = User.count # +# # Act +# post users_path, params: user_data +# +# # Assert # must_respond_with :redirect -# must_redirect_to work_path +# must_redirect_to users_path # -# # Check that the change went through -# work.reload -# work.title.must_equal work_data[:work][:title] -# end +# User.count.must_equal start_user_count + 1 +# end #adds work test # -# it "returns not_found if the book ID is invalid" do -# invalid_work_id = Work.last.id + 1 -# work_data = { -# work: { -# title: "New title", -# creator: "New Creator" +# it "sends bad_request when the user data is bogus" do +# # Arrange +# invalid_user_data = { +# user: { +# # NO TITLE!!! +# username: "" # } # } +# # Double check the data is truly invalid +# User.new(invalid_user_data[:user]).wont_be :valid? # -# patch work_path(invalid_work_id), params: work_data +# start_user_count = User.count # -# must_respond_with :not_found -# end +# # Act +# post users_path, params: invalid_user_data # -# it "returns bad_request if the change is invalid" do -# work = Work.first -# invalid_work_data = { -# work: { -# title: "" -# } -# } -# # Check that the update is actually invalid -# work.update_attributes(invalid_work_data[:work]) -# work.wont_be :valid? +# # Assert +# must_respond_with :bad_request +# # Vanilla rails doesn't provide any way to do this +# # assert_template :new +# User.count.must_equal start_user_count +# end #bad request +# end #create tests # -# patch work_path(work), params: invalid_work_data +# describe "show" do +# it "returns success when given a valid User ID" do +# # Arrange +# user_id = User.first.id # -# must_respond_with :bad_request +# # Act +# get user_path(user_id) # -# work.reload -# work.title.wont_equal invalid_work_data[:work][:title] -# end -# end +# # Assert +# must_respond_with :success +# end #success # -# describe "destroy" do -# it "returns success and destroys the book when given a valid book ID" do -# # Arrange -# work_id = Work.first.id # +# it "returns not_found when given an invalid user ID" do +# invalid_user_id = User.last.id + 1 +# get user_path(invalid_user_id) +# must_respond_with :not_found +# end #invalid +# +# +# end #showtests +# describe "log in" do +# it "returns success when given a valid username" do +# # Arrange +# user_id = User.first.id # # Act -# delete work_path(work_id) +# get login_path(user_id) # # # Assert -# must_respond_with :redirect -# must_redirect_to works_path -# Work.find_by(id: work_id).must_be_nil +# must_respond_with :success # end # -# it "returns not_found when given an invalid book ID" do -# invalid_work_id = Work.last.id + 1 -# -# start_work_count = Work.count -# -# delete work_path(invalid_work_id) -# +# it "renders new form if no username is entered" do +# invalid_user_id = User.first.id + 1 +# get login_path(invalid_user_id) # must_respond_with :not_found -# Work.count.must_equal start_work_count # end # end -# end +# +# +# +# end #all tests diff --git a/test/controllers/votes_controller_test.rb b/test/controllers/votes_controller_test.rb index 5ee394fb8d..13e5aa606e 100644 --- a/test/controllers/votes_controller_test.rb +++ b/test/controllers/votes_controller_test.rb @@ -7,3 +7,31 @@ # end # # end + + +describe VotesController do + describe "create" do + it "creates a new vote" do + # Arrange + vote_data = { + vote: { + user_id: 1, + work_id: 2 + } + } + + Vote.create(vote_data[:vote]).must_be :valid? + + start_vote_count = Vote.count + + # Act + post votes_path, params: vote_data + + # Assert + must_respond_with :redirect + must_redirect_to votes_path + + Vote.count.must_equal start_vote_count + 1 + end #adds work test + end #r +end #all Vote tests From 16ee45a1e4a18f1b99a009bdce7d1049fce94606 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 20:46:00 -0700 Subject: [PATCH 25/26] Cleaning up styles a little --- app/assets/stylesheets/application.css | 62 ++++++++++++++++++++------ app/controllers/works_controller.rb | 2 +- app/views/layouts/application.html.erb | 22 +++++---- app/views/main/index.html.erb | 2 + app/views/works/index.html.erb | 8 +--- app/views/works/show.html.erb | 9 ++-- app/views/works/topten.html.erb | 13 +----- config/routes.rb | 48 ++------------------ 8 files changed, 74 insertions(+), 92 deletions(-) diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css index d05ea0f511..5125854355 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.css @@ -1,15 +1,49 @@ /* - * 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. - * - *= require_tree . - *= require_self - */ +* 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. +* +*= require_tree . +*= require_self +*/ +header h1 { + background-color: green; +} + +.error-messages { + color: red; +} + +.field_with_errors { + display: inline; +} + +.flash.success { + color: green; +} + +.flash.failure { + color: red; +} + + +nav div { + padding: 0px; + box-sizing: border-box; +} + +nav div .button { + box-sizing: border-box; + border-width: 0px; +} + +ul { + list-style-type: none; +} diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index e9475ca6f9..9c8f6fe43f 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -65,7 +65,7 @@ def destroy end def topten - + @works = Work.group(:category) end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index b836039ae6..76a42f9bd2 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -10,14 +10,20 @@
    -
+ Votes + Category
+ Votes Will Go Here + <%= work.category %> <%= work.description %> + UpVote Button will Go Here +
- <%= user.username%> + <%= link_to user.username, user%> Votes diff --git a/app/views/users/login_form.html.erb b/app/views/users/login_form.html.erb new file mode 100644 index 0000000000..88263dc3c7 --- /dev/null +++ b/app/views/users/login_form.html.erb @@ -0,0 +1,6 @@ +<%= form_tag do %> + +<%= label_tag "Username" %> +<%= text_field :user, :username %> +<%= submit_tag "Log In!" %> +<% end %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb new file mode 100644 index 0000000000..3d60933448 --- /dev/null +++ b/app/views/users/new.html.erb @@ -0,0 +1 @@ +

Page for creating a new user

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

Show Page for User

+ +<%= @user.username %> + +

+ Books this person voted for + go through votes associated with each user, then find title associated with vote (through works table) + if no votes, put not yet voted. +

diff --git a/app/views/votes/new.html.erb b/app/views/votes/new.html.erb new file mode 100644 index 0000000000..51a1334f3b --- /dev/null +++ b/app/views/votes/new.html.erb @@ -0,0 +1,24 @@ +

This is the voting page

+ +<%= form_for @vote do |f| %> +
    + +
  • + <%= f.label :creator %> + <%= f.text_field :creator, class: "form-field"%> +
  • +
  • + <%= f.label :publication_year %> + <%= f.text_field :publication_year, class: "form-field"%> +
  • +
  • + <%= f.label :description %> + <%= f.text_field :description, class: "form-field"%> +
  • + +
  • + <%= f.submit class: "submit"%> +
  • + +
+ <% end %> diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 6fb230939c..5ca7a18a19 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,5 +1,7 @@

Welcome to Media Ranker

+

Media Spotlight: Link to Top Vote Getter

+ @@ -21,6 +23,9 @@ + <% @works.each do |work| %> @@ -44,7 +49,7 @@ <%= work.description %> <% end %> diff --git a/config/routes.rb b/config/routes.rb index 2108f184a9..8141b66612 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,8 +1,29 @@ Rails.application.routes.draw do + # get '/', to: 'work#index', as: 'root' + root 'main#index' + + + # resources :works + # post '/work/:id/voted_for', to: 'work#voted_for', as: 'vote_counted' + + + # resources :authors, only: [:index, :new, :create] do + # resources :books, only: [:index, :new] + # end + + get 'login', to: 'users#login_form', as: 'login' + post 'login', to: 'users#login' + get 'votes/index' get '/users/index' + get '/votes/new', to: 'votes#new', as: 'new_vote' + + get '/votes', to: 'votes#create', as: 'votes' + + + get '/users/new', to: 'users#new', as: 'new_user' get '/users/:id/edit', to: 'users#edit', as: 'edit_user' diff --git a/test/controllers/main_controller_test.rb b/test/controllers/main_controller_test.rb new file mode 100644 index 0000000000..33c8140433 --- /dev/null +++ b/test/controllers/main_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe MainController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end From 73119ed380d2ab0380d4f341ca2195a56bfe16a2 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 14:28:25 -0700 Subject: [PATCH 11/26] Fix Logins --- app/controllers/users_controller.rb | 30 +++++++++++++++++++++++--- app/views/layouts/application.html.erb | 12 ++++++----- app/views/users/new.html.erb | 13 +++++++++++ 3 files changed, 47 insertions(+), 8 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 21e81adcd6..752c45f232 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -3,9 +3,26 @@ def index @users = User.all end - def login_form + def new + @user = User.new end + def create + @user = User.new(user_params) + @user.save + redirect_to login_path + # if @user.save + # flash[:success] = "Welcome New User!" + # redirect_to works_index_path + # else + # render :new + # end + end + + def login_form + end + + def login username = params[:user][:username] @@ -13,15 +30,22 @@ def login if user session[:logged_in_user] = user.id - redirect_to works_path + redirect_to works_index_path else flash.now[:status] = :failure flash.now[:message] = "No such user exists." - render :login_form + redirect_to new_user_path end end def show @user = User.find(params[:id]) end + + private + + def user_params + return params.require(:user).permit(:username) + end + end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6924c84fa4..355ae067f7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,10 +11,12 @@
@@ -25,7 +27,7 @@

<% else %>

- Not Logged In Link to Log in Form + link_to " Create User "

<% end %>
diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index 3d60933448..c5e61d6676 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1 +1,14 @@

Page for creating a new user

+ +<%= form_for @user do |f| %> +
    +
  • + <%= f.label :username %> + <%= f.text_field :username, class: "form-field"%> +
  • + +
  • + <%= f.submit class: "submit"%> +
  • +
+<% end %> From ae3d4d21a22a0c25e0aeec0f923c8ada38bc1efe Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 14:54:34 -0700 Subject: [PATCH 12/26] Fix bug in log-in --- app/controllers/users_controller.rb | 9 ++++++++- app/controllers/votes_controller.rb | 5 ++++- app/controllers/works_controller.rb | 5 +++++ app/views/layouts/application.html.erb | 5 +++-- app/views/main/index.html.erb | 2 +- app/views/users/login_form.html.erb | 3 +++ app/views/users/new.html.erb | 2 +- app/views/users/show.html.erb | 6 +++--- app/views/works/index.html.erb | 6 ++++++ app/views/works/show.html.erb | 4 ++++ app/views/works/topten.html.erb | 7 +++++++ config/routes.rb | 7 +++++-- 12 files changed, 50 insertions(+), 11 deletions(-) create mode 100644 app/views/works/topten.html.erb diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 752c45f232..b9b1878fe0 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -10,7 +10,8 @@ def new def create @user = User.new(user_params) @user.save - redirect_to login_path + session[:logged_in_user] = @user.id + redirect_to works_index_path # if @user.save # flash[:success] = "Welcome New User!" # redirect_to works_index_path @@ -42,6 +43,12 @@ def show @user = User.find(params[:id]) end + # def upvote + # @work = Work.find(params[:id]) + # @work.votes.create + # redirect_to works_index_path + # end + private def user_params diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index 0183c891ad..9be847fd6e 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -3,5 +3,8 @@ def index end def new - end + end + + def create + end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 992754d50d..5fae162978 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -38,6 +38,11 @@ def destroy redirect_to works_index_path end + def topten + end + + + private def work_params diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 355ae067f7..5fd43a3937 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,12 +11,13 @@
diff --git a/app/views/main/index.html.erb b/app/views/main/index.html.erb index b630b4f4ad..d1946a9f70 100644 --- a/app/views/main/index.html.erb +++ b/app/views/main/index.html.erb @@ -1 +1 @@ -

Main Page

+

Welcome to Media Ranker

diff --git a/app/views/users/login_form.html.erb b/app/views/users/login_form.html.erb index 88263dc3c7..45648d3117 100644 --- a/app/views/users/login_form.html.erb +++ b/app/views/users/login_form.html.erb @@ -1,3 +1,6 @@ + +

Please Log In

+ <%= form_tag do %> <%= label_tag "Username" %> diff --git a/app/views/users/new.html.erb b/app/views/users/new.html.erb index c5e61d6676..54af603c9d 100644 --- a/app/views/users/new.html.erb +++ b/app/views/users/new.html.erb @@ -1,4 +1,4 @@ -

Page for creating a new user

+

Choose a User Name

<%= form_for @user do |f| %>
    diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index f8e3fd9694..353f2e7ddb 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,9 +1,9 @@ -

    Show Page for User

    -<%= @user.username %> + +

    Welcome<%= @user.username %>

    Books this person voted for go through votes associated with each user, then find title associated with vote (through works table) - if no votes, put not yet voted. + if no votes, put not yet voted.

    diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 5ca7a18a19..e62ec7c47b 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -1,6 +1,12 @@

    Welcome to Media Ranker

    Media Spotlight: Link to Top Vote Getter

    +

    + Rank by category and then number of votes +

    +

    + List of all media, votes, title, creator, year, upvote +

Description + VOTE! +
- UpVote Button will Go Here + <%= link_to "Vote", new_vote_path %>
diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index befd32980c..49a94dba63 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -4,6 +4,10 @@ User who created it

+

+ List of users who voted for this work +

+

Link to Edit, Link to Upvote, Link to Delete

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

This will have the top media

+ +

Link to Spotlight

+ +

+ top in each category with creator and number of votes +

diff --git a/config/routes.rb b/config/routes.rb index 8141b66612..2482011a7e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,13 +16,12 @@ get 'votes/index' - get '/users/index' - get '/votes/new', to: 'votes#new', as: 'new_vote' get '/votes', to: 'votes#create', as: 'votes' + get '/users/index' get '/users/new', to: 'users#new', as: 'new_user' @@ -41,6 +40,8 @@ get '/works/new', to: 'works#new', as: 'new_work' + get '/works/topten', to: 'works#topten', as: 'topten' + get '/works/:id/edit', to: 'works#edit', as: 'edit_work' post '/works', to: 'works#create', as: 'works' @@ -51,5 +52,7 @@ delete '/works/:id', to: 'works#destroy' + + # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html end From 975bbb23211e42bb5015dabfb2c7c35a80e2d215 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 18:11:35 -0700 Subject: [PATCH 13/26] votes seem to be working again maybe --- app/controllers/users_controller.rb | 19 ++++--- app/controllers/votes_controller.rb | 38 ++++++++++++-- app/controllers/works_controller.rb | 25 +++++++-- app/models/user.rb | 5 +- app/models/vote.rb | 3 ++ app/models/work.rb | 5 +- app/views/layouts/application.html.erb | 6 +-- app/views/votes/new.html.erb | 29 +++-------- app/views/works/index.html.erb | 2 +- app/views/works/topten.html.erb | 5 ++ config/routes.rb | 71 ++++++++++++++++---------- 11 files changed, 137 insertions(+), 71 deletions(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index b9b1878fe0..418ecb3205 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -11,10 +11,10 @@ def create @user = User.new(user_params) @user.save session[:logged_in_user] = @user.id - redirect_to works_index_path + redirect_to works_path # if @user.save # flash[:success] = "Welcome New User!" - # redirect_to works_index_path + # redirect_to works__path # else # render :new # end @@ -31,7 +31,7 @@ def login if user session[:logged_in_user] = user.id - redirect_to works_index_path + redirect_to works_path else flash.now[:status] = :failure flash.now[:message] = "No such user exists." @@ -43,11 +43,14 @@ def show @user = User.find(params[:id]) end - # def upvote - # @work = Work.find(params[:id]) - # @work.votes.create - # redirect_to works_index_path - # end + + + # votes_index GET /votes/index(.:format) votes#index + # user_votes POST /users/:user_id/votes(.:format) votes#create + # new_user_vote GET /users/:user_id/votes/new(.:format) votes#new + + + private diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index 9be847fd6e..b091b0af1e 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -1,10 +1,40 @@ class VotesController < ApplicationController - def index - end - def new + @vote = Vote.new + # if params[:user_id] + @vote.user_id = session[:logged_in_user] + @vote.work_id = params[:work_id] + @vote.save + redirect_to works_path + # end end - def create + # def create + # @vote = Vote.new(vote_params) + # @vote.save + # redirect_to works_path + # end + # def create + # @vote = Vote.new(vote_params) + # @vote.work = Work.find(params[:work_id]) + # @vote.save + # end + + + private + + def vote_params + return params.require(:vote).permit(:user_id, :work_id) end + + end + +# +# def create +# @vote = Vote.new +# @vote.user_id = session[:logged_in_user] +# @vote.work_id = @work.id +# @vote.save +# redirect_to works_path +# end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 5fae162978..4897bec307 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -15,7 +15,7 @@ def create @work = Work.new(work_params) if @work.save flash[:success] = "Saved!" - redirect_to works_index_path + redirect_to works_path else render :new end @@ -29,19 +29,38 @@ def update @work = Work.find(params[:id]) @work.update_attributes(work_params) @work.save - redirect_to works_index_path + redirect_to works_path end def destroy @work = Work.find(params[:id]) @work.destroy - redirect_to works_index_path + redirect_to works_path end def topten end + # def upvote + # @work = Work.find(params[:id]) + # @work.votes.create + # redirect_to new_vote_path + # + # # render :topten + # #want to create a vote, which includes this id as the work id and the logged in user id as the user-id + # # @work.votes.create + # # redirect_to works_index_path + # # get 'works/:id/upvote', to: 'works#upvote', as: 'upvote_work' + # # + # + # end +# def upvote +# @startup = Startup.find(params[:id]) +# upvote = @startup.upvotes.find_or_create_by(user: current_user) +# upvote.save +# redirect_to startups_path +# end private diff --git a/app/models/user.rb b/app/models/user.rb index 1565249500..ff4ce37990 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -2,12 +2,15 @@ class User < ApplicationRecord has_many :votes, dependent: :destroy validates :username, presence: {message: "%{value} must be given"} - + def list_votes #go through votes #if the user id is found, print name of work end + # def votes?(work) + # work.votes.where(user_id: id).any? + # end # @zombies.each do |zombie| # zombie.name # zombie.brain.flavor diff --git a/app/models/vote.rb b/app/models/vote.rb index f26566620e..8250ba6f86 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -2,4 +2,7 @@ class Vote < ApplicationRecord belongs_to :user belongs_to :work + + validates :work, uniqueness: { scope: :user } + validates :user, uniqueness: { scope: :work } end diff --git a/app/models/work.rb b/app/models/work.rb index 034952c07b..36e8730749 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -4,6 +4,9 @@ class Work < ApplicationRecord # must provide a title validates :title, presence: {message: "%{value} must be given"} -# must provide a vin +# def score +# votes.count +# end + # validates :title, uniqueness: {message: "%{value} already exists"} end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 5fd43a3937..7f15569c6e 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,10 +11,10 @@
<% end %> diff --git a/app/views/works/topten.html.erb b/app/views/works/topten.html.erb index a931b43560..c070e74aa8 100644 --- a/app/views/works/topten.html.erb +++ b/app/views/works/topten.html.erb @@ -5,3 +5,8 @@

top in each category with creator and number of votes

+ + +

Logged in user is <%= session[:logged_in_user] %>

+ +

Work being voted on is <%= @work.title %>

diff --git a/config/routes.rb b/config/routes.rb index 2482011a7e..47afcfa24e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,45 +14,62 @@ get 'login', to: 'users#login_form', as: 'login' post 'login', to: 'users#login' - get 'votes/index' - get '/votes/new', to: 'votes#new', as: 'new_vote' - get '/votes', to: 'votes#create', as: 'votes' + # get '/votes/new', to: 'votes#new', as: 'new_vote' + # + # get '/votes', to: 'votes#create', as: 'votes' - get '/users/index' + resources :users - get '/users/new', to: 'users#new', as: 'new_user' - get '/users/:id/edit', to: 'users#edit', as: 'edit_user' + resources :works do + resources :votes + end - post '/users', to: 'users#create', as: 'users' - - patch '/users/:id', to: 'users#update' - - get '/users/:id', to: 'users#show', as: 'user' - - delete '/users/:id', to: 'users#destroy' - - - get 'works/index' - - get '/works/new', to: 'works#new', as: 'new_work' - - get '/works/topten', to: 'works#topten', as: 'topten' + # get 'works/:id/upvote', to: 'works#upvote', as: 'upvote_work' + # get '/works/topten', to: 'works#topten', as: 'topten' + # + # get '/votes/new', to: 'votes#new', as: 'new_vote' + # post '/votes', to: 'votes#create', as: 'votes' +end - get '/works/:id/edit', to: 'works#edit', as: 'edit_work' - post '/works', to: 'works#create', as: 'works' + # get '/works/:id/upvote', to: 'works#upvote, ' - patch '/works/:id', to: 'works#update' + # get 'works/index' + # + # get '/works/new', to: 'works#new', as: 'new_work' + # + # + # + # get '/works/:id/edit', to: 'works#edit', as: 'edit_work' + # + # post '/works', to: 'works#create', as: 'works' + # + # patch '/works/:id', to: 'works#update' + # + # get 'works/:id', to: 'works#show', as: 'work' + # + # delete '/works/:id', to: 'works#destroy' - get 'works/:id', to: 'works#show', as: 'work' - delete '/works/:id', to: 'works#destroy' + # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html -end +# +# get '/users/index' +# +# get '/users/new', to: 'users#new', as: 'new_user' +# +# get '/users/:id/edit', to: 'users#edit', as: 'edit_user' +# +# post '/users', to: 'users#create', as: 'users' +# +# patch '/users/:id', to: 'users#update' +# +# get '/users/:id', to: 'users#show', as: 'user' +# +# delete '/users/:id', to: 'users#destroy' From e092e1f0d2d287baeca99c55e201b50a620ed0c6 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 19:16:49 -0700 Subject: [PATCH 14/26] Add Vote Totals --- app/controllers/votes_controller.rb | 13 ------------- app/controllers/works_controller.rb | 2 ++ app/models/work.rb | 9 ++++++--- app/views/works/index.html.erb | 5 ++++- 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index b091b0af1e..a4c8ad2952 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -6,21 +6,8 @@ def new @vote.work_id = params[:work_id] @vote.save redirect_to works_path - # end end - # def create - # @vote = Vote.new(vote_params) - # @vote.save - # redirect_to works_path - # end - # def create - # @vote = Vote.new(vote_params) - # @vote.work = Work.find(params[:work_id]) - # @vote.save - # end - - private def vote_params diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 4897bec307..494b2d2b6d 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -41,6 +41,8 @@ def destroy def topten end + + # def upvote # @work = Work.find(params[:id]) # @work.votes.create diff --git a/app/models/work.rb b/app/models/work.rb index 36e8730749..b2aaab25cf 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -4,9 +4,12 @@ class Work < ApplicationRecord # must provide a title validates :title, presence: {message: "%{value} must be given"} -# def score -# votes.count -# end +def score + @work = Work.find(params[:id]) + @votes = Vote.all + @votes.where(work_id: @work.id).count + return scores +end # validates :title, uniqueness: {message: "%{value} already exists"} end diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 8c4c9aa8ee..8a0d284c01 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -34,10 +34,13 @@ +<% @votes = Vote.all %> + <% @works.each do |work| %> - +<%# @votes.where(work_id: work.id).count +%> <% @votes = Vote.all %> <% @works.each do |work| %> -<% end %> --> +<% end %>
- <%= link_to "Vote", new_vote_path %> + <%= link_to "Vote", new_work_vote_path(work.id) %>
- Votes Will Go Here + <%= @votes.where(work_id: work.id).count + %> <%= work.category %> From 7844d78e2c3a4247f68df2e84f1f47d9dc628414 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 19:46:55 -0700 Subject: [PATCH 15/26] Add method to show works user voted on --- app/views/users/show.html.erb | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 353f2e7ddb..ff4d58df72 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -1,8 +1,19 @@ +<% @votes = Vote.all %> +<% @works = Work.all %> -

Welcome<%= @user.username %>

+

Welcome <%= @user.username %>

+ +

Works Voted On

+ <% votes = @votes.where(user_id: @user.id) %> + + <% votes.each do |vote| %> + <% work = @works.find(vote.work_id) %> + <%= work.title %> + <% end %> + Books this person voted for go through votes associated with each user, then find title associated with vote (through works table) if no votes, put not yet voted. From aa69cda225422b22483eb4aa5934e829b1e97acb Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 21:38:11 -0700 Subject: [PATCH 16/26] Update vote system again --- app/models/vote.rb | 9 +++++++++ app/models/work.rb | 15 ++++++++++----- app/views/users/show.html.erb | 5 +---- app/views/works/index.html.erb | 6 +++--- 4 files changed, 23 insertions(+), 12 deletions(-) diff --git a/app/models/vote.rb b/app/models/vote.rb index 8250ba6f86..fd1a24a922 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -5,4 +5,13 @@ class Vote < ApplicationRecord validates :work, uniqueness: { scope: :user } validates :user, uniqueness: { scope: :work } + + + +def list_votes(user) +end + + + + end diff --git a/app/models/work.rb b/app/models/work.rb index b2aaab25cf..10190c2c1f 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -4,12 +4,17 @@ class Work < ApplicationRecord # must provide a title validates :title, presence: {message: "%{value} must be given"} -def score - @work = Work.find(params[:id]) +def testing(input) @votes = Vote.all - @votes.where(work_id: @work.id).count - return scores + total = @votes.where(work_id: input).count + return total end -# validates :title, uniqueness: {message: "%{value} already exists"} +# def score +# @work = Work.find(params[:id]) +# @votes = Vote.all +# @votes.where(work_id: @work.id).count +# return scores +# end + end diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index ff4d58df72..ad773248bf 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -11,10 +11,7 @@ <% votes.each do |vote| %> <% work = @works.find(vote.work_id) %> - <%= work.title %> +

<%= work.title %>

<% end %> - Books this person voted for - go through votes associated with each user, then find title associated with vote (through works table) - if no votes, put not yet voted.

diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 8a0d284c01..2a33ec954f 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -33,14 +33,14 @@ VOTE!
- <%= @votes.where(work_id: work.id).count - %> + <%= work.testing(work.id) %> <%= work.category %> From 0331d8b419825aac4519b13620d5c3c2d8327883 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sat, 14 Oct 2017 22:28:07 -0700 Subject: [PATCH 17/26] move methods to models from views --- app/controllers/works_controller.rb | 14 +++++++++---- app/models/user.rb | 23 ++++++++++++++++++--- app/models/vote.rb | 3 +-- app/models/work.rb | 11 ++++++++++ app/views/users/index.html.erb | 2 +- app/views/users/show.html.erb | 12 ++++------- app/views/works/index.html.erb | 6 ++---- app/views/works/show.html.erb | 31 +++++++++++++---------------- 8 files changed, 63 insertions(+), 39 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 494b2d2b6d..7e88f657bf 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,10 +1,16 @@ class WorksController < ApplicationController + before_action :get_work , only: [:show, :edit, :update, :destroy] + + def get_work + @work = Work.find(params[:id]) + end + def index @works = Work.all.order(:category, :title) end def show - @work = Work.find(params[:id]) + # @work = Work.find(params[:id]) end def new @@ -22,18 +28,18 @@ def create end def edit - @work = Work.find(params[:id]) + # @work = Work.find(params[:id]) end def update - @work = Work.find(params[:id]) + # @work = Work.find(params[:id]) @work.update_attributes(work_params) @work.save redirect_to works_path end def destroy - @work = Work.find(params[:id]) + # @work = Work.find(params[:id]) @work.destroy redirect_to works_path end diff --git a/app/models/user.rb b/app/models/user.rb index ff4ce37990..5ccdac341e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -3,11 +3,28 @@ class User < ApplicationRecord validates :username, presence: {message: "%{value} must be given"} - def list_votes - #go through votes - #if the user id is found, print name of work + def list_votes(user) + @votes = Vote.all + @works = Work.all + user_works = [] + votes = @votes.where(user_id: user) + votes.each do |vote| + user_works << @works.find(vote.work_id).title + end + return user_works end + + + + + + # <%# votes = @votes.where(user_id: @user.id) %> + # + # <%# votes.each do |vote| %> + # <%# work = @works.find(vote.work_id) %> + # <%#= work.title %> + # <%# end %> # def votes?(work) # work.votes.where(user_id: id).any? # end diff --git a/app/models/vote.rb b/app/models/vote.rb index fd1a24a922..c6da6fa2d7 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -8,8 +8,7 @@ class Vote < ApplicationRecord -def list_votes(user) -end + diff --git a/app/models/work.rb b/app/models/work.rb index 10190c2c1f..f0de6a9910 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -10,6 +10,17 @@ def testing(input) return total end +def list_users(work) + @votes = Vote.all + @users = User.all + work_voters = [] + votes = @votes.where(work_id: work) + votes.each do |vote| + work_voters << @users.find(vote.user_id).username + end + return work_voters +end + # def score # @work = Work.find(params[:id]) # @votes = Vote.all diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 89f684ff72..09c215c719 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -22,7 +22,7 @@ <%= link_to user.username, user%> - Votes + <%= (user.list_votes(user.id)).length %> <%= user.created_at %> diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index ad773248bf..5443bb6bc6 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -6,12 +6,8 @@

Works Voted On

-

- <% votes = @votes.where(user_id: @user.id) %> - <% votes.each do |vote| %> - <% work = @works.find(vote.work_id) %> -

<%= work.title %>

- <% end %> - -

+<% list_works = @user.list_votes(@user.id) %> +<% list_works.each do |w| %> +

<%= w %>

+<% end %> diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 2a33ec954f..0abfde713c 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -4,9 +4,7 @@

Rank by category and then number of votes

-

- List of all media, votes, title, creator, year, upvote -

+ @@ -35,7 +33,7 @@ <%# @votes.where(work_id: work.id).count %> -<% @votes = Vote.all %> +<%# @votes = Vote.all %> <% @works.each do |work| %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 49a94dba63..4fcc01600f 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -1,22 +1,19 @@ -

This is the Show Page

+

<%= @work.title %> was loved by:

-

- User who created it -

- -

- List of users who voted for this work -

+<% list_users = @work.list_users(@work.id) %> +<% list_users.each do |u| %> +

<%= u %>

+<% end %> -

- Link to Edit, Link to Upvote, Link to Delete -

+
    +
  • Category: <%= @work.category %>
  • +
  • Creator: <%= @work.creator %>
  • +
  • Year: <%= @work.publication_year %>
  • +
  • Description: <%= @work.description %>
  • +
<%= link_to "Edit Work", edit_work_path %> <%= link_to "Delete Work", work_path(@work.id), method: :delete, data: {:confirm => "Are you sure you want to delete this Work?"} %> - -<%= @work.category %> -<%= @work.title %> -<%= @work.creator %> -<%= @work.publication_year %> -<%= @work.description %> +

+ link to upvote +

From 2cdb91d9a2ce0707db415f99a681c6bde20161d7 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 10:25:18 -0700 Subject: [PATCH 18/26] Voting myabe working again after I broke it --- app/controllers/votes_controller.rb | 13 +++++--- app/views/works/index.html.erb | 24 +++++++++++++-- config/routes.rb | 47 +++++++++++++++++++++-------- 3 files changed, 65 insertions(+), 19 deletions(-) diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index a4c8ad2952..88d2b281d5 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -2,10 +2,15 @@ class VotesController < ApplicationController def new @vote = Vote.new # if params[:user_id] - @vote.user_id = session[:logged_in_user] - @vote.work_id = params[:work_id] - @vote.save - redirect_to works_path + + end + + def create + @vote = Vote.new + @vote.user_id = session[:logged_in_user] + @vote.work_id = params[:work_id] + @vote.save + redirect_to works_path end private diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index 0abfde713c..c92fbb835c 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -56,9 +56,29 @@ <%= work.description %> + <% end %> + + -<% end %> +
- <%= link_to "Vote", new_work_vote_path(work.id) %> + VOTES + <%= form_for(Vote.new) do |f| %> + <%= f.submit %> + <% end %> + + VOTERS + <%# @vote = Vote.new %> + <%#= form_for @vote url {action: create} + + <%# orm_for (:thing, url:{:controller=>'thing', :action=>'update'}, html:{method:'put'}). %> + <%#{}= form_for(:vote, url: {:controller => votes, action: create} do |f| %> + <%#= form_tag("/upvote", method: :post) do %> + <%#= text_field :user_id, work.id %> + <%#= submit_tag "Vote" %> + + + + <%#= link_to "Vote", new_work_vote_path(work.id) %>
diff --git a/config/routes.rb b/config/routes.rb index 47afcfa24e..0f16681c59 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -3,16 +3,36 @@ root 'main#index' - # resources :works - # post '/work/:id/voted_for', to: 'work#voted_for', as: 'vote_counted' + get 'login', to: 'users#login_form', as: 'login' + post 'login', to: 'users#login' + resources :users - # resources :authors, only: [:index, :new, :create] do - # resources :books, only: [:index, :new] - # end + resources :works + resources :votes - get 'login', to: 'users#login_form', as: 'login' - post 'login', to: 'users#login' + get 'upvote', to: 'votes#new', as: 'upvote' + post 'upvote', to: 'votes#create' + + +end +# <% if user_signed_in? && current_user.likes?(post) %> +# <%= link_to "Unlike", post_like_path(post), method: :delete, remote: true %> +# <% else %> +# <%= link_to "Like", post_like_path(post), method: :post, remote: true %> +# <% end %> +# +# <%= post.likes.count %> +# +# # <% post.likes.each do |like| %> +# # <%= image_tag like.user.avatar_url, width: 20 %> +# # <% end %> +# <% @posts.each do |post| %> +#

<%= link_to post.title, post %>

+#
<%= simple_format post.body %>
+#

<%= render partial: "likes", locals: {post: post} %>

+#
+# <% end %> @@ -21,20 +41,21 @@ # # get '/votes', to: 'votes#create', as: 'votes' - resources :users - - resources :works do - resources :votes - end # get 'works/:id/upvote', to: 'works#upvote', as: 'upvote_work' # get '/works/topten', to: 'works#topten', as: 'topten' # # get '/votes/new', to: 'votes#new', as: 'new_vote' # post '/votes', to: 'votes#create', as: 'votes' -end +# resources :works +# post '/work/:id/voted_for', to: 'work#voted_for', as: 'vote_counted' + + +# resources :authors, only: [:index, :new, :create] do +# resources :books, only: [:index, :new] +# end # get '/works/:id/upvote', to: 'works#upvote, ' From f766ba0cff048b4d983bba955fa0dcae26cd3692 Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 11:58:48 -0700 Subject: [PATCH 19/26] Testing, fix error messages --- app/controllers/votes_controller.rb | 18 +- app/controllers/works_controller.rb | 25 ++- app/models/work.rb | 6 +- app/views/users/index.html.erb | 2 +- app/views/works/index.html.erb | 9 +- config/routes.rb | 3 +- test/controllers/users_controller_test.rb | 16 +- test/controllers/votes_controller_test.rb | 16 +- test/controllers/works_controller_test.rb | 221 +++++++++++++++++++++- test/models/user_test.rb | 14 +- test/models/vote_test.rb | 16 +- test/models/work_test.rb | 14 +- 12 files changed, 302 insertions(+), 58 deletions(-) diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index 88d2b281d5..db2bfa86e4 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -1,23 +1,31 @@ class VotesController < ApplicationController def new - @vote = Vote.new + #@vote = Vote.new # if params[:user_id] end def create + # user_id = session[:logged_in_user] + @vote = Vote.new + @vote.user_id = session[:logged_in_user] @vote.work_id = params[:work_id] @vote.save redirect_to works_path end - private + # @user = User.new(user_params) + # @user.save + # session[:logged_in_user] = @user.id + # redirect_to works_path - def vote_params - return params.require(:vote).permit(:user_id, :work_id) - end + # private + # + # def vote_params + # return params.require(:vote).permit(:user_id, :work_id) + # end end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 7e88f657bf..01b318f4c7 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -20,13 +20,34 @@ def new def create @work = Work.new(work_params) if @work.save - flash[:success] = "Saved!" + flash[:status] = :success + flash[:message] = "Successfully created work #{@work.id}" redirect_to works_path else - render :new + flash.now[:status] = :failure + flash.now[:message] = "Failed to create new work" + render :new, status: :bad_request end end + # @book = Book.new(book_params) + + # if @book.save + # flash[:status] = :success + # flash[:message] = "Successfully created book #{@book.id}" + # redirect_to books_path + # else + # Tell the user what went wrong + # flash.now[:status] = :failure + # flash.now[:message] = "Failed to create book" + # flash.now[:details] = @work.errors.messages + # render :new, status: :bad_request + # end +# end + + + + def edit # @work = Work.find(params[:id]) end diff --git a/app/models/work.rb b/app/models/work.rb index f0de6a9910..21a9d2be3b 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -2,7 +2,11 @@ class Work < ApplicationRecord has_many :votes, dependent: :destroy # must provide a title -validates :title, presence: {message: "%{value} must be given"} +validates :title, presence: {message: "Title is Required"} +validates :title, uniqueness: {message: "Someone Else has Already Added that Work"} + + + def testing(input) @votes = Vote.all diff --git a/app/views/users/index.html.erb b/app/views/users/index.html.erb index 09c215c719..63c763001c 100644 --- a/app/views/users/index.html.erb +++ b/app/views/users/index.html.erb @@ -28,7 +28,7 @@ <%= user.created_at %>
diff --git a/app/views/works/index.html.erb b/app/views/works/index.html.erb index c92fbb835c..b1157a1607 100644 --- a/app/views/works/index.html.erb +++ b/app/views/works/index.html.erb @@ -38,7 +38,7 @@ <% @works.each do |work| %>
- <%= work.testing(work.id) %> + work.testing(work.id) <%= work.category %> @@ -57,14 +57,13 @@ VOTES - <%= form_for(Vote.new) do |f| %> + <%= form_for Vote.new do |f| %> + <%= f.hidden_field :work_id, value: 1 %> <%= f.submit %> <% end %> - VOTERS <%# @vote = Vote.new %> <%#= form_for @vote url {action: create} @@ -77,7 +76,7 @@ <%#= link_to "Vote", new_work_vote_path(work.id) %> -
- work.testing(work.id) + <%= work.id %> + <%= work.category %> @@ -58,7 +59,8 @@ VOTES <%= form_for Vote.new do |f| %> - <%= f.hidden_field :work_id, value: 1 %> + <%= f.hidden_field :work_id, value: work.id %> + <%= f.hidden_field :user_id, value: session[:logged_in_user] %> <%= f.submit %> <% end %>
- <%= work.id %> - + <%#= work.id %> + <%= work.count %> <%= work.category %> @@ -57,7 +57,6 @@ <%= work.description %> - VOTES <%= form_for Vote.new do |f| %> <%= f.hidden_field :work_id, value: work.id %> <%= f.hidden_field :user_id, value: session[:logged_in_user] %> diff --git a/app/views/works/new.html.erb b/app/views/works/new.html.erb index 551f1d989f..cac46fe1a0 100644 --- a/app/views/works/new.html.erb +++ b/app/views/works/new.html.erb @@ -1,3 +1,4 @@ -

This is the New Page

+

Add A New Work Here

+

Work Must Contain Title

<%= render partial: 'form' %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 4fcc01600f..cdb152c1fd 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -5,6 +5,8 @@

<%= u %>

<% end %> + +
  • Category: <%= @work.category %>
  • Creator: <%= @work.creator %>
  • diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index bea5a74873..569be42dbf 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -15,109 +15,108 @@ end#index tests - describe "new" do - it "gets a new form" do - get new_user_path + describe "new" do + it "gets a new form" do + get new_user_path + must_respond_with :success + end #get form + end #new tests + + + describe "create" do + it "adds the User to the DB and redirects when the user data is valid" do + # Arrange + user_data = { + user: { + username: "Test Name" + } + } + # Test data should result in a valid work, otherwise + # the test is broken + User.new(user_data[:user]).must_be :valid? + + start_user_count = User.count + + # Act + post users_path, params: user_data + + # Assert + must_respond_with :redirect + must_redirect_to users_path + + User.count.must_equal start_user_count + 1 + end #adds work test + + it "sends bad_request when the user data is bogus" do + # Arrange + invalid_user_data = { + user: { + # NO TITLE!!! + username: "" + } + } + # Double check the data is truly invalid + User.new(invalid_user_data[:user]).wont_be :valid? + + start_user_count = User.count + + # Act + post users_path, params: invalid_user_data + + # Assert + must_respond_with :bad_request + # Vanilla rails doesn't provide any way to do this + # assert_template :new + User.count.must_equal start_user_count + end #bad request + end #create tests + + describe "show" do + it "returns success when given a valid User ID" do + # Arrange + user_id = User.first.id + + # Act + get user_path(user_id) + + # Assert + must_respond_with :success + end #success + + + it "returns not_found when given an invalid user ID" do + invalid_user_id = User.last.id + 1 + get user_path(invalid_user_id) + must_respond_with :not_found + end #invalid + + + + + end #showtests + describe "log in" do + it "returns success when given a valid username" do + # Arrange + user_id = User.first.id + # Act + get login_path(user_id) + + # Assert must_respond_with :success - end #get form - end #new tests - - - describe "create" do - it "adds the User to the DB and redirects when the user data is valid" do - # Arrange - user_data = { - user: { - username: "Test Name" - } - } - # Test data should result in a valid work, otherwise - # the test is broken - User.new(user_data[:user]).must_be :valid? - - start_user_count = User.count - - # Act - post users_path, params: user_data - - # Assert - must_respond_with :redirect - must_redirect_to users_path - - User.count.must_equal start_user_count + 1 - end #adds work test - - # it "sends bad_request when the work data is bogus" do - # # Arrange - # invalid_work_data = { - # work: { - # # NO TITLE!!! - # creator: "Fake Creator" - # } - # } - # # Double check the data is truly invalid - # Work.new(invalid_work_data[:work]).wont_be :valid? - # - # start_work_count = Work.count - # - # # Act - # post works_path, params: invalid_work_data - # - # # Assert - # must_respond_with :bad_request - # # Vanilla rails doesn't provide any way to do this - # # assert_template :new - # Work.count.must_equal start_work_count - # end #bad request - # end #create - # - # - # - - end #create tests + end + + it "renders new form if no username is entered" do + invalid_user_id = User.first.id + 1 + get login_path(invalid_user_id) + must_respond_with :not_found + end + end end #all tests -# describe "show" do -# it "returns success when given a valid work ID" do -# # Arrange -# work_id = Work.first.id -# -# # Act -# get work_path(work_id) -# -# # Assert -# must_respond_with :success -# end -# -# it "returns not_found when given an invalid book ID" do -# invalid_work_id = Work.last.id + 1 -# get work_path(invalid_work_id) -# must_respond_with :not_found -# end -# end -# -# describe "edit" do -# it "returns success when given a valid work ID" do -# # Arrange -# work_id = Work.first.id -# -# # Act -# get edit_work_path(work_id) -# -# # Assert -# must_respond_with :success -# end -# -# it "returns not_found when given an invalid work ID" do -# invalid_work_id = Work.last.id + 1 -# get edit_work_path(invalid_work_id) -# must_respond_with :not_found -# end -# end -# + # # describe "update" do # it "returns success if the work ID is valid and the change is valid" do From 6e1935d092c1974237bce89b2f197531890e7e0d Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Sun, 15 Oct 2017 19:25:32 -0700 Subject: [PATCH 24/26] Trying to fix many things --- app/controllers/works_controller.rb | 8 +- app/models/work.rb | 17 +- app/views/layouts/application.html.erb | 2 +- app/views/works/index.html.erb | 19 +- app/views/works/show.html.erb | 1 - app/views/works/topten.html.erb | 4 +- config/routes.rb | 39 +--- test/controllers/users_controller_test.rb | 267 ++++++++-------------- test/controllers/votes_controller_test.rb | 28 +++ 9 files changed, 127 insertions(+), 258 deletions(-) diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 48caf0af25..e9475ca6f9 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -63,15 +63,9 @@ def destroy return end end - # @work = Work.find(params[:id]) - # unless @work - # head :not_found - # enda - # @work.destroy - # redirect_to works_path - def topten + end diff --git a/app/models/work.rb b/app/models/work.rb index 8aecfe5a77..205b26f08f 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -7,17 +7,13 @@ class Work < ApplicationRecord validates :description, length: { maximum: 200, message: "The description is too long" } -# def testing(input) -# @votes = Vote.all -# total = @votes.where(work_id: input).count -# return total -# end -def count +def counter @votes = Vote.all return @votes.where(work_id: id).count end + def list_users(work) @votes = Vote.all @users = User.all @@ -29,14 +25,5 @@ def list_users(work) return work_voters end -def users_voted - @votes = Vote.all - @users = User.all - votes = @votes.where(work_id: id) - votes.each do |vote| - work_voters << @users.find(vote.user_id).username - end - return work_voters -end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 7f15569c6e..b836039ae6 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -11,7 +11,7 @@
- <%#= work.id %> - <%= work.count %> + <%= work.counter %> <%= work.category %> @@ -65,19 +61,6 @@
@@ -56,7 +50,7 @@ <%= form_for Vote.new do |f| %> <%= f.hidden_field :work_id, value: work.id %> <%= f.hidden_field :user_id, value: session[:logged_in_user] %> - <%= f.submit %> + <%= f.submit class: "button"%> <% end %> <% end %> diff --git a/app/views/works/show.html.erb b/app/views/works/show.html.erb index 886e4fb993..6578598d1b 100644 --- a/app/views/works/show.html.erb +++ b/app/views/works/show.html.erb @@ -1,4 +1,4 @@ -

<%= @work.title %> was loved by:

+

<%= @work.title %> Was Loved By:

<% list_users = @work.list_users(@work.id) %> <% list_users.each do |u| %> @@ -13,8 +13,5 @@
  • Description: <%= @work.description %>
  • -<%= link_to "Edit Work", edit_work_path %> -<%= link_to "Delete Work", work_path(@work.id), method: :delete, data: {:confirm => "Are you sure you want to delete this Work?"} %> -

    - link to upvote -

    +<%= link_to "Edit Work", edit_work_path, class: "warning button" %> +<%= link_to "Delete Work", work_path(@work.id), class: "alert button", method: :delete, data: {:confirm => "Are you sure you want to delete this Work?"} %> diff --git a/app/views/works/topten.html.erb b/app/views/works/topten.html.erb index be22c25857..29ea099f34 100644 --- a/app/views/works/topten.html.erb +++ b/app/views/works/topten.html.erb @@ -1,12 +1 @@ -

    This will have the top media

    - -

    Link to Spotlight

    - -

    - top in each category with creator and number of votes -

    - - - +

    They're The Tops!

    diff --git a/config/routes.rb b/config/routes.rb index 5f43bca610..0d26184465 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,62 +1,22 @@ Rails.application.routes.draw do - # get '/', to: 'work#index', as: 'root' + root 'main#index' get 'login', to: 'users#login_form', as: 'login' post 'login', to: 'users#login' + get 'logout', to: 'users#logout', as: 'logout' get 'topten', to: 'works#topten', as: 'topten' resources :users resources :works - resources :votes - + resources :votes, only: [:new, :create] + end - - - - - - # get '/works/:id/upvote', to: 'works#upvote, ' - - # get 'works/index' - # - # get '/works/new', to: 'works#new', as: 'new_work' - # - # - # - # get '/works/:id/edit', to: 'works#edit', as: 'edit_work' - # - # post '/works', to: 'works#create', as: 'works' - # - # patch '/works/:id', to: 'works#update' - # - # get 'works/:id', to: 'works#show', as: 'work' - # - # delete '/works/:id', to: 'works#destroy' - - - # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html - - -# -# get '/users/index' -# -# get '/users/new', to: 'users#new', as: 'new_user' -# -# get '/users/:id/edit', to: 'users#edit', as: 'edit_user' -# -# post '/users', to: 'users#create', as: 'users' -# -# patch '/users/:id', to: 'users#update' -# -# get '/users/:id', to: 'users#show', as: 'user' -# -# delete '/users/:id', to: 'users#destroy' From e3acfe79eb274a43ebde87792c50c02dc1d2978d Mon Sep 17 00:00:00 2001 From: Amy Cash Date: Mon, 16 Oct 2017 07:34:12 -0700 Subject: [PATCH 26/26] Small fixes --- app/controllers/votes_controller.rb | 22 -- app/controllers/works_controller.rb | 29 +- app/models/user.rb | 26 +- app/models/vote.rb | 6 - app/models/work.rb | 3 - app/views/layouts/application.html.erb | 4 +- app/views/votes/new.html.erb | 6 +- test/controllers/users_controller_test.rb | 228 +++++++------ test/controllers/votes_controller_test.rb | 7 - test/controllers/works_controller_test.rb | 394 +++++++++++----------- test/fixtures/votes.yml | 4 +- test/models/user_test.rb | 104 +----- test/models/work_test.rb | 153 ++++----- 13 files changed, 392 insertions(+), 594 deletions(-) diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index 73b2ed0f2b..1f5da6da01 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -1,26 +1,13 @@ class VotesController < ApplicationController def new - #@vote = Vote.new - # if params[:user_id] - end def create - # user_id = session[:logged_in_user] - @vote = Vote.new(vote_params) - - # @vote.user_id = session[:logged_in_user] - # @vote.work_id = params[:work_id] @vote.save redirect_to works_path end - # @user = User.new(user_params) - # @user.save - # session[:logged_in_user] = @user.id - # redirect_to works_path - private def vote_params @@ -29,12 +16,3 @@ def vote_params end - -# -# def create -# @vote = Vote.new -# @vote.user_id = session[:logged_in_user] -# @vote.work_id = @work.id -# @vote.save -# redirect_to works_path -# end diff --git a/app/controllers/works_controller.rb b/app/controllers/works_controller.rb index 9c8f6fe43f..63617cef4e 100644 --- a/app/controllers/works_controller.rb +++ b/app/controllers/works_controller.rb @@ -1,9 +1,9 @@ class WorksController < ApplicationController - # before_action :get_work , only: [:update, :destroy] + # before_action :get_work , only: [:show, :edit, :update, :destroy] - def get_work - @work = Work.find(params[:id]) - end + # def get_work + # @work = Work.find(params[:id]) + # end def index @works = Work.all.order(:category, :title) @@ -84,24 +84,3 @@ def find_work_by_params_id end end - -# def upvote -# @work = Work.find(params[:id]) -# @work.votes.create -# redirect_to new_vote_path -# -# # render :topten -# #want to create a vote, which includes this id as the work id and the logged in user id as the user-id -# # @work.votes.create -# # redirect_to works_index_path -# # get 'works/:id/upvote', to: 'works#upvote', as: 'upvote_work' -# # -# -# end - -# def upvote -# @startup = Startup.find(params[:id]) -# upvote = @startup.upvotes.find_or_create_by(user: current_user) -# upvote.save -# redirect_to startups_path -# end diff --git a/app/models/user.rb b/app/models/user.rb index 8f253c1141..2442dd3e04 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -4,7 +4,7 @@ class User < ApplicationRecord validates :username, presence: {message: "Username is required."} validates :username, uniqueness: {message: "Username already exists."} - + def list_votes(user) @votes = Vote.all @@ -17,28 +17,4 @@ def list_votes(user) return user_works end - - - - - - # <%# votes = @votes.where(user_id: @user.id) %> - # - # <%# votes.each do |vote| %> - # <%# work = @works.find(vote.work_id) %> - # <%#= work.title %> - # <%# end %> - # def votes?(work) - # work.votes.where(user_id: id).any? - # end - # @zombies.each do |zombie| - # zombie.name - # zombie.brain.flavor - # - # for each zombie, seach list of brains to find flavor, - # - # or - # @zombies = Zombie.includes(:brain).all - # will select zombies that are in the brain list - # then just seek out those ids end diff --git a/app/models/vote.rb b/app/models/vote.rb index c6da6fa2d7..50f5dff14b 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -7,10 +7,4 @@ class Vote < ApplicationRecord validates :user, uniqueness: { scope: :work } - - - - - - end diff --git a/app/models/work.rb b/app/models/work.rb index 205b26f08f..d55ddc7a50 100644 --- a/app/models/work.rb +++ b/app/models/work.rb @@ -1,13 +1,10 @@ class Work < ApplicationRecord has_many :votes, dependent: :destroy -# must provide a title validates :title, presence: {message: "Title is Required"} validates :title, uniqueness: {message: "Someone Else has Already Added that Work"} validates :description, length: { maximum: 200, message: "The description is too long" } - - def counter @votes = Vote.all return @votes.where(work_id: id).count diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 76a42f9bd2..5915e4437d 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -33,9 +33,7 @@ <%= User.find(session[:logged_in_user]).username %>

    <% else %> -

    - link_to " Create User " -

    + <% end %> diff --git a/app/views/votes/new.html.erb b/app/views/votes/new.html.erb index 5e4804e135..9f9075e55f 100644 --- a/app/views/votes/new.html.erb +++ b/app/views/votes/new.html.erb @@ -1,7 +1,3 @@ -<%# = form_for @vote do |f| %> - <%# = f.hidden_field :user_id, value: session[:logged_in_user] %> - <%# = f.submit 'Vote' %> -<%# end %>

    VOTE NEW

    -redirect_to +redirect_to diff --git a/test/controllers/users_controller_test.rb b/test/controllers/users_controller_test.rb index e4ead8ac02..6ca97b6d9c 100644 --- a/test/controllers/users_controller_test.rb +++ b/test/controllers/users_controller_test.rb @@ -1,115 +1,113 @@ -# require "test_helper" -# -# describe UsersController do -# describe "index" do -# it "returns a success status for all works" do -# get users_path -# must_respond_with :success -# end #success1 -# -# it "returns a success status when there are no users" do -# User.destroy_all -# get users_path -# must_respond_with :success -# end #success2 -# end#index tests -# -# -# describe "new" do -# it "gets a new form" do -# get new_user_path -# must_respond_with :success -# end #get form -# end #new tests -# -# -# describe "create" do -# it "adds the User to the DB and redirects when the user data is valid" do -# # Arrange -# user_data = { -# user: { -# username: "Test Name" -# } -# } -# # Test data should result in a valid work, otherwise -# # the test is broken -# User.new(user_data[:user]).must_be :valid? -# -# start_user_count = User.count -# -# # Act -# post users_path, params: user_data -# -# # Assert -# must_respond_with :redirect -# must_redirect_to users_path -# -# User.count.must_equal start_user_count + 1 -# end #adds work test -# -# it "sends bad_request when the user data is bogus" do -# # Arrange -# invalid_user_data = { -# user: { -# # NO TITLE!!! -# username: "" -# } -# } -# # Double check the data is truly invalid -# User.new(invalid_user_data[:user]).wont_be :valid? -# -# start_user_count = User.count -# -# # Act -# post users_path, params: invalid_user_data -# -# # Assert -# must_respond_with :bad_request -# # Vanilla rails doesn't provide any way to do this -# # assert_template :new -# User.count.must_equal start_user_count -# end #bad request -# end #create tests -# -# describe "show" do -# it "returns success when given a valid User ID" do -# # Arrange -# user_id = User.first.id -# -# # Act -# get user_path(user_id) -# -# # Assert -# must_respond_with :success -# end #success -# -# -# it "returns not_found when given an invalid user ID" do -# invalid_user_id = User.last.id + 1 -# get user_path(invalid_user_id) -# must_respond_with :not_found -# end #invalid -# -# -# end #showtests -# describe "log in" do -# it "returns success when given a valid username" do -# # Arrange -# user_id = User.first.id -# # Act -# get login_path(user_id) -# -# # Assert -# must_respond_with :success -# end -# -# it "renders new form if no username is entered" do -# invalid_user_id = User.first.id + 1 -# get login_path(invalid_user_id) -# must_respond_with :not_found -# end -# end -# -# -# -# end #all tests +require "test_helper" + +describe UsersController do + describe "index" do + it "returns a success status for all works" do + get users_path + must_respond_with :success + end #success1 + + it "returns a success status when there are no users" do + User.destroy_all + get users_path + must_respond_with :success + end #success2 + end#index tests + + + describe "new" do + it "gets a new form" do + get new_user_path + must_respond_with :success + end #get form + end #new tests + + + describe "create" do + it "adds the User to the DB and redirects when the user data is valid" do + # Arrange + user_data = { + user: { + username: "Test Name" + } + } + # Test data should result in a valid work, otherwise + # the test is broken + User.new(user_data[:user]).must_be :valid? + + start_user_count = User.count + + # Act + post users_path, params: user_data + + # Assert + must_respond_with :redirect + must_redirect_to users_path + + User.count.must_equal start_user_count + 1 + end #adds work test + + it "sends bad_request when the user data is bogus" do + # Arrange + invalid_user_data = { + user: { + # NO TITLE!!! + username: "" + } + } + # Double check the data is truly invalid + User.new(invalid_user_data[:user]).wont_be :valid? + + start_user_count = User.count + + # Act + post users_path, params: invalid_user_data + + # Assert + must_respond_with :bad_request + # Vanilla rails doesn't provide any way to do this + # assert_template :new + User.count.must_equal start_user_count + end #bad request + end #create tests + + describe "show" do + it "returns success when given a valid User ID" do + # Arrange + user_id = User.first.id + + # Act + get user_path(user_id) + + # Assert + must_respond_with :success + end #success + + + it "returns not_found when given an invalid user ID" do + invalid_user_id = User.last.id + 1 + get user_path(invalid_user_id) + must_respond_with :not_found + end #invalid + + + end #showtests + describe "log in" do + it "returns success when given a valid username" do + # Arrange + user_id = User.first.id + # Act + get login_path(user_id) + + # Assert + must_respond_with :success + end + + it "renders new form if no username is entered" do + invalid_user_id = User.first.id + 1 + get login_path(invalid_user_id) + must_respond_with :not_found + end + end + +end #all tests diff --git a/test/controllers/votes_controller_test.rb b/test/controllers/votes_controller_test.rb index 13e5aa606e..b98df9133c 100644 --- a/test/controllers/votes_controller_test.rb +++ b/test/controllers/votes_controller_test.rb @@ -1,12 +1,5 @@ require "test_helper" # -# describe VotesController do -# it "should get index" do -# get votes_index_url -# value(response).must_be :success? -# end -# -# end describe VotesController do diff --git a/test/controllers/works_controller_test.rb b/test/controllers/works_controller_test.rb index e3e3f04102..ee9d829c28 100644 --- a/test/controllers/works_controller_test.rb +++ b/test/controllers/works_controller_test.rb @@ -1,197 +1,197 @@ -# require "test_helper" -# -# describe WorksController do -# describe "index" do -# it "returns a success status for all works" do -# get works_path -# must_respond_with :success -# end -# -# it "returns a success status when there are no works" do -# Work.destroy_all -# get works_path -# must_respond_with :success -# end -# -# end #index tests -# -# describe "new" do -# it "gets a new form" do -# get new_work_path -# must_respond_with :success -# end -# end #new tests -# -# -# -# describe "create" do -# it "adds the work to the DB and redirects when the work data is valid" do -# # Arrange -# work_data = { -# work: { -# title: "Test work" -# } -# } -# # Test data should result in a valid work, otherwise -# # the test is broken -# Work.new(work_data[:work]).must_be :valid? -# -# start_work_count = Work.count -# -# # Act -# post works_path, params: work_data -# -# # Assert -# must_respond_with :redirect -# must_redirect_to works_path -# -# Work.count.must_equal start_work_count + 1 -# end #adds work test -# -# it "sends bad_request when the work data is bogus" do -# # Arrange -# invalid_work_data = { -# work: { -# # NO TITLE!!! -# creator: "Fake Creator" -# } -# } -# # Double check the data is truly invalid -# Work.new(invalid_work_data[:work]).wont_be :valid? -# -# start_work_count = Work.count -# -# # Act -# post works_path, params: invalid_work_data -# -# # Assert -# must_respond_with :bad_request -# # Vanilla rails doesn't provide any way to do this -# # assert_template :new -# Work.count.must_equal start_work_count -# end #bad request -# end #create -# -# -# -# describe "show" do -# it "returns success when given a valid work ID" do -# # Arrange -# work_id = Work.first.id -# -# # Act -# get work_path(work_id) -# -# # Assert -# must_respond_with :success -# end -# -# it "returns not_found when given an invalid book ID" do -# invalid_work_id = Work.last.id + 1 -# get work_path(invalid_work_id) -# must_respond_with :not_found -# end -# end -# -# describe "edit" do -# it "returns success when given a valid work ID" do -# # Arrange -# work_id = Work.first.id -# -# # Act -# get edit_work_path(work_id) -# -# # Assert -# must_respond_with :success -# end -# -# it "returns not_found when given an invalid work ID" do -# invalid_work_id = Work.last.id + 1 -# get edit_work_path(invalid_work_id) -# must_respond_with :not_found -# end -# end -# -# -# describe "update" do -# it "returns success if the work ID is valid and the change is valid" do -# work = Work.first -# work_data = { -# work: { -# title: "New title", -# creator: "New Creator" -# } -# } -# work.update_attributes(work_data[:work]) -# work.must_be :valid? -# -# patch work_path(work), params: work_data -# -# must_respond_with :redirect -# must_redirect_to work_path -# -# # Check that the change went through -# work.reload -# work.title.must_equal work_data[:work][:title] -# end -# -# it "returns not_found if the book ID is invalid" do -# invalid_work_id = Work.last.id + 1 -# work_data = { -# work: { -# title: "New title", -# creator: "New Creator" -# } -# } -# -# patch work_path(invalid_work_id), params: work_data -# -# must_respond_with :not_found -# end -# -# it "returns bad_request if the change is invalid" do -# work = Work.first -# invalid_work_data = { -# work: { -# title: "" -# } -# } -# # Check that the update is actually invalid -# work.update_attributes(invalid_work_data[:work]) -# work.wont_be :valid? -# -# patch work_path(work), params: invalid_work_data -# -# must_respond_with :bad_request -# -# work.reload -# work.title.wont_equal invalid_work_data[:work][:title] -# end -# end -# -# describe "destroy" do -# it "returns success and destroys the book when given a valid book ID" do -# # Arrange -# work_id = Work.first.id -# -# # Act -# delete work_path(work_id) -# -# # Assert -# must_respond_with :redirect -# must_redirect_to works_path -# Work.find_by(id: work_id).must_be_nil -# end -# -# it "returns not_found when given an invalid book ID" do -# invalid_work_id = Work.last.id + 1 -# -# start_work_count = Work.count -# -# delete work_path(invalid_work_id) -# -# must_respond_with :not_found -# Work.count.must_equal start_work_count -# end -# end -# end +require "test_helper" + +describe WorksController do + describe "index" do + it "returns a success status for all works" do + get works_path + must_respond_with :success + end + + it "returns a success status when there are no works" do + Work.destroy_all + get works_path + must_respond_with :success + end + + end #index tests + + describe "new" do + it "gets a new form" do + get new_work_path + must_respond_with :success + end + end #new tests + + + + describe "create" do + it "adds the work to the DB and redirects when the work data is valid" do + # Arrange + work_data = { + work: { + title: "Test work" + } + } + # Test data should result in a valid work, otherwise + # the test is broken + Work.new(work_data[:work]).must_be :valid? + + start_work_count = Work.count + + # Act + post works_path, params: work_data + + # Assert + must_respond_with :redirect + must_redirect_to works_path + + Work.count.must_equal start_work_count + 1 + end #adds work test + + it "sends bad_request when the work data is bogus" do + # Arrange + invalid_work_data = { + work: { + # NO TITLE!!! + creator: "Fake Creator" + } + } + # Double check the data is truly invalid + Work.new(invalid_work_data[:work]).wont_be :valid? + + start_work_count = Work.count + + # Act + post works_path, params: invalid_work_data + + # Assert + must_respond_with :bad_request + # Vanilla rails doesn't provide any way to do this + # assert_template :new + Work.count.must_equal start_work_count + end #bad request + end #create + + + + describe "show" do + it "returns success when given a valid work ID" do + # Arrange + work_id = Work.first.id + + # Act + get work_path(work_id) + + # Assert + must_respond_with :success + end + + it "returns not_found when given an invalid book ID" do + invalid_work_id = Work.last.id + 1 + get work_path(invalid_work_id) + must_respond_with :not_found + end + end + + describe "edit" do + it "returns success when given a valid work ID" do + # Arrange + work_id = Work.first.id + + # Act + get edit_work_path(work_id) + + # Assert + must_respond_with :success + end + + it "returns not_found when given an invalid work ID" do + invalid_work_id = Work.last.id + 1 + get edit_work_path(invalid_work_id) + must_respond_with :not_found + end + end + + + describe "update" do + it "returns success if the work ID is valid and the change is valid" do + work = Work.first + work_data = { + work: { + title: "New title", + creator: "New Creator" + } + } + work.update_attributes(work_data[:work]) + work.must_be :valid? + + patch work_path(work), params: work_data + + must_respond_with :redirect + must_redirect_to work_path + + # Check that the change went through + work.reload + work.title.must_equal work_data[:work][:title] + end + + it "returns not_found if the book ID is invalid" do + invalid_work_id = Work.last.id + 1 + work_data = { + work: { + title: "New title", + creator: "New Creator" + } + } + + patch work_path(invalid_work_id), params: work_data + + must_respond_with :not_found + end + + it "returns bad_request if the change is invalid" do + work = Work.first + invalid_work_data = { + work: { + title: "" + } + } + # Check that the update is actually invalid + work.update_attributes(invalid_work_data[:work]) + work.wont_be :valid? + + patch work_path(work), params: invalid_work_data + + must_respond_with :bad_request + + work.reload + work.title.wont_equal invalid_work_data[:work][:title] + end + end + + describe "destroy" do + it "returns success and destroys the book when given a valid book ID" do + # Arrange + work_id = Work.first.id + + # Act + delete work_path(work_id) + + # Assert + must_respond_with :redirect + must_redirect_to works_path + Work.find_by(id: work_id).must_be_nil + end + + it "returns not_found when given an invalid book ID" do + invalid_work_id = Work.last.id + 1 + + start_work_count = Work.count + + delete work_path(invalid_work_id) + + must_respond_with :not_found + Work.count.must_equal start_work_count + end + end + end diff --git a/test/fixtures/votes.yml b/test/fixtures/votes.yml index d64a071206..1e0a68e12c 100644 --- a/test/fixtures/votes.yml +++ b/test/fixtures/votes.yml @@ -5,5 +5,5 @@ one: user_id: 1 two: - work_id: 1 - user_id: 1 + work_id: 2 + user_id: 2 diff --git a/test/models/user_test.rb b/test/models/user_test.rb index 7679b75ba7..cc862ac2d9 100644 --- a/test/models/user_test.rb +++ b/test/models/user_test.rb @@ -1,101 +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 } -# Write at least one test for each custom method on a model -# Write at least one test for each model relationship on a model -# Write at least two tests for each validation on a model -# Write at least two tests for each scope on a model (we'll talk about scopes next week) - -# describe Work do -# let(:work) { Work.new } -# -# it "must be valid" do -# value(work).must_be :valid? -# end -# params.require(:work).permit(:category, :title, :creator, :publication_year, :description) -# -# describe Work do -# let :work{ Work.first } -# describe "validations" do -# it "can be created with all fields" do -# b = Work.new(category: "Album", title: "testing", creator: "Fake Creator", publication_year: 1999, description: "A Good Album") -# b.must_be :valid? -# end # validations -# -# it "requires a title" do -# b = Work.new -# is_valid = b.valid? -# is_valid.must_equal false -# b.errors.messages.must_include :title -# end #require title -# -# it "requires a unique title" do -# title = "test book" -# b1 = Work.create!(title: title) -# b2 = Work.new(title: title) -# -# b2.wont_be :valid? -# end #uniqueness -# end #work do -# -# describe "description" do -# it "rejects descriptions longer than 200 chars" do -# desc = "a" * 201 -# b = Work.new(title: "Fake Title", description: desc) -# -# # b.wont_be :valid? -# is_valid = b.valid? -# is_valid.must_equal false -# -# b.errors.messages.must_include :description -# end -# -# it "allows descriptions <= 200 chars" do -# descriptions = [ -# "a" * 199, -# "a" * 10, -# "a" * 0 -# ] -# -# descriptions.each do |desc| -# b = Work.new(title: "test", description: desc) -# b.must_be :valid? -# end -# end -# end -# -# -# -# # describe "relations" do -# # it "has an author" do -# # b = books(:poodr) -# # a = authors(:metz) -# # -# # binding.pry -# # -# # b.must_respond_to :author -# # b.author.must_equal a -# # b.author_id.must_equal a.id -# # end -# # -# # it "has a collection of genres" do -# # b = Book.new -# # b.must_respond_to :genres -# # b.genres.must_be :empty? -# # -# # g = Genre.create!(name: "test genre") -# # b.genres << g -# # b.genres.must_include g -# # end -# # end -# # -# # describe "age" do -# # end -# end #all tests + it "must be valid" do + value(user).must_be :valid? + end +end diff --git a/test/models/work_test.rb b/test/models/work_test.rb index 2ad325a070..c9aed8bc55 100644 --- a/test/models/work_test.rb +++ b/test/models/work_test.rb @@ -1,88 +1,69 @@ require "test_helper" -# describe Work do -# let(:work) { Work.new } -# -# it "must be valid" do -# value(work).must_be :valid? -# end -# params.require(:work).permit(:category, :title, :creator, :publication_year, :description) -# -# describe Work do -# let :work{ Work.first } -# describe "validations" do -# it "can be created with all fields" do -# b = Work.new(category: "Album", title: "testing", creator: "Fake Creator", publication_year: 1999, description: "A Good Album") -# b.must_be :valid? -# end # validations -# -# it "requires a title" do -# b = Work.new -# is_valid = b.valid? -# is_valid.must_equal false -# b.errors.messages.must_include :title -# end #require title -# -# it "requires a unique title" do -# title = "test book" -# b1 = Work.create!(title: title) -# b2 = Work.new(title: title) -# -# b2.wont_be :valid? -# end #uniqueness -# end #work do -# -# describe "description" do -# it "rejects descriptions longer than 200 chars" do -# desc = "a" * 201 -# b = Work.new(title: "Fake Title", description: desc) -# -# # b.wont_be :valid? -# is_valid = b.valid? -# is_valid.must_equal false -# -# b.errors.messages.must_include :description -# end -# -# it "allows descriptions <= 200 chars" do -# descriptions = [ -# "a" * 199, -# "a" * 10, -# "a" * 0 -# ] -# -# descriptions.each do |desc| -# b = Work.new(title: "test", description: desc) -# b.must_be :valid? -# end -# end -# end -# -# -# -# # describe "relations" do -# # it "has an author" do -# # b = books(:poodr) -# # a = authors(:metz) -# # -# # binding.pry -# # -# # b.must_respond_to :author -# # b.author.must_equal a -# # b.author_id.must_equal a.id -# # end -# # -# # it "has a collection of genres" do -# # b = Book.new -# # b.must_respond_to :genres -# # b.genres.must_be :empty? -# # -# # g = Genre.create!(name: "test genre") -# # b.genres << g -# # b.genres.must_include g -# # end -# # end -# # -# # describe "age" do -# # end -# end #all tests +describe Work do + let(:work) { Work.new } + + it "must be valid" do + value(work).must_be :valid? + end + params.require(:work).permit(:category, :title, :creator, :publication_year, :description) + + describe Work do + let :work{ Work.first } + describe "validations" do + it "can be created with all fields" do + b = Work.new(category: "Album", title: "testing", creator: "Fake Creator", publication_year: 1999, description: "A Good Album") + b.must_be :valid? + end # validations + + it "requires a title" do + b = Work.new + is_valid = b.valid? + is_valid.must_equal false + b.errors.messages.must_include :title + end #require title + + it "requires a unique title" do + title = "test book" + b1 = Work.create!(title: title) + b2 = Work.new(title: title) + + b2.wont_be :valid? + end #uniqueness + end #work do + + describe "description" do + it "rejects descriptions longer than 200 chars" do + desc = "a" * 201 + b = Work.new(title: "Fake Title", description: desc) + + is_valid = b.valid? + is_valid.must_equal false + + b.errors.messages.must_include :description + end #long + + it "allows descriptions <= 200 chars" do + descriptions = [ + "a" * 199, + "a" * 10, + "a" * 0 + ] + + descriptions.each do |desc| + b = Work.new(title: "test", description: desc) + b.must_be :valid? + end #each + end #short + + end #all + + + describe "counter" do + it "can count votes" do + a = Work.new(title: "one", creator: "Fake Creator") + a.count + end # validations + end #counter + + end #all tests