From 5ca52201e30b71786d56d188da5e623f676cf06b Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 09:45:52 -0600 Subject: [PATCH 01/12] Starting rails app --- .gitignore | 4 +- Gemfile | 47 +++++ Gemfile.lock | 166 ++++++++++++++++++ README.rdoc | 28 +++ Rakefile | 6 + app/assets/images/.keep | 0 app/assets/javascripts/application.js | 16 ++ app/assets/stylesheets/application.css | 15 ++ app/controllers/application_controller.rb | 5 + app/controllers/concerns/.keep | 0 app/helpers/application_helper.rb | 2 + app/mailers/.keep | 0 app/models/.keep | 0 app/models/concerns/.keep | 0 app/views/layouts/application.html.erb | 14 ++ bin/bundle | 3 + bin/rails | 9 + bin/rake | 9 + bin/setup | 29 +++ bin/spring | 17 ++ config.ru | 4 + config/application.rb | 26 +++ config/boot.rb | 3 + config/database.yml | 25 +++ config/environment.rb | 5 + config/environments/development.rb | 41 +++++ config/environments/production.rb | 79 +++++++++ config/environments/test.rb | 42 +++++ config/initializers/assets.rb | 11 ++ config/initializers/backtrace_silencers.rb | 7 + config/initializers/cookies_serializer.rb | 3 + .../initializers/filter_parameter_logging.rb | 4 + config/initializers/inflections.rb | 16 ++ config/initializers/mime_types.rb | 4 + config/initializers/session_store.rb | 3 + .../to_time_preserves_timezone.rb | 10 ++ config/initializers/wrap_parameters.rb | 14 ++ config/locales/en.yml | 23 +++ config/routes.rb | 56 ++++++ config/secrets.yml | 22 +++ db/seeds.rb | 7 + lib/assets/.keep | 0 lib/tasks/.keep | 0 public/404.html | 67 +++++++ public/422.html | 67 +++++++ public/500.html | 66 +++++++ public/favicon.ico | 0 public/robots.txt | 5 + test/controllers/.keep | 0 test/fixtures/.keep | 0 test/helpers/.keep | 0 test/integration/.keep | 0 test/mailers/.keep | 0 test/models/.keep | 0 test/test_helper.rb | 10 ++ vendor/assets/javascripts/.keep | 0 vendor/assets/stylesheets/.keep | 0 57 files changed, 989 insertions(+), 1 deletion(-) create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 README.rdoc create mode 100644 Rakefile create mode 100644 app/assets/images/.keep create mode 100644 app/assets/javascripts/application.js create mode 100644 app/assets/stylesheets/application.css create mode 100644 app/controllers/application_controller.rb create mode 100644 app/controllers/concerns/.keep create mode 100644 app/helpers/application_helper.rb create mode 100644 app/mailers/.keep create mode 100644 app/models/.keep create mode 100644 app/models/concerns/.keep create mode 100644 app/views/layouts/application.html.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 100644 config.ru create mode 100644 config/application.rb create mode 100644 config/boot.rb 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/assets.rb create mode 100644 config/initializers/backtrace_silencers.rb create mode 100644 config/initializers/cookies_serializer.rb create mode 100644 config/initializers/filter_parameter_logging.rb create mode 100644 config/initializers/inflections.rb create mode 100644 config/initializers/mime_types.rb create mode 100644 config/initializers/session_store.rb create mode 100644 config/initializers/to_time_preserves_timezone.rb create mode 100644 config/initializers/wrap_parameters.rb create mode 100644 config/locales/en.yml create mode 100644 config/routes.rb create mode 100644 config/secrets.yml create mode 100644 db/seeds.rb create mode 100644 lib/assets/.keep create mode 100644 lib/tasks/.keep create mode 100644 public/404.html create mode 100644 public/422.html create mode 100644 public/500.html create mode 100644 public/favicon.ico create mode 100644 public/robots.txt create mode 100644 test/controllers/.keep create mode 100644 test/fixtures/.keep create mode 100644 test/helpers/.keep create mode 100644 test/integration/.keep create mode 100644 test/mailers/.keep create mode 100644 test/models/.keep create mode 100644 test/test_helper.rb create mode 100644 vendor/assets/javascripts/.keep create mode 100644 vendor/assets/stylesheets/.keep diff --git a/.gitignore b/.gitignore index 54cb8bbbc..8d3c88c2a 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,6 @@ capybara-*.html /spec/tmp/* **.orig rerun.txt -pickle-email-*.html \ No newline at end of file +pickle-email-*.html +.idea/* +.history \ No newline at end of file diff --git a/Gemfile b/Gemfile new file mode 100644 index 000000000..c9ede23d1 --- /dev/null +++ b/Gemfile @@ -0,0 +1,47 @@ +source 'https://rubygems.org' + + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '4.2.11.3' +# Use sqlite3 as the database for Active Record +gem 'sqlite3', '~> 1.3.11' +# Use SCSS for stylesheets +gem 'sass-rails', '~> 5.0' +# Use Uglifier as compressor for JavaScript assets +gem 'uglifier', '>= 1.3.0' +# Use CoffeeScript for .coffee assets and views +gem 'coffee-rails', '~> 4.1.0' +# See https://github.com/rails/execjs#readme for more supported runtimes +# gem 'therubyracer', platforms: :ruby + +# Use jquery as the JavaScript library +gem 'jquery-rails' +# Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks +gem 'turbolinks' +# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder +gem 'jbuilder', '~> 2.0' +# bundle exec rake doc:rails generates the API under doc/api. +gem 'sdoc', '~> 0.4.0', group: :doc + +# Use ActiveModel has_secure_password +# gem 'bcrypt', '~> 3.1.7' + +# Use Unicorn as the app server +# gem 'unicorn' + +# Use Capistrano for deployment +# gem 'capistrano-rails', group: :development + +group :development, :test do + # Call 'byebug' anywhere in the code to stop execution and get a debugger console + gem 'byebug' +end + +group :development do + # Access an IRB console on exception pages or by using <%= console %> in views + gem 'web-console', '~> 2.0' + + # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring + gem 'spring' +end + diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 000000000..a2a910ebd --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,166 @@ +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.2.11.3) + actionpack (= 4.2.11.3) + actionview (= 4.2.11.3) + activejob (= 4.2.11.3) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 1.0, >= 1.0.5) + actionpack (4.2.11.3) + actionview (= 4.2.11.3) + activesupport (= 4.2.11.3) + rack (~> 1.6) + rack-test (~> 0.6.2) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (4.2.11.3) + activesupport (= 4.2.11.3) + builder (~> 3.1) + erubis (~> 2.7.0) + rails-dom-testing (~> 1.0, >= 1.0.5) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (4.2.11.3) + activesupport (= 4.2.11.3) + globalid (>= 0.3.0) + activemodel (4.2.11.3) + activesupport (= 4.2.11.3) + builder (~> 3.1) + activerecord (4.2.11.3) + activemodel (= 4.2.11.3) + activesupport (= 4.2.11.3) + arel (~> 6.0) + activesupport (4.2.11.3) + i18n (~> 0.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + arel (6.0.4) + binding_of_caller (0.8.0) + debug_inspector (>= 0.0.1) + builder (3.2.4) + byebug (11.1.3) + coffee-rails (4.1.1) + coffee-script (>= 2.2.0) + railties (>= 4.0.0, < 5.1.x) + coffee-script (2.4.1) + coffee-script-source + execjs + coffee-script-source (1.12.2) + concurrent-ruby (1.1.7) + crass (1.0.6) + debug_inspector (0.0.3) + erubis (2.7.0) + execjs (2.7.0) + ffi (1.13.1) + globalid (0.4.2) + activesupport (>= 4.2.0) + i18n (0.9.5) + concurrent-ruby (~> 1.0) + jbuilder (2.9.1) + activesupport (>= 4.2.0) + jquery-rails (4.4.0) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + json (1.8.6) + loofah (2.7.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.1) + mini_mime (>= 0.1.1) + mini_mime (1.0.2) + mini_portile2 (2.4.0) + minitest (5.14.2) + nokogiri (1.10.10) + mini_portile2 (~> 2.4.0) + rack (1.6.13) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.2.11.3) + actionmailer (= 4.2.11.3) + actionpack (= 4.2.11.3) + actionview (= 4.2.11.3) + activejob (= 4.2.11.3) + activemodel (= 4.2.11.3) + activerecord (= 4.2.11.3) + activesupport (= 4.2.11.3) + bundler (>= 1.3.0, < 2.0) + railties (= 4.2.11.3) + sprockets-rails + rails-deprecated_sanitizer (1.0.3) + activesupport (>= 4.2.0.alpha) + rails-dom-testing (1.0.9) + activesupport (>= 4.2.0, < 5.0) + nokogiri (~> 1.6) + rails-deprecated_sanitizer (>= 1.0.1) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + railties (4.2.11.3) + actionpack (= 4.2.11.3) + activesupport (= 4.2.11.3) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (13.0.1) + rb-fsevent (0.10.4) + rb-inotify (0.10.1) + ffi (~> 1.0) + rdoc (4.3.0) + sass (3.7.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + sass-rails (5.0.7) + railties (>= 4.0.0, < 6) + sass (~> 3.1) + sprockets (>= 2.8, < 4.0) + sprockets-rails (>= 2.0, < 4.0) + tilt (>= 1.1, < 3) + sdoc (0.4.2) + json (~> 1.7, >= 1.7.7) + rdoc (~> 4.0) + spring (2.1.1) + sprockets (3.7.2) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.2) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + sqlite3 (1.3.13) + thor (1.0.1) + thread_safe (0.3.6) + tilt (2.0.10) + turbolinks (5.2.1) + turbolinks-source (~> 5.2) + turbolinks-source (5.2.0) + tzinfo (1.2.7) + thread_safe (~> 0.1) + uglifier (4.2.0) + execjs (>= 0.3.0, < 3) + web-console (2.3.0) + activemodel (>= 4.0) + binding_of_caller (>= 0.7.2) + railties (>= 4.0) + sprockets-rails (>= 2.0, < 4.0) + +PLATFORMS + ruby + +DEPENDENCIES + byebug + coffee-rails (~> 4.1.0) + jbuilder (~> 2.0) + jquery-rails + rails (= 4.2.11.3) + sass-rails (~> 5.0) + sdoc (~> 0.4.0) + spring + sqlite3 (~> 1.3.11) + turbolinks + uglifier (>= 1.3.0) + web-console (~> 2.0) + +BUNDLED WITH + 1.17.3 diff --git a/README.rdoc b/README.rdoc new file mode 100644 index 000000000..dd4e97e22 --- /dev/null +++ b/README.rdoc @@ -0,0 +1,28 @@ +== README + +This README would normally document whatever steps are necessary to get the +application up and running. + +Things you may want to cover: + +* Ruby version + +* System dependencies + +* Configuration + +* Database creation + +* Database initialization + +* How to run the test suite + +* Services (job queues, cache servers, search engines, etc.) + +* Deployment instructions + +* ... + + +Please feel free to use a different markup language if you do not plan to run +rake doc:app. diff --git a/Rakefile b/Rakefile new file mode 100644 index 000000000..ba6b733dd --- /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 File.expand_path('../config/application', __FILE__) + +Rails.application.load_tasks diff --git a/app/assets/images/.keep b/app/assets/images/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js new file mode 100644 index 000000000..e07c5a830 --- /dev/null +++ b/app/assets/javascripts/application.js @@ -0,0 +1,16 @@ +// This is a manifest file that'll be compiled into application.js, which will include all the files +// listed below. +// +// Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts, +// or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path. +// +// It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the +// compiled file. +// +// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details +// about supported directives. +// +//= require jquery +//= require jquery_ujs +//= require turbolinks +//= require_tree . diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.css new file mode 100644 index 000000000..f9cd5b348 --- /dev/null +++ b/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets, + * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path. + * + * You're free to add application-wide styles to this file and they'll appear at the bottom of the + * compiled file so the styles you add here take precedence over styles defined in any styles + * defined in the other CSS/SCSS files in this directory. It is generally better to create a new + * file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb new file mode 100644 index 000000000..d83690e1b --- /dev/null +++ b/app/controllers/application_controller.rb @@ -0,0 +1,5 @@ +class ApplicationController < ActionController::Base + # Prevent CSRF attacks by raising an exception. + # For APIs, you may want to use :null_session instead. + protect_from_forgery with: :exception +end diff --git a/app/controllers/concerns/.keep b/app/controllers/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb new file mode 100644 index 000000000..de6be7945 --- /dev/null +++ b/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/app/mailers/.keep b/app/mailers/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/app/models/.keep b/app/models/.keep new file mode 100644 index 000000000..e69de29bb 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/application.html.erb b/app/views/layouts/application.html.erb new file mode 100644 index 000000000..0b13f942e --- /dev/null +++ b/app/views/layouts/application.html.erb @@ -0,0 +1,14 @@ + + + + ExampleRailsChallenge + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> + <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> + <%= csrf_meta_tags %> + + + +<%= 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..0138d79b7 --- /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', __FILE__) +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..acdb2c138 --- /dev/null +++ b/bin/setup @@ -0,0 +1,29 @@ +#!/usr/bin/env ruby +require 'pathname' + +# path to your application root. +APP_ROOT = Pathname.new File.expand_path('../../', __FILE__) + +Dir.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 || bundle install" + + # puts "\n== Copying sample files ==" + # unless File.exist?("config/database.yml") + # system "cp config/database.yml.sample config/database.yml" + # end + + puts "\n== Preparing database ==" + system "bin/rake db:setup" + + puts "\n== Removing old logs and tempfiles ==" + system "rm -f log/*" + system "rm -rf tmp/cache" + + puts "\n== Restarting application server ==" + system "touch tmp/restart.txt" +end diff --git a/bin/spring b/bin/spring new file mode 100755 index 000000000..d89ee495f --- /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/config.ru b/config.ru new file mode 100644 index 000000000..bd83b2541 --- /dev/null +++ b/config.ru @@ -0,0 +1,4 @@ +# This file is used by Rack-based servers to start the application. + +require ::File.expand_path('../config/environment', __FILE__) +run Rails.application diff --git a/config/application.rb b/config/application.rb new file mode 100644 index 000000000..b9a9df3e0 --- /dev/null +++ b/config/application.rb @@ -0,0 +1,26 @@ +require File.expand_path('../boot', __FILE__) + +require 'rails/all' + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module ExampleRailsChallenge + class Application < Rails::Application + # 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. + + # Set Time.zone default to the specified zone and make Active Record auto-convert to this zone. + # Run "rake -D time" for a list of tasks for finding time zone names. Default is UTC. + # config.time_zone = 'Central Time (US & Canada)' + + # The default locale is :en and all translations from config/locales/*.rb,yml are auto loaded. + # config.i18n.load_path += Dir[Rails.root.join('my', 'locales', '*.{rb,yml}').to_s] + # config.i18n.default_locale = :de + + # Do not swallow errors in after_commit/after_rollback callbacks. + config.active_record.raise_in_transactional_callbacks = true + end +end diff --git a/config/boot.rb b/config/boot.rb new file mode 100644 index 000000000..6b750f00b --- /dev/null +++ b/config/boot.rb @@ -0,0 +1,3 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. diff --git a/config/database.yml b/config/database.yml new file mode 100644 index 000000000..1c1a37ca8 --- /dev/null +++ b/config/database.yml @@ -0,0 +1,25 @@ +# SQLite version 3.x +# gem install sqlite3 +# +# Ensure the SQLite 3 gem is defined in your Gemfile +# gem 'sqlite3' +# +default: &default + adapter: sqlite3 + pool: 5 + timeout: 5000 + +development: + <<: *default + database: db/development.sqlite3 + +# 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: db/test.sqlite3 + +production: + <<: *default + database: db/production.sqlite3 diff --git a/config/environment.rb b/config/environment.rb new file mode 100644 index 000000000..ee8d90dc6 --- /dev/null +++ b/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require File.expand_path('../application', __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 000000000..b55e2144b --- /dev/null +++ b/config/environments/development.rb @@ -0,0 +1,41 @@ +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 and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Debug mode disables concatenation and preprocessing of assets. + # This option may cause significant delays in view rendering with a large + # number of complex assets. + config.assets.debug = true + + # Asset digests allow you to set far-future HTTP expiration dates on all assets, + # yet still be able to expire them through the digest params. + config.assets.digest = true + + # Adds additional error checking when serving assets at runtime. + # Checks for improperly declared sprockets dependencies. + # Raises helpful error messages. + config.assets.raise_runtime_errors = true + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/config/environments/production.rb b/config/environments/production.rb new file mode 100644 index 000000000..5c1b32e48 --- /dev/null +++ b/config/environments/production.rb @@ -0,0 +1,79 @@ +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 + + # Enable Rack::Cache to put a simple HTTP cache in front of your application + # Add `rack-cache` to your Gemfile before enabling this. + # For large-scale production use, consider using a caching reverse proxy like + # NGINX, varnish or squid. + # config.action_dispatch.rack_cache = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.serve_static_files = ENV['RAILS_SERVE_STATIC_FILES'].present? + + # Compress JavaScripts and CSS. + config.assets.js_compressor = :uglifier + # config.assets.css_compressor = :sass + + # Do not fallback to assets pipeline if a precompiled asset is missed. + config.assets.compile = false + + # Asset digests allow you to set far-future HTTP expiration dates on all assets, + # yet still be able to expire them through the digest params. + config.assets.digest = true + + # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb + + # 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 + + # 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 = [ :subdomain, :uuid ] + + # Use a different logger for distributed setups. + # config.logger = ActiveSupport::TaggedLogging.new(SyslogLogger.new) + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = 'http://assets.example.com' + + # 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 + + # 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..1c19f08b2 --- /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 static file server for tests with Cache-Control for performance. + config.serve_static_files = true + config.static_cache_control = 'public, max-age=3600' + + # 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 + + # 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 + + # Randomize the order test cases are executed. + config.active_support.test_order = :random + + # 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/assets.rb b/config/initializers/assets.rb new file mode 100644 index 000000000..01ef3e663 --- /dev/null +++ b/config/initializers/assets.rb @@ -0,0 +1,11 @@ +# Be sure to restart your server when you modify this file. + +# Version of your assets, change this if you want to expire all your assets. +Rails.application.config.assets.version = '1.0' + +# Add additional assets to the asset load path +# Rails.application.config.assets.paths << Emoji.images_path + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in app/assets folder are already added. +# Rails.application.config.assets.precompile += %w( search.js ) 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/cookies_serializer.rb b/config/initializers/cookies_serializer.rb new file mode 100644 index 000000000..7f70458de --- /dev/null +++ b/config/initializers/cookies_serializer.rb @@ -0,0 +1,3 @@ +# Be sure to restart your server when you modify this file. + +Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/config/initializers/filter_parameter_logging.rb b/config/initializers/filter_parameter_logging.rb new file mode 100644 index 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/session_store.rb b/config/initializers/session_store.rb new file mode 100644 index 000000000..6fd486220 --- /dev/null +++ b/config/initializers/session_store.rb @@ -0,0 +1,3 @@ +# Be sure to restart your server when you modify this file. + +Rails.application.config.session_store :cookie_store, key: '_example-rails-challenge_session' diff --git a/config/initializers/to_time_preserves_timezone.rb b/config/initializers/to_time_preserves_timezone.rb new file mode 100644 index 000000000..8674be322 --- /dev/null +++ b/config/initializers/to_time_preserves_timezone.rb @@ -0,0 +1,10 @@ +# Be sure to restart your server when you modify this file. + +# Preserve the timezone of the receiver when calling to `to_time`. +# Ruby 2.4 will change the behavior of `to_time` to preserve the timezone +# when converting to an instance of `Time` instead of the previous behavior +# of converting to the local system timezone. +# +# Rails 5.0 introduced this config option so that apps made with earlier +# versions of Rails are not affected when upgrading. +ActiveSupport.to_time_preserves_timezone = true diff --git a/config/initializers/wrap_parameters.rb b/config/initializers/wrap_parameters.rb new file mode 100644 index 000000000..33725e95f --- /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] if respond_to?(:wrap_parameters) +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..065395716 --- /dev/null +++ b/config/locales/en.yml @@ -0,0 +1,23 @@ +# 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. +# +# 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/routes.rb b/config/routes.rb new file mode 100644 index 000000000..3f66539d5 --- /dev/null +++ b/config/routes.rb @@ -0,0 +1,56 @@ +Rails.application.routes.draw do + # The priority is based upon order of creation: first created -> highest priority. + # See how all your routes lay out with "rake routes". + + # You can have the root of your site routed with "root" + # root 'welcome#index' + + # Example of regular route: + # get 'products/:id' => 'catalog#view' + + # Example of named route that can be invoked with purchase_url(id: product.id) + # get 'products/:id/purchase' => 'catalog#purchase', as: :purchase + + # Example resource route (maps HTTP verbs to controller actions automatically): + # resources :products + + # Example resource route with options: + # resources :products do + # member do + # get 'short' + # post 'toggle' + # end + # + # collection do + # get 'sold' + # end + # end + + # Example resource route with sub-resources: + # resources :products do + # resources :comments, :sales + # resource :seller + # end + + # Example resource route with more complex sub-resources: + # resources :products do + # resources :comments + # resources :sales do + # get 'recent', on: :collection + # end + # end + + # Example resource route with concerns: + # concern :toggleable do + # post 'toggle' + # end + # resources :posts, concerns: :toggleable + # resources :photos, concerns: :toggleable + + # Example resource route within a namespace: + # namespace :admin do + # # Directs /admin/products/* to Admin::ProductsController + # # (app/controllers/admin/products_controller.rb) + # resources :products + # end +end diff --git a/config/secrets.yml b/config/secrets.yml new file mode 100644 index 000000000..dc8bacf7d --- /dev/null +++ b/config/secrets.yml @@ -0,0 +1,22 @@ +# 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 `rake secret` to generate a secure secret key. + +# Make sure the secrets in this file are kept private +# if you're sharing your code publicly. + +development: + secret_key_base: 186faadaacdacc23146b085002dcf96c8740bdd7da3dd8c1178f737b4be422f65e75f911bdbd5e437a359ba2bdf33f86cfc65308767ceffa47d038bf0bba9049 + +test: + secret_key_base: 3a105b0d9e71d8faaecd2b16e3e49f1dcde0bd71ba112170da6e6866ddee99d9d701844139728d40872edd31b95138f888bc1d74e19f42e7b72f58a35f7b9659 + +# Do not keep production secrets in the repository, +# instead read values from the environment. +production: + secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> diff --git a/db/seeds.rb b/db/seeds.rb new file mode 100644 index 000000000..4edb1e857 --- /dev/null +++ b/db/seeds.rb @@ -0,0 +1,7 @@ +# This file should contain all the record creation needed to seed the database with its default values. +# The data can then be loaded with the rake db:seed (or created alongside the db with db:setup). +# +# Examples: +# +# cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) +# Mayor.create(name: 'Emanuel', city: cities.first) diff --git a/lib/assets/.keep b/lib/assets/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/lib/tasks/.keep b/lib/tasks/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/public/404.html b/public/404.html new file mode 100644 index 000000000..b612547fc --- /dev/null +++ b/public/404.html @@ -0,0 +1,67 @@ + + + + The page you were looking for doesn't exist (404) + + + + + + +
+
+

The page you were looking for doesn't exist.

+

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

+
+

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

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

The change you wanted was rejected.

+

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

+
+

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

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

We're sorry, but something went wrong.

+
+

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

+
+ + diff --git a/public/favicon.ico b/public/favicon.ico new file mode 100644 index 000000000..e69de29bb diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 000000000..3c9c7c01f --- /dev/null +++ b/public/robots.txt @@ -0,0 +1,5 @@ +# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file +# +# To ban all spiders from the entire site uncomment the next two lines: +# User-agent: * +# Disallow: / 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/helpers/.keep b/test/helpers/.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..92e39b2d7 --- /dev/null +++ b/test/test_helper.rb @@ -0,0 +1,10 @@ +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +require 'rails/test_help' + +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/vendor/assets/javascripts/.keep b/vendor/assets/javascripts/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/vendor/assets/stylesheets/.keep b/vendor/assets/stylesheets/.keep new file mode 100644 index 000000000..e69de29bb From c8c055d30c1492a39d2e7186fdc203675c8219ce Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 10:12:21 -0600 Subject: [PATCH 02/12] Setting up simple_form & rspec --- .rbenv-gemsets | 2 + Gemfile | 8 +- Gemfile.lock | 85 ++++ app/views/layouts/application.html.erb | 8 + app/views/static/index.html.erb | 1 + config/initializers/simple_form.rb | 182 ++++++++ config/initializers/simple_form_bootstrap.rb | 439 +++++++++++++++++++ config/locales/simple_form.en.yml | 31 ++ config/routes.rb | 3 +- 9 files changed, 757 insertions(+), 2 deletions(-) create mode 100644 .rbenv-gemsets create mode 100644 app/views/static/index.html.erb create mode 100644 config/initializers/simple_form.rb create mode 100644 config/initializers/simple_form_bootstrap.rb create mode 100644 config/locales/simple_form.en.yml diff --git a/.rbenv-gemsets b/.rbenv-gemsets new file mode 100644 index 000000000..5cdec4b1b --- /dev/null +++ b/.rbenv-gemsets @@ -0,0 +1,2 @@ +g3ortega +-global \ No newline at end of file diff --git a/Gemfile b/Gemfile index c9ede23d1..188da10bf 100644 --- a/Gemfile +++ b/Gemfile @@ -13,11 +13,12 @@ gem 'uglifier', '>= 1.3.0' gem 'coffee-rails', '~> 4.1.0' # See https://github.com/rails/execjs#readme for more supported runtimes # gem 'therubyracer', platforms: :ruby - # Use jquery as the JavaScript library gem 'jquery-rails' # Turbolinks makes following links in your web application faster. Read more: https://github.com/rails/turbolinks gem 'turbolinks' + +gem 'simple_form' # Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder gem 'jbuilder', '~> 2.0' # bundle exec rake doc:rails generates the API under doc/api. @@ -35,6 +36,11 @@ gem 'sdoc', '~> 0.4.0', group: :doc group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' + gem 'rspec-rails', '3.9.1' + gem 'factory_bot', '5.2.0' + gem 'rubocop', '0.91.0' + gem 'guard-rspec', require: false + gem 'annotate' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index a2a910ebd..a5c2f711e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -35,11 +35,16 @@ GEM minitest (~> 5.1) thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) + annotate (3.1.1) + activerecord (>= 3.2, < 7.0) + rake (>= 10.4, < 14.0) arel (6.0.4) + ast (2.4.1) binding_of_caller (0.8.0) debug_inspector (>= 0.0.1) builder (3.2.4) byebug (11.1.3) + coderay (1.1.3) coffee-rails (4.1.1) coffee-script (>= 2.2.0) railties (>= 4.0.0, < 5.1.x) @@ -50,11 +55,29 @@ GEM concurrent-ruby (1.1.7) crass (1.0.6) debug_inspector (0.0.3) + diff-lcs (1.4.4) erubis (2.7.0) execjs (2.7.0) + factory_bot (5.2.0) + activesupport (>= 4.2.0) ffi (1.13.1) + formatador (0.2.5) globalid (0.4.2) activesupport (>= 4.2.0) + guard (2.16.2) + formatador (>= 0.2.4) + listen (>= 2.7, < 4.0) + lumberjack (>= 1.0.12, < 2.0) + nenv (~> 0.1) + notiffany (~> 0.0) + pry (>= 0.9.12) + shellany (~> 0.0) + thor (>= 0.18.1) + guard-compat (1.2.1) + guard-rspec (4.7.3) + guard (~> 2.1) + guard-compat (~> 1.1) + rspec (>= 2.99.0, < 4.0) i18n (0.9.5) concurrent-ruby (~> 1.0) jbuilder (2.9.1) @@ -64,16 +87,31 @@ GEM railties (>= 4.2.0) thor (>= 0.14, < 2.0) json (1.8.6) + listen (3.2.1) + rb-fsevent (~> 0.10, >= 0.10.3) + rb-inotify (~> 0.9, >= 0.9.10) loofah (2.7.0) crass (~> 1.0.2) nokogiri (>= 1.5.9) + lumberjack (1.2.8) mail (2.7.1) mini_mime (>= 0.1.1) + method_source (1.0.0) mini_mime (1.0.2) mini_portile2 (2.4.0) minitest (5.14.2) + nenv (0.3.0) nokogiri (1.10.10) mini_portile2 (~> 2.4.0) + notiffany (0.1.3) + nenv (~> 0.1) + shellany (~> 0.0) + parallel (1.19.2) + parser (2.7.1.4) + ast (~> 2.4.1) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) rack (1.6.13) rack-test (0.6.3) rack (>= 1.0) @@ -101,11 +139,47 @@ GEM activesupport (= 4.2.11.3) rake (>= 0.8.7) thor (>= 0.18.1, < 2.0) + rainbow (3.0.0) rake (13.0.1) rb-fsevent (0.10.4) rb-inotify (0.10.1) ffi (~> 1.0) rdoc (4.3.0) + regexp_parser (1.7.1) + rexml (3.2.4) + rspec (3.9.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-core (3.9.2) + rspec-support (~> 3.9.3) + rspec-expectations (3.9.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-mocks (3.9.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.9.0) + rspec-rails (3.9.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + railties (>= 3.0) + rspec-core (~> 3.9.0) + rspec-expectations (~> 3.9.0) + rspec-mocks (~> 3.9.0) + rspec-support (~> 3.9.0) + rspec-support (3.9.3) + rubocop (0.91.0) + parallel (~> 1.10) + parser (>= 2.7.1.1) + rainbow (>= 2.2.2, < 4.0) + regexp_parser (>= 1.7) + rexml + rubocop-ast (>= 0.4.0, < 1.0) + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 2.0) + rubocop-ast (0.4.0) + parser (>= 2.7.1.4) + ruby-progressbar (1.10.1) sass (3.7.4) sass-listen (~> 4.0.0) sass-listen (4.0.0) @@ -120,6 +194,10 @@ GEM sdoc (0.4.2) json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) + shellany (0.0.1) + simple_form (4.0.0) + actionpack (> 4) + activemodel (> 4) spring (2.1.1) sprockets (3.7.2) concurrent-ruby (~> 1.0) @@ -139,6 +217,7 @@ GEM thread_safe (~> 0.1) uglifier (4.2.0) execjs (>= 0.3.0, < 3) + unicode-display_width (1.7.0) web-console (2.3.0) activemodel (>= 4.0) binding_of_caller (>= 0.7.2) @@ -149,13 +228,19 @@ PLATFORMS ruby DEPENDENCIES + annotate byebug coffee-rails (~> 4.1.0) + factory_bot (= 5.2.0) + guard-rspec jbuilder (~> 2.0) jquery-rails rails (= 4.2.11.3) + rspec-rails (= 3.9.1) + rubocop (= 0.91.0) sass-rails (~> 5.0) sdoc (~> 0.4.0) + simple_form spring sqlite3 (~> 1.3.11) turbolinks diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 0b13f942e..4ec8a7265 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -2,6 +2,14 @@ ExampleRailsChallenge + + + + + + + + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> <%= csrf_meta_tags %> diff --git a/app/views/static/index.html.erb b/app/views/static/index.html.erb new file mode 100644 index 000000000..9a1eb8d21 --- /dev/null +++ b/app/views/static/index.html.erb @@ -0,0 +1 @@ +

Hello World

\ No newline at end of file diff --git a/config/initializers/simple_form.rb b/config/initializers/simple_form.rb new file mode 100644 index 000000000..2c5270384 --- /dev/null +++ b/config/initializers/simple_form.rb @@ -0,0 +1,182 @@ +# frozen_string_literal: true +# +# Uncomment this and change the path if necessary to include your own +# components. +# See https://github.com/plataformatec/simple_form#custom-components to know +# more about custom components. +# Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f } +# +# Use this setup block to configure all options available in SimpleForm. +SimpleForm.setup do |config| + # Wrappers are used by the form builder to generate a + # complete input. You can remove any component from the + # wrapper, change the order or even add your own to the + # stack. The options given below are used to wrap the + # whole input. + config.wrappers :default, class: :input, + hint_class: :field_with_hint, error_class: :field_with_errors, valid_class: :field_without_errors do |b| + ## Extensions enabled by default + # Any of these extensions can be disabled for a + # given input by passing: `f.input EXTENSION_NAME => false`. + # You can make any of these extensions optional by + # renaming `b.use` to `b.optional`. + + # Determines whether to use HTML5 (:email, :url, ...) + # and required attributes + b.use :html5 + + # Calculates placeholders automatically from I18n + # You can also pass a string as f.input placeholder: "Placeholder" + b.use :placeholder + + ## Optional extensions + # They are disabled unless you pass `f.input EXTENSION_NAME => true` + # to the input. If so, they will retrieve the values from the model + # if any exists. If you want to enable any of those + # extensions by default, you can change `b.optional` to `b.use`. + + # Calculates maxlength from length validations for string inputs + # and/or database column lengths + b.optional :maxlength + + # Calculate minlength from length validations for string inputs + b.optional :minlength + + # Calculates pattern from format validations for string inputs + b.optional :pattern + + # Calculates min and max from length validations for numeric inputs + b.optional :min_max + + # Calculates readonly automatically from readonly attributes + b.optional :readonly + + ## Inputs + # b.use :input, class: 'input', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :label_input + b.use :hint, wrap_with: { tag: :span, class: :hint } + b.use :error, wrap_with: { tag: :span, class: :error } + + ## full_messages_for + # If you want to display the full error message for the attribute, you can + # use the component :full_error, like: + # + # b.use :full_error, wrap_with: { tag: :span, class: :error } + end + + # The default wrapper to be used by the FormBuilder. + config.default_wrapper = :default + + # Define the way to render check boxes / radio buttons with labels. + # Defaults to :nested for bootstrap config. + # inline: input + label + # nested: label > input + config.boolean_style = :nested + + # Default class for buttons + config.button_class = 'btn' + + # Method used to tidy up errors. Specify any Rails Array method. + # :first lists the first message for each field. + # Use :to_sentence to list all errors for each field. + # config.error_method = :first + + # Default tag used for error notification helper. + config.error_notification_tag = :div + + # CSS class to add for error notification helper. + config.error_notification_class = 'error_notification' + + # ID to add for error notification helper. + # config.error_notification_id = nil + + # Series of attempts to detect a default label method for collection. + # config.collection_label_methods = [ :to_label, :name, :title, :to_s ] + + # Series of attempts to detect a default value method for collection. + # config.collection_value_methods = [ :id, :to_s ] + + # You can wrap a collection of radio/check boxes in a pre-defined tag, defaulting to none. + # config.collection_wrapper_tag = nil + + # You can define the class to use on all collection wrappers. Defaulting to none. + # config.collection_wrapper_class = nil + + # You can wrap each item in a collection of radio/check boxes with a tag, + # defaulting to :span. + # config.item_wrapper_tag = :span + + # You can define a class to use in all item wrappers. Defaulting to none. + # config.item_wrapper_class = nil + + # How the label text should be generated altogether with the required text. + # config.label_text = lambda { |label, required, explicit_label| "#{required} #{label}" } + + # You can define the class to use on all labels. Default is nil. + # config.label_class = nil + + # You can define the default class to be used on forms. Can be overriden + # with `html: { :class }`. Defaulting to none. + # config.default_form_class = nil + + # You can define which elements should obtain additional classes + # config.generate_additional_classes_for = [:wrapper, :label, :input] + + # Whether attributes are required by default (or not). Default is true. + # config.required_by_default = true + + # Tell browsers whether to use the native HTML5 validations (novalidate form option). + # These validations are enabled in SimpleForm's internal config but disabled by default + # in this configuration, which is recommended due to some quirks from different browsers. + # To stop SimpleForm from generating the novalidate option, enabling the HTML5 validations, + # change this configuration to true. + config.browser_validations = false + + # Collection of methods to detect if a file type was given. + # config.file_methods = [ :mounted_as, :file?, :public_filename :attached? ] + + # Custom mappings for input types. This should be a hash containing a regexp + # to match as key, and the input type that will be used when the field name + # matches the regexp as value. + # config.input_mappings = { /count/ => :integer } + + # Custom wrappers for input types. This should be a hash containing an input + # type as key and the wrapper that will be used for all inputs with specified type. + # config.wrapper_mappings = { string: :prepend } + + # Namespaces where SimpleForm should look for custom input classes that + # override default inputs. + # config.custom_inputs_namespaces << "CustomInputs" + + # Default priority for time_zone inputs. + # config.time_zone_priority = nil + + # Default priority for country inputs. + # config.country_priority = nil + + # When false, do not use translations for labels. + # config.translate_labels = true + + # Automatically discover new inputs in Rails' autoload path. + # config.inputs_discovery = true + + # Cache SimpleForm inputs discovery + # config.cache_discovery = !Rails.env.development? + + # Default class for inputs + # config.input_class = nil + + # Define the default class of the input wrapper of the boolean input. + config.boolean_label_class = 'checkbox' + + # Defines if the default input wrapper class should be included in radio + # collection wrappers. + # config.include_default_input_wrapper_class = true + + # Defines which i18n scope will be used in Simple Form. + # config.i18n_scope = 'simple_form' + + # Defines validation classes to the input_field. By default it's nil. + # config.input_field_valid_class = 'is-valid' + # config.input_field_error_class = 'is-invalid' +end diff --git a/config/initializers/simple_form_bootstrap.rb b/config/initializers/simple_form_bootstrap.rb new file mode 100644 index 000000000..0178fd53c --- /dev/null +++ b/config/initializers/simple_form_bootstrap.rb @@ -0,0 +1,439 @@ +# frozen_string_literal: true + +# Please do not make direct changes to this file! +# This generator is maintained by the community around simple_form-bootstrap: +# https://github.com/rafaelfranca/simple_form-bootstrap +# All future development, tests, and organization should happen there. +# Background history: https://github.com/plataformatec/simple_form/issues/1561 + +# Uncomment this and change the path if necessary to include your own +# components. +# See https://github.com/plataformatec/simple_form#custom-components +# to know more about custom components. +# Dir[Rails.root.join('lib/components/**/*.rb')].each { |f| require f } + +# Use this setup block to configure all options available in SimpleForm. +SimpleForm.setup do |config| + # Default class for buttons + config.button_class = 'btn' + + # Define the default class of the input wrapper of the boolean input. + config.boolean_label_class = 'form-check-label' + + # How the label text should be generated altogether with the required text. + config.label_text = lambda { |label, required, explicit_label| "#{label} #{required}" } + + # Define the way to render check boxes / radio buttons with labels. + config.boolean_style = :inline + + # You can wrap each item in a collection of radio/check boxes with a tag + config.item_wrapper_tag = :div + + # Defines if the default input wrapper class should be included in radio + # collection wrappers. + config.include_default_input_wrapper_class = false + + # CSS class to add for error notification helper. + config.error_notification_class = 'alert alert-danger' + + # Method used to tidy up errors. Specify any Rails Array method. + # :first lists the first message for each field. + # :to_sentence to list all errors for each field. + config.error_method = :to_sentence + + # add validation classes to `input_field` + config.input_field_error_class = 'is-invalid' + config.input_field_valid_class = 'is-valid' + + + # vertical forms + # + # vertical default_wrapper + config.wrappers :vertical_form, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'form-control-label' + b.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # vertical input for boolean + config.wrappers :vertical_boolean, tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper :form_check_wrapper, tag: 'div', class: 'form-check' do |bb| + bb.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' + bb.use :label, class: 'form-check-label' + bb.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + bb.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + # vertical input for radio buttons and check boxes + config.wrappers :vertical_collection, item_wrapper_class: 'form-check', tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper :legend_tag, tag: 'legend', class: 'col-form-label pt-0' do |ba| + ba.use :label_text + end + b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # vertical input for inline radio buttons and check boxes + config.wrappers :vertical_collection_inline, item_wrapper_class: 'form-check form-check-inline', tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper :legend_tag, tag: 'legend', class: 'col-form-label pt-0' do |ba| + ba.use :label_text + end + b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # vertical file input + config.wrappers :vertical_file, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :readonly + b.use :label + b.use :input, class: 'form-control-file', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # vertical multi select + config.wrappers :vertical_multi_select, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'form-control-label' + b.wrapper tag: 'div', class: 'd-flex flex-row justify-content-between align-items-center' do |ba| + ba.use :input, class: 'form-control mx-1', error_class: 'is-invalid', valid_class: 'is-valid' + end + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # vertical range input + config.wrappers :vertical_range, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :readonly + b.optional :step + b.use :label + b.use :input, class: 'form-control-range', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + + # horizontal forms + # + # horizontal default_wrapper + config.wrappers :horizontal_form, tag: 'div', class: 'form-group row', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'col-sm-3 col-form-label' + b.wrapper :grid_wrapper, tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: 'is-valid' + ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + ba.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + # horizontal input for boolean + config.wrappers :horizontal_boolean, tag: 'div', class: 'form-group row', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper tag: 'label', class: 'col-sm-3' do |ba| + ba.use :label_text + end + b.wrapper :grid_wrapper, tag: 'div', class: 'col-sm-9' do |wr| + wr.wrapper :form_check_wrapper, tag: 'div', class: 'form-check' do |bb| + bb.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' + bb.use :label, class: 'form-check-label' + bb.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + bb.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + end + + # horizontal input for radio buttons and check boxes + config.wrappers :horizontal_collection, item_wrapper_class: 'form-check', tag: 'div', class: 'form-group row', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'col-sm-3 form-control-label' + b.wrapper :grid_wrapper, tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' + ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + ba.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + # horizontal input for inline radio buttons and check boxes + config.wrappers :horizontal_collection_inline, item_wrapper_class: 'form-check form-check-inline', tag: 'div', class: 'form-group row', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'col-sm-3 form-control-label' + b.wrapper :grid_wrapper, tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' + ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + ba.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + # horizontal file input + config.wrappers :horizontal_file, tag: 'div', class: 'form-group row', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :readonly + b.use :label, class: 'col-sm-3 form-control-label' + b.wrapper :grid_wrapper, tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input, error_class: 'is-invalid', valid_class: 'is-valid' + ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + ba.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + # horizontal multi select + config.wrappers :horizontal_multi_select, tag: 'div', class: 'form-group row', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'col-sm-3 control-label' + b.wrapper :grid_wrapper, tag: 'div', class: 'col-sm-9' do |ba| + ba.wrapper tag: 'div', class: 'd-flex flex-row justify-content-between align-items-center' do |bb| + bb.use :input, class: 'form-control mx-1', error_class: 'is-invalid', valid_class: 'is-valid' + end + ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + ba.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + # horizontal range input + config.wrappers :horizontal_range, tag: 'div', class: 'form-group row', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :readonly + b.optional :step + b.use :label, class: 'col-sm-3 form-control-label' + b.wrapper :grid_wrapper, tag: 'div', class: 'col-sm-9' do |ba| + ba.use :input, class: 'form-control-range', error_class: 'is-invalid', valid_class: 'is-valid' + ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + ba.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + + # inline forms + # + # inline default_wrapper + config.wrappers :inline_form, tag: 'span', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :label, class: 'sr-only' + + b.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + b.optional :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # inline input for boolean + config.wrappers :inline_boolean, tag: 'span', class: 'form-check flex-wrap justify-content-start mr-sm-2', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.use :input, class: 'form-check-input', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :label, class: 'form-check-label' + b.use :error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + b.optional :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + + # bootstrap custom forms + # + # custom input for boolean + config.wrappers :custom_boolean, tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper :form_check_wrapper, tag: 'div', class: 'custom-control custom-checkbox' do |bb| + bb.use :input, class: 'custom-control-input', error_class: 'is-invalid', valid_class: 'is-valid' + bb.use :label, class: 'custom-control-label' + bb.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + bb.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + config.wrappers :custom_boolean_switch, tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper :form_check_wrapper, tag: 'div', class: 'custom-control custom-checkbox-switch' do |bb| + bb.use :input, class: 'custom-control-input', error_class: 'is-invalid', valid_class: 'is-valid' + bb.use :label, class: 'custom-control-label' + bb.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + bb.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + end + + # custom input for radio buttons and check boxes + config.wrappers :custom_collection, item_wrapper_class: 'custom-control', tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper :legend_tag, tag: 'legend', class: 'col-form-label pt-0' do |ba| + ba.use :label_text + end + b.use :input, class: 'custom-control-input', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # custom input for inline radio buttons and check boxes + config.wrappers :custom_collection_inline, item_wrapper_class: 'custom-control custom-control-inline', tag: 'fieldset', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.wrapper :legend_tag, tag: 'legend', class: 'col-form-label pt-0' do |ba| + ba.use :label_text + end + b.use :input, class: 'custom-control-input', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # custom file input + config.wrappers :custom_file, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :readonly + b.use :label, class: 'form-control-label' + b.wrapper :custom_file_wrapper, tag: 'div', class: 'custom-file' do |ba| + ba.use :input, class: 'custom-file-input', error_class: 'is-invalid', valid_class: 'is-valid' + ba.use :label, class: 'custom-file-label' + ba.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + end + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # custom multi select + config.wrappers :custom_multi_select, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.use :label, class: 'form-control-label' + b.wrapper tag: 'div', class: 'd-flex flex-row justify-content-between align-items-center' do |ba| + ba.use :input, class: 'custom-select mx-1', error_class: 'is-invalid', valid_class: 'is-valid' + end + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # custom range input + config.wrappers :custom_range, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :readonly + b.optional :step + b.use :label, class: 'form-control-label' + b.use :input, class: 'custom-range', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + + # Input Group - custom component + # see example app and config at https://github.com/rafaelfranca/simple_form-bootstrap + # config.wrappers :input_group, tag: 'div', class: 'form-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + # b.use :html5 + # b.use :placeholder + # b.optional :maxlength + # b.optional :minlength + # b.optional :pattern + # b.optional :min_max + # b.optional :readonly + # b.use :label, class: 'form-control-label' + # b.wrapper :input_group_tag, tag: 'div', class: 'input-group' do |ba| + # ba.optional :prepend + # ba.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: 'is-valid' + # ba.optional :append + # end + # b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback d-block' } + # b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + # end + + + # Floating Labels form + # + # floating labels default_wrapper + config.wrappers :floating_labels_form, tag: 'div', class: 'form-label-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.use :placeholder + b.optional :maxlength + b.optional :minlength + b.optional :pattern + b.optional :min_max + b.optional :readonly + b.use :input, class: 'form-control', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :label, class: 'form-control-label' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + # custom multi select + config.wrappers :floating_labels_select, tag: 'div', class: 'form-label-group', error_class: 'form-group-invalid', valid_class: 'form-group-valid' do |b| + b.use :html5 + b.optional :readonly + b.use :input, class: 'custom-select custom-select-lg', error_class: 'is-invalid', valid_class: 'is-valid' + b.use :label, class: 'form-control-label' + b.use :full_error, wrap_with: { tag: 'div', class: 'invalid-feedback' } + b.use :hint, wrap_with: { tag: 'small', class: 'form-text text-muted' } + end + + + # The default wrapper to be used by the FormBuilder. + config.default_wrapper = :vertical_form + + # Custom wrappers for input types. This should be a hash containing an input + # type as key and the wrapper that will be used for all inputs with specified type. + config.wrapper_mappings = { + boolean: :vertical_boolean, + check_boxes: :vertical_collection, + date: :vertical_multi_select, + datetime: :vertical_multi_select, + file: :vertical_file, + radio_buttons: :vertical_collection, + range: :vertical_range, + time: :vertical_multi_select + } + + # enable custom form wrappers + # config.wrapper_mappings = { + # boolean: :custom_boolean, + # check_boxes: :custom_collection, + # date: :custom_multi_select, + # datetime: :custom_multi_select, + # file: :custom_file, + # radio_buttons: :custom_collection, + # range: :custom_range, + # time: :custom_multi_select + # } +end diff --git a/config/locales/simple_form.en.yml b/config/locales/simple_form.en.yml new file mode 100644 index 000000000..237438334 --- /dev/null +++ b/config/locales/simple_form.en.yml @@ -0,0 +1,31 @@ +en: + simple_form: + "yes": 'Yes' + "no": 'No' + required: + text: 'required' + mark: '*' + # You can uncomment the line below if you need to overwrite the whole required html. + # When using html, text and mark won't be used. + # html: '*' + error_notification: + default_message: "Please review the problems below:" + # Examples + # labels: + # defaults: + # password: 'Password' + # user: + # new: + # email: 'E-mail to sign in.' + # edit: + # email: 'E-mail.' + # hints: + # defaults: + # username: 'User name to sign in.' + # password: 'No special characters, please.' + # include_blanks: + # defaults: + # age: 'Rather not say' + # prompts: + # defaults: + # age: 'Select your age' diff --git a/config/routes.rb b/config/routes.rb index 3f66539d5..a42e75cc5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,9 +1,10 @@ Rails.application.routes.draw do + resources :customers # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". # You can have the root of your site routed with "root" - # root 'welcome#index' + root 'static#index' # Example of regular route: # get 'products/:id' => 'catalog#view' From 0384a2266c3cad0d53d1a81a3266b1cab59e1248 Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 10:12:38 -0600 Subject: [PATCH 03/12] Customers generated --- app/assets/javascripts/customers.coffee | 3 + app/assets/stylesheets/customers.scss | 3 + app/assets/stylesheets/scaffolds.scss | 73 +++++++++ app/controllers/customers_controller.rb | 74 +++++++++ app/helpers/customers_helper.rb | 2 + app/models/customer.rb | 2 + app/views/customers/_customer.json.jbuilder | 2 + app/views/customers/_form.html.erb | 14 ++ app/views/customers/edit.html.erb | 6 + app/views/customers/index.html.erb | 29 ++++ app/views/customers/index.json.jbuilder | 1 + app/views/customers/new.html.erb | 5 + app/views/customers/show.html.erb | 14 ++ app/views/customers/show.json.jbuilder | 1 + db/migrate/20200915161058_create_customers.rb | 10 ++ db/schema.rb | 23 +++ lib/templates/erb/scaffold/_form.html.erb | 15 ++ spec/controllers/customers_controller_spec.rb | 141 ++++++++++++++++++ spec/helpers/customers_helper_spec.rb | 15 ++ spec/models/customer_spec.rb | 5 + spec/requests/customers_spec.rb | 10 ++ spec/routing/customers_routing_spec.rb | 38 +++++ spec/views/customers/edit.html.erb_spec.rb | 21 +++ spec/views/customers/index.html.erb_spec.rb | 22 +++ spec/views/customers/new.html.erb_spec.rb | 21 +++ spec/views/customers/show.html.erb_spec.rb | 16 ++ 26 files changed, 566 insertions(+) create mode 100644 app/assets/javascripts/customers.coffee create mode 100644 app/assets/stylesheets/customers.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/customers_controller.rb create mode 100644 app/helpers/customers_helper.rb create mode 100644 app/models/customer.rb create mode 100644 app/views/customers/_customer.json.jbuilder create mode 100644 app/views/customers/_form.html.erb create mode 100644 app/views/customers/edit.html.erb create mode 100644 app/views/customers/index.html.erb create mode 100644 app/views/customers/index.json.jbuilder create mode 100644 app/views/customers/new.html.erb create mode 100644 app/views/customers/show.html.erb create mode 100644 app/views/customers/show.json.jbuilder create mode 100644 db/migrate/20200915161058_create_customers.rb create mode 100644 db/schema.rb create mode 100644 lib/templates/erb/scaffold/_form.html.erb create mode 100644 spec/controllers/customers_controller_spec.rb create mode 100644 spec/helpers/customers_helper_spec.rb create mode 100644 spec/models/customer_spec.rb create mode 100644 spec/requests/customers_spec.rb create mode 100644 spec/routing/customers_routing_spec.rb create mode 100644 spec/views/customers/edit.html.erb_spec.rb create mode 100644 spec/views/customers/index.html.erb_spec.rb create mode 100644 spec/views/customers/new.html.erb_spec.rb create mode 100644 spec/views/customers/show.html.erb_spec.rb diff --git a/app/assets/javascripts/customers.coffee b/app/assets/javascripts/customers.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/app/assets/javascripts/customers.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/customers.scss b/app/assets/stylesheets/customers.scss new file mode 100644 index 000000000..b6c7ce5eb --- /dev/null +++ b/app/assets/stylesheets/customers.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Customers controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 000000000..ed7a765d9 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,73 @@ +body { + background-color: #fff; + color: #333; + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; +} + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; +} + +a { + color: #000; + + &:visited { + color: #666; + } + + &:hover { + color: #fff; + background-color: #000; + } +} + +div { + &.field, &.actions { + margin-bottom: 10px; + } +} + +#notice { + color: green; +} + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; +} + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px; + padding-bottom: 0; + margin-bottom: 20px; + background-color: #f0f0f0; + + h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px; + margin-bottom: 0px; + background-color: #c00; + color: #fff; + } + + ul li { + font-size: 12px; + list-style: square; + } +} diff --git a/app/controllers/customers_controller.rb b/app/controllers/customers_controller.rb new file mode 100644 index 000000000..c5cf7e989 --- /dev/null +++ b/app/controllers/customers_controller.rb @@ -0,0 +1,74 @@ +class CustomersController < ApplicationController + before_action :set_customer, only: [:show, :edit, :update, :destroy] + + # GET /customers + # GET /customers.json + def index + @customers = Customer.all + end + + # GET /customers/1 + # GET /customers/1.json + def show + end + + # GET /customers/new + def new + @customer = Customer.new + end + + # GET /customers/1/edit + def edit + end + + # POST /customers + # POST /customers.json + def create + @customer = Customer.new(customer_params) + + respond_to do |format| + if @customer.save + format.html { redirect_to @customer, notice: 'Customer was successfully created.' } + format.json { render :show, status: :created, location: @customer } + else + format.html { render :new } + format.json { render json: @customer.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /customers/1 + # PATCH/PUT /customers/1.json + def update + respond_to do |format| + if @customer.update(customer_params) + format.html { redirect_to @customer, notice: 'Customer was successfully updated.' } + format.json { render :show, status: :ok, location: @customer } + else + format.html { render :edit } + format.json { render json: @customer.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /customers/1 + # DELETE /customers/1.json + def destroy + @customer.destroy + respond_to do |format| + format.html { redirect_to customers_url, notice: 'Customer was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_customer + @customer = Customer.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def customer_params + params.require(:customer).permit(:first_name, :last_name) + end +end diff --git a/app/helpers/customers_helper.rb b/app/helpers/customers_helper.rb new file mode 100644 index 000000000..a07ce2943 --- /dev/null +++ b/app/helpers/customers_helper.rb @@ -0,0 +1,2 @@ +module CustomersHelper +end diff --git a/app/models/customer.rb b/app/models/customer.rb new file mode 100644 index 000000000..047dcb0c0 --- /dev/null +++ b/app/models/customer.rb @@ -0,0 +1,2 @@ +class Customer < ActiveRecord::Base +end diff --git a/app/views/customers/_customer.json.jbuilder b/app/views/customers/_customer.json.jbuilder new file mode 100644 index 000000000..9f2b36660 --- /dev/null +++ b/app/views/customers/_customer.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! customer, :id, :first_name, :last_name, :created_at, :updated_at +json.url customer_url(customer, format: :json) diff --git a/app/views/customers/_form.html.erb b/app/views/customers/_form.html.erb new file mode 100644 index 000000000..590db84a2 --- /dev/null +++ b/app/views/customers/_form.html.erb @@ -0,0 +1,14 @@ + +<%= simple_form_for(@customer) do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +
+ <%= f.input :first_name %> + <%= f.input :last_name %> +
+ +
+ <%= f.button :submit %> +
+<% end %> diff --git a/app/views/customers/edit.html.erb b/app/views/customers/edit.html.erb new file mode 100644 index 000000000..11a41c26e --- /dev/null +++ b/app/views/customers/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Customer

