From cfbd01a2f2fe6d822ce05e2b27cfa24a132c540c Mon Sep 17 00:00:00 2001 From: Jamila Date: Mon, 7 May 2018 15:45:14 -0700 Subject: [PATCH 01/59] new project --- .gitignore | 16 ++ Gemfile | 50 ++++++ Gemfile.lock | 154 ++++++++++++++++++ Rakefile | 6 + app/channels/application_cable/channel.rb | 4 + app/channels/application_cable/connection.rb | 4 + app/controllers/application_controller.rb | 2 + app/controllers/concerns/.keep | 0 app/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/mailer.html.erb | 13 ++ app/views/layouts/mailer.text.erb | 1 + bin/bundle | 3 + bin/rails | 9 + bin/rake | 9 + bin/setup | 35 ++++ bin/spring | 17 ++ bin/update | 29 ++++ config.ru | 5 + config/application.rb | 40 +++++ config/boot.rb | 3 + config/cable.yml | 10 ++ config/database.yml | 85 ++++++++++ config/environment.rb | 5 + config/environments/development.rb | 47 ++++++ config/environments/production.rb | 83 ++++++++++ config/environments/test.rb | 42 +++++ .../application_controller_renderer.rb | 8 + config/initializers/backtrace_silencers.rb | 7 + config/initializers/cors.rb | 16 ++ .../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 + lib/tasks/.keep | 0 log/.keep | 0 public/robots.txt | 1 + test/controllers/.keep | 0 test/fixtures/.keep | 0 test/fixtures/files/.keep | 0 test/integration/.keep | 0 test/mailers/.keep | 0 test/models/.keep | 0 test/test_helper.rb | 26 +++ tmp/.keep | 0 vendor/.keep | 0 53 files changed, 907 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Rakefile 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/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/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 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/backtrace_silencers.rb create mode 100644 config/initializers/cors.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 lib/tasks/.keep create mode 100644 log/.keep create mode 100644 public/robots.txt create mode 100644 test/controllers/.keep create mode 100644 test/fixtures/.keep create mode 100644 test/fixtures/files/.keep create mode 100644 test/integration/.keep create mode 100644 test/mailers/.keep create mode 100644 test/models/.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 000000000..68ac019ec --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# 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 + +.byebug_history diff --git a/Gemfile b/Gemfile new file mode 100644 index 000000000..78bff8d26 --- /dev/null +++ b/Gemfile @@ -0,0 +1,50 @@ +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.6' +# Use postgresql as the database for Active Record +gem 'pg', '>= 0.18', '< 2.0' +# Use Puma as the app server +gem 'puma', '~> 3.7' +# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder +# gem 'jbuilder', '~> 2.5' +# Use Redis adapter to run Action Cable in production +# gem 'redis', '~> 4.0' +# Use ActiveModel has_secure_password +# gem 'bcrypt', '~> 3.1.7' + +# Use Capistrano for deployment +# gem 'capistrano-rails', group: :development + +# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible +# gem 'rack-cors' + +group :development, :test do + # Call 'byebug' anywhere in the code to stop execution and get a debugger console + gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] +end + +group :development do + 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] + +group :development, :test do + gem 'pry-rails' +end + +group :test do + gem 'minitest-rails' + gem 'minitest-reporters' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 000000000..e547d2449 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,154 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (5.1.6) + actionpack (= 5.1.6) + nio4r (~> 2.0) + websocket-driver (~> 0.6.1) + actionmailer (5.1.6) + actionpack (= 5.1.6) + actionview (= 5.1.6) + activejob (= 5.1.6) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.1.6) + actionview (= 5.1.6) + activesupport (= 5.1.6) + rack (~> 2.0) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.1.6) + activesupport (= 5.1.6) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.1.6) + activesupport (= 5.1.6) + globalid (>= 0.3.6) + activemodel (5.1.6) + activesupport (= 5.1.6) + activerecord (5.1.6) + activemodel (= 5.1.6) + activesupport (= 5.1.6) + arel (~> 8.0) + activesupport (5.1.6) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + ansi (1.5.0) + arel (8.0.0) + builder (3.2.3) + byebug (10.0.2) + coderay (1.1.2) + concurrent-ruby (1.0.5) + crass (1.0.4) + erubi (1.7.1) + ffi (1.9.23) + globalid (0.4.1) + activesupport (>= 4.2.0) + i18n (1.0.1) + concurrent-ruby (~> 1.0) + listen (3.1.5) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + ruby_dep (~> 1.2) + loofah (2.2.2) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.0) + mini_mime (>= 0.1.1) + method_source (0.9.0) + mini_mime (1.0.0) + mini_portile2 (2.3.0) + minitest (5.11.3) + minitest-rails (3.0.0) + minitest (~> 5.8) + railties (~> 5.0) + minitest-reporters (1.2.0) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + nio4r (2.3.1) + nokogiri (1.8.2) + mini_portile2 (~> 2.3.0) + pg (1.0.0) + pry (0.11.3) + coderay (~> 1.1.0) + method_source (~> 0.9.0) + pry-rails (0.3.6) + pry (>= 0.10.4) + puma (3.11.4) + rack (2.0.5) + rack-test (1.0.0) + rack (>= 1.0, < 3) + rails (5.1.6) + actioncable (= 5.1.6) + actionmailer (= 5.1.6) + actionpack (= 5.1.6) + actionview (= 5.1.6) + activejob (= 5.1.6) + activemodel (= 5.1.6) + activerecord (= 5.1.6) + activesupport (= 5.1.6) + bundler (>= 1.3.0) + railties (= 5.1.6) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.0.4) + loofah (~> 2.2, >= 2.2.2) + railties (5.1.6) + actionpack (= 5.1.6) + activesupport (= 5.1.6) + method_source + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (12.3.1) + rb-fsevent (0.10.3) + rb-inotify (0.9.10) + ffi (>= 0.5.0, < 2) + ruby-progressbar (1.9.0) + ruby_dep (1.5.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) + tzinfo (1.2.5) + thread_safe (~> 0.1) + websocket-driver (0.6.5) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) + +PLATFORMS + ruby + +DEPENDENCIES + byebug + listen (>= 3.0.5, < 3.2) + minitest-rails + minitest-reporters + pg (>= 0.18, < 2.0) + pry-rails + puma (~> 3.7) + rails (~> 5.1.6) + spring + spring-watcher-listen (~> 2.0.0) + tzinfo-data + +BUNDLED WITH + 1.16.1 diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..e85f91391 --- /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/channels/application_cable/channel.rb b/app/channels/application_cable/channel.rb new file mode 100644 index 000000000..d67269728 --- /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 000000000..0ff5442f4 --- /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 000000000..4ac8823b0 --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,2 @@ +class ApplicationController < ActionController::API +end diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/app/jobs/application_job.rb b/app/jobs/application_job.rb new file mode 100644 index 000000000..a009ace51 --- /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 000000000..286b2239d --- /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 000000000..10a4cba84 --- /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 000000000..e69de29bb diff --git a/app/views/layouts/mailer.html.erb b/app/views/layouts/mailer.html.erb new file mode 100644 index 000000000..cbd34d2e9 --- /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 000000000..37f0bddbd --- /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 000000000..66e9889e8 --- /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 000000000..5badb2fde --- /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 000000000..d87d5f578 --- /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 000000000..104e40c1c --- /dev/null +++ b/bin/setup @@ -0,0 +1,35 @@ +#!/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') + + + # 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 000000000..fb2ec2ebb --- /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 000000000..a8e4462f2 --- /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/config.ru b/config.ru new file mode 100644 index 000000000..f7ba0b527 --- /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 000000000..2fe7718ea --- /dev/null +++ b/config/application.rb @@ -0,0 +1,40 @@ +require_relative 'boot' + +require "rails" +# Pick the frameworks you want: +require "active_model/railtie" +require "active_job/railtie" +require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +require "action_view/railtie" +require "action_cable/engine" +# require "sprockets/railtie" +require "rails/test_unit/railtie" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module VideoStoreAPI + 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. + + # Only loads a smaller set of middleware suitable for API only apps. + # Middleware like session, flash, cookies can be added back manually. + # Skip views, helpers and assets when generating a new resource. + config.api_only = true + end +end diff --git a/config/boot.rb b/config/boot.rb new file mode 100644 index 000000000..30f5120df --- /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 000000000..ad59bcd88 --- /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: VideoStoreAPI_production diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 000000000..720570700 --- /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: VideoStoreAPI_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: VideoStoreAPI + + # 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: VideoStoreAPI_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: VideoStoreAPI_production + username: VideoStoreAPI + password: <%= ENV['VIDEOSTOREAPI_DATABASE_PASSWORD'] %> diff --git a/config/environment.rb b/config/environment.rb new file mode 100644 index 000000000..426333bb4 --- /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 000000000..abc82221c --- /dev/null +++ b/config/environments/development.rb @@ -0,0 +1,47 @@ +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 + + + # 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 000000000..3bd8115ea --- /dev/null +++ b/config/environments/production.rb @@ -0,0 +1,83 @@ +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? + + + # 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 = "VideoStoreAPI_#{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 000000000..8e5cbde53 --- /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 000000000..89d2efab2 --- /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/backtrace_silencers.rb b/config/initializers/backtrace_silencers.rb new file mode 100644 index 000000000..59385cdf3 --- /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/cors.rb b/config/initializers/cors.rb new file mode 100644 index 000000000..3b1c1b5ed --- /dev/null +++ b/config/initializers/cors.rb @@ -0,0 +1,16 @@ +# Be sure to restart your server when you modify this file. + +# Avoid CORS issues when API is called from the frontend app. +# Handle Cross-Origin Resource Sharing (CORS) in order to accept cross-origin AJAX requests. + +# Read more: https://github.com/cyu/rack-cors + +# Rails.application.config.middleware.insert_before 0, Rack::Cors do +# allow do +# origins 'example.com' +# +# resource '*', +# headers: :any, +# methods: [:get, :post, :put, :patch, :delete, :options, :head] +# end +# end diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb new file mode 100644 index 000000000..4a994e1e7 --- /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 000000000..ac033bf9d --- /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 000000000..dc1899682 --- /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 000000000..bbfc3961b --- /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 000000000..decc5a857 --- /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 000000000..1e19380dc --- /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 000000000..787824f88 --- /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 000000000..6ce342802 --- /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: 747735aa6c549c4a7d2ff391bace6cc0aea48bb6faedff8be6e5b8a9a37ba804cfeadad6c6dd3371f1bd329799396e423c456e835c2d237f254790f443fda327 + +test: + secret_key_base: c581f29010479ce918b1a38a2ef9f40a4ebf3aee349c804287358f3a8d9c528b045e3a66912394922be2216ccb7cd50ad8406259004b2b5b6751029bb2f06f5c + +# 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 000000000..c9119b40c --- /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/lib/tasks/.keep b/lib/tasks/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/log/.keep b/log/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 000000000..37b576a4a --- /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/controllers/.keep b/test/controllers/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/.keep b/test/fixtures/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/fixtures/files/.keep b/test/fixtures/files/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/integration/.keep b/test/integration/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/mailers/.keep b/test/mailers/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/models/.keep b/test/models/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/test/test_helper.rb b/test/test_helper.rb new file mode 100644 index 000000000..10594a324 --- /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 000000000..e69de29bb diff --git a/vendor/.keep b/vendor/.keep new file mode 100644 index 000000000..e69de29bb From 24161f5b07332abcfde4b6a83d6391b6903b8937 Mon Sep 17 00:00:00 2001 From: Cara Date: Mon, 7 May 2018 15:50:00 -0700 Subject: [PATCH 02/59] Schema etc --- db/schema.rb | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 db/schema.rb diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..47f2312b5 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,39 @@ +# 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: 20180507212509) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "customers", force: :cascade do |t| + t.string "name" + t.datetime "registered_at" + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "movies", force: :cascade do |t| + t.string "title" + t.string "overview" + t.datetime "release_date" + t.integer "inventory" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end From b15fc44334421f342e05fc8fb4fae3790fa1cf64 Mon Sep 17 00:00:00 2001 From: Jamila Date: Mon, 7 May 2018 15:58:35 -0700 Subject: [PATCH 03/59] Remove the suspicious schema file --- db/schema.rb | 39 --------------------------------------- 1 file changed, 39 deletions(-) delete mode 100644 db/schema.rb diff --git a/db/schema.rb b/db/schema.rb deleted file mode 100644 index 47f2312b5..000000000 --- a/db/schema.rb +++ /dev/null @@ -1,39 +0,0 @@ -# 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: 20180507212509) do - - # These are extensions that must be enabled in order to support this database - enable_extension "plpgsql" - - create_table "customers", force: :cascade do |t| - t.string "name" - t.datetime "registered_at" - t.string "address" - t.string "city" - t.string "state" - t.string "postal_code" - t.string "phone" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - - create_table "movies", force: :cascade do |t| - t.string "title" - t.string "overview" - t.datetime "release_date" - t.integer "inventory" - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - end - -end From 45aca21241f7b56a15a8559abec9245e4e0fb5f2 Mon Sep 17 00:00:00 2001 From: Cara Date: Mon, 7 May 2018 16:11:31 -0700 Subject: [PATCH 04/59] initial models, testing, controllers --- app/controllers/customers_controller.rb | 2 + app/controllers/movies_controller.rb | 2 + app/models/customer.rb | 4 ++ app/models/movie.rb | 3 ++ config/routes.rb | 4 ++ db/migrate/20180507230036_create_movies.rb | 12 ++++++ db/migrate/20180507230044_create_customers.rb | 15 +++++++ db/schema.rb | 39 +++++++++++++++++++ test/controllers/customers_controller_test.rb | 7 ++++ test/controllers/movies_controller_test.rb | 7 ++++ test/fixtures/customers.yml | 15 +++++++ test/fixtures/movies.yml | 15 +++++++ test/models/customer_test.rb | 20 ++++++++++ test/models/movie_test.rb | 17 ++++++++ 14 files changed, 162 insertions(+) create mode 100644 app/controllers/customers_controller.rb create mode 100644 app/controllers/movies_controller.rb create mode 100644 app/models/customer.rb create mode 100644 app/models/movie.rb create mode 100644 db/migrate/20180507230036_create_movies.rb create mode 100644 db/migrate/20180507230044_create_customers.rb create mode 100644 db/schema.rb create mode 100644 test/controllers/customers_controller_test.rb create mode 100644 test/controllers/movies_controller_test.rb create mode 100644 test/fixtures/customers.yml create mode 100644 test/fixtures/movies.yml create mode 100644 test/models/customer_test.rb create mode 100644 test/models/movie_test.rb diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb new file mode 100644 index 000000000..ca3b6e024 --- /dev/null +++ b/app/controllers/customers_controller.rb @@ -0,0 +1,2 @@ +class CustomersController < ApplicationController +end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb new file mode 100644 index 000000000..6c4c51614 --- /dev/null +++ b/app/controllers/movies_controller.rb @@ -0,0 +1,2 @@ +class MoviesController < ApplicationController +end diff --git a/app/models/customer.rb b/app/models/customer.rb new file mode 100644 index 000000000..7ddaafc8f --- /dev/null +++ b/app/models/customer.rb @@ -0,0 +1,4 @@ +class Customer < ApplicationRecord + validates :name, presence: true + +end diff --git a/app/models/movie.rb b/app/models/movie.rb new file mode 100644 index 000000000..5ca08fa00 --- /dev/null +++ b/app/models/movie.rb @@ -0,0 +1,3 @@ +class Movie < ApplicationRecord + validates :title, presence: true +end diff --git a/config/routes.rb b/config/routes.rb index 787824f88..4e63d2033 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,7 @@ Rails.application.routes.draw do # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html + + resources :movies, only: [:index, :show, :create] + resources :customers, only: [:index, :show] + end diff --git a/db/migrate/20180507230036_create_movies.rb b/db/migrate/20180507230036_create_movies.rb new file mode 100644 index 000000000..c880abf99 --- /dev/null +++ b/db/migrate/20180507230036_create_movies.rb @@ -0,0 +1,12 @@ +class CreateMovies < ActiveRecord::Migration[5.1] + def change + create_table :movies do |t| + t.string "title" + t.string "overview" + t.datetime "release_date" + t.integer "inventory" + + t.timestamps + end + end +end diff --git a/db/migrate/20180507230044_create_customers.rb b/db/migrate/20180507230044_create_customers.rb new file mode 100644 index 000000000..315f63701 --- /dev/null +++ b/db/migrate/20180507230044_create_customers.rb @@ -0,0 +1,15 @@ +class CreateCustomers < ActiveRecord::Migration[5.1] + def change + create_table :customers do |t| + t.string "name" + t.datetime "registered_at" + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..f3b100516 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,39 @@ +# 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: 20180507230044) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "customers", force: :cascade do |t| + t.string "name" + t.datetime "registered_at" + t.string "address" + t.string "city" + t.string "state" + t.string "postal_code" + t.string "phone" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + create_table "movies", force: :cascade do |t| + t.string "title" + t.string "overview" + t.datetime "release_date" + t.integer "inventory" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb new file mode 100644 index 000000000..5e123f6cd --- /dev/null +++ b/test/controllers/customers_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe CustomersController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb new file mode 100644 index 000000000..67fabbcfb --- /dev/null +++ b/test/controllers/movies_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe MoviesController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml new file mode 100644 index 000000000..f8c6f4c7a --- /dev/null +++ b/test/fixtures/customers.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: { + name: "Pinky" + } +# column: value +# +two: { + name: "Brain" + } +# column: value diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml new file mode 100644 index 000000000..dc09a0d9d --- /dev/null +++ b/test/fixtures/movies.yml @@ -0,0 +1,15 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: { + title: "A wonderful movie" + } +# column: value +# +two: { + title: "Lion King" + } +# column: value diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb new file mode 100644 index 000000000..4c1e9954c --- /dev/null +++ b/test/models/customer_test.rb @@ -0,0 +1,20 @@ +require "test_helper" + +describe Customer do + let(:customer) { Customer.new } + + it "must be valid" do + value(customer).must_be :valid? + end + + it "must have a name" do + customer = Customer.first + + customer.name = nil + customer.reload + + a = customer.valid? + + a.must_equal false + end +end diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb new file mode 100644 index 000000000..e9e359b51 --- /dev/null +++ b/test/models/movie_test.rb @@ -0,0 +1,17 @@ +require "test_helper" + +describe Movie do + let(:movie) { Movie.new } + + it "must be valid" do + value(movie).must_be :valid? + end + + it "must have a name" do + movie = Movie.first + + movie.title = nil + + movie.wont_be_valid + end +end From 0dbf050a6857bd690d90c9546fb3521304e4e9dd Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Mon, 7 May 2018 16:27:28 -0700 Subject: [PATCH 05/59] Customer tests passing --- test/models/customer_test.rb | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/test/models/customer_test.rb b/test/models/customer_test.rb index 4c1e9954c..fa8b5dcd5 100644 --- a/test/models/customer_test.rb +++ b/test/models/customer_test.rb @@ -1,20 +1,19 @@ require "test_helper" describe Customer do - let(:customer) { Customer.new } - it "must be valid" do - value(customer).must_be :valid? + it "will be valid with a name" do + customer = Customer.first + customer.name.nil?.must_equal false + customer.valid?.must_equal true end - it "must have a name" do + it "will be invalid without a name" do customer = Customer.first - customer.name = nil - customer.reload + customer.save + customer.valid?.must_equal false + end - a = customer.valid? - a.must_equal false - end end From d0080531e76e2ae256428f7ee2fc23838cb56ef6 Mon Sep 17 00:00:00 2001 From: Jamila Date: Mon, 7 May 2018 16:28:44 -0700 Subject: [PATCH 06/59] basic tests passing for movie model --- test/models/movie_test.rb | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index e9e359b51..73d040d0a 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -1,7 +1,7 @@ require "test_helper" describe Movie do - let(:movie) { Movie.new } + let(:movie) { Movie.first } it "must be valid" do value(movie).must_be :valid? @@ -12,6 +12,8 @@ movie.title = nil - movie.wont_be_valid + movie.wont_be :valid? end + + # Maybe raise an error if inventory = 0? end From 5e3415d7c42c911f00d847df1d2d7a563445bea2 Mon Sep 17 00:00:00 2001 From: Cara Date: Mon, 7 May 2018 16:29:54 -0700 Subject: [PATCH 07/59] initial controller methods --- app/controllers/customers_controller.rb | 19 +++++++++++++++++++ app/controllers/movies_controller.rb | 21 +++++++++++++++++++++ 2 files changed, 40 insertions(+) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index ca3b6e024..19435fd97 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,2 +1,21 @@ class CustomersController < ApplicationController + + def index + customers = Customer.all + render(json: customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok) + end + + def show + customer = Customer.find_by(id: params[:id]) + if movie.nil? + render json: { + errors: { + id: ["No customer with ID #{params[:id]}"] + } + }, status: :not_found + else + render json: customer.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone], status: :ok) + end + end + end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 6c4c51614..76f431f5e 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,2 +1,23 @@ class MoviesController < ApplicationController + def index + movies = Movie.all + render(json: movies.as_json(only: [:title, :overview, :inventory]), status: :ok) + end + + def show + movie = Movie.find_by(id: params[:id]) + if movie.nil? + render json: { + errors: { + id: ["No movie with ID #{params[:id]}"] + } + }, status: :not_found + else + render json: movie.as_json(only: [:title, :overview, :inventory], status: :ok) + end + end + + def create + + end end From c273efed9da657816bc5e70fae3829a3e329378a Mon Sep 17 00:00:00 2001 From: Cara Date: Mon, 7 May 2018 16:34:19 -0700 Subject: [PATCH 08/59] Added create method to movies_controller.rb --- app/controllers/movies_controller.rb | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 76f431f5e..486edaffa 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,7 +1,7 @@ class MoviesController < ApplicationController def index movies = Movie.all - render(json: movies.as_json(only: [:title, :overview, :inventory]), status: :ok) + render(json: movies.as_json(only: [:title, :overview, :release_date, :inventory]), status: :ok) end def show @@ -13,11 +13,19 @@ def show } }, status: :not_found else - render json: movie.as_json(only: [:title, :overview, :inventory], status: :ok) + render json: movie.as_json(only: [:title, :overview, :release_date, :inventory], status: :ok) end end def create - + movie = Movie.new(movie_params) + movie.save! + render json: {id: movie.id}, status: :ok + + end + + private + def movie_params + params.require(:movie).permit(:title, :overview, :release_date, :inventory) end end From 8a42a8dbb8964cf3f2a22b19cd385762fa4f7f97 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Mon, 7 May 2018 16:34:47 -0700 Subject: [PATCH 09/59] Controller tests --- app/controllers/movies_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 76f431f5e..45232c1d0 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,4 +1,5 @@ class MoviesController < ApplicationController + def index movies = Movie.all render(json: movies.as_json(only: [:title, :overview, :inventory]), status: :ok) From d29874accac392af0fd23d100fda6f51635fc2d6 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Mon, 7 May 2018 17:14:24 -0700 Subject: [PATCH 10/59] Added ids and fixed movie controller methods to show requested stuff --- .DS_Store | Bin 0 -> 6148 bytes app/controllers/movies_controller.rb | 5 ++--- app/models/rental.rb | 2 ++ test/fixtures/rentals.yml | 11 +++++++++++ test/models/rental_test.rb | 9 +++++++++ 5 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 .DS_Store create mode 100644 app/models/rental.rb create mode 100644 test/fixtures/rentals.yml create mode 100644 test/models/rental_test.rb diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000000000000000000000000000000000000..c3ac046f6c51a2cd4bc2df32c603e493458c382e GIT binary patch literal 6148 zcmeH~F^{dt=u>Bl;zddfqZjB=JE!l7EiIwCR znHYd=e=Zwf1hAw#vGy=AV?N-4FWm9?zTD5J+wJN_+D8XGrH`2G=e8gPq<|EV0#ZN< z%t(Pe#+RQndL})J6p#Y*P{6+rh3>4$))}7;h8O|Jf#oo+W0oL`7s#4yovhF-rw7Yc zi!sFO(N31Ut|nV&Z-?dZVR>isDTZdf9afmotOgXMfE1W0u;}^d=l`Dm-~2ymQ7Q$b zz?&&x!|t%#@}=@@{quTWKW5d}jZVhp3{O7+O#CQb(Zjf3d_mS^>tuzdAAyiTK?=N7 FfnTX=61e~X literal 0 HcmV?d00001 diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 353d906be..2af2a3a1c 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,7 +2,7 @@ class MoviesController < ApplicationController def index movies = Movie.all - render(json: movies.as_json(only: [:title, :overview, :release_date, :inventory]), status: :ok) + render(json: movies.as_json(only: [:id, :title, :release_date]), status: :ok) end def show @@ -14,7 +14,7 @@ def show } }, status: :not_found else - render json: movie.as_json(only: [:title, :overview, :release_date, :inventory], status: :ok) + render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory], status: :ok) end end @@ -22,7 +22,6 @@ def create movie = Movie.new(movie_params) movie.save! render json: {id: movie.id}, status: :ok - end private diff --git a/app/models/rental.rb b/app/models/rental.rb new file mode 100644 index 000000000..79e3a65ca --- /dev/null +++ b/app/models/rental.rb @@ -0,0 +1,2 @@ +class Rental < ApplicationRecord +end diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml new file mode 100644 index 000000000..dc3ee79b5 --- /dev/null +++ b/test/fixtures/rentals.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb new file mode 100644 index 000000000..6ea53d94f --- /dev/null +++ b/test/models/rental_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe Rental do + let(:rental) { Rental.new } + + it "must be valid" do + value(rental).must_be :valid? + end +end From f74a90716103609304999ac4e1c7ccf65f8fea08 Mon Sep 17 00:00:00 2001 From: Cara Date: Mon, 7 May 2018 17:21:58 -0700 Subject: [PATCH 11/59] added available_inventory --- .../20180508001957_add_available_inventory_to_movies.rb | 5 +++++ db/schema.rb | 3 ++- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 db/migrate/20180508001957_add_available_inventory_to_movies.rb diff --git a/db/migrate/20180508001957_add_available_inventory_to_movies.rb b/db/migrate/20180508001957_add_available_inventory_to_movies.rb new file mode 100644 index 000000000..20126c002 --- /dev/null +++ b/db/migrate/20180508001957_add_available_inventory_to_movies.rb @@ -0,0 +1,5 @@ +class AddAvailableInventoryToMovies < ActiveRecord::Migration[5.1] + def change + add_column :movies, :available_inventory, :integer, default: 10 + end +end diff --git a/db/schema.rb b/db/schema.rb index f3b100516..8c3e5a7b8 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180507230044) do +ActiveRecord::Schema.define(version: 20180508001957) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -34,6 +34,7 @@ t.integer "inventory" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "available_inventory", default: 10 end end From b8f512dcb5c2e6be70058b77dd5c573b09e8ffe9 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Mon, 7 May 2018 17:29:26 -0700 Subject: [PATCH 12/59] Added id --- app/controllers/customers_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 19435fd97..9469161f7 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -2,7 +2,7 @@ class CustomersController < ApplicationController def index customers = Customer.all - render(json: customers.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone]), status: :ok) + render(json: customers.as_json(only: [:id, :name, :registered_at, :postal_code, :phone]), status: :ok) end def show From d138614f56244940d01c062a9531a97355565af0 Mon Sep 17 00:00:00 2001 From: Jamila Date: Mon, 7 May 2018 22:29:49 -0700 Subject: [PATCH 13/59] made available inventory show --- app/controllers/movies_controller.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 2af2a3a1c..eb09857a8 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,7 +2,7 @@ class MoviesController < ApplicationController def index movies = Movie.all - render(json: movies.as_json(only: [:id, :title, :release_date]), status: :ok) + render(json: movies.as_json(only: [:id, :title, :release_date, :inventory]), status: :ok) end def show @@ -14,7 +14,7 @@ def show } }, status: :not_found else - render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory], status: :ok) + render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory, :available_inventory], status: :ok) end end From d6d8efb0b4d6148596fd238bffad57ef00fe6523 Mon Sep 17 00:00:00 2001 From: Jamila Date: Mon, 7 May 2018 22:51:49 -0700 Subject: [PATCH 14/59] added column for checked out movies to customers and modified controller --- app/controllers/customers_controller.rb | 2 +- db/migrate/20180508053036_add_column_to_customers.rb | 5 +++++ db/migrate/20180508054311_remove_column_from_customers.rb | 5 +++++ db/migrate/20180508054736_add_correct_column_to_customers.rb | 5 +++++ db/schema.rb | 3 ++- 5 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 db/migrate/20180508053036_add_column_to_customers.rb create mode 100644 db/migrate/20180508054311_remove_column_from_customers.rb create mode 100644 db/migrate/20180508054736_add_correct_column_to_customers.rb diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 9469161f7..6a4bf3d91 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -2,7 +2,7 @@ class CustomersController < ApplicationController def index customers = Customer.all - render(json: customers.as_json(only: [:id, :name, :registered_at, :postal_code, :phone]), status: :ok) + render(json: customers.as_json(only: [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count]), status: :ok) end def show diff --git a/db/migrate/20180508053036_add_column_to_customers.rb b/db/migrate/20180508053036_add_column_to_customers.rb new file mode 100644 index 000000000..38a57423b --- /dev/null +++ b/db/migrate/20180508053036_add_column_to_customers.rb @@ -0,0 +1,5 @@ +class AddColumnToCustomers < ActiveRecord::Migration[5.1] + def change + add_column :customers, :movies_checked_out, :integer + end +end diff --git a/db/migrate/20180508054311_remove_column_from_customers.rb b/db/migrate/20180508054311_remove_column_from_customers.rb new file mode 100644 index 000000000..8c1a92839 --- /dev/null +++ b/db/migrate/20180508054311_remove_column_from_customers.rb @@ -0,0 +1,5 @@ +class RemoveColumnFromCustomers < ActiveRecord::Migration[5.1] + def change + remove_column :customers, :movies_checked_out, :integer + end +end diff --git a/db/migrate/20180508054736_add_correct_column_to_customers.rb b/db/migrate/20180508054736_add_correct_column_to_customers.rb new file mode 100644 index 000000000..9305a280c --- /dev/null +++ b/db/migrate/20180508054736_add_correct_column_to_customers.rb @@ -0,0 +1,5 @@ +class AddCorrectColumnToCustomers < ActiveRecord::Migration[5.1] + def change + add_column :customers, :movies_checked_out_count, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 8c3e5a7b8..9ab45824d 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180508001957) do +ActiveRecord::Schema.define(version: 20180508054736) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -25,6 +25,7 @@ t.string "phone" t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "movies_checked_out_count" end create_table "movies", force: :cascade do |t| From 7c7b54d775be5767ebe62c82b60c5c5eafe0213d Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 10:21:39 -0700 Subject: [PATCH 15/59] fix for rentals ghost model --- app/models/rental.rb | 2 -- test/fixtures/rentals.yml | 11 ----------- test/models/rental_test.rb | 9 --------- 3 files changed, 22 deletions(-) delete mode 100644 app/models/rental.rb delete mode 100644 test/fixtures/rentals.yml delete mode 100644 test/models/rental_test.rb diff --git a/app/models/rental.rb b/app/models/rental.rb deleted file mode 100644 index 79e3a65ca..000000000 --- a/app/models/rental.rb +++ /dev/null @@ -1,2 +0,0 @@ -class Rental < ApplicationRecord -end diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml deleted file mode 100644 index dc3ee79b5..000000000 --- a/test/fixtures/rentals.yml +++ /dev/null @@ -1,11 +0,0 @@ -# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html - -# This model initially had no columns defined. If you add columns to the -# model remove the "{}" from the fixture names and add the columns immediately -# below each fixture, per the syntax in the comments below -# -one: {} -# column: value -# -two: {} -# column: value diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb deleted file mode 100644 index 6ea53d94f..000000000 --- a/test/models/rental_test.rb +++ /dev/null @@ -1,9 +0,0 @@ -require "test_helper" - -describe Rental do - let(:rental) { Rental.new } - - it "must be valid" do - value(rental).must_be :valid? - end -end From e3c63a7fdfb59601188317586559faf48645ac97 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 10:27:33 -0700 Subject: [PATCH 16/59] Fixed params --- app/controllers/movies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index eb09857a8..0dad576cb 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,6 +26,6 @@ def create private def movie_params - params.require(:movie).permit(:title, :overview, :release_date, :inventory) + params.permit(:title, :overview, :release_date, :inventory) end end From 2c54d5282b83be25fe0564a41042944578e37925 Mon Sep 17 00:00:00 2001 From: Cara Date: Tue, 8 May 2018 10:29:19 -0700 Subject: [PATCH 17/59] Adding changes --- app/controllers/movies_controller.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index eb09857a8..0268388e9 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,6 +26,7 @@ def create private def movie_params - params.require(:movie).permit(:title, :overview, :release_date, :inventory) + #expecting where key is movie and value is hash + params.permit(:title, :overview, :release_date, :inventory) end end From 0d5d9a7f67dfe204afda56eb4655c8b9234fde16 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 11:14:44 -0700 Subject: [PATCH 18/59] Tests for customers index function --- app/controllers/movies_controller.rb | 4 -- test/controllers/customers_controller_test.rb | 55 ++++++++++++++++++- 2 files changed, 52 insertions(+), 7 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 08cf2bc92..0dad576cb 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,10 +26,6 @@ def create private def movie_params -<<<<<<< HEAD - #expecting where key is movie and value is hash -======= ->>>>>>> e3c63a7fdfb59601188317586559faf48645ac97 params.permit(:title, :overview, :release_date, :inventory) end end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 5e123f6cd..8890292ab 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -1,7 +1,56 @@ require "test_helper" describe CustomersController do - # it "must be a real test" do - # flunk "Need real tests" - # end + + describe "index" do + + it "is a working route that returns json" do + get customers_url + must_respond_with :success + response.header['Content-Type'].must_include 'json' + end + + it "returns pets with exactly the required fields" do + keys = %w(id movies_checked_out_count name phone postal_code registered_at) + get customers_url + body = JSON.parse(response.body) + body.each do |customer| + customer.keys.sort.must_equal keys + end + end + + it "must return nil when 0 customers" do + Customer.destroy_all + Customer.all.count.must_equal 0 + + get customers_url + body = JSON.parse(response.body) + body.empty?.must_equal true + end + + it "must return one customer when one customer" do + Customer.destroy_all + test_customer = Customer.new(name: "Test Name", registered_at: Time.now, address: "123 Main Street", city: "Seattle", state: "WA", postal_code: "98125", phone: "206-123-5555" ) + test_customer.save + + Customer.all.count.must_equal 1 + + get customers_url + body = JSON.parse(response.body) + body.must_be_kind_of Array + body.length.must_equal 1 + end + + it "must return many customers when many customers" do + count = Customer.all.count + count.must_be :>,1 + + get customers_url + body = JSON.parse(response.body) + body.must_be_kind_of Array + body.length.must_equal count + end + + end + end From a48f2a78272ad12b08ee5cf7cb2bf049b6f61676 Mon Sep 17 00:00:00 2001 From: Cara Date: Tue, 8 May 2018 11:22:55 -0700 Subject: [PATCH 19/59] Show Tests --- app/controllers/customers_controller.rb | 4 +- app/controllers/movies_controller.rb | 4 -- test/controllers/customers_controller_test.rb | 36 ++++++++++++++++++ test/controllers/movies_controller_test.rb | 38 +++++++++++++++++++ 4 files changed, 76 insertions(+), 6 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index 6a4bf3d91..f9b6c8c0c 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -7,14 +7,14 @@ def index def show customer = Customer.find_by(id: params[:id]) - if movie.nil? + if customer.nil? render json: { errors: { id: ["No customer with ID #{params[:id]}"] } }, status: :not_found else - render json: customer.as_json(only: [:name, :registered_at, :address, :city, :state, :postal_code, :phone], status: :ok) + render json: customer.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone], status: :ok) end end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 08cf2bc92..0dad576cb 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,10 +26,6 @@ def create private def movie_params -<<<<<<< HEAD - #expecting where key is movie and value is hash -======= ->>>>>>> e3c63a7fdfb59601188317586559faf48645ac97 params.permit(:title, :overview, :release_date, :inventory) end end diff --git a/test/controllers/customers_controller_test.rb b/test/controllers/customers_controller_test.rb index 5e123f6cd..b12d9122e 100644 --- a/test/controllers/customers_controller_test.rb +++ b/test/controllers/customers_controller_test.rb @@ -4,4 +4,40 @@ # it "must be a real test" do # flunk "Need real tests" # end + + describe 'show' do + + it 'can get a customer' do + + keys = %w(address city id name phone postal_code registered_at state) + + customer = customers(:two) + # Act + get customer_path(customer.id) + + # Assert + must_respond_with :success + + response.header['Content-Type'].must_include 'json' + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.keys.sort.must_equal keys + body["id"].must_equal customer.id + end + + it 'yields a not_found status and also return some error if the customer DNE' do + customer_id = Customer.last.id + 1 + + get customer_path(customer_id) + + must_respond_with :not_found + + # Check that it sent us some error text + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.must_include "errors" + body["errors"].must_include "id" + end + + end end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 67fabbcfb..6eac8cb02 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -4,4 +4,42 @@ # it "must be a real test" do # flunk "Need real tests" # end + + + describe 'show' do + + it 'can get a movie' do + + keys = %w(available_inventory id inventory overview release_date title) + + movie = movies(:two) + + # Act + get movie_path(movie.id) + + # Assert + must_respond_with :success + + response.header['Content-Type'].must_include 'json' + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.keys.sort.must_equal keys + body["id"].must_equal movie.id + end + + it 'yields a not_found status and also return some error if the movie DNE' do + movie_id = Movie.last.id + 1 + + get movie_path(movie_id) + + must_respond_with :not_found + + # Check that it sent us some error text + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.must_include "errors" + body["errors"].must_include "id" + end + + end end From bc8b38744c11911f44ed4e396e274333dadfc657 Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 11:34:01 -0700 Subject: [PATCH 20/59] movies controller tests passing --- app/controllers/movies_controller.rb | 3 -- test/controllers/movies_controller_test.rb | 53 ++++++++++++++++++++-- 2 files changed, 50 insertions(+), 6 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 08cf2bc92..0268388e9 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,10 +26,7 @@ def create private def movie_params -<<<<<<< HEAD #expecting where key is movie and value is hash -======= ->>>>>>> e3c63a7fdfb59601188317586559faf48645ac97 params.permit(:title, :overview, :release_date, :inventory) end end diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 67fabbcfb..375dd1b67 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -1,7 +1,54 @@ require "test_helper" describe MoviesController do - # it "must be a real test" do - # flunk "Need real tests" - # end + describe "index" do + it "must provide a list of movies" do + get movies_url + + must_respond_with :success + end + + it "returns all the movies" do + get movies_url + + body = JSON.parse(response.body) + body.length.must_equal Movie.count + + end + + it "returns json" do + get movies_url + response.header['Content-Type'].must_include 'json' + end + + end + + describe "create" do + movie_data = { + title: "Hook", + overview: "Robin Williams duh", + release_date: 1997, + inventory: 5 + } + + it "Creates a new movie" do + # this is taking the count before and after + before_count = Movie.count + + post movies_url, params: movie_data + must_respond_with :success + + Movie.count.must_equal before_count + 1 + + + body = JSON.parse(response.body) + body.must_be_kind_of Hash + body.must_include "id" + + # Check that the ID matches + Movie.find(body["id"]).title.must_equal movie_data[:title] + end + + end + end From 0c0ef7f9cb832cd6021143e3c084c00752adce8e Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 11:37:35 -0700 Subject: [PATCH 21/59] resolving merge conflict --- app/controllers/movies_controller.rb | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index c5b05634e..0dad576cb 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -26,10 +26,6 @@ def create private def movie_params -<<<<<<< HEAD - #expecting where key is movie and value is hash -======= ->>>>>>> 984e21d20f2cad50e9003ef6db8642ee58125a77 params.permit(:title, :overview, :release_date, :inventory) end end From 006c6352f8348e440bde5136d641d517ec71ccb7 Mon Sep 17 00:00:00 2001 From: Cara Date: Tue, 8 May 2018 12:31:14 -0700 Subject: [PATCH 22/59] Rabl --- Gemfile | 4 ++++ Gemfile.lock | 10 ++++++++++ app/controllers/movies_controller.rb | 15 +++++++++------ app/views/movies/index.json.rabl | 0 app/views/movies/show.rabl | 4 ++++ 5 files changed, 27 insertions(+), 6 deletions(-) create mode 100644 app/views/movies/index.json.rabl create mode 100644 app/views/movies/show.rabl diff --git a/Gemfile b/Gemfile index 78bff8d26..b440335b8 100644 --- a/Gemfile +++ b/Gemfile @@ -25,6 +25,10 @@ gem 'puma', '~> 3.7' # Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible # gem 'rack-cors' +gem 'rabl' +gem 'oj' +gem 'jbuilder', '~> 2.5' + 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] diff --git a/Gemfile.lock b/Gemfile.lock index e547d2449..1583e0bb7 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -51,6 +51,9 @@ GEM activesupport (>= 4.2.0) i18n (1.0.1) concurrent-ruby (~> 1.0) + jbuilder (2.7.0) + activesupport (>= 4.2.0) + multi_json (>= 1.2) listen (3.1.5) rb-fsevent (~> 0.9, >= 0.9.4) rb-inotify (~> 0.9, >= 0.9.7) @@ -72,9 +75,11 @@ GEM builder minitest (>= 5.0) ruby-progressbar + multi_json (1.13.1) nio4r (2.3.1) nokogiri (1.8.2) mini_portile2 (~> 2.3.0) + oj (3.6.0) pg (1.0.0) pry (0.11.3) coderay (~> 1.1.0) @@ -82,6 +87,8 @@ GEM pry-rails (0.3.6) pry (>= 0.10.4) puma (3.11.4) + rabl (0.13.1) + activesupport (>= 2.3.14) rack (2.0.5) rack-test (1.0.0) rack (>= 1.0, < 3) @@ -139,12 +146,15 @@ PLATFORMS DEPENDENCIES byebug + jbuilder (~> 2.5) listen (>= 3.0.5, < 3.2) minitest-rails minitest-reporters + oj pg (>= 0.18, < 2.0) pry-rails puma (~> 3.7) + rabl rails (~> 5.1.6) spring spring-watcher-listen (~> 2.0.0) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 0dad576cb..03600c797 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -1,26 +1,29 @@ class MoviesController < ApplicationController def index - movies = Movie.all + @movies = Movie.all render(json: movies.as_json(only: [:id, :title, :release_date, :inventory]), status: :ok) end def show - movie = Movie.find_by(id: params[:id]) - if movie.nil? + Rails.logger.level=:debug + @movie = Movie.find_by(id: params[:id]) + if @movie.nil? render json: { errors: { id: ["No movie with ID #{params[:id]}"] } }, status: :not_found else - render json: movie.as_json(only: [:id, :title, :overview, :release_date, :inventory, :available_inventory], status: :ok) + puts "DPR about to do render" + # render json: @movie.as_json, status: :ok + render :show, status: :ok end end def create - movie = Movie.new(movie_params) - movie.save! + @movie = Movie.new(movie_params) + @movie.save! render json: {id: movie.id}, status: :ok end diff --git a/app/views/movies/index.json.rabl b/app/views/movies/index.json.rabl new file mode 100644 index 000000000..e69de29bb diff --git a/app/views/movies/show.rabl b/app/views/movies/show.rabl new file mode 100644 index 000000000..e9875126d --- /dev/null +++ b/app/views/movies/show.rabl @@ -0,0 +1,4 @@ +object @movie +puts "DPR: inside the movie show rabl" +puts "Movie is #{@movie.inspect}" +attributes :id, :title, :overview , :release_date, :inventory, :available_inventory From cc3cdb2ff039cd8717e23f3a0cd4bf9cb58a2ad4 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 14:34:38 -0700 Subject: [PATCH 23/59] Rabl working and all test passing --- app/controllers/customers_controller.rb | 10 +++++----- app/controllers/movies_controller.rb | 10 +++++----- app/views/customers/index.rabl | 3 +++ app/views/customers/show.rabl | 3 +++ app/views/movies/create.rabl | 3 +++ app/views/movies/index.json.rabl | 0 app/views/movies/index.rabl | 3 +++ app/views/movies/show.rabl | 3 +-- config/initializers/rabl_config.rb | 3 +++ 9 files changed, 26 insertions(+), 12 deletions(-) create mode 100644 app/views/customers/index.rabl create mode 100644 app/views/customers/show.rabl create mode 100644 app/views/movies/create.rabl delete mode 100644 app/views/movies/index.json.rabl create mode 100644 app/views/movies/index.rabl create mode 100644 config/initializers/rabl_config.rb diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index f9b6c8c0c..bf2ecc059 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -1,20 +1,20 @@ class CustomersController < ApplicationController def index - customers = Customer.all - render(json: customers.as_json(only: [:id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count]), status: :ok) + @customers = Customer.all + render :index, status: :ok end def show - customer = Customer.find_by(id: params[:id]) - if customer.nil? + @customer = Customer.find_by(id: params[:id]) + if @customer.nil? render json: { errors: { id: ["No customer with ID #{params[:id]}"] } }, status: :not_found else - render json: customer.as_json(only: [:id, :name, :registered_at, :address, :city, :state, :postal_code, :phone], status: :ok) + render :show, status: :ok end end diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 03600c797..45a337bde 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,11 +2,10 @@ class MoviesController < ApplicationController def index @movies = Movie.all - render(json: movies.as_json(only: [:id, :title, :release_date, :inventory]), status: :ok) + render :index, status: :ok end def show - Rails.logger.level=:debug @movie = Movie.find_by(id: params[:id]) if @movie.nil? render json: { @@ -15,8 +14,6 @@ def show } }, status: :not_found else - puts "DPR about to do render" - # render json: @movie.as_json, status: :ok render :show, status: :ok end end @@ -24,7 +21,10 @@ def show def create @movie = Movie.new(movie_params) @movie.save! - render json: {id: movie.id}, status: :ok + render :create, status: :ok + # @movie = Movie.new(movie_params) + # @movie.save! + # render json: {id: movie.id}, status: :ok end private diff --git a/app/views/customers/index.rabl b/app/views/customers/index.rabl new file mode 100644 index 000000000..8926c1cb7 --- /dev/null +++ b/app/views/customers/index.rabl @@ -0,0 +1,3 @@ +collection @customers + +attributes :id, :name, :registered_at, :postal_code, :phone, :movies_checked_out_count diff --git a/app/views/customers/show.rabl b/app/views/customers/show.rabl new file mode 100644 index 000000000..005582a93 --- /dev/null +++ b/app/views/customers/show.rabl @@ -0,0 +1,3 @@ +object @customer + +attributes :id, :name, :registered_at, :address, :city, :state, :postal_code, :phone diff --git a/app/views/movies/create.rabl b/app/views/movies/create.rabl new file mode 100644 index 000000000..d25ca4515 --- /dev/null +++ b/app/views/movies/create.rabl @@ -0,0 +1,3 @@ +object @movie + +attributes :id diff --git a/app/views/movies/index.json.rabl b/app/views/movies/index.json.rabl deleted file mode 100644 index e69de29bb..000000000 diff --git a/app/views/movies/index.rabl b/app/views/movies/index.rabl new file mode 100644 index 000000000..384d1c219 --- /dev/null +++ b/app/views/movies/index.rabl @@ -0,0 +1,3 @@ +collection @movies + +attributes :id, :title, :release_date diff --git a/app/views/movies/show.rabl b/app/views/movies/show.rabl index e9875126d..1c4e265a0 100644 --- a/app/views/movies/show.rabl +++ b/app/views/movies/show.rabl @@ -1,4 +1,3 @@ object @movie -puts "DPR: inside the movie show rabl" -puts "Movie is #{@movie.inspect}" + attributes :id, :title, :overview , :release_date, :inventory, :available_inventory diff --git a/config/initializers/rabl_config.rb b/config/initializers/rabl_config.rb new file mode 100644 index 000000000..f11eb190d --- /dev/null +++ b/config/initializers/rabl_config.rb @@ -0,0 +1,3 @@ +Rabl.configure do |config| + config.include_json_root = false +end From 2098fe54072151d6791636f452e70fa76dbb68d0 Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 14:54:56 -0700 Subject: [PATCH 24/59] stuff --- app/controllers/movies_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 03600c797..1f0b4ac03 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -2,7 +2,7 @@ class MoviesController < ApplicationController def index @movies = Movie.all - render(json: movies.as_json(only: [:id, :title, :release_date, :inventory]), status: :ok) + render :index, status: :ok end def show From d835256d41147ac427114e121058f9ea2d39e073 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 15:53:00 -0700 Subject: [PATCH 25/59] Added rentals table and relationships --- app/models/customer.rb | 5 +++-- app/models/movie.rb | 2 ++ app/models/rental.rb | 4 ++++ db/migrate/20180508221402_create_rentals.rb | 11 +++++++++++ db/migrate/20180508224222_add_foreign_key.rb | 6 ++++++ db/schema.rb | 16 +++++++++++++++- test/fixtures/rentals.yml | 11 +++++++++++ test/models/rental_test.rb | 9 +++++++++ 8 files changed, 61 insertions(+), 3 deletions(-) create mode 100644 app/models/rental.rb create mode 100644 db/migrate/20180508221402_create_rentals.rb create mode 100644 db/migrate/20180508224222_add_foreign_key.rb create mode 100644 test/fixtures/rentals.yml create mode 100644 test/models/rental_test.rb diff --git a/app/models/customer.rb b/app/models/customer.rb index 7ddaafc8f..8c3ca0953 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,4 +1,5 @@ class Customer < ApplicationRecord - validates :name, presence: true - + validates :name, presence: true + has_many :rentals + has_many :movies, through: :rentals end diff --git a/app/models/movie.rb b/app/models/movie.rb index 5ca08fa00..4bfc710bc 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -1,3 +1,5 @@ class Movie < ApplicationRecord validates :title, presence: true + has_many :rentals + has_many :customers, through: :rentals end diff --git a/app/models/rental.rb b/app/models/rental.rb new file mode 100644 index 000000000..e001ff37c --- /dev/null +++ b/app/models/rental.rb @@ -0,0 +1,4 @@ +class Rental < ApplicationRecord + belongs_to :customer + belongs_to :movie +end diff --git a/db/migrate/20180508221402_create_rentals.rb b/db/migrate/20180508221402_create_rentals.rb new file mode 100644 index 000000000..870e84958 --- /dev/null +++ b/db/migrate/20180508221402_create_rentals.rb @@ -0,0 +1,11 @@ +class CreateRentals < ActiveRecord::Migration[5.1] + def change + create_table :rentals do |t| + t.datetime "due_date" + t.datetime "checkout_date" + t.datetime "checkin_date" + + t.timestamps + end + end +end diff --git a/db/migrate/20180508224222_add_foreign_key.rb b/db/migrate/20180508224222_add_foreign_key.rb new file mode 100644 index 000000000..81d2069f0 --- /dev/null +++ b/db/migrate/20180508224222_add_foreign_key.rb @@ -0,0 +1,6 @@ +class AddForeignKey < ActiveRecord::Migration[5.1] + def change + add_reference :rentals, :customer, foreign_key: true + add_reference :rentals, :movie, foreign_key: true + end +end diff --git a/db/schema.rb b/db/schema.rb index 9ab45824d..6e14125c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180508054736) do +ActiveRecord::Schema.define(version: 20180508224222) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -38,4 +38,18 @@ t.integer "available_inventory", default: 10 end + create_table "rentals", force: :cascade do |t| + t.datetime "due_date" + t.datetime "checkout_date" + t.datetime "checkin_date" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.bigint "customer_id" + t.bigint "movie_id" + t.index ["customer_id"], name: "index_rentals_on_customer_id" + t.index ["movie_id"], name: "index_rentals_on_movie_id" + end + + add_foreign_key "rentals", "customers" + add_foreign_key "rentals", "movies" end diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml new file mode 100644 index 000000000..dc3ee79b5 --- /dev/null +++ b/test/fixtures/rentals.yml @@ -0,0 +1,11 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +# This model initially had no columns defined. If you add columns to the +# model remove the "{}" from the fixture names and add the columns immediately +# below each fixture, per the syntax in the comments below +# +one: {} +# column: value +# +two: {} +# column: value diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb new file mode 100644 index 000000000..6ea53d94f --- /dev/null +++ b/test/models/rental_test.rb @@ -0,0 +1,9 @@ +require "test_helper" + +describe Rental do + let(:rental) { Rental.new } + + it "must be valid" do + value(rental).must_be :valid? + end +end From 0b9ea38954482977bb079e21eb7229361e40bf57 Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 16:22:20 -0700 Subject: [PATCH 26/59] routes? rental controller --- app/controllers/rentals_controller.rb | 5 +++++ config/routes.rb | 6 +++++- test/controllers/rentals_controller_test.rb | 7 +++++++ 3 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 app/controllers/rentals_controller.rb create mode 100644 test/controllers/rentals_controller_test.rb diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb new file mode 100644 index 000000000..a0fe00c07 --- /dev/null +++ b/app/controllers/rentals_controller.rb @@ -0,0 +1,5 @@ +class RentalsController < ApplicationController + def method_name + + end +end diff --git a/config/routes.rb b/config/routes.rb index 4e63d2033..b66d80aaa 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,6 +2,10 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :movies, only: [:index, :show, :create] - resources :customers, only: [:index, :show] + resources :customers, only: [:index, :show] do + resources :rental, only: [:create], as: 'checkout' + end + + end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb new file mode 100644 index 000000000..f0227216c --- /dev/null +++ b/test/controllers/rentals_controller_test.rb @@ -0,0 +1,7 @@ +require "test_helper" + +describe RentalsController do + # it "must be a real test" do + # flunk "Need real tests" + # end +end From 7cf9f9ba0759b61dc8bce51cf9d5cdb01df1996a Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 16:48:32 -0700 Subject: [PATCH 27/59] Routes --- config/routes.rb | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/routes.rb b/config/routes.rb index 4e63d2033..6ec2a91ea 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -4,4 +4,7 @@ resources :movies, only: [:index, :show, :create] resources :customers, only: [:index, :show] + POST 'rentals/checkout', to: 'rentals#checkout', as: 'checkout' + POST 'rentals/checkin', to: 'rentals#checkin', as: 'checkin' + end From 1e4eb652fdc346967fbcdb522ba79f06b2c38f86 Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 16:48:43 -0700 Subject: [PATCH 28/59] rentals routes modified rental controller --- app/controllers/rentals_controller.rb | 6 +++++- config/routes.rb | 8 +++++--- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index a0fe00c07..e106d5db0 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,5 +1,9 @@ class RentalsController < ApplicationController - def method_name + def checkin + + end + + def checkout end end diff --git a/config/routes.rb b/config/routes.rb index b66d80aaa..ab043efbb 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -2,9 +2,11 @@ # For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html resources :movies, only: [:index, :show, :create] - resources :customers, only: [:index, :show] do - resources :rental, only: [:create], as: 'checkout' - end + resources :customers, only: [:index, :show] + + post '/rentals/check-out', to: 'rentals#checkout', as: 'check-out' + + post '/rentals/check-in', to: 'rentals#checkin', as: 'check-in' From 4dbfa70f75bd2ff3c84062a9f18e0918f204e31f Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 16:57:32 -0700 Subject: [PATCH 29/59] Checkout in progress, params existing --- app/controllers/rentals_controller.rb | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index e106d5db0..22a5e4f8e 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,6 +4,15 @@ def checkin end def checkout + customer = Customer.find(params[:customer_id]) + movie = Movie.find(params[:movie_id]) + Rental.new(customer_id: customer, movie_id: movie) end + + private + def checkout_params + params.permit(:customer_id, :movie_id) + end + end From a4d43e9e94e8e92e7f9d0271e32644b7f5a927ef Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 16:58:04 -0700 Subject: [PATCH 30/59] check_params added --- app/controllers/rentals_controller.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 22a5e4f8e..b43887ee3 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,14 +4,14 @@ def checkin end def checkout - customer = Customer.find(params[:customer_id]) - movie = Movie.find(params[:movie_id]) + customer = Customer.find(check_params[:customer_id]) + movie = Movie.find(check_params[:movie_id]) Rental.new(customer_id: customer, movie_id: movie) end private - def checkout_params + def check_params params.permit(:customer_id, :movie_id) end From f2b70e5bd24e32e8967043b7ee5ee9d0ac54a370 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Tue, 8 May 2018 17:51:50 -0700 Subject: [PATCH 31/59] Added checkout method --- app/controllers/movies_controller.rb | 9 +++---- app/controllers/rentals_controller.rb | 37 ++++++++++++++++++++++++--- app/views/rentals/checkout.rabl | 3 +++ 3 files changed, 40 insertions(+), 9 deletions(-) create mode 100644 app/views/rentals/checkout.rabl diff --git a/app/controllers/movies_controller.rb b/app/controllers/movies_controller.rb index 45a337bde..3b93ae0b5 100644 --- a/app/controllers/movies_controller.rb +++ b/app/controllers/movies_controller.rb @@ -6,11 +6,11 @@ def index end def show - @movie = Movie.find_by(id: params[:id]) + @movie = Movie.find_by(id: movie_params[:id]) if @movie.nil? render json: { errors: { - id: ["No movie with ID #{params[:id]}"] + id: ["No movie with ID #{movie_params[:id]}"] } }, status: :not_found else @@ -22,13 +22,10 @@ def create @movie = Movie.new(movie_params) @movie.save! render :create, status: :ok - # @movie = Movie.new(movie_params) - # @movie.save! - # render json: {id: movie.id}, status: :ok end private def movie_params - params.permit(:title, :overview, :release_date, :inventory) + params.permit(:id, :title, :overview, :release_date, :inventory) end end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index b43887ee3..c99362eb6 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -4,10 +4,41 @@ def checkin end def checkout - customer = Customer.find(check_params[:customer_id]) - movie = Movie.find(check_params[:movie_id]) + customer = Customer.find_by(id: check_params[:customer_id]) + movie = Movie.find_by(id: check_params[:movie_id]) - Rental.new(customer_id: customer, movie_id: movie) + if customer.nil? + render json: { + errors: { + id: ["No such customer with ID #{check_params[:customer_id]}"] + } + }, status: :not_found + elsif movie.nil? + render json: { + errors: { + id: ["No such movie with ID #{check_params[:movie_id]}"] + } + }, status: :not_found + else + customer = customer.id + movie = movie.id + checkout_date = DateTime.now + due_date = checkout_date + 7 + checkin_date = nil + + @rental = Rental.new(customer_id: customer, movie_id: movie, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) + + if @rental.save + + render :checkout, status: :ok + else + render json: { + errors: { + unknown: ["Something went wrong"] + } + }, status: :bad_request + end + end end private diff --git a/app/views/rentals/checkout.rabl b/app/views/rentals/checkout.rabl new file mode 100644 index 000000000..7ae4e9282 --- /dev/null +++ b/app/views/rentals/checkout.rabl @@ -0,0 +1,3 @@ +object @rental + +attributes :customer_id, :movie_id, :checkout_date, :due_date, :checkin_date From 9f08263edbdbd7c8a82bb74a264badbb535fa308 Mon Sep 17 00:00:00 2001 From: Cara Date: Tue, 8 May 2018 20:51:42 -0700 Subject: [PATCH 32/59] Controller for Checkin --- app/controllers/rentals_controller.rb | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index b43887ee3..09589db33 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -10,9 +10,28 @@ def checkout Rental.new(customer_id: customer, movie_id: movie) end - private - def check_params - params.permit(:customer_id, :movie_id) - end + def checkin + + rental = Rental.find(check_params[:rental_id]) + rental.update_attributes checkin_date: Date.now + + if rental.save + Customer.find(rental.customer_id).update_attributes movies_checked_out_count: movies_checked_out_count-1 + + Movie.find(rental.movie_id).update_attributes available_inventory: available_inventory+1 + @movie = rental.movie_id + @movie.increase_inventory + + render json: { id: rental.id }, status: :ok + else + render json: { errors: rental.errors.messages }, status: :bad_request + + end + + private + def check_params + params.permit(:customer_id, :movie_id) + end + end end From d9a48ddfec3ecfbddd18ea09092213f959a2ba0d Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 22:28:32 -0700 Subject: [PATCH 33/59] rental model possible validation and tests --- app/models/rental.rb | 6 ++++++ test/models/rental_test.rb | 12 ++++++++++-- 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/models/rental.rb b/app/models/rental.rb index e001ff37c..dcbd1c5f7 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -1,4 +1,10 @@ class Rental < ApplicationRecord belongs_to :customer belongs_to :movie + validates :due_date, presence: true, if: :checkout_date? + + def checkout_date? + checkout_date == DateTime.now + end + end diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 6ea53d94f..9669205b3 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -3,7 +3,15 @@ describe Rental do let(:rental) { Rental.new } - it "must be valid" do + it "must be valid with all fields" do value(rental).must_be :valid? - end + +end + it "wont be valid if there is no due date once checked out" do + let(:rental) { Rental.new(checkout_date: DateTime.now, due_date: " ") } + value(rental).wont_be :valid? + end + + + end From d4047fd6950660f9e5d6503617874b58c0a97cf0 Mon Sep 17 00:00:00 2001 From: Jamila Date: Tue, 8 May 2018 23:13:05 -0700 Subject: [PATCH 34/59] tried writing rentals model validation some tests for that and logic for movies model --- app/models/movie.rb | 23 +++++++++++++++++++++ test/controllers/rentals_controller_test.rb | 18 +++++++++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 4bfc710bc..c49262b4b 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -2,4 +2,27 @@ class Movie < ApplicationRecord validates :title, presence: true has_many :rentals has_many :customers, through: :rentals + + # possible logic to decrement available_inventory + def self.available_inventory? + unless self.available_inventory == 0 + rented_copies = self.rental.where(id: self.id) + + rented_copies.each do |copy| + if copy.checkout_date && copy.rental.checkin_date == nil + + available_inventory -= 1 + + end + + end + else + + return "We do not have that title available now." + + end + + + end + end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index f0227216c..a5b38a734 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -1,7 +1,19 @@ require "test_helper" describe RentalsController do - # it "must be a real test" do - # flunk "Need real tests" - # end + describe "checkout" do + it "can check out a valid movie" do + + end + + it "can't checkout an invalid movie" do + + end + + it "creates a new rental" do + + end + + end + end From 5f411dc896f9efa7054734bf600307ef82793963 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Wed, 9 May 2018 10:10:07 -0700 Subject: [PATCH 35/59] fix checkout method --- app/controllers/customers_controller.rb | 9 +++++++-- app/controllers/rentals_controller.rb | 17 +++++------------ app/models/movie.rb | 16 ++++++++-------- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb index bf2ecc059..569dff10f 100644 --- a/app/controllers/customers_controller.rb +++ b/app/controllers/customers_controller.rb @@ -6,11 +6,11 @@ def index end def show - @customer = Customer.find_by(id: params[:id]) + @customer = Customer.find_by(id: cust_params[:id]) if @customer.nil? render json: { errors: { - id: ["No customer with ID #{params[:id]}"] + id: ["No customer with ID #{cust_params[:id]}"] } }, status: :not_found else @@ -18,4 +18,9 @@ def show end end + private + def cust_params + params.permit(:id) + end + end diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index cbadbcdc9..6f4c313ce 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,8 +1,4 @@ class RentalsController < ApplicationController - def checkin - - end - def checkout customer = Customer.find_by(id: check_params[:customer_id]) movie = Movie.find_by(id: check_params[:movie_id]) @@ -44,25 +40,22 @@ def checkout def checkin rental = Rental.find(check_params[:rental_id]) - rental.update_attributes checkin_date: Date.now + rental.update_attributes checkin_date: DateTime.now if rental.save Customer.find(rental.customer_id).update_attributes movies_checked_out_count: movies_checked_out_count-1 Movie.find(rental.movie_id).update_attributes available_inventory: available_inventory+1 - @movie = rental.movie_id - @movie.increase_inventory - render json: { id: rental.id }, status: :ok else render json: { errors: rental.errors.messages }, status: :bad_request end + end - private - def check_params - params.permit(:customer_id, :movie_id) - end + private + def check_params + params.permit(:customer_id, :movie_id) end end diff --git a/app/models/movie.rb b/app/models/movie.rb index c49262b4b..c388cb6b7 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -8,17 +8,17 @@ def self.available_inventory? unless self.available_inventory == 0 rented_copies = self.rental.where(id: self.id) - rented_copies.each do |copy| - if copy.checkout_date && copy.rental.checkin_date == nil - - available_inventory -= 1 - - end + rented_copies.each do |copy| + if copy.checkout_date && copy.rental.checkin_date == nil + + available_inventory -= 1 end - else - return "We do not have that title available now." + end + else + + return "We do not have that title available now." end From 9b21c8ccf1fee5b0636e2d032e1594ac14edcee0 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Wed, 9 May 2018 10:40:15 -0700 Subject: [PATCH 36/59] reviewing movie model methods --- app/controllers/rentals_controller.rb | 4 +-- app/models/movie.rb | 30 ++++++++++++--------- test/controllers/rentals_controller_test.rb | 2 +- 3 files changed, 21 insertions(+), 15 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 6f4c313ce..86966e9b9 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -39,7 +39,7 @@ def checkout def checkin - rental = Rental.find(check_params[:rental_id]) + rental = Rental.find_by(id: check_params[:rental_id]) rental.update_attributes checkin_date: DateTime.now if rental.save @@ -56,6 +56,6 @@ def checkin private def check_params - params.permit(:customer_id, :movie_id) + params.permit(:customer_id, :movie_id, :rental_id) end end diff --git a/app/models/movie.rb b/app/models/movie.rb index c388cb6b7..bb8d86a8a 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -4,24 +4,30 @@ class Movie < ApplicationRecord has_many :customers, through: :rentals # possible logic to decrement available_inventory - def self.available_inventory? - unless self.available_inventory == 0 - rented_copies = self.rental.where(id: self.id) - rented_copies.each do |copy| - if copy.checkout_date && copy.rental.checkin_date == nil - - available_inventory -= 1 + # returns true if movie is available for checkout + # returns false if movie isn't available for checkout + def self.available_inventory? + self.available_inventory >= 1 + end - end + def self.reduce_available_inventory + self.available_inventory - 1 + end - end - else + def self.available_inventory? + unless self.available_inventory == 0 + rental_history = self.rentals - return "We do not have that title available now." + rental_history.each do |item| + if item.checkout_date.nil && item.rental.checkin_date == nil + available_inventory -= 1 + end + end - end + end + return "We do not have that title available now." end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index a5b38a734..649b2a18a 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -2,7 +2,7 @@ describe RentalsController do describe "checkout" do - it "can check out a valid movie" do + it "can checkout a valid movie" do end From ce2d4f3fa37036ad1ea3f9d70dbd4bb92a940d25 Mon Sep 17 00:00:00 2001 From: Cara Date: Wed, 9 May 2018 10:42:27 -0700 Subject: [PATCH 37/59] Checkout method --- app/controllers/rentals_controller.rb | 81 ++++++++++++--------------- app/models/movie.rb | 2 +- 2 files changed, 38 insertions(+), 45 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index cbadbcdc9..02c0463dd 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,68 +1,61 @@ class RentalsController < ApplicationController - def checkin - - end - def checkout customer = Customer.find_by(id: check_params[:customer_id]) movie = Movie.find_by(id: check_params[:movie_id]) if customer.nil? - render json: { - errors: { - id: ["No such customer with ID #{check_params[:customer_id]}"] - } - }, status: :not_found + render json: {errors: {id: ["No such customer with ID #{check_params[:customer_id]}"]}}, status: :not_found elsif movie.nil? - render json: { - errors: { - id: ["No such movie with ID #{check_params[:movie_id]}"] - } - }, status: :not_found + render json: { errors: { id: ["No such movie with ID #{check_params[:movie_id]}"]}}, status: :not_found else - customer = customer.id - movie = movie.id - checkout_date = DateTime.now - due_date = checkout_date + 7 - checkin_date = nil + customer_id = customer.id + movie_id = movie.id + checkout_date = DateTime.now + due_date = checkout_date + 7 + checkin_date = nil + + @rental = Rental.new(customer_id: customer_id, movie_id: movie_id, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) + + if @rental.save + if customer.movies_checked_out_count == nil + customer.movies_checked_out_count = 0 + customer.save + end + Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 + + Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 + render :checkout, status: :ok + else + render json: {errors: { unknown: ["Something went wrong"]}}, status: :bad_request + end + end - @rental = Rental.new(customer_id: customer, movie_id: movie, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) + end - if @rental.save - render :checkout, status: :ok - else - render json: { - errors: { - unknown: ["Something went wrong"] - } - }, status: :bad_request - end - end - end + def checkin - def checkin + rental = Rental.find(params[:rental_id]) + rental.update_attributes checkin_date: DateTime.now - rental = Rental.find(check_params[:rental_id]) - rental.update_attributes checkin_date: Date.now + customer = Customer.find_by(rental.customer_id) + movie = Movie.find(rental.movie_id) - if rental.save - Customer.find(rental.customer_id).update_attributes movies_checked_out_count: movies_checked_out_count-1 + if rental.save + Customer.find(rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 - Movie.find(rental.movie_id).update_attributes available_inventory: available_inventory+1 + Movie.find(rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 - @movie = rental.movie_id - @movie.increase_inventory + render json: { id: rental.id }, status: :ok + else + render json: { errors: rental.errors.messages }, status: :bad_request - render json: { id: rental.id }, status: :ok - else - render json: { errors: rental.errors.messages }, status: :bad_request + end end private def check_params - params.permit(:customer_id, :movie_id) + params.permit(:customer_id, :movie_id, :rental_ida) end - end end diff --git a/app/models/movie.rb b/app/models/movie.rb index c49262b4b..c1387de5c 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -12,7 +12,7 @@ def self.available_inventory? if copy.checkout_date && copy.rental.checkin_date == nil available_inventory -= 1 - + end end From fac7dd0029e46d1b1fe27bf462be89f4853008a6 Mon Sep 17 00:00:00 2001 From: Cara Date: Wed, 9 May 2018 10:48:46 -0700 Subject: [PATCH 38/59] adding again --- app/controllers/rentals_controller.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 4d70246ac..ae98e4a9a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -33,6 +33,7 @@ def checkout end + def checkin rental = Rental.find(params[:rental_id]) From 24c40ff66644f3fcb9ea0ab77efa585a81aa562f Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Wed, 9 May 2018 10:50:30 -0700 Subject: [PATCH 39/59] stuff --- app/controllers/rentals_controller.rb | 62 +++++++++++++-------------- app/models/movie.rb | 6 --- 2 files changed, 31 insertions(+), 37 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 4d70246ac..a84761555 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -6,56 +6,56 @@ def checkout if customer.nil? render json: {errors: {id: ["No such customer with ID #{check_params[:customer_id]}"]}}, status: :not_found elsif movie.nil? - render json: { errors: { id: ["No such movie with ID #{check_params[:movie_id]}"]}}, status: :not_found + render json: { errors: { id: ["No such movie with ID #{check_params[:movie_id]}"]}}, status: :not_found else - customer_id = customer.id - movie_id = movie.id - checkout_date = DateTime.now - due_date = checkout_date + 7 - checkin_date = nil + customer_id = customer.id + movie_id = movie.id + checkout_date = DateTime.now + due_date = checkout_date + 7 + checkin_date = nil - @rental = Rental.new(customer_id: customer_id, movie_id: movie_id, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) + @rental = Rental.new(customer_id: customer_id, movie_id: movie_id, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) - if @rental.save - if customer.movies_checked_out_count == nil - customer.movies_checked_out_count = 0 - customer.save - end - Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 + if @rental.save + if customer.movies_checked_out_count == nil + customer.movies_checked_out_count = 0 + customer.save + end + Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 - Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 - render :checkout, status: :ok - else - render json: {errors: { unknown: ["Something went wrong"]}}, status: :bad_request - end + Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 + render :checkout, status: :ok + else + render json: {errors: { unknown: ["Something went wrong"]}}, status: :bad_request + end end end - def checkin + def checkin - rental = Rental.find(params[:rental_id]) - rental.update_attributes checkin_date: DateTime.now + rental = Rental.find(params[:rental_id]) + rental.update_attributes checkin_date: DateTime.now - customer = Customer.find_by(rental.customer_id) - movie = Movie.find(rental.movie_id) + customer = Customer.find_by(rental.customer_id) + movie = Movie.find(rental.movie_id) rental = Rental.find_by(id: check_params[:rental_id]) rental.update_attributes checkin_date: DateTime.now - if rental.save - Customer.find(rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 + if rental.save + Customer.find(rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 - Movie.find(rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 - - render json: { id: rental.id }, status: :ok - else - render json: { errors: rental.errors.messages }, status: :bad_request + Movie.find(rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 - end + render json: { id: rental.id }, status: :ok + else + render json: { errors: rental.errors.messages }, status: :bad_request end + end +end private def check_params diff --git a/app/models/movie.rb b/app/models/movie.rb index 5a97efc3a..bb8d86a8a 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -5,11 +5,6 @@ class Movie < ApplicationRecord # possible logic to decrement available_inventory -<<<<<<< HEAD - available_inventory -= 1 - - end -======= # returns true if movie is available for checkout # returns false if movie isn't available for checkout def self.available_inventory? @@ -19,7 +14,6 @@ def self.available_inventory? def self.reduce_available_inventory self.available_inventory - 1 end ->>>>>>> 9b21c8ccf1fee5b0636e2d032e1594ac14edcee0 def self.available_inventory? unless self.available_inventory == 0 From ece389e9ae48b6e9c96c07133fb8057bd6e578c4 Mon Sep 17 00:00:00 2001 From: Cara Date: Wed, 9 May 2018 11:41:53 -0700 Subject: [PATCH 40/59] pushing to change drivers --- app/controllers/rentals_controller.rb | 86 ++++++++++++++++----------- app/models/movie.rb | 30 ---------- 2 files changed, 51 insertions(+), 65 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index ae98e4a9a..9af0865cb 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -6,58 +6,74 @@ def checkout if customer.nil? render json: {errors: {id: ["No such customer with ID #{check_params[:customer_id]}"]}}, status: :not_found elsif movie.nil? - render json: { errors: { id: ["No such movie with ID #{check_params[:movie_id]}"]}}, status: :not_found + render json: { errors: { id: ["No such movie with ID #{check_params[:movie_id]}"]}}, status: :not_found else - customer_id = customer.id - movie_id = movie.id - checkout_date = DateTime.now - due_date = checkout_date + 7 - checkin_date = nil - - @rental = Rental.new(customer_id: customer_id, movie_id: movie_id, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) - - if @rental.save - if customer.movies_checked_out_count == nil - customer.movies_checked_out_count = 0 - customer.save - end - Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 - - Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 - render :checkout, status: :ok - else - render json: {errors: { unknown: ["Something went wrong"]}}, status: :bad_request - end + customer_id = customer.id + movie_id = movie.id + checkout_date = DateTime.now + due_date = checkout_date + 7 + checkin_date = nil + + @rental = Rental.new(customer_id: customer_id, movie_id: movie_id, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) + + if @rental.save + if customer.movies_checked_out_count == nil + customer.movies_checked_out_count = 0 + customer.save + end + Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 + + Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 + render :checkout, status: :ok + else + render json: {errors: { unknown: ["Something went wrong"]}}, status: :bad_request + end end end - def checkin + def checkin + p "top of checkin" + (Rental.all).each do |rental| + puts rental.id + puts "customerid" + puts rental.customer_id + puts "rental id" + puts rental.movie_id + end + rental = Rental.find_by(id: check_params[:rental_id]) + puts rental.inspect - rental = Rental.find(params[:rental_id]) - rental.update_attributes checkin_date: DateTime.now - customer = Customer.find_by(rental.customer_id) - movie = Movie.find(rental.movie_id) - rental = Rental.find_by(id: check_params[:rental_id]) - rental.update_attributes checkin_date: DateTime.now + rental.checkin_date = DateTime.now + rental.save + p "in checkin" + puts rental.inspect - if rental.save - Customer.find(rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 + movie_id = rental.movie_id + customer_id = rental.customer_id - Movie.find(rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 + customer = Customer.find_by(id: customer_id) + movie = Movie.find_by(id: movie_id) - render json: { id: rental.id }, status: :ok - else - render json: { errors: rental.errors.messages }, status: :bad_request + if rental.save + p "in rental.save" + puts rental.inspect + Customer.find(rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 - end + Movie.find(rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 + + render json: { id: rental.id }, status: :ok + else + render json: { errors: rental.errors.messages }, status: :bad_request end + end + private def check_params params.permit(:customer_id, :movie_id, :rental_id) diff --git a/app/models/movie.rb b/app/models/movie.rb index 5a97efc3a..c2b07e9e2 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -5,36 +5,6 @@ class Movie < ApplicationRecord # possible logic to decrement available_inventory -<<<<<<< HEAD - available_inventory -= 1 - - end -======= - # returns true if movie is available for checkout - # returns false if movie isn't available for checkout - def self.available_inventory? - self.available_inventory >= 1 - end - def self.reduce_available_inventory - self.available_inventory - 1 - end ->>>>>>> 9b21c8ccf1fee5b0636e2d032e1594ac14edcee0 - - def self.available_inventory? - unless self.available_inventory == 0 - rental_history = self.rentals - - rental_history.each do |item| - if item.checkout_date.nil && item.rental.checkin_date == nil - available_inventory -= 1 - end - end - - end - - return "We do not have that title available now." - - end end From af203649b15bbf810ea6eaffef304ca9d04235fa Mon Sep 17 00:00:00 2001 From: Jamila Date: Wed, 9 May 2018 11:43:05 -0700 Subject: [PATCH 41/59] trying to pull --- app/models/movie.rb | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/app/models/movie.rb b/app/models/movie.rb index 5a97efc3a..50c749dd5 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -5,11 +5,6 @@ class Movie < ApplicationRecord # possible logic to decrement available_inventory -<<<<<<< HEAD - available_inventory -= 1 - - end -======= # returns true if movie is available for checkout # returns false if movie isn't available for checkout def self.available_inventory? @@ -19,7 +14,7 @@ def self.available_inventory? def self.reduce_available_inventory self.available_inventory - 1 end ->>>>>>> 9b21c8ccf1fee5b0636e2d032e1594ac14edcee0 + def self.available_inventory? unless self.available_inventory == 0 From 89b8d400bbbb5b1ad2aa06ddb915c2e5da2474a8 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Wed, 9 May 2018 12:05:27 -0700 Subject: [PATCH 42/59] checkin working --- app/controllers/rentals_controller.rb | 41 ++++++++------------------- app/models/movie.rb | 32 --------------------- app/views/rentals/checkin.rabl | 3 ++ 3 files changed, 15 insertions(+), 61 deletions(-) create mode 100644 app/views/rentals/checkin.rabl diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 49b2d7fac..3f10b0ce4 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -33,49 +33,32 @@ def checkout end def checkin - p "top of checkin" - (Rental.all).each do |rental| - puts rental.id - puts "customerid" - puts rental.customer_id - puts "rental id" - puts rental.movie_id - end - rental = Rental.find_by(id: check_params[:rental_id]) - puts rental.inspect - + @rental = Rental.find_by(customer_id: check_params[:customer_id], movie_id: check_params[:movie_id] ) - rental.checkin_date = DateTime.now - rental.save - p "in checkin" - puts rental.inspect + @rental.checkin_date = DateTime.now + @rental.save - movie_id = rental.movie_id - customer_id = rental.customer_id + movie_id = @rental.movie_id + customer_id = @rental.customer_id customer = Customer.find_by(id: customer_id) movie = Movie.find_by(id: movie_id) - if rental.save - p "in rental.save" - puts rental.inspect - Customer.find(rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 + if @rental.save - Movie.find(rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 + Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 - render json: { id: rental.id }, status: :ok - else - render json: { errors: rental.errors.messages }, status: :bad_request + Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 - render json: { id: rental.id }, status: :ok + render :checkin, status: :ok else - render json: { errors: rental.errors.messages }, status: :bad_request + render json: { errors: @rental.errors.messages }, status: :bad_request end - + end private def check_params - params.permit(:customer_id, :movie_id, :rental_id) + params.permit(:customer_id, :movie_id) end end diff --git a/app/models/movie.rb b/app/models/movie.rb index dc9a94619..07b486217 100644 --- a/app/models/movie.rb +++ b/app/models/movie.rb @@ -3,36 +3,4 @@ class Movie < ApplicationRecord has_many :rentals has_many :customers, through: :rentals - # possible logic to decrement available_inventory - -<<<<<<< HEAD - # returns true if movie is available for checkout - # returns false if movie isn't available for checkout - def self.available_inventory? - self.available_inventory >= 1 - end - - def self.reduce_available_inventory - self.available_inventory - 1 - end - - def self.available_inventory? - unless self.available_inventory == 0 - rental_history = self.rentals - - rental_history.each do |item| - if item.checkout_date.nil && item.rental.checkin_date == nil - available_inventory -= 1 - end - end - - end - - return "We do not have that title available now." - - end -======= - ->>>>>>> ece389e9ae48b6e9c96c07133fb8057bd6e578c4 - end diff --git a/app/views/rentals/checkin.rabl b/app/views/rentals/checkin.rabl new file mode 100644 index 000000000..7ae4e9282 --- /dev/null +++ b/app/views/rentals/checkin.rabl @@ -0,0 +1,3 @@ +object @rental + +attributes :customer_id, :movie_id, :checkout_date, :due_date, :checkin_date From 20a098b10c398e8af84319b807009646d8d97285 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Wed, 9 May 2018 12:06:54 -0700 Subject: [PATCH 43/59] consolidated check view --- app/controllers/rentals_controller.rb | 4 ++-- app/views/rentals/{checkin.rabl => check.rabl} | 0 app/views/rentals/checkout.rabl | 3 --- 3 files changed, 2 insertions(+), 5 deletions(-) rename app/views/rentals/{checkin.rabl => check.rabl} (100%) delete mode 100644 app/views/rentals/checkout.rabl diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 3f10b0ce4..b502fa0bd 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -24,7 +24,7 @@ def checkout Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 - render :checkout, status: :ok + render :check, status: :ok else render json: {errors: { unknown: ["Something went wrong"]}}, status: :bad_request end @@ -50,7 +50,7 @@ def checkin Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 - render :checkin, status: :ok + render :check, status: :ok else render json: { errors: @rental.errors.messages }, status: :bad_request end diff --git a/app/views/rentals/checkin.rabl b/app/views/rentals/check.rabl similarity index 100% rename from app/views/rentals/checkin.rabl rename to app/views/rentals/check.rabl diff --git a/app/views/rentals/checkout.rabl b/app/views/rentals/checkout.rabl deleted file mode 100644 index 7ae4e9282..000000000 --- a/app/views/rentals/checkout.rabl +++ /dev/null @@ -1,3 +0,0 @@ -object @rental - -attributes :customer_id, :movie_id, :checkout_date, :due_date, :checkin_date From b481f07f4143273cdd87fcaafb82244d592fe5e5 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Wed, 9 May 2018 12:15:54 -0700 Subject: [PATCH 44/59] minor edits to checkin/checkout --- app/controllers/rentals_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index b502fa0bd..4c792d17c 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -46,7 +46,7 @@ def checkin if @rental.save - Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 + Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 From f0c50876ca1792c05e6d4b3aa99deb0b4bfd668e Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Wed, 9 May 2018 15:25:48 -0700 Subject: [PATCH 45/59] Tests passing --- app/models/rental.rb | 6 ------ test/fixtures/movies.yml | 2 +- test/models/rental_test.rb | 12 ------------ 3 files changed, 1 insertion(+), 19 deletions(-) diff --git a/app/models/rental.rb b/app/models/rental.rb index dcbd1c5f7..e001ff37c 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -1,10 +1,4 @@ class Rental < ApplicationRecord belongs_to :customer belongs_to :movie - validates :due_date, presence: true, if: :checkout_date? - - def checkout_date? - checkout_date == DateTime.now - end - end diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index dc09a0d9d..825b0db14 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -5,7 +5,7 @@ # below each fixture, per the syntax in the comments below # one: { - title: "A wonderful movie" + title: "Little Mermaid" } # column: value # diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 9669205b3..0bea59f1c 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -1,17 +1,5 @@ require "test_helper" describe Rental do - let(:rental) { Rental.new } - - it "must be valid with all fields" do - value(rental).must_be :valid? - -end - it "wont be valid if there is no due date once checked out" do - let(:rental) { Rental.new(checkout_date: DateTime.now, due_date: " ") } - value(rental).wont_be :valid? - end - - end From a7b4b03cb7278b22307a4e7a46392facf390b2a4 Mon Sep 17 00:00:00 2001 From: Jamila Date: Thu, 10 May 2018 09:56:48 -0700 Subject: [PATCH 46/59] working on rental controller tests for check_in method --- app/models/rental.rb | 4 ++-- test/controllers/rentals_controller_test.rb | 22 +++++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/app/models/rental.rb b/app/models/rental.rb index dcbd1c5f7..a58e3b1ca 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -2,9 +2,9 @@ class Rental < ApplicationRecord belongs_to :customer belongs_to :movie validates :due_date, presence: true, if: :checkout_date? - + def checkout_date? - checkout_date == DateTime.now + !checkout_date.nil? end end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 649b2a18a..139de3262 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -16,4 +16,26 @@ end + describe "checkin" do + it "responds with success" do + post check_in_url + must_respond_with :success + end + + it "returns json" do + post check_in_url + response.header['Content-Type'].must_include 'json' + end + + it "update checkin field on valid rental" do + + end + + it "does not checkin an invalid rental" do + + end + end + + + end From 5a386a43b602aba03f51dfb3ae36f027a35647a7 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Thu, 10 May 2018 13:54:47 -0700 Subject: [PATCH 47/59] added tests to rental_test.rb --- test/models/rental_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/models/rental_test.rb b/test/models/rental_test.rb index 0bea59f1c..afbd13ea8 100644 --- a/test/models/rental_test.rb +++ b/test/models/rental_test.rb @@ -2,4 +2,16 @@ describe Rental do + describe "relations" do + it "must respond to customer" do + rental = Rental.first + rental.must_respond_to :customer + end + + it "must repond to movie" do + rental = Rental.first + rental.must_respond_to :movie + end + end + end From 3c84c701924fea99b500f726798bed3d59407c4a Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Thu, 10 May 2018 15:23:39 -0700 Subject: [PATCH 48/59] Added tests to rentals_controller --- app/controllers/rentals_controller.rb | 4 +- test/controllers/rentals_controller_test.rb | 65 ++++++++++++++++++++- 2 files changed, 65 insertions(+), 4 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 4c792d17c..47f149f83 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -21,10 +21,12 @@ def checkout customer.movies_checked_out_count = 0 customer.save end - Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 + + Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 render :check, status: :ok + else render json: {errors: { unknown: ["Something went wrong"]}}, status: :bad_request end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 649b2a18a..aa7a9d699 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -2,16 +2,75 @@ describe RentalsController do describe "checkout" do - it "can checkout a valid movie" do + it "can post to the checkout path w/ valid movie" do + customer_id = Customer.first.id + movie_id = Movie.first.id + pre_count = Rental.all.count + + post check_out_path(customer_id:customer_id, movie_id: movie_id) + must_respond_with :ok + + post_count = Rental.all.count + post_count.must_equal pre_count + 1 end - it "can't checkout an invalid movie" do + it "can't post to the checkout path w/ invalid customer" do + customer_id = Customer.last.id + 1 + movie_id = Movie.first.id + + pre_count = Rental.all.count + + post check_out_path(customer_id:customer_id, movie_id: movie_id) + must_respond_with :not_found + + post_count = Rental.all.count + post_count.must_equal pre_count + body = JSON.parse(response.body) + body.must_include "errors" + body["errors"]["id"].must_equal ["No such customer with ID #{customer_id}"] end - it "creates a new rental" do + it "can't post to the checkout path w/ invalid movie" do + customer_id = Customer.first.id + movie_id = Movie.last.id + 1 + + pre_count = Rental.all.count + + post check_out_path(customer_id:customer_id, movie_id: movie_id) + must_respond_with :not_found + + post_count = Rental.all.count + post_count.must_equal pre_count + + body = JSON.parse(response.body) + body.must_include "errors" + body["errors"]["id"].must_equal ["No such movie with ID #{movie_id}"] + end + + it "movies_checked_out_count increases when checkout is ran" do + customer = Customer.first + movie = Movie.first + + post check_out_path(customer_id: customer.id, movie_id: movie.id) + + amount = customer.movies_checked_out_count + + amount.must_equal 1 + end + + it "available_inventory decreases when checkout is ran" do + customer = Customer.first + movie = Movie.first + + pre_inventory = movie.available_inventory + + post check_out_path(customer_id: customer.id, movie_id: movie.id) + + post_inventory = movie.available_inventory + post_inventory.must_equal pre_inventory - 1 end end From 76da64d9bbd050af8b30c6cf4ba8dd5c48cccae1 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Thu, 10 May 2018 15:31:35 -0700 Subject: [PATCH 49/59] Rentals controller tests --- test/controllers/movies_controller_test.rb | 3 ++- test/fixtures/customers.yml | 4 +--- test/fixtures/movies.yml | 10 +++++----- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/test/controllers/movies_controller_test.rb b/test/controllers/movies_controller_test.rb index 4bd9b3a41..83e78a242 100644 --- a/test/controllers/movies_controller_test.rb +++ b/test/controllers/movies_controller_test.rb @@ -29,7 +29,8 @@ title: "Hook", overview: "Robin Williams duh", release_date: 1997, - inventory: 5 + inventory: 5, + available_inventory: 10 } it "Creates a new movie" do diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index f8c6f4c7a..a9710582d 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -7,9 +7,7 @@ one: { name: "Pinky" } -# column: value -# + two: { name: "Brain" } -# column: value diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index 825b0db14..b5503779c 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -5,11 +5,11 @@ # below each fixture, per the syntax in the comments below # one: { - title: "Little Mermaid" + title: "Little Mermaid", + overview: "Kids movie about a mermaid" } -# column: value -# + two: { - title: "Lion King" + title: "Lion King", + overview: "Kids movie about a lion" } -# column: value From 05d94113efa790abb5319886590a9fcb49209e3d Mon Sep 17 00:00:00 2001 From: Jamila Date: Thu, 10 May 2018 21:51:44 -0700 Subject: [PATCH 50/59] rental controller checkin method tests --- test/controllers/rentals_controller_test.rb | 49 ++++++++++++++++++++- test/fixtures/customers.yml | 4 ++ test/fixtures/movies.yml | 4 ++ test/fixtures/rentals.yml | 19 +++++--- test/models/movie_test.rb | 6 ++- 5 files changed, 74 insertions(+), 8 deletions(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 3b8697930..c71c378bb 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require "Date" describe RentalsController do describe "checkout" do @@ -17,23 +18,67 @@ end describe "checkin" do + + customer_id = Customer.first.id + movie_id = Movie.first.id + + url_data = { + customer_id: customer_id, + movie_id: movie_id + } + it "responds with success" do - post check_in_url + post check_out_url, params: url_data + post check_in_url, params: url_data must_respond_with :success end it "returns json" do - post check_in_url + post check_out_url, params: url_data + post check_in_url, params: url_data response.header['Content-Type'].must_include 'json' end it "update checkin field on valid rental" do + post check_out_url, params: url_data + post check_in_url, params: url_data + + rental = Rental.find_by(customer_id: url_data[:customer_id], movie_id: url_data[:movie_id]) + rental.checkin_date.must_equal DateTime.now + + + end it "does not checkin an invalid rental" do + bad_data = url_data.delete(:movie_id) + + post check_out_url, params: url_data + post check_in_url, params: bad_data + must_respond_with :bad_request + + + end + + it "updates movies_checked_out_count field for customer and available_inventory for movie associated with rental" do + + post check_out_url, params: url_data + customer = Customer.find_by(id: url_data[:customer_id]) + movie = Movie.find_by(id: url_data[:movie_id]) + after_checkout_cust = customer.movies_checked_out_count + after_checkout_movie = movie.available_inventory + + post check_in_url, params: url_data + + customer = Customer.find_by(id: url_data[:customer_id]) + movie = Movie.find_by(id: url_data[:movie_id]) + + customer.movies_checked_out_count.must_equal after_checkout_cust - 1 + movie.available_inventory.must_equal after_checkout_movie + 1 + end end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index f8c6f4c7a..22c764f14 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -13,3 +13,7 @@ two: { name: "Brain" } # column: value +# +three: { + name: "Minerva" + } diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index 825b0db14..e5065411b 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -12,4 +12,8 @@ one: { two: { title: "Lion King" } + +three: { + title: "The shining" + } # column: value diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml index dc3ee79b5..04e2e6c5a 100644 --- a/test/fixtures/rentals.yml +++ b/test/fixtures/rentals.yml @@ -4,8 +4,17 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: {} -# column: value -# -two: {} -# column: value +one: + customer_id: 1 + movie_id: 1 + + +two: + customer_id: 2 + movie_id: 2 + + +three: + customer_id: 3 + movie_id: 3 + diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index 73d040d0a..b1042d2c2 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -15,5 +15,9 @@ movie.wont_be :valid? end - # Maybe raise an error if inventory = 0? + # it "can get a customer from movie through rentals" do + # movie = Movie.first + # + # + # end end From 72f43eec63fcf55b36e551f0d1ccd7e1938ae719 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Fri, 11 May 2018 11:18:11 -0700 Subject: [PATCH 51/59] Checkout tests passing --- app/controllers/rentals_controller.rb | 4 ++-- app/models/rental.rb | 1 + test/controllers/rentals_controller_test.rb | 2 ++ 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 47f149f83..617c6658a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -22,9 +22,9 @@ def checkout customer.save end - Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count+1 + customer.update! movies_checked_out_count: customer.movies_checked_out_count+1 - Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory-1 + movie.update! available_inventory: movie.available_inventory-1 render :check, status: :ok else diff --git a/app/models/rental.rb b/app/models/rental.rb index e001ff37c..522033080 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -1,4 +1,5 @@ class Rental < ApplicationRecord belongs_to :customer belongs_to :movie + end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index aa7a9d699..cdf782ef0 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -55,6 +55,7 @@ post check_out_path(customer_id: customer.id, movie_id: movie.id) + customer.reload amount = customer.movies_checked_out_count amount.must_equal 1 @@ -68,6 +69,7 @@ post check_out_path(customer_id: customer.id, movie_id: movie.id) + movie.reload post_inventory = movie.available_inventory post_inventory.must_equal pre_inventory - 1 From 488c4383a238a4adc9b15dccbc03788c5a082749 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Fri, 11 May 2018 12:14:19 -0700 Subject: [PATCH 52/59] Add rental.nil? to checkin method --- app/controllers/rentals_controller.rb | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 617c6658a..0f2fed44a 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -37,6 +37,10 @@ def checkout def checkin @rental = Rental.find_by(customer_id: check_params[:customer_id], movie_id: check_params[:movie_id] ) + if rental.nil? + render json: {errors: {id: ["No such rental with customer ID #{check_params[:customer_id]} and movie ID #{check_params[:movie_id]}"]}}, status: :not_found + end + @rental.checkin_date = DateTime.now @rental.save From 00e24ac7b594708f0e42c761d6e934ce08e6eba7 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Fri, 11 May 2018 12:15:01 -0700 Subject: [PATCH 53/59] Fixed missing @ --- app/controllers/rentals_controller.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 0f2fed44a..9f60383a9 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -37,7 +37,7 @@ def checkout def checkin @rental = Rental.find_by(customer_id: check_params[:customer_id], movie_id: check_params[:movie_id] ) - if rental.nil? + if @rental.nil? render json: {errors: {id: ["No such rental with customer ID #{check_params[:customer_id]} and movie ID #{check_params[:movie_id]}"]}}, status: :not_found end From 0bb1595e01b1697e20764ed8a7862c3e71322a4f Mon Sep 17 00:00:00 2001 From: Jamila Date: Fri, 11 May 2018 12:15:39 -0700 Subject: [PATCH 54/59] fixing tests --- test/controllers/rentals_controller_test.rb | 9 +++------ test/fixtures/customers.yml | 12 +++++++---- test/fixtures/movies.yml | 22 ++++++++++++--------- test/fixtures/rentals.yml | 1 - 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 81864fac2..0e11e26c0 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -106,14 +106,11 @@ rental = Rental.find_by(customer_id: url_data[:customer_id], movie_id: url_data[:movie_id]) rental.checkin_date.must_equal DateTime.now - - - end it "does not checkin an invalid rental" do - customer_id = Customer.first.id - movie_id = Movie.first.id + 1 + url_data[:movie_id].delete + bad_data = { customer_id: customer_id, @@ -123,7 +120,7 @@ post check_out_url, params: url_data post check_in_url, params: bad_data - must_respond_with :not_found + must_respond_with :bad_request end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index a9710582d..596baeab8 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -4,10 +4,14 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: { +one: name: "Pinky" - } -two: { + +two: name: "Brain" - } + + +three: + name: "Minerva" + diff --git a/test/fixtures/movies.yml b/test/fixtures/movies.yml index b5503779c..51b4095a3 100644 --- a/test/fixtures/movies.yml +++ b/test/fixtures/movies.yml @@ -4,12 +4,16 @@ # model remove the "{}" from the fixture names and add the columns immediately # below each fixture, per the syntax in the comments below # -one: { - title: "Little Mermaid", - overview: "Kids movie about a mermaid" - } - -two: { - title: "Lion King", - overview: "Kids movie about a lion" - } +one: + title: Little Mermaid + overview: Kids movie about a mermaid + + +two: + title: Lion King + overview: Kids movie about a lion + + +three: + title: The shining + overview: Scary ghost movie diff --git a/test/fixtures/rentals.yml b/test/fixtures/rentals.yml index 04e2e6c5a..93e4a1f2e 100644 --- a/test/fixtures/rentals.yml +++ b/test/fixtures/rentals.yml @@ -17,4 +17,3 @@ two: three: customer_id: 3 movie_id: 3 - From f102b2c6157a2c2f32aa0a481c490acda32ea2d3 Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Fri, 11 May 2018 13:50:48 -0700 Subject: [PATCH 55/59] Added can't checkout when no available copies --- app/controllers/rentals_controller.rb | 4 ++++ test/controllers/rentals_controller_test.rb | 18 ++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 9f60383a9..95bbdd8ad 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,3 +1,5 @@ +require 'pry' + class RentalsController < ApplicationController def checkout customer = Customer.find_by(id: check_params[:customer_id]) @@ -7,6 +9,8 @@ def checkout render json: {errors: {id: ["No such customer with ID #{check_params[:customer_id]}"]}}, status: :not_found elsif movie.nil? render json: { errors: { id: ["No such movie with ID #{check_params[:movie_id]}"]}}, status: :not_found + elsif movie.available_inventory < 1 + render json: { errors: { id: ["No copies of the movie with ID #{check_params[:movie_id]} are available"]}}, status: :not_found else customer_id = customer.id movie_id = movie.id diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index cdf782ef0..74b637948 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -75,6 +75,24 @@ post_inventory.must_equal pre_inventory - 1 end + it "can't checkout a movie when there are no available copies" do + customer = Customer.first + movie = Movie.first + + 10.times do + post check_out_path(customer_id: customer.id, movie_id: movie.id) + end + + movie.reload + movie.available_inventory.must_equal 0 + + post check_out_path(customer_id: customer.id, movie_id: movie.id) + + body = JSON.parse(response.body) + body.must_include "errors" + body["errors"]["id"].must_equal ["No copies of the movie with ID #{movie.id} are available"] + end + end end From d6ad444db4dafc3f2f5970715e85f294d781cb82 Mon Sep 17 00:00:00 2001 From: Jamila Date: Fri, 11 May 2018 14:33:01 -0700 Subject: [PATCH 56/59] rental controller tests passing --- app/controllers/rentals_controller.rb | 6 +++- ...210626_delete_column_from_rentals_table.rb | 5 +++ .../20180511210821_add_column_to_rentals.rb | 5 +++ db/schema.rb | 4 +-- test/controllers/rentals_controller_test.rb | 31 ++++++++----------- test/fixtures/customers.yml | 1 - 6 files changed, 30 insertions(+), 22 deletions(-) create mode 100644 db/migrate/20180511210626_delete_column_from_rentals_table.rb create mode 100644 db/migrate/20180511210821_add_column_to_rentals.rb diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 9f60383a9..e88437100 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -1,3 +1,4 @@ +# require 'pry' class RentalsController < ApplicationController def checkout customer = Customer.find_by(id: check_params[:customer_id]) @@ -37,11 +38,14 @@ def checkout def checkin @rental = Rental.find_by(customer_id: check_params[:customer_id], movie_id: check_params[:movie_id] ) + if @rental.nil? + render json: {errors: {id: ["No such rental with customer ID #{check_params[:customer_id]} and movie ID #{check_params[:movie_id]}"]}}, status: :not_found + return end - @rental.checkin_date = DateTime.now + @rental.checkin_date = Date.today @rental.save movie_id = @rental.movie_id diff --git a/db/migrate/20180511210626_delete_column_from_rentals_table.rb b/db/migrate/20180511210626_delete_column_from_rentals_table.rb new file mode 100644 index 000000000..3bd28d453 --- /dev/null +++ b/db/migrate/20180511210626_delete_column_from_rentals_table.rb @@ -0,0 +1,5 @@ +class DeleteColumnFromRentalsTable < ActiveRecord::Migration[5.1] + def change + remove_column :rentals, :checkin_date, :datetime + end +end diff --git a/db/migrate/20180511210821_add_column_to_rentals.rb b/db/migrate/20180511210821_add_column_to_rentals.rb new file mode 100644 index 000000000..a4db2d471 --- /dev/null +++ b/db/migrate/20180511210821_add_column_to_rentals.rb @@ -0,0 +1,5 @@ +class AddColumnToRentals < ActiveRecord::Migration[5.1] + def change + add_column :rentals, :checkin_date, :date + end +end diff --git a/db/schema.rb b/db/schema.rb index 6e14125c5..3d45ee011 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20180508224222) do +ActiveRecord::Schema.define(version: 20180511210821) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -41,11 +41,11 @@ create_table "rentals", force: :cascade do |t| t.datetime "due_date" t.datetime "checkout_date" - t.datetime "checkin_date" t.datetime "created_at", null: false t.datetime "updated_at", null: false t.bigint "customer_id" t.bigint "movie_id" + t.date "checkin_date" t.index ["customer_id"], name: "index_rentals_on_customer_id" t.index ["movie_id"], name: "index_rentals_on_movie_id" end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index dbcde1a0b..39488bbab 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -1,5 +1,6 @@ require "test_helper" require "Date" +require "pry" describe RentalsController do describe "checkout" do @@ -55,7 +56,6 @@ movie = Movie.first post check_out_path(customer_id: customer.id, movie_id: movie.id) - customer.reload amount = customer.movies_checked_out_count @@ -69,8 +69,8 @@ pre_inventory = movie.available_inventory post check_out_path(customer_id: customer.id, movie_id: movie.id) - movie.reload + post_inventory = movie.available_inventory post_inventory.must_equal pre_inventory - 1 @@ -80,13 +80,13 @@ describe "checkin" do - customer_id = Customer.first.id - movie_id = Movie.first.id + customer_id = Customer.first.id + movie_id = Movie.first.id - url_data = { - customer_id: customer_id, - movie_id: movie_id - } + url_data = { + customer_id: customer_id, + movie_id: movie_id + } it "responds with success" do @@ -107,23 +107,18 @@ post check_in_url, params: url_data rental = Rental.find_by(customer_id: url_data[:customer_id], movie_id: url_data[:movie_id]) - rental.checkin_date.must_equal DateTime.now + rental.checkin_date.must_equal Date.today + end it "does not checkin an invalid rental" do - url_data[:movie_id].delete - - - bad_data = { - customer_id: customer_id, - movie_id: movie_id - } + bad_data = url_data.clone + bad_data.delete(:movie_id) post check_out_url, params: url_data post check_in_url, params: bad_data - must_respond_with :bad_request - + must_respond_with :not_found end diff --git a/test/fixtures/customers.yml b/test/fixtures/customers.yml index 596baeab8..200e2c926 100644 --- a/test/fixtures/customers.yml +++ b/test/fixtures/customers.yml @@ -14,4 +14,3 @@ two: three: name: "Minerva" - From 5d84f984558678a2ab1324c9ecadf121a8a0428d Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Fri, 11 May 2018 14:47:20 -0700 Subject: [PATCH 57/59] Moved some logic to the controller --- app/controllers/rentals_controller.rb | 60 ++++++++++----------- app/models/rental.rb | 43 +++++++++++++++ test/controllers/rentals_controller_test.rb | 6 +-- 3 files changed, 74 insertions(+), 35 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 95bbdd8ad..0b1501dac 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -2,33 +2,23 @@ class RentalsController < ApplicationController def checkout - customer = Customer.find_by(id: check_params[:customer_id]) - movie = Movie.find_by(id: check_params[:movie_id]) - - if customer.nil? - render json: {errors: {id: ["No such customer with ID #{check_params[:customer_id]}"]}}, status: :not_found - elsif movie.nil? - render json: { errors: { id: ["No such movie with ID #{check_params[:movie_id]}"]}}, status: :not_found - elsif movie.available_inventory < 1 - render json: { errors: { id: ["No copies of the movie with ID #{check_params[:movie_id]} are available"]}}, status: :not_found + customer_id = check_params[:customer_id] + movie_id = check_params[:movie_id] + + find_customer_and_movie(customer_id, movie_id) + + errors = Rental.check_dependencies(@customer, @movie) + + if errors + render json: {errors: errors}, status: :not_found else - customer_id = customer.id - movie_id = movie.id - checkout_date = DateTime.now - due_date = checkout_date + 7 - checkin_date = nil - @rental = Rental.new(customer_id: customer_id, movie_id: movie_id, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) + @rental = Rental.create_checkout(@customer, @movie) if @rental.save - if customer.movies_checked_out_count == nil - customer.movies_checked_out_count = 0 - customer.save - end - customer.update! movies_checked_out_count: customer.movies_checked_out_count+1 + Rental.process_checkout(@customer, @movie) - movie.update! available_inventory: movie.available_inventory-1 render :check, status: :ok else @@ -39,26 +29,23 @@ def checkout end def checkin - @rental = Rental.find_by(customer_id: check_params[:customer_id], movie_id: check_params[:movie_id] ) + + customer_id = check_params[:customer_id] + movie_id = check_params[:movie_id] + + find_customer_and_movie(customer_id, movie_id) + + @rental = find_rental(customer_id, movie_id) if @rental.nil? render json: {errors: {id: ["No such rental with customer ID #{check_params[:customer_id]} and movie ID #{check_params[:movie_id]}"]}}, status: :not_found end @rental.checkin_date = DateTime.now - @rental.save - - movie_id = @rental.movie_id - customer_id = @rental.customer_id - - customer = Customer.find_by(id: customer_id) - movie = Movie.find_by(id: movie_id) if @rental.save - Customer.find(@rental.customer_id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 - - Movie.find(@rental.movie_id).update_attributes available_inventory: movie.available_inventory+1 + Rental.process_checkin(customer, movie) render :check, status: :ok else @@ -71,4 +58,13 @@ def checkin def check_params params.permit(:customer_id, :movie_id) end + + def find_customer_and_movie(customer_id, movie_id) + @customer = Customer.find_by(id: customer_id) + @movie = Movie.find_by(id: movie_id) + end + + def find_rental(customer_id, movie_id) + @rental = Rental.find_by(customer_id: customer_id, movie_id: movie_id) + end end diff --git a/app/models/rental.rb b/app/models/rental.rb index 522033080..0122304cf 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -2,4 +2,47 @@ class Rental < ApplicationRecord belongs_to :customer belongs_to :movie + def self.check_dependencies(customer, movie) + if customer.nil? + return { + customer_id: ["No such customer"] + } + elsif movie.nil? + return { + movie_id: ["No such movie"] + } + elsif movie.available_inventory < 1 + return { + inventory: ["No copies of that movie are available"] + } + end + return nil + end + + def self.create_checkout(customer, movie) + checkout_date = DateTime.now + due_date = checkout_date + 7 + checkin_date = nil + + rental = Rental.new(customer_id: customer.id, movie_id: movie.id, checkout_date: checkout_date, due_date: due_date, checkin_date: checkin_date) + + return rental + end + + def self.process_checkout(customer, movie) + if customer.movies_checked_out_count == nil + customer.movies_checked_out_count = 0 + customer.save + end + + customer.update! movies_checked_out_count: customer.movies_checked_out_count+1 + + movie.update! available_inventory: movie.available_inventory-1 + end + + def self.process_checkin(customer, movie) + Customer.find(@rental.customer.id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 + + Movie.find(@rental.movie.id).update_attributes available_inventory: movie.available_inventory+1 + end end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 74b637948..9f060feea 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -29,7 +29,7 @@ body = JSON.parse(response.body) body.must_include "errors" - body["errors"]["id"].must_equal ["No such customer with ID #{customer_id}"] + body["errors"]["customer_id"].must_equal ["No such customer"] end it "can't post to the checkout path w/ invalid movie" do @@ -46,7 +46,7 @@ body = JSON.parse(response.body) body.must_include "errors" - body["errors"]["id"].must_equal ["No such movie with ID #{movie_id}"] + body["errors"]["movie_id"].must_equal ["No such movie"] end it "movies_checked_out_count increases when checkout is ran" do @@ -90,7 +90,7 @@ body = JSON.parse(response.body) body.must_include "errors" - body["errors"]["id"].must_equal ["No copies of the movie with ID #{movie.id} are available"] + body["errors"]["inventory"].must_equal ["No copies of that movie are available"] end end From a93e03ee5c98348fe8648d0f17c8f12110b7cb8c Mon Sep 17 00:00:00 2001 From: Brandy Austin Date: Fri, 11 May 2018 15:04:31 -0700 Subject: [PATCH 58/59] Put process_checkin in rental model --- app/controllers/rentals_controller.rb | 7 ++----- app/models/rental.rb | 5 +++-- test/controllers/rentals_controller_test.rb | 12 ++++++------ 3 files changed, 11 insertions(+), 13 deletions(-) diff --git a/app/controllers/rentals_controller.rb b/app/controllers/rentals_controller.rb index 236c157be..9a726c59d 100644 --- a/app/controllers/rentals_controller.rb +++ b/app/controllers/rentals_controller.rb @@ -46,11 +46,8 @@ def checkin @rental.checkin_date = Date.today @rental.save - movie_id = @rental.movie_id - customer_id = @rental.customer_id - - customer = Customer.find_by(id: customer_id) - movie = Movie.find_by(id: movie_id) + customer = Customer.find_by(id: @rental.customer_id) + movie = Movie.find_by(id: @rental.movie_id) if @rental.save diff --git a/app/models/rental.rb b/app/models/rental.rb index 0122304cf..1119f36b1 100644 --- a/app/models/rental.rb +++ b/app/models/rental.rb @@ -41,8 +41,9 @@ def self.process_checkout(customer, movie) end def self.process_checkin(customer, movie) - Customer.find(@rental.customer.id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 + Customer.find(customer.id).update_attributes movies_checked_out_count: customer.movies_checked_out_count-1 - Movie.find(@rental.movie.id).update_attributes available_inventory: movie.available_inventory+1 + Movie.find(movie.id).update_attributes available_inventory: movie.available_inventory+1 end + end diff --git a/test/controllers/rentals_controller_test.rb b/test/controllers/rentals_controller_test.rb index 10facbc80..319746064 100644 --- a/test/controllers/rentals_controller_test.rb +++ b/test/controllers/rentals_controller_test.rb @@ -98,13 +98,13 @@ describe "checkin" do - customer_id = Customer.first.id - movie_id = Movie.first.id + customer_id = Customer.first.id + movie_id = Movie.first.id - url_data = { - customer_id: customer_id, - movie_id: movie_id - } + url_data = { + customer_id: customer_id, + movie_id: movie_id + } it "responds with success" do From fae40f7b55ee220ded92b0e5aa6ca27f44f77030 Mon Sep 17 00:00:00 2001 From: Jamila Date: Fri, 11 May 2018 15:06:38 -0700 Subject: [PATCH 59/59] added test for movie model --- test/models/movie_test.rb | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/models/movie_test.rb b/test/models/movie_test.rb index b1042d2c2..d6926ff17 100644 --- a/test/models/movie_test.rb +++ b/test/models/movie_test.rb @@ -15,9 +15,9 @@ movie.wont_be :valid? end - # it "can get a customer from movie through rentals" do - # movie = Movie.first - # - # - # end + it "must respond to customer" do + movie = Movie.first + movie.must_respond_to :rentals + + end end