diff --git a/.gitignore b/.gitignore index e69de29..c9cfef5 100644 --- a/.gitignore +++ b/.gitignore @@ -0,0 +1,28 @@ +# 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 the default SQLite database. +/db/*.sqlite3 +/db/*.sqlite3-* + +# Ignore all logfiles and tempfiles. +/log/* +/tmp/* +!/log/.keep +!/tmp/.keep + +# Ignore pidfiles, but keep the directory. +/tmp/pids/* +!/tmp/pids/ +!/tmp/pids/.keep + +.byebug_history + +# Ignore master key for decrypting credentials and more. +/config/master.key diff --git a/.learn b/.learn deleted file mode 100644 index 9092bbf..0000000 --- a/.learn +++ /dev/null @@ -1,6 +0,0 @@ -tags: - - tag1 -languages: - - language1 -resources: 0 -parent_template: git@github.com:learn-co-curriculum/sinatra-basic-forms-lab.git diff --git a/Gemfile b/Gemfile index fd00811..1a1b15d 100644 --- a/Gemfile +++ b/Gemfile @@ -1,17 +1,47 @@ -# A sample Gemfile -source "https://rubygems.org" +source 'https://rubygems.org' +git_source(:github) { |repo| "https://github.com/#{repo}.git" } -# gem "rails" +# Ruby 3.3+ compatibility gems +gem 'bigdecimal' +gem 'fiddle' +gem 'logger' +gem 'mutex_m' +gem 'ostruct' + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' +gem 'rails', '~> 6.1.3', '>= 6.1.3.1' +# Use sqlite3 as the database for Active Record (updated for Ruby 3.3 compatibility) +gem 'sqlite3', '~> 1.6.0' +# Use Puma as the app server +gem 'puma', '~> 5.0' +# Asset pipeline gems +gem 'sprockets-rails' +gem 'stimulus-rails' +gem 'turbo-rails' +# nio4r for Ruby 3.3 compatibility +gem 'nio4r', '~> 2.7' +# Use Active Model has_secure_password +# gem 'bcrypt', '~> 3.1.7' + +# 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: %i[mri mingw x64_mingw] + gem 'capybara' + gem 'rspec-rails', '~> 5.0.0' +end + +group :development do + gem 'listen', '~> 3.3' + gem 'rubocop' +end + +# Windows does not include zoneinfo files, so bundle the tzinfo-data gem +gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] -gem 'sinatra' - gem 'rake' - gem 'thin' - gem 'shotgun' - gem 'pry' - gem 'require_all' - group :test do - gem 'rspec' - gem 'capybara' - gem 'rack-test' - end \ No newline at end of file + gem 'rspec-json_expectations' + gem 'shoulda-matchers', '~> 4.0' +end diff --git a/Gemfile.lock b/Gemfile.lock index 6072aff..caea9fb 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,77 +1,270 @@ GEM remote: https://rubygems.org/ specs: - addressable (2.5.2) - public_suffix (>= 2.0.2, < 4.0) - capybara (2.18.0) + actioncable (6.1.7.10) + actionpack (= 6.1.7.10) + activesupport (= 6.1.7.10) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailbox (6.1.7.10) + actionpack (= 6.1.7.10) + activejob (= 6.1.7.10) + activerecord (= 6.1.7.10) + activestorage (= 6.1.7.10) + activesupport (= 6.1.7.10) + mail (>= 2.7.1) + actionmailer (6.1.7.10) + actionpack (= 6.1.7.10) + actionview (= 6.1.7.10) + activejob (= 6.1.7.10) + activesupport (= 6.1.7.10) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (6.1.7.10) + actionview (= 6.1.7.10) + activesupport (= 6.1.7.10) + rack (~> 2.0, >= 2.0.9) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.2.0) + actiontext (6.1.7.10) + actionpack (= 6.1.7.10) + activerecord (= 6.1.7.10) + activestorage (= 6.1.7.10) + activesupport (= 6.1.7.10) + nokogiri (>= 1.8.5) + actionview (6.1.7.10) + activesupport (= 6.1.7.10) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.1, >= 1.2.0) + activejob (6.1.7.10) + activesupport (= 6.1.7.10) + globalid (>= 0.3.6) + activemodel (6.1.7.10) + activesupport (= 6.1.7.10) + activerecord (6.1.7.10) + activemodel (= 6.1.7.10) + activesupport (= 6.1.7.10) + activestorage (6.1.7.10) + actionpack (= 6.1.7.10) + activejob (= 6.1.7.10) + activerecord (= 6.1.7.10) + activesupport (= 6.1.7.10) + marcel (~> 1.0) + mini_mime (>= 1.1.0) + activesupport (6.1.7.10) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 1.6, < 2) + minitest (>= 5.1) + tzinfo (~> 2.0) + zeitwerk (~> 2.3) + addressable (2.8.7) + public_suffix (>= 2.0.2, < 7.0) + ast (2.4.3) + base64 (0.3.0) + bigdecimal (3.2.2) + builder (3.3.0) + byebug (12.0.0) + capybara (3.40.0) addressable + matrix mini_mime (>= 0.1.3) - nokogiri (>= 1.3.3) - rack (>= 1.0.0) - rack-test (>= 0.5.4) - xpath (>= 2.0, < 4.0) - coderay (1.1.2) - daemons (1.2.6) - diff-lcs (1.3) - eventmachine (1.2.5) - method_source (0.9.0) - mini_mime (1.0.0) - mini_portile2 (2.3.0) - mustermann (1.0.2) - nokogiri (1.8.2) - mini_portile2 (~> 2.3.0) - pry (0.11.3) - coderay (~> 1.1.0) - method_source (~> 0.9.0) - public_suffix (3.0.2) - rack (2.0.4) - rack-protection (2.0.1) - rack - rack-test (0.8.2) - rack (>= 1.0, < 3) - rake (12.3.0) - require_all (1.5.0) - rspec (3.7.0) - rspec-core (~> 3.7.0) - rspec-expectations (~> 3.7.0) - rspec-mocks (~> 3.7.0) - rspec-core (3.7.1) - rspec-support (~> 3.7.0) - rspec-expectations (3.7.0) + nokogiri (~> 1.11) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (>= 1.5, < 3.0) + xpath (~> 3.2) + concurrent-ruby (1.3.5) + crass (1.0.6) + date (3.4.1) + diff-lcs (1.6.2) + erubi (1.13.1) + ffi (1.17.2) + ffi (1.17.2-arm64-darwin) + fiddle (1.1.8) + globalid (1.2.1) + activesupport (>= 6.1) + i18n (1.14.7) + concurrent-ruby (~> 1.0) + json (2.13.2) + language_server-protocol (3.17.0.5) + lint_roller (1.1.0) + listen (3.9.0) + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) + logger (1.7.0) + loofah (2.24.1) + crass (~> 1.0.2) + nokogiri (>= 1.12.0) + mail (2.8.1) + mini_mime (>= 0.1.1) + net-imap + net-pop + net-smtp + marcel (1.0.4) + matrix (0.4.3) + method_source (1.1.0) + mini_mime (1.1.5) + mini_portile2 (2.8.9) + minitest (5.25.5) + mutex_m (0.3.0) + net-imap (0.5.9) + date + net-protocol + net-pop (0.1.2) + net-protocol + net-protocol (0.2.2) + timeout + net-smtp (0.5.1) + net-protocol + nio4r (2.7.4) + nokogiri (1.18.9) + mini_portile2 (~> 2.8.2) + racc (~> 1.4) + nokogiri (1.18.9-arm64-darwin) + racc (~> 1.4) + ostruct (0.6.3) + parallel (1.27.0) + parser (3.3.9.0) + ast (~> 2.4.1) + racc + prism (1.4.0) + public_suffix (6.0.2) + puma (5.6.9) + nio4r (~> 2.0) + racc (1.8.1) + rack (2.2.17) + rack-cors (2.0.2) + rack (>= 2.0.0) + rack-test (2.2.0) + rack (>= 1.3) + rails (6.1.7.10) + actioncable (= 6.1.7.10) + actionmailbox (= 6.1.7.10) + actionmailer (= 6.1.7.10) + actionpack (= 6.1.7.10) + actiontext (= 6.1.7.10) + actionview (= 6.1.7.10) + activejob (= 6.1.7.10) + activemodel (= 6.1.7.10) + activerecord (= 6.1.7.10) + activestorage (= 6.1.7.10) + activesupport (= 6.1.7.10) + bundler (>= 1.15.0) + railties (= 6.1.7.10) + sprockets-rails (>= 2.0.0) + rails-dom-testing (2.3.0) + activesupport (>= 5.0.0) + minitest + nokogiri (>= 1.6) + rails-html-sanitizer (1.6.2) + loofah (~> 2.21) + nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0) + railties (6.1.7.10) + actionpack (= 6.1.7.10) + activesupport (= 6.1.7.10) + method_source + rake (>= 12.2) + thor (~> 1.0) + rainbow (3.1.1) + rake (13.3.0) + rb-fsevent (0.11.2) + rb-inotify (0.11.1) + ffi (~> 1.0) + regexp_parser (2.11.1) + rspec-core (3.13.5) + rspec-support (~> 3.13.0) + rspec-expectations (3.13.5) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-mocks (3.7.0) + rspec-support (~> 3.13.0) + rspec-json_expectations (2.2.0) + rspec-mocks (3.13.5) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.7.0) - rspec-support (3.7.1) - shotgun (0.9.2) - rack (>= 1.0) - sinatra (2.0.1) - mustermann (~> 1.0) - rack (~> 2.0) - rack-protection (= 2.0.1) - tilt (~> 2.0) - thin (1.7.2) - daemons (~> 1.0, >= 1.0.9) - eventmachine (~> 1.0, >= 1.0.4) - rack (>= 1, < 3) - tilt (2.0.8) - xpath (3.0.0) + rspec-support (~> 3.13.0) + rspec-rails (5.0.3) + actionpack (>= 5.2) + activesupport (>= 5.2) + railties (>= 5.2) + rspec-core (~> 3.10) + rspec-expectations (~> 3.10) + rspec-mocks (~> 3.10) + rspec-support (~> 3.10) + rspec-support (3.13.4) + rubocop (1.79.2) + json (~> 2.3) + language_server-protocol (~> 3.17.0.2) + lint_roller (~> 1.1.0) + parallel (~> 1.10) + parser (>= 3.3.0.2) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 2.9.3, < 3.0) + rubocop-ast (>= 1.46.0, < 2.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 2.4.0, < 4.0) + rubocop-ast (1.46.0) + parser (>= 3.3.7.2) + prism (~> 1.4) + ruby-progressbar (1.13.0) + shoulda-matchers (4.5.1) + activesupport (>= 4.2.0) + sprockets (4.2.2) + concurrent-ruby (~> 1.0) + logger + rack (>= 2.2.4, < 4) + sprockets-rails (3.5.2) + actionpack (>= 6.1) + activesupport (>= 6.1) + sprockets (>= 3.0.0) + sqlite3 (1.6.9) + mini_portile2 (~> 2.8.0) + stimulus-rails (1.3.4) + railties (>= 6.0.0) + thor (1.4.0) + timeout (0.4.3) + turbo-rails (2.0.12) + actionpack (>= 6.0.0) + railties (>= 6.0.0) + tzinfo (2.0.6) + concurrent-ruby (~> 1.0) + unicode-display_width (3.1.4) + unicode-emoji (~> 4.0, >= 4.0.4) + unicode-emoji (4.0.4) + websocket-driver (0.8.0) + base64 + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.5) + xpath (3.2.0) nokogiri (~> 1.8) + zeitwerk (2.7.3) PLATFORMS + arm64-darwin-24 ruby DEPENDENCIES + bigdecimal + byebug capybara - pry - rack-test - rake - require_all - rspec - shotgun - sinatra - thin + fiddle + listen (~> 3.3) + logger + mutex_m + nio4r (~> 2.7) + ostruct + puma (~> 5.0) + rack-cors + rails (~> 6.1.3, >= 6.1.3.1) + rspec-json_expectations + rspec-rails (~> 5.0.0) + rubocop + shoulda-matchers (~> 4.0) + sprockets-rails + sqlite3 (~> 1.6.0) + stimulus-rails + turbo-rails + tzinfo-data BUNDLED WITH - 1.16.1 + 2.7.1 diff --git a/README.md b/README.md index 1c8ffd2..6a0ac7a 100644 --- a/README.md +++ b/README.md @@ -1,17 +1,18 @@ -# Sinatra Basic Forms Lab +# Rails Basic Forms Lab Your local pet adoption store has asked you to build their new website. First and foremost, the store needs a form for their staff to add puppies that are -available for adoption. Good thing you love puppies _and_ coding! +available for adoption. Good thing you love puppies _and_ coding! ## Objectives 1. Implement a POST request to the controller to display data from a user in -the view + the view 2. Implement both POST and GET requests 3. Connect a controller action with both a view and a model +4. Practice using Rails form helpers and routing ## Instructions @@ -19,33 +20,58 @@ The focus of this lab is to build a way for a user to go to a homepage, follow a link to a form where they can enter a puppy's information, and, upon submission, view the puppy's information. -1. Build out a puppy class in `models/puppy.rb`. Puppies should have name, -breed, and age attributes. You will need to be able to pass these three -attributes to initialization, as well as readers and writers for the attributes. +1. Run `bundle install` +2. Run `bin/rails server` -2. In `app.rb` build out a GET request to load a homepage. The homepage -should go to the main route `/`. +3. **Build out a puppy class** in `app/models/puppy.rb`. Puppies should have name, + breed, and age attributes. You will need to be able to pass these three + attributes to initialization, as well as readers and writers for the attributes. -3. The home page will also need a new view `index.erb`. This page should -welcome you to the Puppy Adoption Site. Add this view to the controller action. +4. **Set up your routes** in `config/routes.rb`: -4. Now, we need to create a form for a user to list a new puppy that is -available for adoption. You can create this form in `views/create_puppy.erb`. -Remember, you'll need to set up another controller action for a user to be -able to view this form in the browser. Another reminder: the "submit" button -of a form is an `` element with a `type` of `"submit"`, *not* a -`` element. + - Create a route that responds to a GET request at `/` that goes to `puppies#index` + - Create a route that responds to a GET request at `/new` that goes to `puppies#new` + - Create a route that responds to a POST request at `/puppy` that goes to `puppies#create` -5. Now we need to make sure the form is being submitted properly. You'll need -to have a third controller action that takes the input from the user and -renders a third view (`views/display_puppy.erb`) which displays the info for -the puppy that was just created. +5. **Create controller actions** in `app/controllers/puppies_controller.rb`: -6. Add a link on the homepage to the new puppy form. + - Add an `index` action to display the homepage + - Add a `new` action to display the form + - Add a `create` action to process the form submission and pass data to the view -## Does this need an update? +6. **Build the homepage** in `app/views/puppies/index.html.erb`. This page should + welcome you to the Puppy Adoption Site and include a link to the new puppy form. + Use Rails `link_to` helper to create a link with the text "Click Here To List A Puppy" + that goes to `/new`. -Please open a [GitHub issue](https://github.com/learn-co-curriculum/phrg-sinatra-basic-forms-lab/issues) or [pull-request](https://github.com/learn-co-curriculum/phrg-sinatra-basic-forms-lab/pulls). Provide a detailed description that explains the issue you have found or the change you are proposing. Then "@" mention your instructor on the issue or pull-request, and send them a link via Connect. +7. **Create the form** in `app/views/puppies/new.html.erb`. You can create this form + using Rails form helpers. Remember, you'll need to set up the form to submit to the + correct route. Use Rails `form_with` helper to create your form. The form should + submit to `/puppy` using POST method and have fields for name, breed, and age. + Remember: the "submit" button should be an `` element with a `type` of + `"submit"` and text content of "submit". -View Sinatra Basic Forms Lab on Learn.co and start learning to code for free. -PHRG Sinatra Basic Forms Lab +8. **Display the results** in `app/views/puppies/create.html.erb`. Make sure the form + is being submitted properly. This view should display the info for the puppy that + was just created. Display the puppy information with labels like: + - Puppy Name: [name] + - Puppy Breed: [breed] + - Puppy Age: [age] + +## Testing + +Run `bundle exec rspec` to test your implementation. Make sure all tests pass! + +The tests will check that: + +- Your routes are set up correctly +- Your form has all the required fields +- Your form submits to the correct endpoint +- Your results page displays all the submitted data correctly +- Your Puppy class works as expected + +## Resources + +- [Rails Forms Guide](https://guides.rubyonrails.org/form_helpers.html) +- [Rails Routing Guide](https://guides.rubyonrails.org/routing.html) +- [Rails Controller Guide](https://guides.rubyonrails.org/action_controller_overview.html) diff --git a/Rakefile b/Rakefile index 62140e3..e85f913 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,6 @@ -ENV["SINATRA_ENV"] ||= "development" +# 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/environment' +require_relative 'config/application' -# Type `rake -T` on your command line to see the available rake tasks. - -task :console do - Pry.start -end \ No newline at end of file +Rails.application.load_tasks diff --git a/app.rb b/app.rb deleted file mode 100644 index b1f74e9..0000000 --- a/app.rb +++ /dev/null @@ -1,5 +0,0 @@ -require_relative 'config/environment' - -class App < Sinatra::Base - -end \ No newline at end of file diff --git a/app/assets/config/manifest.js b/app/assets/config/manifest.js new file mode 100644 index 0000000..b16e53d --- /dev/null +++ b/app/assets/config/manifest.js @@ -0,0 +1,3 @@ +//= link_tree ../images +//= link_directory ../javascripts .js +//= link_directory ../stylesheets .css diff --git a/models/.keep b/app/assets/images/.keep similarity index 100% rename from models/.keep rename to app/assets/images/.keep diff --git a/public/images/.keep b/app/assets/javascripts/.keep similarity index 100% rename from public/images/.keep rename to app/assets/javascripts/.keep diff --git a/public/javascripts/.keep b/app/assets/stylesheets/.keep similarity index 100% rename from public/javascripts/.keep rename to app/assets/stylesheets/.keep diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 0000000..4355099 --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,3 @@ +class ApplicationController < ActionController::Base + # protect_from_forgery with: :exception +end diff --git a/app/controllers/puppies_controller.rb b/app/controllers/puppies_controller.rb new file mode 100644 index 0000000..ed1730d --- /dev/null +++ b/app/controllers/puppies_controller.rb @@ -0,0 +1,6 @@ +class PuppiesController < ApplicationController + # TODO: Add your controller actions here + # You'll need an 'index' action to display the homepage + # You'll need a 'new' action to display the form + # You'll need a 'create' action to process the form submission and display the puppy +end diff --git a/app/models/application_record.rb b/app/models/application_record.rb new file mode 100644 index 0000000..10a4cba --- /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/puppy.rb b/app/models/puppy.rb new file mode 100644 index 0000000..77f6078 --- /dev/null +++ b/app/models/puppy.rb @@ -0,0 +1,10 @@ +# TODO: Build out your Puppy class here +# Your puppy should have name, breed, and age attributes +# You will need to be able to pass these three attributes to initialization +# as well as readers and writers for the attributes + +class Puppy + # TODO: Add your code here + # Hint: You'll need attr_accessor for name, breed, and age + # You'll also need an initialize method that takes name, breed, and age as parameters +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb new file mode 100644 index 0000000..109a5dd --- /dev/null +++ b/app/views/layouts/application.html.erb @@ -0,0 +1,13 @@ + + + + Puppy Adoption Site + + <%= csrf_meta_tags %> + <%= csp_meta_tag %> + + + + <%= yield %> + + diff --git a/app/views/puppies/create.html.erb b/app/views/puppies/create.html.erb new file mode 100644 index 0000000..6f54b98 --- /dev/null +++ b/app/views/puppies/create.html.erb @@ -0,0 +1,11 @@ + diff --git a/app/views/puppies/index.html.erb b/app/views/puppies/index.html.erb new file mode 100644 index 0000000..2be4660 --- /dev/null +++ b/app/views/puppies/index.html.erb @@ -0,0 +1,10 @@ + diff --git a/app/views/puppies/new.html.erb b/app/views/puppies/new.html.erb new file mode 100644 index 0000000..45ca039 --- /dev/null +++ b/app/views/puppies/new.html.erb @@ -0,0 +1,11 @@ + diff --git a/bin/rails b/bin/rails new file mode 100755 index 0000000..0739660 --- /dev/null +++ b/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path('../config/application', __dir__) +require_relative '../config/boot' +require 'rails/commands' diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 0000000..93e191c --- /dev/null +++ b/bin/rspec @@ -0,0 +1,16 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +# +# This file was generated by Bundler. +# +# The application 'rspec' is installed as part of a gem, and +# this file is here to facilitate running it. +# + +ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../Gemfile", __dir__) + +require "rubygems" +require "bundler/setup" + +load Gem.bin_path("rspec-core", "rspec") diff --git a/config.ru b/config.ru index c356a49..ad1fbf2 100644 --- a/config.ru +++ b/config.ru @@ -1,3 +1,6 @@ -require './config/environment' +# This file is used by Rack-based servers to start the application. -run App \ No newline at end of file +require_relative 'config/environment' + +run Rails.application +Rails.application.load_server diff --git a/config/application.rb b/config/application.rb new file mode 100644 index 0000000..983c1be --- /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 "active_storage/engine" +require 'action_controller/railtie' +# require "action_mailer/railtie" +# require "action_mailbox/engine" +# require "action_text/engine" +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 RailsBasicFormsLab + class Application < Rails::Application + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 6.1 + + # Configuration for the application, engines, and railties goes here. + # + # These settings can be overridden in specific environments using the files + # in config/environments, which are processed later. + # + # config.time_zone = "Central Time (US & Canada)" + # config.eager_load_paths << Rails.root.join("extras") + + # 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 = false + end +end diff --git a/config/boot.rb b/config/boot.rb new file mode 100644 index 0000000..fe207ee --- /dev/null +++ b/config/boot.rb @@ -0,0 +1,4 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. +require 'logger' # Required for Ruby 3.3+ compatibility diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 0000000..c52c94d --- /dev/null +++ b/config/database.yml @@ -0,0 +1,16 @@ +default: &default + adapter: sqlite3 + pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> + timeout: 5000 + +development: + <<: *default + database: db/development.sqlite3 + +test: + <<: *default + database: db/test.sqlite3 + +production: + <<: *default + database: db/production.sqlite3 diff --git a/config/environment.rb b/config/environment.rb index 024d89a..426333b 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,7 +1,5 @@ -ENV['SINATRA_ENV'] ||= "development" +# Load the Rails application. +require_relative 'application' -require 'bundler/setup' -Bundler.require(:default, ENV['SINATRA_ENV']) - -require './app' -require_all 'models' \ No newline at end of file +# Initialize the Rails application. +Rails.application.initialize! diff --git a/config/environments/development.rb b/config/environments/development.rb new file mode 100644 index 0000000..a1427f1 --- /dev/null +++ b/config/environments/development.rb @@ -0,0 +1,76 @@ +require 'active_support/core_ext/integer/time' + +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 any time + # it changes. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join('tmp', 'caching-dev.txt').exist? + config.action_controller.perform_caching = true + config.action_controller.enable_fragment_cache_logging = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options). + config.active_storage.variant_processor = :mini_magick if defined?(ActiveStorage) + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false if defined?(ActionMailer) + + config.action_mailer.perform_caching = false if defined?(ActionMailer) + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load if defined?(ActiveRecord) + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true if defined?(ActiveRecord) + + # Debug mode disables concatenation and preprocessing of assets. + # This option may cause significant delays in view rendering with a large + # number of complex assets. + config.assets.debug = true if defined?(Sprockets) + + # Suppress logger output for asset requests. + config.assets.quiet = true if defined?(Sprockets) + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = 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 + + # Uncomment if you wish to allow Action Cable access from any origin. + # config.action_cable.disable_request_forgery_protection = true +end diff --git a/config/environments/test.rb b/config/environments/test.rb new file mode 100644 index 0000000..c2bd7dd --- /dev/null +++ b/config/environments/test.rb @@ -0,0 +1,59 @@ +require 'active_support/core_ext/integer/time' + +# 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! + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + config.cache_classes = true + + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + config.cache_store = :null_store + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory. + config.active_storage.variant_processor = :mini_magick if defined?(ActiveStorage) + + config.action_mailer.perform_caching = false if defined?(ActionMailer) + + # 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 if defined?(ActionMailer) + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raise exceptions for disallowed deprecations. + config.active_support.disallowed_deprecation = :raise + + # Tell Active Support which deprecation messages to disallow. + config.active_support.disallowed_deprecation_warnings = [] + + # Raises error for missing translations. + # config.i18n.raise_on_missing_translations = true + + # Annotate rendered view with file names. + # config.action_view.annotate_rendered_view_with_filenames = true +end diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000..3e69694 --- /dev/null +++ b/config/initializers/filter_parameter_logging.rb @@ -0,0 +1,6 @@ +# 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 += %i[ + passw secret token _key crypt salt certificate otp ssn +] diff --git a/config/routes.rb b/config/routes.rb new file mode 100644 index 0000000..4c97056 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,7 @@ +Rails.application.routes.draw do + # TODO: Add your routes here + # You need: + # - A GET route to '/' that goes to the puppies controller index action + # - A GET route to '/new' that goes to the puppies controller new action + # - A POST route to '/puppy' that goes to the puppies controller create action +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..ae89000 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,14 @@ +# 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. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 0) do +end diff --git a/db/test.sqlite3 b/db/test.sqlite3 new file mode 100644 index 0000000..9fb98b5 Binary files /dev/null and b/db/test.sqlite3 differ diff --git a/models/puppy.rb b/models/puppy.rb deleted file mode 100644 index e69de29..0000000 diff --git a/public/stylesheets/.keep b/public/stylesheets/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/spec/models/puppy_spec.rb b/spec/models/puppy_spec.rb index 7e65639..e2aea6d 100644 --- a/spec/models/puppy_spec.rb +++ b/spec/models/puppy_spec.rb @@ -1,34 +1,36 @@ -describe 'Puppy class' do - let!(:puppy) { Puppy.new("brad", "black lab", 2) } +require "rails_helper" - it 'can create a new instance of the puppy class' do - expect(Puppy.new("brad", "black lab", 2)).to be_an_instance_of(Puppy) +RSpec.describe Puppy, type: :model do + let!(:puppy) { Puppy.new(name: "brad", breed: "black lab", age: 2) } + + it "can create a new instance of the puppy class" do + expect(Puppy.new(name: "brad", breed: "black lab", age: 2)).to be_an_instance_of(Puppy) end - it 'can read a puppy name' do + it "can read a puppy name" do expect(puppy.name).to eq("brad") end - it 'can read a puppy breed' do + it "can read a puppy breed" do expect(puppy.breed).to eq("black lab") end - it 'can read a puppy age' do + it "can read a puppy age" do expect(puppy.age).to eq(2) end - it 'can change puppy age' do + it "can change puppy age" do puppy.age = 3 expect(puppy.age).to eq(3) end - it 'can change puppy name' do + it "can change puppy name" do puppy.name = "brad the beast" expect(puppy.name).to eq("brad the beast") end - it 'can change puppy breed' do + it "can change puppy breed" do puppy.breed = "the best black lab" expect(puppy.breed).to eq("the best black lab") end -end \ No newline at end of file +end diff --git a/spec/rails_forms_lab_spec.rb b/spec/rails_forms_lab_spec.rb new file mode 100644 index 0000000..fb0c49a --- /dev/null +++ b/spec/rails_forms_lab_spec.rb @@ -0,0 +1,49 @@ +require 'rails_helper' + +RSpec.describe 'Rails Forms Lab', type: :feature do + describe 'GET /' do + it 'sends a 200 status code' do + visit '/' + expect(page.status_code).to eq(200) + end + + it 'renders welcome' do + visit '/' + expect(page).to have_link('Click Here To List A Puppy') + end + end + + describe 'GET /new' do + it 'sends a 200 status code' do + visit '/new' + expect(page.status_code).to eq(200) + end + + it 'renders the form' do + visit '/new' + expect(page).to have_selector('form') + expect(page).to have_field(:name) + expect(page).to have_field(:breed) + expect(page).to have_field(:age) + end + end + + describe 'POST /puppy' do + it 'displays the puppy' do + visit '/new' + + fill_in(:name, with: 'Butch') + fill_in(:breed, with: 'Mastiff') + fill_in(:age, with: '6 months') + click_button 'submit' + + # Check that the page contains the puppy information + expect(page).to have_text('Puppy Name') + expect(page).to have_text('Butch') + expect(page).to have_text('Puppy Breed') + expect(page).to have_text('Mastiff') + expect(page).to have_text('Puppy Age') + expect(page).to have_text('6 months') + end + end +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 0000000..b1f817e --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,72 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../config/environment', __dir__) +# Prevent database truncation if the environment is production +abort('The Rails environment is running in production mode!') if Rails.env.production? +require 'rspec/rails' +require 'capybara/rails' +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.fixture_path = "#{Rails.root}/spec/fixtures" + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = true + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end + +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end +end diff --git a/spec/sinatra_basic_forms_lab_spec.rb b/spec/sinatra_basic_forms_lab_spec.rb deleted file mode 100644 index a7ba96b..0000000 --- a/spec/sinatra_basic_forms_lab_spec.rb +++ /dev/null @@ -1,46 +0,0 @@ -describe App do - - describe 'GET /' do - - it 'sends a 200 status code' do - get '/' - expect(last_response.status).to eq(200) - end - - it 'renders welcome' do - visit '/' - expect(page).to have_link("Click Here To List A Puppy") - end - end - - describe 'GET /NEW' do - it 'sends a 200 status code' do - get '/new' - expect(last_response.status).to eq(200) - end - - it 'renders the form' do - visit '/new' - expect(page).to have_selector("form") - expect(page).to have_field(:name) - expect(page).to have_field(:breed) - expect(page).to have_field(:age) - end - end - - describe 'POST /' do - it "displays the puppy" do - visit '/new' - - fill_in(:name, :with => "Butch") - fill_in(:breed, :with => "Mastiff") - fill_in(:age, :with => "6 months") - click_button "submit" - expect(page).to have_text("Puppy Name:\nButch") - expect(page).to have_text("Puppy Breed:\nMastiff") - expect(page).to have_text("Puppy Age:\n6 months") - end - end - - -end \ No newline at end of file diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index cc6174d..3f1dd87 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,17 +1,97 @@ -ENV["SINATRA_ENV"] = "test" -require_relative '../config/environment.rb' -require 'capybara/dsl' -require 'rack/test' -require 'capybara/dsl' - +require 'rspec/json_expectations' +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration RSpec.configure do |config| - config.include Capybara::DSL - config.include Rack::Test::Methods - config.order = 'default' -end + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end -def app - Rack::Builder.parse_file('config.ru').first -end + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" -Capybara.app = app + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/tmp/development_secret.txt b/tmp/development_secret.txt new file mode 100644 index 0000000..43be7b2 --- /dev/null +++ b/tmp/development_secret.txt @@ -0,0 +1 @@ +73db45b83089277f37a7f05c4fecb06f509eb6e2616973b0df7bebf020d8b6b8c51bc404c9aa2f90f4168b925aca4fd63d5664aefdcbd4584b466329d60f54a2 diff --git a/views/.keep b/views/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/views/create_puppy.erb b/views/create_puppy.erb deleted file mode 100644 index e69de29..0000000 diff --git a/views/display_puppy.erb b/views/display_puppy.erb deleted file mode 100644 index e69de29..0000000 diff --git a/views/index.erb b/views/index.erb deleted file mode 100644 index e69de29..0000000
View Sinatra Basic Forms Lab on Learn.co and start learning to code for free.
PHRG Sinatra Basic Forms Lab