+ +<%= render 'form' %> + +<%= link_to 'Show', @customer %> | +<%= link_to 'Back', customers_path %> diff --git a/app/views/customers/index.html.erb b/app/views/customers/index.html.erb new file mode 100644 index 000000000..f77c27f8b --- /dev/null +++ b/app/views/customers/index.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

Listing Customers

+ + + + + + + + + + + + <% @customers.each do |customer| %> + + + + + + + + <% end %> + +
First nameLast name
<%= customer.first_name %><%= customer.last_name %><%= link_to 'Show', customer %><%= link_to 'Edit', edit_customer_path(customer) %><%= link_to 'Destroy', customer, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Customer', new_customer_path %> diff --git a/app/views/customers/index.json.jbuilder b/app/views/customers/index.json.jbuilder new file mode 100644 index 000000000..328edbd7a --- /dev/null +++ b/app/views/customers/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @customers, partial: "customers/customer", as: :customer diff --git a/app/views/customers/new.html.erb b/app/views/customers/new.html.erb new file mode 100644 index 000000000..10ca8eeb1 --- /dev/null +++ b/app/views/customers/new.html.erb @@ -0,0 +1,5 @@ +

New Customer

+ +<%= render 'form' %> + +<%= link_to 'Back', customers_path %> diff --git a/app/views/customers/show.html.erb b/app/views/customers/show.html.erb new file mode 100644 index 000000000..cacd47343 --- /dev/null +++ b/app/views/customers/show.html.erb @@ -0,0 +1,14 @@ +

<%= notice %>

+ +

+ First name: + <%= @customer.first_name %> +

+ +

+ Last name: + <%= @customer.last_name %> +

+ +<%= link_to 'Edit', edit_customer_path(@customer) %> | +<%= link_to 'Back', customers_path %> diff --git a/app/views/customers/show.json.jbuilder b/app/views/customers/show.json.jbuilder new file mode 100644 index 000000000..14cfc837e --- /dev/null +++ b/app/views/customers/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "customers/customer", customer: @customer diff --git a/db/migrate/20200915161058_create_customers.rb b/db/migrate/20200915161058_create_customers.rb new file mode 100644 index 000000000..4a0ac76c1 --- /dev/null +++ b/db/migrate/20200915161058_create_customers.rb @@ -0,0 +1,10 @@ +class CreateCustomers < ActiveRecord::Migration + def change + create_table :customers do |t| + t.string :first_name + t.string :last_name + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 000000000..b14dc897a --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,23 @@ +# encoding: UTF-8 +# 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: 20200915161058) do + + create_table "customers", force: :cascade do |t| + t.string "first_name" + t.string "last_name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/lib/templates/erb/scaffold/_form.html.erb b/lib/templates/erb/scaffold/_form.html.erb new file mode 100644 index 000000000..106b71eef --- /dev/null +++ b/lib/templates/erb/scaffold/_form.html.erb @@ -0,0 +1,15 @@ +<%# frozen_string_literal: true %> +<%%= simple_form_for(@<%= singular_table_name %>) do |f| %> + <%%= f.error_notification %> + <%%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +
+ <%- attributes.each do |attribute| -%> + <%%= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %> %> + <%- end -%> +
+ +
+ <%%= f.button :submit %> +
+<%% end %> diff --git a/spec/controllers/customers_controller_spec.rb b/spec/controllers/customers_controller_spec.rb new file mode 100644 index 000000000..60980a9d4 --- /dev/null +++ b/spec/controllers/customers_controller_spec.rb @@ -0,0 +1,141 @@ +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. +# +# Also compared to earlier versions of this generator, there are no longer any +# expectations of assigns and templates rendered. These features have been +# removed from Rails core in Rails 5, but can be added back in via the +# `rails-controller-testing` gem. + +RSpec.describe CustomersController, type: :controller do + + # This should return the minimal set of attributes required to create a valid + # Customer. As you add validations to Customer, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # CustomersController. Be sure to keep this updated too. + let(:valid_session) { {} } + + describe "GET #index" do + it "returns a success response" do + Customer.create! valid_attributes + get :index, {}, valid_session + expect(response).to be_successful + end + end + + describe "GET #show" do + it "returns a success response" do + customer = Customer.create! valid_attributes + get :show, {:id => customer.to_param}, valid_session + expect(response).to be_successful + end + end + + describe "GET #new" do + it "returns a success response" do + get :new, {}, valid_session + expect(response).to be_successful + end + end + + describe "GET #edit" do + it "returns a success response" do + customer = Customer.create! valid_attributes + get :edit, {:id => customer.to_param}, valid_session + expect(response).to be_successful + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new Customer" do + expect { + post :create, {:customer => valid_attributes}, valid_session + }.to change(Customer, :count).by(1) + end + + it "redirects to the created customer" do + post :create, {:customer => valid_attributes}, valid_session + expect(response).to redirect_to(Customer.last) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'new' template)" do + post :create, {:customer => invalid_attributes}, valid_session + expect(response).to be_successful + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested customer" do + customer = Customer.create! valid_attributes + put :update, {:id => customer.to_param, :customer => new_attributes}, valid_session + customer.reload + skip("Add assertions for updated state") + end + + it "redirects to the customer" do + customer = Customer.create! valid_attributes + put :update, {:id => customer.to_param, :customer => valid_attributes}, valid_session + expect(response).to redirect_to(customer) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'edit' template)" do + customer = Customer.create! valid_attributes + put :update, {:id => customer.to_param, :customer => invalid_attributes}, valid_session + expect(response).to be_successful + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested customer" do + customer = Customer.create! valid_attributes + expect { + delete :destroy, {:id => customer.to_param}, valid_session + }.to change(Customer, :count).by(-1) + end + + it "redirects to the customers list" do + customer = Customer.create! valid_attributes + delete :destroy, {:id => customer.to_param}, valid_session + expect(response).to redirect_to(customers_url) + end + end + +end diff --git a/spec/helpers/customers_helper_spec.rb b/spec/helpers/customers_helper_spec.rb new file mode 100644 index 000000000..fa160374f --- /dev/null +++ b/spec/helpers/customers_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the CustomersHelper. For example: +# +# describe CustomersHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe CustomersHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/customer_spec.rb b/spec/models/customer_spec.rb new file mode 100644 index 000000000..3399b1f56 --- /dev/null +++ b/spec/models/customer_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Customer, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/customers_spec.rb b/spec/requests/customers_spec.rb new file mode 100644 index 000000000..081166fb1 --- /dev/null +++ b/spec/requests/customers_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe "Customers", type: :request do + describe "GET /customers" do + it "works! (now write some real specs)" do + get customers_path + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/routing/customers_routing_spec.rb b/spec/routing/customers_routing_spec.rb new file mode 100644 index 000000000..622cd626a --- /dev/null +++ b/spec/routing/customers_routing_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +RSpec.describe CustomersController, type: :routing do + describe "routing" do + it "routes to #index" do + expect(:get => "/customers").to route_to("customers#index") + end + + it "routes to #new" do + expect(:get => "/customers/new").to route_to("customers#new") + end + + it "routes to #show" do + expect(:get => "/customers/1").to route_to("customers#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/customers/1/edit").to route_to("customers#edit", :id => "1") + end + + + it "routes to #create" do + expect(:post => "/customers").to route_to("customers#create") + end + + it "routes to #update via PUT" do + expect(:put => "/customers/1").to route_to("customers#update", :id => "1") + end + + it "routes to #update via PATCH" do + expect(:patch => "/customers/1").to route_to("customers#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/customers/1").to route_to("customers#destroy", :id => "1") + end + end +end diff --git a/spec/views/customers/edit.html.erb_spec.rb b/spec/views/customers/edit.html.erb_spec.rb new file mode 100644 index 000000000..35f127c2c --- /dev/null +++ b/spec/views/customers/edit.html.erb_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe "customers/edit", type: :view do + before(:each) do + @customer = assign(:customer, Customer.create!( + :first_name => "MyString", + :last_name => "MyString" + )) + end + + it "renders the edit customer form" do + render + + assert_select "form[action=?][method=?]", customer_path(@customer), "post" do + + assert_select "input#customer_first_name[name=?]", "customer[first_name]" + + assert_select "input#customer_last_name[name=?]", "customer[last_name]" + end + end +end diff --git a/spec/views/customers/index.html.erb_spec.rb b/spec/views/customers/index.html.erb_spec.rb new file mode 100644 index 000000000..76a42155b --- /dev/null +++ b/spec/views/customers/index.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "customers/index", type: :view do + before(:each) do + assign(:customers, [ + Customer.create!( + :first_name => "First Name", + :last_name => "Last Name" + ), + Customer.create!( + :first_name => "First Name", + :last_name => "Last Name" + ) + ]) + end + + it "renders a list of customers" do + render + assert_select "tr>td", :text => "First Name".to_s, :count => 2 + assert_select "tr>td", :text => "Last Name".to_s, :count => 2 + end +end diff --git a/spec/views/customers/new.html.erb_spec.rb b/spec/views/customers/new.html.erb_spec.rb new file mode 100644 index 000000000..bb26fd937 --- /dev/null +++ b/spec/views/customers/new.html.erb_spec.rb @@ -0,0 +1,21 @@ +require 'rails_helper' + +RSpec.describe "customers/new", type: :view do + before(:each) do + assign(:customer, Customer.new( + :first_name => "MyString", + :last_name => "MyString" + )) + end + + it "renders new customer form" do + render + + assert_select "form[action=?][method=?]", customers_path, "post" do + + assert_select "input#customer_first_name[name=?]", "customer[first_name]" + + assert_select "input#customer_last_name[name=?]", "customer[last_name]" + end + end +end diff --git a/spec/views/customers/show.html.erb_spec.rb b/spec/views/customers/show.html.erb_spec.rb new file mode 100644 index 000000000..28ce03fd9 --- /dev/null +++ b/spec/views/customers/show.html.erb_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +RSpec.describe "customers/show", type: :view do + before(:each) do + @customer = assign(:customer, Customer.create!( + :first_name => "First Name", + :last_name => "Last Name" + )) + end + + it "renders attributes in

" do + render + expect(rendered).to match(/First Name/) + expect(rendered).to match(/Last Name/) + end +end From 69672d488600924a68ebf10ea093b5445bdc42b2 Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 10:19:57 -0600 Subject: [PATCH 04/12] Adding charges --- app/assets/javascripts/charges.coffee | 3 + app/assets/stylesheets/charges.scss | 3 + app/assets/stylesheets/scaffolds.scss | 73 ---------- app/controllers/charges_controller.rb | 74 ++++++++++ app/helpers/charges_helper.rb | 2 + app/models/charge.rb | 16 +++ app/models/customer.rb | 14 ++ app/views/charges/_charge.json.jbuilder | 2 + app/views/charges/_form.html.erb | 17 +++ app/views/charges/edit.html.erb | 6 + app/views/charges/index.html.erb | 35 +++++ app/views/charges/index.json.jbuilder | 1 + app/views/charges/new.html.erb | 5 + app/views/charges/show.html.erb | 29 ++++ app/views/charges/show.json.jbuilder | 1 + config/routes.rb | 1 + db/migrate/20200915161434_create_charges.rb | 13 ++ db/schema.rb | 14 +- spec/controllers/charges_controller_spec.rb | 141 ++++++++++++++++++++ spec/helpers/charges_helper_spec.rb | 15 +++ spec/models/charge_spec.rb | 18 +++ spec/models/customer_spec.rb | 10 ++ spec/requests/charges_spec.rb | 10 ++ spec/routing/charges_routing_spec.rb | 38 ++++++ spec/views/charges/edit.html.erb_spec.rb | 30 +++++ spec/views/charges/index.html.erb_spec.rb | 31 +++++ spec/views/charges/new.html.erb_spec.rb | 30 +++++ spec/views/charges/show.html.erb_spec.rb | 22 +++ 28 files changed, 580 insertions(+), 74 deletions(-) create mode 100644 app/assets/javascripts/charges.coffee create mode 100644 app/assets/stylesheets/charges.scss delete mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/charges_controller.rb create mode 100644 app/helpers/charges_helper.rb create mode 100644 app/models/charge.rb create mode 100644 app/views/charges/_charge.json.jbuilder create mode 100644 app/views/charges/_form.html.erb create mode 100644 app/views/charges/edit.html.erb create mode 100644 app/views/charges/index.html.erb create mode 100644 app/views/charges/index.json.jbuilder create mode 100644 app/views/charges/new.html.erb create mode 100644 app/views/charges/show.html.erb create mode 100644 app/views/charges/show.json.jbuilder create mode 100644 db/migrate/20200915161434_create_charges.rb create mode 100644 spec/controllers/charges_controller_spec.rb create mode 100644 spec/helpers/charges_helper_spec.rb create mode 100644 spec/models/charge_spec.rb create mode 100644 spec/requests/charges_spec.rb create mode 100644 spec/routing/charges_routing_spec.rb create mode 100644 spec/views/charges/edit.html.erb_spec.rb create mode 100644 spec/views/charges/index.html.erb_spec.rb create mode 100644 spec/views/charges/new.html.erb_spec.rb create mode 100644 spec/views/charges/show.html.erb_spec.rb diff --git a/app/assets/javascripts/charges.coffee b/app/assets/javascripts/charges.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/app/assets/javascripts/charges.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/charges.scss b/app/assets/stylesheets/charges.scss new file mode 100644 index 000000000..c3a893042 --- /dev/null +++ b/app/assets/stylesheets/charges.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Charges controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss deleted file mode 100644 index ed7a765d9..000000000 --- a/app/assets/stylesheets/scaffolds.scss +++ /dev/null @@ -1,73 +0,0 @@ -body { - background-color: #fff; - color: #333; - font-family: verdana, arial, helvetica, sans-serif; - font-size: 13px; - line-height: 18px; -} - -p, ol, ul, td { - font-family: verdana, arial, helvetica, sans-serif; - font-size: 13px; - line-height: 18px; -} - -pre { - background-color: #eee; - padding: 10px; - font-size: 11px; -} - -a { - color: #000; - - &:visited { - color: #666; - } - - &:hover { - color: #fff; - background-color: #000; - } -} - -div { - &.field, &.actions { - margin-bottom: 10px; - } -} - -#notice { - color: green; -} - -.field_with_errors { - padding: 2px; - background-color: red; - display: table; -} - -#error_explanation { - width: 450px; - border: 2px solid red; - padding: 7px; - padding-bottom: 0; - margin-bottom: 20px; - background-color: #f0f0f0; - - h2 { - text-align: left; - font-weight: bold; - padding: 5px 5px 5px 15px; - font-size: 12px; - margin: -7px; - margin-bottom: 0px; - background-color: #c00; - color: #fff; - } - - ul li { - font-size: 12px; - list-style: square; - } -} diff --git a/app/controllers/charges_controller.rb b/app/controllers/charges_controller.rb new file mode 100644 index 000000000..b7454206e --- /dev/null +++ b/app/controllers/charges_controller.rb @@ -0,0 +1,74 @@ +class ChargesController < ApplicationController + before_action :set_charge, only: [:show, :edit, :update, :destroy] + + # GET /charges + # GET /charges.json + def index + @charges = Charge.all + end + + # GET /charges/1 + # GET /charges/1.json + def show + end + + # GET /charges/new + def new + @charge = Charge.new + end + + # GET /charges/1/edit + def edit + end + + # POST /charges + # POST /charges.json + def create + @charge = Charge.new(charge_params) + + respond_to do |format| + if @charge.save + format.html { redirect_to @charge, notice: 'Charge was successfully created.' } + format.json { render :show, status: :created, location: @charge } + else + format.html { render :new } + format.json { render json: @charge.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /charges/1 + # PATCH/PUT /charges/1.json + def update + respond_to do |format| + if @charge.update(charge_params) + format.html { redirect_to @charge, notice: 'Charge was successfully updated.' } + format.json { render :show, status: :ok, location: @charge } + else + format.html { render :edit } + format.json { render json: @charge.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /charges/1 + # DELETE /charges/1.json + def destroy + @charge.destroy + respond_to do |format| + format.html { redirect_to charges_url, notice: 'Charge was successfully destroyed.' } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_charge + @charge = Charge.find(params[:id]) + end + + # Never trust parameters from the scary internet, only allow the white list through. + def charge_params + params.require(:charge).permit(:customer_id, :paid, :amount, :currency, :refunded) + end +end diff --git a/app/helpers/charges_helper.rb b/app/helpers/charges_helper.rb new file mode 100644 index 000000000..414ee900f --- /dev/null +++ b/app/helpers/charges_helper.rb @@ -0,0 +1,2 @@ +module ChargesHelper +end diff --git a/app/models/charge.rb b/app/models/charge.rb new file mode 100644 index 000000000..9f0fd5fb7 --- /dev/null +++ b/app/models/charge.rb @@ -0,0 +1,16 @@ +# == Schema Information +# +# Table name: charges +# +# id :integer not null, primary key +# customer_id :integer +# paid :boolean +# amount :float +# currency :string +# refunded :boolean +# created_at :datetime not null +# updated_at :datetime not null +# +class Charge < ActiveRecord::Base + belongs_to :customer +end diff --git a/app/models/customer.rb b/app/models/customer.rb index 047dcb0c0..ade45bc64 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -1,2 +1,16 @@ +# == Schema Information +# +# Table name: customers +# +# id :integer not null, primary key +# first_name :string +# last_name :string +# created_at :datetime not null +# updated_at :datetime not null +# class Customer < ActiveRecord::Base + + def full_name + "#{first_name} #{last_name}" + end end diff --git a/app/views/charges/_charge.json.jbuilder b/app/views/charges/_charge.json.jbuilder new file mode 100644 index 000000000..254a252ff --- /dev/null +++ b/app/views/charges/_charge.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! charge, :id, :customer_id, :paid, :amount, :currency, :refunded, :created_at, :updated_at +json.url charge_url(charge, format: :json) diff --git a/app/views/charges/_form.html.erb b/app/views/charges/_form.html.erb new file mode 100644 index 000000000..fabce43fe --- /dev/null +++ b/app/views/charges/_form.html.erb @@ -0,0 +1,17 @@ + +<%= simple_form_for(@charge) do |f| %> + <%= f.error_notification %> + <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %> + +

+ <%= f.association :customer, label_method: :full_name %> + <%= f.input :paid %> + <%= f.input :amount %> + <%= f.input :currency %> + <%= f.input :refunded %> +
+ +
+ <%= f.button :submit, class: 'btn btn-primary' %> +
+<% end %> diff --git a/app/views/charges/edit.html.erb b/app/views/charges/edit.html.erb new file mode 100644 index 000000000..8ed23b109 --- /dev/null +++ b/app/views/charges/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Charge

+ +<%= render 'form' %> + +<%= link_to 'Show', @charge %> | +<%= link_to 'Back', charges_path %> diff --git a/app/views/charges/index.html.erb b/app/views/charges/index.html.erb new file mode 100644 index 000000000..eeb5005a5 --- /dev/null +++ b/app/views/charges/index.html.erb @@ -0,0 +1,35 @@ +

<%= notice %>

+ +

Listing Charges

+ + + + + + + + + + + + + + + <% @charges.each do |charge| %> + + + + + + + + + + + <% end %> + +
CustomerPaidAmountCurrencyRefunded
<%= charge.customer %><%= charge.paid %><%= charge.amount %><%= charge.currency %><%= charge.refunded %><%= link_to 'Show', charge %><%= link_to 'Edit', edit_charge_path(charge) %><%= link_to 'Destroy', charge, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Charge', new_charge_path %> diff --git a/app/views/charges/index.json.jbuilder b/app/views/charges/index.json.jbuilder new file mode 100644 index 000000000..bf20b255c --- /dev/null +++ b/app/views/charges/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @charges, partial: "charges/charge", as: :charge diff --git a/app/views/charges/new.html.erb b/app/views/charges/new.html.erb new file mode 100644 index 000000000..55047d4e1 --- /dev/null +++ b/app/views/charges/new.html.erb @@ -0,0 +1,5 @@ +

New Charge

+ +<%= render 'form' %> + +<%= link_to 'Back', charges_path %> diff --git a/app/views/charges/show.html.erb b/app/views/charges/show.html.erb new file mode 100644 index 000000000..b2d7f921f --- /dev/null +++ b/app/views/charges/show.html.erb @@ -0,0 +1,29 @@ +

<%= notice %>

+ +

+ Customer: + <%= @charge.customer %> +

+ +

+ Paid: + <%= @charge.paid %> +

+ +

+ Amount: + <%= @charge.amount %> +

+ +

+ Currency: + <%= @charge.currency %> +

+ +

+ Refunded: + <%= @charge.refunded %> +

+ +<%= link_to 'Edit', edit_charge_path(@charge) %> | +<%= link_to 'Back', charges_path %> diff --git a/app/views/charges/show.json.jbuilder b/app/views/charges/show.json.jbuilder new file mode 100644 index 000000000..54f867331 --- /dev/null +++ b/app/views/charges/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "charges/charge", charge: @charge diff --git a/config/routes.rb b/config/routes.rb index a42e75cc5..4994c0068 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,5 @@ Rails.application.routes.draw do + resources :charges resources :customers # The priority is based upon order of creation: first created -> highest priority. # See how all your routes lay out with "rake routes". diff --git a/db/migrate/20200915161434_create_charges.rb b/db/migrate/20200915161434_create_charges.rb new file mode 100644 index 000000000..00afbbb62 --- /dev/null +++ b/db/migrate/20200915161434_create_charges.rb @@ -0,0 +1,13 @@ +class CreateCharges < ActiveRecord::Migration + def change + create_table :charges do |t| + t.references :customer, index: true, foreign_key: true + t.boolean :paid + t.float :amount + t.string :currency + t.boolean :refunded + + t.timestamps null: false + end + end +end diff --git a/db/schema.rb b/db/schema.rb index b14dc897a..70bf26409 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,7 +11,19 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20200915161058) do +ActiveRecord::Schema.define(version: 20200915161434) do + + create_table "charges", force: :cascade do |t| + t.integer "customer_id" + t.boolean "paid" + t.float "amount" + t.string "currency" + t.boolean "refunded" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + + add_index "charges", ["customer_id"], name: "index_charges_on_customer_id" create_table "customers", force: :cascade do |t| t.string "first_name" diff --git a/spec/controllers/charges_controller_spec.rb b/spec/controllers/charges_controller_spec.rb new file mode 100644 index 000000000..e2a7f9c4c --- /dev/null +++ b/spec/controllers/charges_controller_spec.rb @@ -0,0 +1,141 @@ +require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to specify the controller code that +# was generated by Rails when you ran the scaffold generator. +# +# It assumes that the implementation code is generated by the rails scaffold +# generator. If you are using any extension libraries to generate different +# controller code, this generated spec may or may not pass. +# +# It only uses APIs available in rails and/or rspec-rails. There are a number +# of tools you can use to make these specs even more expressive, but we're +# sticking to rails and rspec-rails APIs to keep things simple and stable. +# +# Compared to earlier versions of this generator, there is very limited use of +# stubs and message expectations in this spec. Stubs are only used when there +# is no simpler way to get a handle on the object needed for the example. +# Message expectations are only used when there is no simpler way to specify +# that an instance is receiving a specific message. +# +# Also compared to earlier versions of this generator, there are no longer any +# expectations of assigns and templates rendered. These features have been +# removed from Rails core in Rails 5, but can be added back in via the +# `rails-controller-testing` gem. + +RSpec.describe ChargesController, type: :controller do + + # This should return the minimal set of attributes required to create a valid + # Charge. As you add validations to Charge, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + # This should return the minimal set of values that should be in the session + # in order to pass any filters (e.g. authentication) defined in + # ChargesController. Be sure to keep this updated too. + let(:valid_session) { {} } + + describe "GET #index" do + it "returns a success response" do + Charge.create! valid_attributes + get :index, {}, valid_session + expect(response).to be_successful + end + end + + describe "GET #show" do + it "returns a success response" do + charge = Charge.create! valid_attributes + get :show, {:id => charge.to_param}, valid_session + expect(response).to be_successful + end + end + + describe "GET #new" do + it "returns a success response" do + get :new, {}, valid_session + expect(response).to be_successful + end + end + + describe "GET #edit" do + it "returns a success response" do + charge = Charge.create! valid_attributes + get :edit, {:id => charge.to_param}, valid_session + expect(response).to be_successful + end + end + + describe "POST #create" do + context "with valid params" do + it "creates a new Charge" do + expect { + post :create, {:charge => valid_attributes}, valid_session + }.to change(Charge, :count).by(1) + end + + it "redirects to the created charge" do + post :create, {:charge => valid_attributes}, valid_session + expect(response).to redirect_to(Charge.last) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'new' template)" do + post :create, {:charge => invalid_attributes}, valid_session + expect(response).to be_successful + end + end + end + + describe "PUT #update" do + context "with valid params" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested charge" do + charge = Charge.create! valid_attributes + put :update, {:id => charge.to_param, :charge => new_attributes}, valid_session + charge.reload + skip("Add assertions for updated state") + end + + it "redirects to the charge" do + charge = Charge.create! valid_attributes + put :update, {:id => charge.to_param, :charge => valid_attributes}, valid_session + expect(response).to redirect_to(charge) + end + end + + context "with invalid params" do + it "returns a success response (i.e. to display the 'edit' template)" do + charge = Charge.create! valid_attributes + put :update, {:id => charge.to_param, :charge => invalid_attributes}, valid_session + expect(response).to be_successful + end + end + end + + describe "DELETE #destroy" do + it "destroys the requested charge" do + charge = Charge.create! valid_attributes + expect { + delete :destroy, {:id => charge.to_param}, valid_session + }.to change(Charge, :count).by(-1) + end + + it "redirects to the charges list" do + charge = Charge.create! valid_attributes + delete :destroy, {:id => charge.to_param}, valid_session + expect(response).to redirect_to(charges_url) + end + end + +end diff --git a/spec/helpers/charges_helper_spec.rb b/spec/helpers/charges_helper_spec.rb new file mode 100644 index 000000000..0c25eaa8c --- /dev/null +++ b/spec/helpers/charges_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the ChargesHelper. For example: +# +# describe ChargesHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe ChargesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/charge_spec.rb b/spec/models/charge_spec.rb new file mode 100644 index 000000000..f419c0060 --- /dev/null +++ b/spec/models/charge_spec.rb @@ -0,0 +1,18 @@ +# == Schema Information +# +# Table name: charges +# +# id :integer not null, primary key +# customer_id :integer +# paid :boolean +# amount :float +# currency :string +# refunded :boolean +# created_at :datetime not null +# updated_at :datetime not null +# +require 'rails_helper' + +RSpec.describe Charge, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/customer_spec.rb b/spec/models/customer_spec.rb index 3399b1f56..e9f403774 100644 --- a/spec/models/customer_spec.rb +++ b/spec/models/customer_spec.rb @@ -1,3 +1,13 @@ +# == Schema Information +# +# Table name: customers +# +# id :integer not null, primary key +# first_name :string +# last_name :string +# created_at :datetime not null +# updated_at :datetime not null +# require 'rails_helper' RSpec.describe Customer, type: :model do diff --git a/spec/requests/charges_spec.rb b/spec/requests/charges_spec.rb new file mode 100644 index 000000000..504ef7846 --- /dev/null +++ b/spec/requests/charges_spec.rb @@ -0,0 +1,10 @@ +require 'rails_helper' + +RSpec.describe "Charges", type: :request do + describe "GET /charges" do + it "works! (now write some real specs)" do + get charges_path + expect(response).to have_http_status(200) + end + end +end diff --git a/spec/routing/charges_routing_spec.rb b/spec/routing/charges_routing_spec.rb new file mode 100644 index 000000000..4b2d9c92f --- /dev/null +++ b/spec/routing/charges_routing_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +RSpec.describe ChargesController, type: :routing do + describe "routing" do + it "routes to #index" do + expect(:get => "/charges").to route_to("charges#index") + end + + it "routes to #new" do + expect(:get => "/charges/new").to route_to("charges#new") + end + + it "routes to #show" do + expect(:get => "/charges/1").to route_to("charges#show", :id => "1") + end + + it "routes to #edit" do + expect(:get => "/charges/1/edit").to route_to("charges#edit", :id => "1") + end + + + it "routes to #create" do + expect(:post => "/charges").to route_to("charges#create") + end + + it "routes to #update via PUT" do + expect(:put => "/charges/1").to route_to("charges#update", :id => "1") + end + + it "routes to #update via PATCH" do + expect(:patch => "/charges/1").to route_to("charges#update", :id => "1") + end + + it "routes to #destroy" do + expect(:delete => "/charges/1").to route_to("charges#destroy", :id => "1") + end + end +end diff --git a/spec/views/charges/edit.html.erb_spec.rb b/spec/views/charges/edit.html.erb_spec.rb new file mode 100644 index 000000000..f1d01b7ec --- /dev/null +++ b/spec/views/charges/edit.html.erb_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +RSpec.describe "charges/edit", type: :view do + before(:each) do + @charge = assign(:charge, Charge.create!( + :customer => nil, + :paid => false, + :amount => 1.5, + :currency => "MyString", + :refunded => false + )) + end + + it "renders the edit charge form" do + render + + assert_select "form[action=?][method=?]", charge_path(@charge), "post" do + + assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" + + assert_select "input#charge_paid[name=?]", "charge[paid]" + + assert_select "input#charge_amount[name=?]", "charge[amount]" + + assert_select "input#charge_currency[name=?]", "charge[currency]" + + assert_select "input#charge_refunded[name=?]", "charge[refunded]" + end + end +end diff --git a/spec/views/charges/index.html.erb_spec.rb b/spec/views/charges/index.html.erb_spec.rb new file mode 100644 index 000000000..5e3999dab --- /dev/null +++ b/spec/views/charges/index.html.erb_spec.rb @@ -0,0 +1,31 @@ +require 'rails_helper' + +RSpec.describe "charges/index", type: :view do + before(:each) do + assign(:charges, [ + Charge.create!( + :customer => nil, + :paid => false, + :amount => 2.5, + :currency => "Currency", + :refunded => false + ), + Charge.create!( + :customer => nil, + :paid => false, + :amount => 2.5, + :currency => "Currency", + :refunded => false + ) + ]) + end + + it "renders a list of charges" do + render + assert_select "tr>td", :text => nil.to_s, :count => 2 + assert_select "tr>td", :text => false.to_s, :count => 2 + assert_select "tr>td", :text => 2.5.to_s, :count => 2 + assert_select "tr>td", :text => "Currency".to_s, :count => 2 + assert_select "tr>td", :text => false.to_s, :count => 2 + end +end diff --git a/spec/views/charges/new.html.erb_spec.rb b/spec/views/charges/new.html.erb_spec.rb new file mode 100644 index 000000000..60c4e23f3 --- /dev/null +++ b/spec/views/charges/new.html.erb_spec.rb @@ -0,0 +1,30 @@ +require 'rails_helper' + +RSpec.describe "charges/new", type: :view do + before(:each) do + assign(:charge, Charge.new( + :customer => nil, + :paid => false, + :amount => 1.5, + :currency => "MyString", + :refunded => false + )) + end + + it "renders new charge form" do + render + + assert_select "form[action=?][method=?]", charges_path, "post" do + + assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" + + assert_select "input#charge_paid[name=?]", "charge[paid]" + + assert_select "input#charge_amount[name=?]", "charge[amount]" + + assert_select "input#charge_currency[name=?]", "charge[currency]" + + assert_select "input#charge_refunded[name=?]", "charge[refunded]" + end + end +end diff --git a/spec/views/charges/show.html.erb_spec.rb b/spec/views/charges/show.html.erb_spec.rb new file mode 100644 index 000000000..af8c23821 --- /dev/null +++ b/spec/views/charges/show.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "charges/show", type: :view do + before(:each) do + @charge = assign(:charge, Charge.create!( + :customer => nil, + :paid => false, + :amount => 2.5, + :currency => "Currency", + :refunded => false + )) + end + + it "renders attributes in

" do + render + expect(rendered).to match(//) + expect(rendered).to match(/false/) + expect(rendered).to match(/2.5/) + expect(rendered).to match(/Currency/) + expect(rendered).to match(/false/) + end +end From 881605a1190091d4710fe5880a475674a43d37ed Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 11:22:00 -0600 Subject: [PATCH 05/12] Adding seeds --- .rbenv-gemsets | 3 +- Gemfile | 1 + Gemfile.lock | 3 ++ app/models/charge.rb | 2 ++ app/models/customer.rb | 4 +++ db/seeds.rb | 70 ++++++++++++++++++++++++++++++++++++++++++ 6 files changed, 81 insertions(+), 2 deletions(-) diff --git a/.rbenv-gemsets b/.rbenv-gemsets index 5cdec4b1b..d648f6468 100644 --- a/.rbenv-gemsets +++ b/.rbenv-gemsets @@ -1,2 +1 @@ -g3ortega --global \ No newline at end of file +g3ortega \ No newline at end of file diff --git a/Gemfile b/Gemfile index 188da10bf..a9cefc36b 100644 --- a/Gemfile +++ b/Gemfile @@ -41,6 +41,7 @@ group :development, :test do gem 'rubocop', '0.91.0' gem 'guard-rspec', require: false gem 'annotate' + gem 'pry-rails' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index a5c2f711e..d73b40176 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -112,6 +112,8 @@ GEM pry (0.13.1) coderay (~> 1.1) method_source (~> 1.0) + pry-rails (0.3.9) + pry (>= 0.10.4) rack (1.6.13) rack-test (0.6.3) rack (>= 1.0) @@ -235,6 +237,7 @@ DEPENDENCIES guard-rspec jbuilder (~> 2.0) jquery-rails + pry-rails rails (= 4.2.11.3) rspec-rails (= 3.9.1) rubocop (= 0.91.0) diff --git a/app/models/charge.rb b/app/models/charge.rb index 9f0fd5fb7..c9fdf19ff 100644 --- a/app/models/charge.rb +++ b/app/models/charge.rb @@ -13,4 +13,6 @@ # class Charge < ActiveRecord::Base belongs_to :customer + + validates_presence_of :customer end diff --git a/app/models/customer.rb b/app/models/customer.rb index ade45bc64..95629e706 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -10,6 +10,10 @@ # class Customer < ActiveRecord::Base + has_many :charges, dependent: :destroy + + validates_presence_of :first_name, :last_name + def full_name "#{first_name} #{last_name}" end diff --git a/db/seeds.rb b/db/seeds.rb index 4edb1e857..b94b59dba 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,3 +5,73 @@ # # cities = City.create([{ name: 'Chicago' }, { name: 'Copenhagen' }]) # Mayor.create(name: 'Emanuel', city: cities.first) +puts "----------- Cleaning data ------------------" +Customer.destroy_all + +puts "----- Seeding customers ----------" + +customers = [ + { + first_name: 'Johny', + last_name: 'Flow' + }, + { + first_name: 'Raj', + last_name: 'Jamnis' + }, + { + first_name: 'Andrew', + last_name: 'Chung' + }, + { + first_name: 'Mike', + last_name: 'Smith' + } +] + +customers = Customer.create(customers) + +puts "----- Seeding charges ----------" +puts "--------------------- Successful transactions" + +successful_charge = {paid: true, currency: 'usd', refunded: false} + +5.times do + customers[0].charges.create(successful_charge.merge!(amount: rand(9..99))) +end + +3.times do + customers[1].charges.create(successful_charge.merge!(amount: rand(9..99))) +end + +customers[2].charges.create(successful_charge.merge!(amount: rand(9..99))) + +customers[3].charges.create(successful_charge.merge!(amount: rand(9..99))) + +puts "--------------------- Failed transactions" + +failed_charge = {paid: false, currency: 'usd', refunded: false} + +3.times do + customers[2].charges.create(failed_charge.merge!(amount: rand(9..99))) +end + +2.times do + customers[3].charges.create(failed_charge.merge!(amount: rand(9..99))) +end + +puts "--------------------- Disputed transactions" + +disputed_transaction = {paid: false, currency: 'usd', refunded: true} + +3.times do + customers[0].charges.create(disputed_transaction.merge!(amount: rand(9..99))) +end + +2.times do + customers[1].charges.create(disputed_transaction.merge!(amount: rand(9..99))) +end + +puts "------------------------ SUMMARY ---------------------" +puts "Customers: #{Customer.all.count}" +puts "Charges: #{Charge.all.count}" \ No newline at end of file From 5981c088856c4f46764987d3a026218f0f9846ae Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 11:47:52 -0600 Subject: [PATCH 06/12] Adding styles & other minor improvements --- .../{application.css => application.scss} | 8 ++++ app/assets/stylesheets/charges.scss | 3 -- app/assets/stylesheets/customers.scss | 3 -- app/models/charge.rb | 4 ++ app/views/charges/_charge_list.html.erb | 29 ++++++++++++++ app/views/charges/index.html.erb | 38 ++++--------------- app/views/customers/index.html.erb | 5 ++- config/routes.rb | 2 +- db/seeds.rb | 2 +- 9 files changed, 54 insertions(+), 40 deletions(-) rename app/assets/stylesheets/{application.css => application.scss} (85%) delete mode 100644 app/assets/stylesheets/charges.scss delete mode 100644 app/assets/stylesheets/customers.scss create mode 100644 app/views/charges/_charge_list.html.erb diff --git a/app/assets/stylesheets/application.css b/app/assets/stylesheets/application.scss similarity index 85% rename from app/assets/stylesheets/application.css rename to app/assets/stylesheets/application.scss index f9cd5b348..078b43dba 100644 --- a/app/assets/stylesheets/application.css +++ b/app/assets/stylesheets/application.scss @@ -13,3 +13,11 @@ *= require_tree . *= require_self */ + +table.table.failed tr { + background-color: #FF0000; +} + +table.table.disputed tr { + background-color: #FF5400; +} \ No newline at end of file diff --git a/app/assets/stylesheets/charges.scss b/app/assets/stylesheets/charges.scss deleted file mode 100644 index c3a893042..000000000 --- a/app/assets/stylesheets/charges.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the Charges controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/assets/stylesheets/customers.scss b/app/assets/stylesheets/customers.scss deleted file mode 100644 index b6c7ce5eb..000000000 --- a/app/assets/stylesheets/customers.scss +++ /dev/null @@ -1,3 +0,0 @@ -// Place all the styles related to the Customers controller here. -// They will automatically be included in application.css. -// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/models/charge.rb b/app/models/charge.rb index c9fdf19ff..3f4b372ee 100644 --- a/app/models/charge.rb +++ b/app/models/charge.rb @@ -15,4 +15,8 @@ class Charge < ActiveRecord::Base belongs_to :customer validates_presence_of :customer + + scope :successful, -> { where(paid: true, refunded: false) } + scope :failed, -> { where(paid: false) } + scope :disputed, -> { where(paid: true, refunded: true) } end diff --git a/app/views/charges/_charge_list.html.erb b/app/views/charges/_charge_list.html.erb new file mode 100644 index 000000000..5f18fa141 --- /dev/null +++ b/app/views/charges/_charge_list.html.erb @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + <% charges.each do |charge| %> + + + + + + + + + + + <% end %> + +
CustomerPaidAmountCurrencyRefunded
<%= charge.customer.full_name %><%= charge.paid %><%= charge.amount %><%= charge.currency %><%= charge.refunded %><%= link_to 'Show', charge %><%= link_to 'Edit', edit_charge_path(charge) %><%= link_to 'Destroy', charge, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
\ No newline at end of file diff --git a/app/views/charges/index.html.erb b/app/views/charges/index.html.erb index eeb5005a5..39a591dfc 100644 --- a/app/views/charges/index.html.erb +++ b/app/views/charges/index.html.erb @@ -1,35 +1,13 @@

<%= notice %>

-

Listing Charges

+

Failed Charges

+<%= render 'charge_list', charges: @charges.failed, css_classes: 'failed' %> - - - - - - - - - - - +

Disputed Charges

+<%= render 'charge_list', charges: @charges.disputed, css_classes: 'disputed' %> - - <% @charges.each do |charge| %> - - - - - - - - - - - <% end %> - -
CustomerPaidAmountCurrencyRefunded
<%= charge.customer %><%= charge.paid %><%= charge.amount %><%= charge.currency %><%= charge.refunded %><%= link_to 'Show', charge %><%= link_to 'Edit', edit_charge_path(charge) %><%= link_to 'Destroy', charge, method: :delete, data: { confirm: 'Are you sure?' } %>
+

Successful Charges

+<%= render 'charge_list', charges: @charges.successful, css_classes: 'successful' %> -
- -<%= link_to 'New Charge', new_charge_path %> +<%= link_to 'Customers', customers_path, class: 'btn btn-warning' %> +<%= link_to 'New Charge', new_charge_path, class: 'btn btn-primary' %> diff --git a/app/views/customers/index.html.erb b/app/views/customers/index.html.erb index f77c27f8b..16c790f75 100644 --- a/app/views/customers/index.html.erb +++ b/app/views/customers/index.html.erb @@ -2,7 +2,7 @@

Listing Customers

- +
@@ -26,4 +26,5 @@
-<%= link_to 'New Customer', new_customer_path %> +<%= link_to 'New Customer', new_customer_path, class: 'btn btn-primary' %> +<%= link_to 'Transactions', charges_path, class: 'btn btn-warning' %> diff --git a/config/routes.rb b/config/routes.rb index 4994c0068..eb00b6b5b 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -5,7 +5,7 @@ # See how all your routes lay out with "rake routes". # You can have the root of your site routed with "root" - root 'static#index' + root 'charges#index' # Example of regular route: # get 'products/:id' => 'catalog#view' diff --git a/db/seeds.rb b/db/seeds.rb index b94b59dba..7ab56cff3 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -62,7 +62,7 @@ puts "--------------------- Disputed transactions" -disputed_transaction = {paid: false, currency: 'usd', refunded: true} +disputed_transaction = {paid: true, currency: 'usd', refunded: true} 3.times do customers[0].charges.create(disputed_transaction.merge!(amount: rand(9..99))) From 6ae10ac693fffdc0c51811c30a031736d0e41237 Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 12:42:03 -0600 Subject: [PATCH 07/12] Advancing specs --- Gemfile | 1 + Gemfile.lock | 3 + spec/factories/charge_factory.rb | 20 +++++ spec/factories/customer_factory.rb | 14 +++ spec/rails_helper.rb | 63 ++++++++++++++ spec/spec_helper.rb | 96 +++++++++++++++++++++ spec/views/charges/edit.html.erb_spec.rb | 30 ------- spec/views/charges/index.html.erb_spec.rb | 27 ++---- spec/views/charges/new.html.erb_spec.rb | 30 ------- spec/views/charges/show.html.erb_spec.rb | 22 ----- spec/views/customers/edit.html.erb_spec.rb | 21 ----- spec/views/customers/index.html.erb_spec.rb | 22 ----- spec/views/customers/new.html.erb_spec.rb | 21 ----- spec/views/customers/show.html.erb_spec.rb | 16 ---- test/controllers/.keep | 0 test/fixtures/.keep | 0 test/helpers/.keep | 0 test/integration/.keep | 0 test/mailers/.keep | 0 test/models/.keep | 0 test/test_helper.rb | 10 --- 21 files changed, 202 insertions(+), 194 deletions(-) create mode 100644 spec/factories/charge_factory.rb create mode 100644 spec/factories/customer_factory.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/spec_helper.rb delete mode 100644 spec/views/charges/edit.html.erb_spec.rb delete mode 100644 spec/views/charges/new.html.erb_spec.rb delete mode 100644 spec/views/charges/show.html.erb_spec.rb delete mode 100644 spec/views/customers/edit.html.erb_spec.rb delete mode 100644 spec/views/customers/index.html.erb_spec.rb delete mode 100644 spec/views/customers/new.html.erb_spec.rb delete mode 100644 spec/views/customers/show.html.erb_spec.rb delete mode 100644 test/controllers/.keep delete mode 100644 test/fixtures/.keep delete mode 100644 test/helpers/.keep delete mode 100644 test/integration/.keep delete mode 100644 test/mailers/.keep delete mode 100644 test/models/.keep delete mode 100644 test/test_helper.rb diff --git a/Gemfile b/Gemfile index a9cefc36b..b59e89bc1 100644 --- a/Gemfile +++ b/Gemfile @@ -42,6 +42,7 @@ group :development, :test do gem 'guard-rspec', require: false gem 'annotate' gem 'pry-rails' + gem 'faker' end group :development do diff --git a/Gemfile.lock b/Gemfile.lock index d73b40176..8ec3e804e 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,6 +60,8 @@ GEM execjs (2.7.0) factory_bot (5.2.0) activesupport (>= 4.2.0) + faker (2.2.1) + i18n (>= 0.8) ffi (1.13.1) formatador (0.2.5) globalid (0.4.2) @@ -234,6 +236,7 @@ DEPENDENCIES byebug coffee-rails (~> 4.1.0) factory_bot (= 5.2.0) + faker guard-rspec jbuilder (~> 2.0) jquery-rails diff --git a/spec/factories/charge_factory.rb b/spec/factories/charge_factory.rb new file mode 100644 index 000000000..2e7c93fab --- /dev/null +++ b/spec/factories/charge_factory.rb @@ -0,0 +1,20 @@ +# == Schema Information +# +# Table name: charges +# +# id :integer not null, primary key +# customer_id :integer +# paid :boolean +# amount :float +# currency :string +# refunded :boolean +# created_at :datetime not null +# updated_at :datetime not null +# +factory :charge do + customer + paid { true } + amount { rand(9..99) } + currency { 'usd' } + refunded { false } +end diff --git a/spec/factories/customer_factory.rb b/spec/factories/customer_factory.rb new file mode 100644 index 000000000..2b01e21d0 --- /dev/null +++ b/spec/factories/customer_factory.rb @@ -0,0 +1,14 @@ +# == Schema Information +# +# Table name: customers +# +# id :integer not null, primary key +# first_name :string +# last_name :string +# created_at :datetime not null +# updated_at :datetime not null +# +factory :customer do + first_name { Faker::Name.first_name } + title { Faker::Name.last_name } +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 000000000..b06351ba4 --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,63 @@ +# 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' +# 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')].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 + + # 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 diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 000000000..ce33d66df --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,96 @@ +# 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| + # 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 + + # 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" + + # 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/spec/views/charges/edit.html.erb_spec.rb b/spec/views/charges/edit.html.erb_spec.rb deleted file mode 100644 index f1d01b7ec..000000000 --- a/spec/views/charges/edit.html.erb_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'rails_helper' - -RSpec.describe "charges/edit", type: :view do - before(:each) do - @charge = assign(:charge, Charge.create!( - :customer => nil, - :paid => false, - :amount => 1.5, - :currency => "MyString", - :refunded => false - )) - end - - it "renders the edit charge form" do - render - - assert_select "form[action=?][method=?]", charge_path(@charge), "post" do - - assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" - - assert_select "input#charge_paid[name=?]", "charge[paid]" - - assert_select "input#charge_amount[name=?]", "charge[amount]" - - assert_select "input#charge_currency[name=?]", "charge[currency]" - - assert_select "input#charge_refunded[name=?]", "charge[refunded]" - end - end -end diff --git a/spec/views/charges/index.html.erb_spec.rb b/spec/views/charges/index.html.erb_spec.rb index 5e3999dab..85f0cfefe 100644 --- a/spec/views/charges/index.html.erb_spec.rb +++ b/spec/views/charges/index.html.erb_spec.rb @@ -1,31 +1,14 @@ require 'rails_helper' RSpec.describe "charges/index", type: :view do - before(:each) do - assign(:charges, [ - Charge.create!( - :customer => nil, - :paid => false, - :amount => 2.5, - :currency => "Currency", - :refunded => false - ), - Charge.create!( - :customer => nil, - :paid => false, - :amount => 2.5, - :currency => "Currency", - :refunded => false - ) - ]) + before do + create_list(:charge, 5, paid: false) + create_list(:charge, 3, paid: true, refunded: false) + create_list(:charge, 2, paid: true, refunded: true) end it "renders a list of charges" do render - assert_select "tr>td", :text => nil.to_s, :count => 2 - assert_select "tr>td", :text => false.to_s, :count => 2 - assert_select "tr>td", :text => 2.5.to_s, :count => 2 - assert_select "tr>td", :text => "Currency".to_s, :count => 2 - assert_select "tr>td", :text => false.to_s, :count => 2 + assert_select "table.table.failed>td", :count => 5 end end diff --git a/spec/views/charges/new.html.erb_spec.rb b/spec/views/charges/new.html.erb_spec.rb deleted file mode 100644 index 60c4e23f3..000000000 --- a/spec/views/charges/new.html.erb_spec.rb +++ /dev/null @@ -1,30 +0,0 @@ -require 'rails_helper' - -RSpec.describe "charges/new", type: :view do - before(:each) do - assign(:charge, Charge.new( - :customer => nil, - :paid => false, - :amount => 1.5, - :currency => "MyString", - :refunded => false - )) - end - - it "renders new charge form" do - render - - assert_select "form[action=?][method=?]", charges_path, "post" do - - assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" - - assert_select "input#charge_paid[name=?]", "charge[paid]" - - assert_select "input#charge_amount[name=?]", "charge[amount]" - - assert_select "input#charge_currency[name=?]", "charge[currency]" - - assert_select "input#charge_refunded[name=?]", "charge[refunded]" - end - end -end diff --git a/spec/views/charges/show.html.erb_spec.rb b/spec/views/charges/show.html.erb_spec.rb deleted file mode 100644 index af8c23821..000000000 --- a/spec/views/charges/show.html.erb_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'rails_helper' - -RSpec.describe "charges/show", type: :view do - before(:each) do - @charge = assign(:charge, Charge.create!( - :customer => nil, - :paid => false, - :amount => 2.5, - :currency => "Currency", - :refunded => false - )) - end - - it "renders attributes in

" do - render - expect(rendered).to match(//) - expect(rendered).to match(/false/) - expect(rendered).to match(/2.5/) - expect(rendered).to match(/Currency/) - expect(rendered).to match(/false/) - end -end diff --git a/spec/views/customers/edit.html.erb_spec.rb b/spec/views/customers/edit.html.erb_spec.rb deleted file mode 100644 index 35f127c2c..000000000 --- a/spec/views/customers/edit.html.erb_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'rails_helper' - -RSpec.describe "customers/edit", type: :view do - before(:each) do - @customer = assign(:customer, Customer.create!( - :first_name => "MyString", - :last_name => "MyString" - )) - end - - it "renders the edit customer form" do - render - - assert_select "form[action=?][method=?]", customer_path(@customer), "post" do - - assert_select "input#customer_first_name[name=?]", "customer[first_name]" - - assert_select "input#customer_last_name[name=?]", "customer[last_name]" - end - end -end diff --git a/spec/views/customers/index.html.erb_spec.rb b/spec/views/customers/index.html.erb_spec.rb deleted file mode 100644 index 76a42155b..000000000 --- a/spec/views/customers/index.html.erb_spec.rb +++ /dev/null @@ -1,22 +0,0 @@ -require 'rails_helper' - -RSpec.describe "customers/index", type: :view do - before(:each) do - assign(:customers, [ - Customer.create!( - :first_name => "First Name", - :last_name => "Last Name" - ), - Customer.create!( - :first_name => "First Name", - :last_name => "Last Name" - ) - ]) - end - - it "renders a list of customers" do - render - assert_select "tr>td", :text => "First Name".to_s, :count => 2 - assert_select "tr>td", :text => "Last Name".to_s, :count => 2 - end -end diff --git a/spec/views/customers/new.html.erb_spec.rb b/spec/views/customers/new.html.erb_spec.rb deleted file mode 100644 index bb26fd937..000000000 --- a/spec/views/customers/new.html.erb_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -require 'rails_helper' - -RSpec.describe "customers/new", type: :view do - before(:each) do - assign(:customer, Customer.new( - :first_name => "MyString", - :last_name => "MyString" - )) - end - - it "renders new customer form" do - render - - assert_select "form[action=?][method=?]", customers_path, "post" do - - assert_select "input#customer_first_name[name=?]", "customer[first_name]" - - assert_select "input#customer_last_name[name=?]", "customer[last_name]" - end - end -end diff --git a/spec/views/customers/show.html.erb_spec.rb b/spec/views/customers/show.html.erb_spec.rb deleted file mode 100644 index 28ce03fd9..000000000 --- a/spec/views/customers/show.html.erb_spec.rb +++ /dev/null @@ -1,16 +0,0 @@ -require 'rails_helper' - -RSpec.describe "customers/show", type: :view do - before(:each) do - @customer = assign(:customer, Customer.create!( - :first_name => "First Name", - :last_name => "Last Name" - )) - end - - it "renders attributes in

" do - render - expect(rendered).to match(/First Name/) - expect(rendered).to match(/Last Name/) - end -end diff --git a/test/controllers/.keep b/test/controllers/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/fixtures/.keep b/test/fixtures/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/helpers/.keep b/test/helpers/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/integration/.keep b/test/integration/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/mailers/.keep b/test/mailers/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/models/.keep b/test/models/.keep deleted file mode 100644 index e69de29bb..000000000 diff --git a/test/test_helper.rb b/test/test_helper.rb deleted file mode 100644 index 92e39b2d7..000000000 --- a/test/test_helper.rb +++ /dev/null @@ -1,10 +0,0 @@ -ENV['RAILS_ENV'] ||= 'test' -require File.expand_path('../../config/environment', __FILE__) -require 'rails/test_help' - -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 From 8d48213eff1ce5b8c0b47ae6cee5d5db523e98ff Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 12:58:54 -0600 Subject: [PATCH 08/12] Improving specs --- Gemfile | 2 +- Gemfile.lock | 5 ++++- app/views/charges/_charge_list.html.erb | 4 ++-- spec/factories/charge_factory.rb | 15 +++++++++------ spec/factories/customer_factory.rb | 10 +++++++--- spec/rails_helper.rb | 2 ++ spec/views/charges/index.html.erb_spec.rb | 10 ++++++++-- 7 files changed, 33 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index b59e89bc1..5028ae4f7 100644 --- a/Gemfile +++ b/Gemfile @@ -37,7 +37,7 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' gem 'rspec-rails', '3.9.1' - gem 'factory_bot', '5.2.0' + gem 'factory_bot_rails' gem 'rubocop', '0.91.0' gem 'guard-rspec', require: false gem 'annotate' diff --git a/Gemfile.lock b/Gemfile.lock index 8ec3e804e..725680f1a 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -60,6 +60,9 @@ GEM execjs (2.7.0) factory_bot (5.2.0) activesupport (>= 4.2.0) + factory_bot_rails (5.2.0) + factory_bot (~> 5.2.0) + railties (>= 4.2.0) faker (2.2.1) i18n (>= 0.8) ffi (1.13.1) @@ -235,7 +238,7 @@ DEPENDENCIES annotate byebug coffee-rails (~> 4.1.0) - factory_bot (= 5.2.0) + factory_bot_rails faker guard-rspec jbuilder (~> 2.0) diff --git a/app/views/charges/_charge_list.html.erb b/app/views/charges/_charge_list.html.erb index 5f18fa141..657ebb1bf 100644 --- a/app/views/charges/_charge_list.html.erb +++ b/app/views/charges/_charge_list.html.erb @@ -1,6 +1,6 @@

First name
- + @@ -12,7 +12,7 @@ <% charges.each do |charge| %> - + diff --git a/spec/factories/charge_factory.rb b/spec/factories/charge_factory.rb index 2e7c93fab..a2ae38c2f 100644 --- a/spec/factories/charge_factory.rb +++ b/spec/factories/charge_factory.rb @@ -11,10 +11,13 @@ # created_at :datetime not null # updated_at :datetime not null # -factory :charge do - customer - paid { true } - amount { rand(9..99) } - currency { 'usd' } - refunded { false } + +FactoryBot.define do + factory :charge do + customer + paid { true } + amount { rand(9..99) } + currency { 'usd' } + refunded { false } + end end diff --git a/spec/factories/customer_factory.rb b/spec/factories/customer_factory.rb index 2b01e21d0..05e4c9f21 100644 --- a/spec/factories/customer_factory.rb +++ b/spec/factories/customer_factory.rb @@ -8,7 +8,11 @@ # created_at :datetime not null # updated_at :datetime not null # -factory :customer do - first_name { Faker::Name.first_name } - title { Faker::Name.last_name } + +FactoryBot.define do + factory :customer do + first_name { Faker::Name.first_name } + last_name { Faker::Name.last_name } + end end + diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index b06351ba4..d2b015c59 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -41,6 +41,8 @@ # instead of true. config.use_transactional_fixtures = true + config.include FactoryBot::Syntax::Methods + # 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`. diff --git a/spec/views/charges/index.html.erb_spec.rb b/spec/views/charges/index.html.erb_spec.rb index 85f0cfefe..3826f6e45 100644 --- a/spec/views/charges/index.html.erb_spec.rb +++ b/spec/views/charges/index.html.erb_spec.rb @@ -5,10 +5,16 @@ create_list(:charge, 5, paid: false) create_list(:charge, 3, paid: true, refunded: false) create_list(:charge, 2, paid: true, refunded: true) + assign(:charges, Charge.all) + render + end + + it 'contains three headers' do + assert_select 'h1', count: 3 end it "renders a list of charges" do - render - assert_select "table.table.failed>td", :count => 5 + assert_select 'tr.customer', count: 10 + assert_select 'tr.header', count: 3 end end From 2b38dc911f2a0bd0a8d187e798f6acdb5d67b0c1 Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 13:23:06 -0600 Subject: [PATCH 09/12] Improving specs even further --- Gemfile | 5 +- Gemfile.lock | 3 + app/models/charge.rb | 2 +- spec/controllers/charges_controller_spec.rb | 137 ++++++++---------- spec/controllers/customers_controller_spec.rb | 48 ++---- spec/helpers/charges_helper_spec.rb | 15 -- spec/helpers/customers_helper_spec.rb | 15 -- spec/models/charge_spec.rb | 18 ++- spec/models/customer_spec.rb | 17 ++- spec/rails_helper.rb | 8 + 10 files changed, 123 insertions(+), 145 deletions(-) delete mode 100644 spec/helpers/charges_helper_spec.rb delete mode 100644 spec/helpers/customers_helper_spec.rb diff --git a/Gemfile b/Gemfile index 5028ae4f7..4dd326a4c 100644 --- a/Gemfile +++ b/Gemfile @@ -1,6 +1,5 @@ source 'https://rubygems.org' - # Bundle edge Rails instead: gem 'rails', github: 'rails/rails' gem 'rails', '4.2.11.3' # Use sqlite3 as the database for Active Record @@ -45,6 +44,10 @@ group :development, :test do gem 'faker' end +group :test do + gem 'shoulda-matchers' +end + group :development do # Access an IRB console on exception pages or by using <%= console %> in views gem 'web-console', '~> 2.0' diff --git a/Gemfile.lock b/Gemfile.lock index 725680f1a..cd306ee58 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -202,6 +202,8 @@ GEM json (~> 1.7, >= 1.7.7) rdoc (~> 4.0) shellany (0.0.1) + shoulda-matchers (4.4.1) + activesupport (>= 4.2.0) simple_form (4.0.0) actionpack (> 4) activemodel (> 4) @@ -249,6 +251,7 @@ DEPENDENCIES rubocop (= 0.91.0) sass-rails (~> 5.0) sdoc (~> 0.4.0) + shoulda-matchers simple_form spring sqlite3 (~> 1.3.11) diff --git a/app/models/charge.rb b/app/models/charge.rb index 3f4b372ee..2b397af71 100644 --- a/app/models/charge.rb +++ b/app/models/charge.rb @@ -14,7 +14,7 @@ class Charge < ActiveRecord::Base belongs_to :customer - validates_presence_of :customer + validates_presence_of :customer, :amount scope :successful, -> { where(paid: true, refunded: false) } scope :failed, -> { where(paid: false) } diff --git a/spec/controllers/charges_controller_spec.rb b/spec/controllers/charges_controller_spec.rb index e2a7f9c4c..0f60b5ee7 100644 --- a/spec/controllers/charges_controller_spec.rb +++ b/spec/controllers/charges_controller_spec.rb @@ -1,141 +1,126 @@ require 'rails_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. -# -# Also compared to earlier versions of this generator, there are no longer any -# expectations of assigns and templates rendered. These features have been -# removed from Rails core in Rails 5, but can be added back in via the -# `rails-controller-testing` gem. - RSpec.describe ChargesController, type: :controller do - # This should return the minimal set of attributes required to create a valid # Charge. As you add validations to Charge, be sure to # adjust the attributes here as well. - let(:valid_attributes) { - skip("Add a hash of attributes valid for your model") - } - let(:invalid_attributes) { - skip("Add a hash of attributes invalid for your model") - } + + let(:customer) do + create(:customer) + end + + let(:valid_attributes) do + { + paid: true, + amount: 99.99, + currency: 'usd', + refunded: false, + customer_id: customer.id + } + end + + let(:invalid_attributes) do + { + paid: true, + amount: 99.99, + currency: 'usd' + } + end # This should return the minimal set of values that should be in the session # in order to pass any filters (e.g. authentication) defined in # ChargesController. Be sure to keep this updated too. let(:valid_session) { {} } - describe "GET #index" do - it "returns a success response" do + describe 'GET #index' do + it 'returns a success response' do Charge.create! valid_attributes get :index, {}, valid_session expect(response).to be_successful end end - describe "GET #show" do - it "returns a success response" do + describe 'GET #show' do + it 'returns a success response' do charge = Charge.create! valid_attributes - get :show, {:id => charge.to_param}, valid_session + get :show, { id: charge.to_param }, valid_session expect(response).to be_successful end end - describe "GET #new" do - it "returns a success response" do + describe 'GET #new' do + it 'returns a success response' do get :new, {}, valid_session expect(response).to be_successful end end - describe "GET #edit" do - it "returns a success response" do + describe 'GET #edit' do + it 'returns a success response' do charge = Charge.create! valid_attributes - get :edit, {:id => charge.to_param}, valid_session + get :edit, { id: charge.to_param }, valid_session expect(response).to be_successful end end - describe "POST #create" do - context "with valid params" do - it "creates a new Charge" do - expect { - post :create, {:charge => valid_attributes}, valid_session - }.to change(Charge, :count).by(1) + describe 'POST #create' do + context 'with valid params' do + it 'creates a new Charge' do + expect do + post :create, { charge: valid_attributes }, valid_session + end.to change(Charge, :count).by(1) end - it "redirects to the created charge" do - post :create, {:charge => valid_attributes}, valid_session + it 'redirects to the created charge' do + post :create, { charge: valid_attributes }, valid_session expect(response).to redirect_to(Charge.last) end end - context "with invalid params" do + context 'with invalid params' do it "returns a success response (i.e. to display the 'new' template)" do - post :create, {:charge => invalid_attributes}, valid_session + post :create, { charge: invalid_attributes }, valid_session expect(response).to be_successful end end end - describe "PUT #update" do - context "with valid params" do - let(:new_attributes) { - skip("Add a hash of attributes valid for your model") - } + describe 'PUT #update' do + context 'with valid params' do + let(:new_attributes) do + { + refunded: true + } + end - it "updates the requested charge" do + it 'updates the requested charge' do charge = Charge.create! valid_attributes - put :update, {:id => charge.to_param, :charge => new_attributes}, valid_session + put :update, { id: charge.to_param, charge: new_attributes }, valid_session charge.reload - skip("Add assertions for updated state") + expect(charge.refunded).to be_truthy end - it "redirects to the charge" do + it 'redirects to the charge' do charge = Charge.create! valid_attributes - put :update, {:id => charge.to_param, :charge => valid_attributes}, valid_session + put :update, { id: charge.to_param, charge: valid_attributes }, valid_session expect(response).to redirect_to(charge) end end - - context "with invalid params" do - it "returns a success response (i.e. to display the 'edit' template)" do - charge = Charge.create! valid_attributes - put :update, {:id => charge.to_param, :charge => invalid_attributes}, valid_session - expect(response).to be_successful - end - end end - describe "DELETE #destroy" do - it "destroys the requested charge" do + describe 'DELETE #destroy' do + it 'destroys the requested charge' do charge = Charge.create! valid_attributes - expect { - delete :destroy, {:id => charge.to_param}, valid_session - }.to change(Charge, :count).by(-1) + expect do + delete :destroy, { id: charge.to_param }, valid_session + end.to change(Charge, :count).by(-1) end - it "redirects to the charges list" do + it 'redirects to the charges list' do charge = Charge.create! valid_attributes - delete :destroy, {:id => charge.to_param}, valid_session + delete :destroy, { id: charge.to_param }, valid_session expect(response).to redirect_to(charges_url) end end - end diff --git a/spec/controllers/customers_controller_spec.rb b/spec/controllers/customers_controller_spec.rb index 60980a9d4..301b3f1ed 100644 --- a/spec/controllers/customers_controller_spec.rb +++ b/spec/controllers/customers_controller_spec.rb @@ -1,39 +1,21 @@ require 'rails_helper' -# This spec was generated by rspec-rails when you ran the scaffold generator. -# It demonstrates how one might use RSpec to specify the controller code that -# was generated by Rails when you ran the scaffold generator. -# -# It assumes that the implementation code is generated by the rails scaffold -# generator. If you are using any extension libraries to generate different -# controller code, this generated spec may or may not pass. -# -# It only uses APIs available in rails and/or rspec-rails. There are a number -# of tools you can use to make these specs even more expressive, but we're -# sticking to rails and rspec-rails APIs to keep things simple and stable. -# -# Compared to earlier versions of this generator, there is very limited use of -# stubs and message expectations in this spec. Stubs are only used when there -# is no simpler way to get a handle on the object needed for the example. -# Message expectations are only used when there is no simpler way to specify -# that an instance is receiving a specific message. -# -# Also compared to earlier versions of this generator, there are no longer any -# expectations of assigns and templates rendered. These features have been -# removed from Rails core in Rails 5, but can be added back in via the -# `rails-controller-testing` gem. - RSpec.describe CustomersController, type: :controller do # This should return the minimal set of attributes required to create a valid # Customer. As you add validations to Customer, be sure to # adjust the attributes here as well. let(:valid_attributes) { - skip("Add a hash of attributes valid for your model") + { + first_name: 'John', + last_name: 'Doe' + } } let(:invalid_attributes) { - skip("Add a hash of attributes invalid for your model") + { + first_name: 'John' + } } # This should return the minimal set of values that should be in the session @@ -97,14 +79,18 @@ describe "PUT #update" do context "with valid params" do let(:new_attributes) { - skip("Add a hash of attributes valid for your model") + { + first_name: 'Peter', + last_name: 'Pan' + } } it "updates the requested customer" do customer = Customer.create! valid_attributes put :update, {:id => customer.to_param, :customer => new_attributes}, valid_session customer.reload - skip("Add assertions for updated state") + expect(customer.first_name).to eq('Peter') + expect(customer.last_name).to eq('Pan') end it "redirects to the customer" do @@ -113,14 +99,6 @@ expect(response).to redirect_to(customer) end end - - context "with invalid params" do - it "returns a success response (i.e. to display the 'edit' template)" do - customer = Customer.create! valid_attributes - put :update, {:id => customer.to_param, :customer => invalid_attributes}, valid_session - expect(response).to be_successful - end - end end describe "DELETE #destroy" do diff --git a/spec/helpers/charges_helper_spec.rb b/spec/helpers/charges_helper_spec.rb deleted file mode 100644 index 0c25eaa8c..000000000 --- a/spec/helpers/charges_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'rails_helper' - -# Specs in this file have access to a helper object that includes -# the ChargesHelper. For example: -# -# describe ChargesHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -RSpec.describe ChargesHelper, type: :helper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/helpers/customers_helper_spec.rb b/spec/helpers/customers_helper_spec.rb deleted file mode 100644 index fa160374f..000000000 --- a/spec/helpers/customers_helper_spec.rb +++ /dev/null @@ -1,15 +0,0 @@ -require 'rails_helper' - -# Specs in this file have access to a helper object that includes -# the CustomersHelper. For example: -# -# describe CustomersHelper do -# describe "string concat" do -# it "concats two strings with spaces" do -# expect(helper.concat_strings("this","that")).to eq("this that") -# end -# end -# end -RSpec.describe CustomersHelper, type: :helper do - pending "add some examples to (or delete) #{__FILE__}" -end diff --git a/spec/models/charge_spec.rb b/spec/models/charge_spec.rb index f419c0060..aff14ac94 100644 --- a/spec/models/charge_spec.rb +++ b/spec/models/charge_spec.rb @@ -14,5 +14,21 @@ require 'rails_helper' RSpec.describe Charge, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe 'associations' do + it { should belong_to(:customer) } + end + + describe 'validations' do + it { should validate_presence_of(:customer) } + it { should validate_presence_of(:amount) } + end + + describe 'attributes' do + + let(:charge) { create(:charge, paid: true, refunded: false) } + + it 'has all expected attributes with the right data' do + expect(charge).to have_attributes(paid: true, refunded: false, amount: charge.amount) + end + end end diff --git a/spec/models/customer_spec.rb b/spec/models/customer_spec.rb index e9f403774..3434b8e69 100644 --- a/spec/models/customer_spec.rb +++ b/spec/models/customer_spec.rb @@ -11,5 +11,20 @@ require 'rails_helper' RSpec.describe Customer, type: :model do - pending "add some examples to (or delete) #{__FILE__}" + describe 'associations' do + it { should have_many(:charges) } + end + + describe 'validations' do + it { should validate_presence_of(:first_name) } + it { should validate_presence_of(:last_name) } + end + + describe 'attributes' do + let(:customer) { create(:customer, first_name: 'John', last_name: 'Doe') } + + it 'has all expected attributes with the right data' do + expect(customer).to have_attributes(first_name: 'John', last_name: 'Doe') + end + end end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb index d2b015c59..101a9d7f0 100644 --- a/spec/rails_helper.rb +++ b/spec/rails_helper.rb @@ -32,6 +32,14 @@ puts e.to_s.strip exit 1 end + +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :rails + end +end + RSpec.configure do |config| # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures config.fixture_path = "#{::Rails.root}/spec/fixtures" From 3cbdb4c43d5419513e0f84d1a87baaf78ab174eb Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 13:26:57 -0600 Subject: [PATCH 10/12] Finishing --- app/models/customer.rb | 1 - spec/controllers/charges_controller_spec.rb | 4 ---- spec/views/charges/index.html.erb_spec.rb | 4 ++-- 3 files changed, 2 insertions(+), 7 deletions(-) diff --git a/app/models/customer.rb b/app/models/customer.rb index 95629e706..c7c4db51b 100644 --- a/app/models/customer.rb +++ b/app/models/customer.rb @@ -9,7 +9,6 @@ # updated_at :datetime not null # class Customer < ActiveRecord::Base - has_many :charges, dependent: :destroy validates_presence_of :first_name, :last_name diff --git a/spec/controllers/charges_controller_spec.rb b/spec/controllers/charges_controller_spec.rb index 0f60b5ee7..9ea1aae45 100644 --- a/spec/controllers/charges_controller_spec.rb +++ b/spec/controllers/charges_controller_spec.rb @@ -1,10 +1,6 @@ require 'rails_helper' RSpec.describe ChargesController, type: :controller do - # This should return the minimal set of attributes required to create a valid - # Charge. As you add validations to Charge, be sure to - # adjust the attributes here as well. - let(:customer) do create(:customer) diff --git a/spec/views/charges/index.html.erb_spec.rb b/spec/views/charges/index.html.erb_spec.rb index 3826f6e45..172644ca0 100644 --- a/spec/views/charges/index.html.erb_spec.rb +++ b/spec/views/charges/index.html.erb_spec.rb @@ -1,6 +1,6 @@ require 'rails_helper' -RSpec.describe "charges/index", type: :view do +RSpec.describe 'charges/index', type: :view do before do create_list(:charge, 5, paid: false) create_list(:charge, 3, paid: true, refunded: false) @@ -13,7 +13,7 @@ assert_select 'h1', count: 3 end - it "renders a list of charges" do + it 'renders a list of charges' do assert_select 'tr.customer', count: 10 assert_select 'tr.header', count: 3 end From 74c1259c6bf9a9a067a822b6b4f81c1affcc39dd Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 13:31:40 -0600 Subject: [PATCH 11/12] Improving front-end --- app/views/charges/_charge_list.html.erb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/app/views/charges/_charge_list.html.erb b/app/views/charges/_charge_list.html.erb index 657ebb1bf..035b62717 100644 --- a/app/views/charges/_charge_list.html.erb +++ b/app/views/charges/_charge_list.html.erb @@ -2,10 +2,8 @@ - - - + @@ -14,13 +12,11 @@ <% charges.each do |charge| %> - - - - - - - + + + + + <% end %> From ec9849e0a8618b4f9f566e2caa808edee509ba94 Mon Sep 17 00:00:00 2001 From: Gerardo Ortega Date: Tue, 15 Sep 2020 14:32:30 -0600 Subject: [PATCH 12/12] Minor improvements --- app/views/charges/index.html.erb | 8 +++++--- spec/views/charges/index.html.erb_spec.rb | 12 ++++++++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/views/charges/index.html.erb b/app/views/charges/index.html.erb index 39a591dfc..8856c0915 100644 --- a/app/views/charges/index.html.erb +++ b/app/views/charges/index.html.erb @@ -1,12 +1,14 @@

<%= notice %>

-

Failed Charges

+

Transactions Report

+ +

Failed Transactions

<%= render 'charge_list', charges: @charges.failed, css_classes: 'failed' %> -

Disputed Charges

+

Disputed Transactions

<%= render 'charge_list', charges: @charges.disputed, css_classes: 'disputed' %> -

Successful Charges

+

Successful Transactions

<%= render 'charge_list', charges: @charges.successful, css_classes: 'successful' %> <%= link_to 'Customers', customers_path, class: 'btn btn-warning' %> diff --git a/spec/views/charges/index.html.erb_spec.rb b/spec/views/charges/index.html.erb_spec.rb index 172644ca0..22989902c 100644 --- a/spec/views/charges/index.html.erb_spec.rb +++ b/spec/views/charges/index.html.erb_spec.rb @@ -17,4 +17,16 @@ assert_select 'tr.customer', count: 10 assert_select 'tr.header', count: 3 end + + it 'render a list of five failed transactions' do + assert_select '.table.failed tr.customer', count: 5 + end + + it 'render a list of three successful transactions' do + assert_select '.table.successful tr.customer', count: 3 + end + + it 'render a list of two disputed transactions' do + assert_select '.table.disputed tr.customer', count: 2 + end end
Customer Paid Amount
<%= charge.customer.full_name %> <%= charge.paid %> <%= charge.amount %>
CustomerPaid AmountCurrencyRefundedDate
<%= charge.customer.full_name %><%= charge.paid %><%= charge.amount %><%= charge.currency %><%= charge.refunded %><%= link_to 'Show', charge %><%= link_to 'Edit', edit_charge_path(charge) %><%= link_to 'Destroy', charge, method: :delete, data: { confirm: 'Are you sure?' } %><%= number_to_currency(charge.amount) %><%= time_ago_in_words(charge.created_at) %><%= link_to 'Show', charge, class: 'btn btn-primary' %><%= link_to 'Edit', edit_charge_path(charge), class: 'btn btn-warning' %><%= link_to 'Destroy', charge, method: :delete, data: { confirm: 'Are you sure?' }, class: 'btn btn-danger' %>