diff --git a/source/example-app/.gitignore b/source/example-app/.gitignore new file mode 100644 index 000000000..25a742dff --- /dev/null +++ b/source/example-app/.gitignore @@ -0,0 +1,16 @@ +# See http://help.github.com/ignore-files/ for more about ignoring files. +# +# If you find yourself ignoring temporary files generated by your text editor +# or operating system, you probably want to add a global ignore instead: +# git config --global core.excludesfile '~/.gitignore_global' + +# Ignore bundler config. +/.bundle + +# Ignore the default SQLite database. +/db/*.sqlite3 +/db/*.sqlite3-journal + +# Ignore all logfiles and tempfiles. +/log/*.log +/tmp diff --git a/source/example-app/Gemfile b/source/example-app/Gemfile new file mode 100644 index 000000000..9e6552489 --- /dev/null +++ b/source/example-app/Gemfile @@ -0,0 +1,51 @@ +source 'https://rubygems.org' + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '4.0.0' + +# Use postgresql as the database for Active Record +gem 'pg', '~> 0.20.0' + +# Use SCSS for stylesheets +gem 'sass-rails', '~> 4.0.0' + +# Use Uglifier as compressor for JavaScript assets +gem 'uglifier', '>= 1.3.0' + +# Use CoffeeScript for .js.coffee assets and views +gem 'coffee-rails', '~> 4.0.0' + +# See https://github.com/sstephenson/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', '~> 1.3' + +# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder +gem 'jbuilder', '~> 1.2' + +group :doc do + # bundle exec rake doc:rails generates the API under doc/api. + gem 'sdoc', require: false +end + +group :development, :test do + gem 'rspec-rails', '~> 3.9.1' + gem 'database_cleaner', '1.8.3' + gem 'capybara' +end + +# Use ActiveModel has_secure_password +# gem 'bcrypt-ruby', '~> 3.0.0' + +# Use unicorn as the app server +# gem 'unicorn' + +# Use Capistrano for deployment +# gem 'capistrano', group: :development + +# Use debugger +# gem 'debugger', group: [:development, :test] diff --git a/source/example-app/Gemfile.lock b/source/example-app/Gemfile.lock new file mode 100644 index 000000000..02f032b51 --- /dev/null +++ b/source/example-app/Gemfile.lock @@ -0,0 +1,157 @@ +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.0.0) + actionpack (= 4.0.0) + mail (~> 2.5.3) + actionpack (4.0.0) + activesupport (= 4.0.0) + builder (~> 3.1.0) + erubis (~> 2.7.0) + rack (~> 1.5.2) + rack-test (~> 0.6.2) + activemodel (4.0.0) + activesupport (= 4.0.0) + builder (~> 3.1.0) + activerecord (4.0.0) + activemodel (= 4.0.0) + activerecord-deprecated_finders (~> 1.0.2) + activesupport (= 4.0.0) + arel (~> 4.0.0) + activerecord-deprecated_finders (1.0.4) + activesupport (4.0.0) + i18n (~> 0.6, >= 0.6.4) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + arel (4.0.2) + builder (3.1.4) + capybara (2.18.0) + addressable + mini_mime (>= 0.1.3) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (>= 2.0, < 4.0) + coffee-rails (4.0.1) + coffee-script (>= 2.2.0) + railties (>= 4.0.0, < 5.0) + coffee-script (2.4.1) + coffee-script-source + execjs + coffee-script-source (1.12.2) + concurrent-ruby (1.1.8) + database_cleaner (1.8.3) + diff-lcs (1.4.4) + erubis (2.7.0) + execjs (2.7.0) + hike (1.2.3) + i18n (0.9.5) + concurrent-ruby (~> 1.0) + jbuilder (1.5.3) + activesupport (>= 3.0.0) + multi_json (>= 1.2.0) + jquery-rails (3.1.5) + railties (>= 3.0, < 5.0) + thor (>= 0.14, < 2.0) + mail (2.5.5) + mime-types (~> 1.16) + treetop (~> 1.4.8) + mime-types (1.25.1) + mini_mime (1.1.0) + mini_portile2 (2.1.0) + minitest (4.7.5) + multi_json (1.15.0) + nokogiri (1.6.8.1) + mini_portile2 (~> 2.1.0) + pg (0.20.0) + polyglot (0.3.5) + public_suffix (2.0.5) + rack (1.5.5) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.0.0) + actionmailer (= 4.0.0) + actionpack (= 4.0.0) + activerecord (= 4.0.0) + activesupport (= 4.0.0) + bundler (>= 1.3.0, < 2.0) + railties (= 4.0.0) + sprockets-rails (~> 2.0.0) + railties (4.0.0) + actionpack (= 4.0.0) + activesupport (= 4.0.0) + rake (>= 0.8.7) + thor (>= 0.18.1, < 2.0) + rake (12.3.3) + rdoc (5.1.0) + rspec-core (3.9.3) + rspec-support (~> 3.9.3) + rspec-expectations (3.9.4) + 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.4) + sass (3.2.19) + sass-rails (4.0.5) + railties (>= 4.0.0, < 5.0) + sass (~> 3.2.2) + sprockets (~> 2.8, < 3.0) + sprockets-rails (~> 2.0) + sdoc (2.1.0) + rdoc (>= 5.0) + sprockets (2.12.5) + hike (~> 1.2) + multi_json (~> 1.0) + rack (~> 1.0) + tilt (~> 1.1, != 1.3.0) + sprockets-rails (2.0.1) + actionpack (>= 3.0) + activesupport (>= 3.0) + sprockets (~> 2.8) + thor (1.1.0) + thread_safe (0.3.6) + tilt (1.4.1) + treetop (1.4.15) + polyglot + polyglot (>= 0.3.1) + turbolinks (1.3.1) + coffee-rails + tzinfo (0.3.60) + uglifier (4.2.0) + execjs (>= 0.3.0, < 3) + xpath (2.1.0) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + capybara + coffee-rails (~> 4.0.0) + database_cleaner (= 1.8.3) + jbuilder (~> 1.2) + jquery-rails + pg (~> 0.20.0) + rails (= 4.0.0) + rspec-rails (~> 3.9.1) + sass-rails (~> 4.0.0) + sdoc + turbolinks (~> 1.3) + uglifier (>= 1.3.0) + +BUNDLED WITH + 1.17.3 diff --git a/source/example-app/README.rdoc b/source/example-app/README.rdoc new file mode 100644 index 000000000..dd4e97e22 --- /dev/null +++ b/source/example-app/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/source/example-app/Rakefile b/source/example-app/Rakefile new file mode 100644 index 000000000..e67201a82 --- /dev/null +++ b/source/example-app/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__) + +ExampleApp::Application.load_tasks diff --git a/source/example-app/app/assets/images/.keep b/source/example-app/app/assets/images/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/app/assets/javascripts/application.js b/source/example-app/app/assets/javascripts/application.js new file mode 100644 index 000000000..d6925fa43 --- /dev/null +++ b/source/example-app/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 vendor/assets/javascripts of plugins, if any, 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/sstephenson/sprockets#sprockets-directives) for details +// about supported directives. +// +//= require jquery +//= require jquery_ujs +//= require turbolinks +//= require_tree . diff --git a/source/example-app/app/assets/javascripts/charges.js.coffee b/source/example-app/app/assets/javascripts/charges.js.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/source/example-app/app/assets/javascripts/charges.js.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/source/example-app/app/assets/javascripts/customers.js.coffee b/source/example-app/app/assets/javascripts/customers.js.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/source/example-app/app/assets/javascripts/customers.js.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/source/example-app/app/assets/stylesheets/application.css b/source/example-app/app/assets/stylesheets/application.css new file mode 100644 index 000000000..a63edf58d --- /dev/null +++ b/source/example-app/app/assets/stylesheets/application.css @@ -0,0 +1,21 @@ +/* + * 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 vendor/assets/stylesheets of plugins, if any, 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 top of the + * compiled file, but it's generally better to create a new file per style scope. + * + *= require_self + *= require_tree . + */ + +.danger { + background-color: #FF0000; +} + +.salmon { + background-color: #FF5400; +} \ No newline at end of file diff --git a/source/example-app/app/assets/stylesheets/charges.css.scss b/source/example-app/app/assets/stylesheets/charges.css.scss new file mode 100644 index 000000000..c3a893042 --- /dev/null +++ b/source/example-app/app/assets/stylesheets/charges.css.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/source/example-app/app/assets/stylesheets/customers.css.scss b/source/example-app/app/assets/stylesheets/customers.css.scss new file mode 100644 index 000000000..892b0ad94 --- /dev/null +++ b/source/example-app/app/assets/stylesheets/customers.css.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/source/example-app/app/assets/stylesheets/scaffolds.css.scss b/source/example-app/app/assets/stylesheets/scaffolds.css.scss new file mode 100644 index 000000000..6ec6a8ff5 --- /dev/null +++ b/source/example-app/app/assets/stylesheets/scaffolds.css.scss @@ -0,0 +1,69 @@ +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/source/example-app/app/controllers/application_controller.rb b/source/example-app/app/controllers/application_controller.rb new file mode 100644 index 000000000..d83690e1b --- /dev/null +++ b/source/example-app/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/source/example-app/app/controllers/charges_controller.rb b/source/example-app/app/controllers/charges_controller.rb new file mode 100644 index 000000000..5c57af40d --- /dev/null +++ b/source/example-app/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 action: 'show', status: :created, location: @charge } + else + format.html { render action: '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 { head :no_content } + else + format.html { render action: '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 } + 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(:created, :paid, :amount, :currency, :refunded, :customer_id) + end +end diff --git a/source/example-app/app/controllers/concerns/.keep b/source/example-app/app/controllers/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/app/controllers/customers_controller.rb b/source/example-app/app/controllers/customers_controller.rb new file mode 100644 index 000000000..0210a69a7 --- /dev/null +++ b/source/example-app/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 action: 'show', status: :created, location: @customer } + else + format.html { render action: '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 { head :no_content } + else + format.html { render action: '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 } + 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/source/example-app/app/helpers/application_helper.rb b/source/example-app/app/helpers/application_helper.rb new file mode 100644 index 000000000..2069138ce --- /dev/null +++ b/source/example-app/app/helpers/application_helper.rb @@ -0,0 +1,9 @@ +module ApplicationHelper + def format_full_name(person) + "#{person.first_name} #{person.last_name}" + end + + def epoch_to_date(epoch) + Time.at(epoch) + end +end diff --git a/source/example-app/app/helpers/charges_helper.rb b/source/example-app/app/helpers/charges_helper.rb new file mode 100644 index 000000000..414ee900f --- /dev/null +++ b/source/example-app/app/helpers/charges_helper.rb @@ -0,0 +1,2 @@ +module ChargesHelper +end diff --git a/source/example-app/app/helpers/customers_helper.rb b/source/example-app/app/helpers/customers_helper.rb new file mode 100644 index 000000000..a07ce2943 --- /dev/null +++ b/source/example-app/app/helpers/customers_helper.rb @@ -0,0 +1,2 @@ +module CustomersHelper +end diff --git a/source/example-app/app/mailers/.keep b/source/example-app/app/mailers/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/app/models/.keep b/source/example-app/app/models/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/app/models/charge.rb b/source/example-app/app/models/charge.rb new file mode 100644 index 000000000..9cd227100 --- /dev/null +++ b/source/example-app/app/models/charge.rb @@ -0,0 +1,12 @@ +class Charge < ActiveRecord::Base + belongs_to :customer + + # I'm not fully knowing the conditions to filter the different types + # I can investigate them, but it's going to take me longer to complete the challenge + # + # we can put indexes on columns to make the queries faster + + scope :failed, -> { where(paid: false) } + scope :disputed, -> { where(paid: true, refunded: true) } + scope :successful, -> { where(paid: true, refunded: false) } +end diff --git a/source/example-app/app/models/concerns/.keep b/source/example-app/app/models/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/app/models/customer.rb b/source/example-app/app/models/customer.rb new file mode 100644 index 000000000..047dcb0c0 --- /dev/null +++ b/source/example-app/app/models/customer.rb @@ -0,0 +1,2 @@ +class Customer < ActiveRecord::Base +end diff --git a/source/example-app/app/views/charges/_charge.html.erb b/source/example-app/app/views/charges/_charge.html.erb new file mode 100644 index 000000000..7eff1dcb0 --- /dev/null +++ b/source/example-app/app/views/charges/_charge.html.erb @@ -0,0 +1,12 @@ + + <%= charge.created %> + <%= charge.paid %> + <%= charge.amount %> + <%= charge.currency %> + <%= charge.refunded %> + + <%= "#{charge.customer.first_name} #{charge.customer.last_name}" %> + <%= 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/source/example-app/app/views/charges/_charges.html.erb b/source/example-app/app/views/charges/_charges.html.erb new file mode 100644 index 000000000..1d2ccd9ea --- /dev/null +++ b/source/example-app/app/views/charges/_charges.html.erb @@ -0,0 +1,19 @@ +

<%= title %>

+> + + + + + + + + + + + + + + + <%= render partial: 'charge', collection: charges, locals: {row_class: row_class} %> + +
CreatedPaidAmountCurrencyRefundedCustomer
\ No newline at end of file diff --git a/source/example-app/app/views/charges/_form.html.erb b/source/example-app/app/views/charges/_form.html.erb new file mode 100644 index 000000000..f818a123a --- /dev/null +++ b/source/example-app/app/views/charges/_form.html.erb @@ -0,0 +1,41 @@ +<%= form_for(@charge) do |f| %> + <% if @charge.errors.any? %> +
+

<%= pluralize(@charge.errors.count, "error") %> prohibited this charge from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :created %>
+ <%= f.number_field :created %> +
+
+ <%= f.label :paid %>
+ <%= f.check_box :paid %> +
+
+ <%= f.label :amount %>
+ <%= f.text_field :amount %> +
+
+ <%= f.label :currency %>
+ <%= f.text_field :currency %> +
+
+ <%= f.label :refunded %>
+ <%= f.check_box :refunded %> +
+
+ <%= f.label :customer %>
+ <%= f.text_field :customer %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/source/example-app/app/views/charges/_not_successful_charge.html.erb b/source/example-app/app/views/charges/_not_successful_charge.html.erb new file mode 100644 index 000000000..43eb2ec05 --- /dev/null +++ b/source/example-app/app/views/charges/_not_successful_charge.html.erb @@ -0,0 +1,9 @@ + + <%= format_full_name(charge.customer) %> + <%= "#{charge.amount} #{charge.currency}" %> + <%= epoch_to_date(charge.created) %> + + <%= 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/source/example-app/app/views/charges/_not_successful_charges.html.erb b/source/example-app/app/views/charges/_not_successful_charges.html.erb new file mode 100644 index 000000000..450dba880 --- /dev/null +++ b/source/example-app/app/views/charges/_not_successful_charges.html.erb @@ -0,0 +1,16 @@ +

<%= title %>

+> + + + + + + + + + + + + <%= render partial: 'not_successful_charge', collection: charges, as: :charge, locals: {row_class: row_class} %> + +
CustomerAmountDate
\ No newline at end of file diff --git a/source/example-app/app/views/charges/edit.html.erb b/source/example-app/app/views/charges/edit.html.erb new file mode 100644 index 000000000..a723d7a86 --- /dev/null +++ b/source/example-app/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/source/example-app/app/views/charges/index.html.erb b/source/example-app/app/views/charges/index.html.erb new file mode 100644 index 000000000..86de5cd2f --- /dev/null +++ b/source/example-app/app/views/charges/index.html.erb @@ -0,0 +1,30 @@ + +<%# we can extract the titles from the scope to assign them to the titles in the partial %> + +<%= render partial: 'not_successful_charges', + locals: { + id: 'failed-charges', + title: 'Failed Charges', + charges: @charges.failed, + row_class: 'danger' + } %> + +<%= render partial: 'not_successful_charges', + locals: { + id: 'disputed-charges', + title: 'Disputed Charges', + charges: @charges.disputed, + row_class: 'salmon' + } %> + +<%= render partial: 'charges', + locals: { + id: 'successful-charges', + title: 'Successful Charges', + charges: @charges.successful, + row_class: '' + } %> + +
+ +<%= link_to 'New Charge', new_charge_path %> diff --git a/source/example-app/app/views/charges/index.json.jbuilder b/source/example-app/app/views/charges/index.json.jbuilder new file mode 100644 index 000000000..9b184d029 --- /dev/null +++ b/source/example-app/app/views/charges/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@charges) do |charge| + json.extract! charge, :id, :created, :paid, :amount, :currency, :refunded, :customer_id + json.url charge_url(charge, format: :json) +end diff --git a/source/example-app/app/views/charges/new.html.erb b/source/example-app/app/views/charges/new.html.erb new file mode 100644 index 000000000..8dc59a97d --- /dev/null +++ b/source/example-app/app/views/charges/new.html.erb @@ -0,0 +1,5 @@ +

New charge

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

<%= notice %>

+ +

+ Created: + <%= @charge.created %> +

+ +

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

+ +

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

+ +

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

+ +

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

+ +

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

+ +<%= link_to 'Edit', edit_charge_path(@charge) %> | +<%= link_to 'Back', charges_path %> diff --git a/source/example-app/app/views/charges/show.json.jbuilder b/source/example-app/app/views/charges/show.json.jbuilder new file mode 100644 index 000000000..8e1676c10 --- /dev/null +++ b/source/example-app/app/views/charges/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @charge, :id, :created, :paid, :amount, :currency, :refunded, :customer_id, :created_at, :updated_at diff --git a/source/example-app/app/views/customers/_form.html.erb b/source/example-app/app/views/customers/_form.html.erb new file mode 100644 index 000000000..f2db3db1e --- /dev/null +++ b/source/example-app/app/views/customers/_form.html.erb @@ -0,0 +1,25 @@ +<%= form_for(@customer) do |f| %> + <% if @customer.errors.any? %> +
+

<%= pluralize(@customer.errors.count, "error") %> prohibited this customer from being saved:

+ + +
+ <% end %> + +
+ <%= f.label :first_name %>
+ <%= f.text_field :first_name %> +
+
+ <%= f.label :last_name %>
+ <%= f.text_field :last_name %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/source/example-app/app/views/customers/edit.html.erb b/source/example-app/app/views/customers/edit.html.erb new file mode 100644 index 000000000..d1ed0a71c --- /dev/null +++ b/source/example-app/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/source/example-app/app/views/customers/index.html.erb b/source/example-app/app/views/customers/index.html.erb new file mode 100644 index 000000000..47ea166c8 --- /dev/null +++ b/source/example-app/app/views/customers/index.html.erb @@ -0,0 +1,29 @@ +

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/source/example-app/app/views/customers/index.json.jbuilder b/source/example-app/app/views/customers/index.json.jbuilder new file mode 100644 index 000000000..6aac73b4c --- /dev/null +++ b/source/example-app/app/views/customers/index.json.jbuilder @@ -0,0 +1,4 @@ +json.array!(@customers) do |customer| + json.extract! customer, :id, :first_name, :last_name + json.url customer_url(customer, format: :json) +end diff --git a/source/example-app/app/views/customers/new.html.erb b/source/example-app/app/views/customers/new.html.erb new file mode 100644 index 000000000..a93c20371 --- /dev/null +++ b/source/example-app/app/views/customers/new.html.erb @@ -0,0 +1,5 @@ +

New customer

+ +<%= render 'form' %> + +<%= link_to 'Back', customers_path %> diff --git a/source/example-app/app/views/customers/show.html.erb b/source/example-app/app/views/customers/show.html.erb new file mode 100644 index 000000000..cacd47343 --- /dev/null +++ b/source/example-app/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/source/example-app/app/views/customers/show.json.jbuilder b/source/example-app/app/views/customers/show.json.jbuilder new file mode 100644 index 000000000..a7698de6a --- /dev/null +++ b/source/example-app/app/views/customers/show.json.jbuilder @@ -0,0 +1 @@ +json.extract! @customer, :id, :first_name, :last_name, :created_at, :updated_at diff --git a/source/example-app/app/views/layouts/application.html.erb b/source/example-app/app/views/layouts/application.html.erb new file mode 100644 index 000000000..3a16810af --- /dev/null +++ b/source/example-app/app/views/layouts/application.html.erb @@ -0,0 +1,14 @@ + + + + ExampleApp + <%= 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/source/example-app/bin/bundle b/source/example-app/bin/bundle new file mode 100755 index 000000000..66e9889e8 --- /dev/null +++ b/source/example-app/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/source/example-app/bin/rails b/source/example-app/bin/rails new file mode 100755 index 000000000..728cd85aa --- /dev/null +++ b/source/example-app/bin/rails @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +APP_PATH = File.expand_path('../../config/application', __FILE__) +require_relative '../config/boot' +require 'rails/commands' diff --git a/source/example-app/bin/rake b/source/example-app/bin/rake new file mode 100755 index 000000000..17240489f --- /dev/null +++ b/source/example-app/bin/rake @@ -0,0 +1,4 @@ +#!/usr/bin/env ruby +require_relative '../config/boot' +require 'rake' +Rake.application.run diff --git a/source/example-app/config.ru b/source/example-app/config.ru new file mode 100644 index 000000000..5bc2a619e --- /dev/null +++ b/source/example-app/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/source/example-app/config/application.rb b/source/example-app/config/application.rb new file mode 100644 index 000000000..42debc6df --- /dev/null +++ b/source/example-app/config/application.rb @@ -0,0 +1,28 @@ +require File.expand_path('../boot', __FILE__) + +# Pick the frameworks you want: +require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +require "sprockets/railtie" +# require "rails/test_unit/railtie" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(:default, Rails.env) + +module ExampleApp + 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 + end +end diff --git a/source/example-app/config/boot.rb b/source/example-app/config/boot.rb new file mode 100644 index 000000000..359673666 --- /dev/null +++ b/source/example-app/config/boot.rb @@ -0,0 +1,4 @@ +# Set up gems listed in the Gemfile. +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../../Gemfile', __FILE__) + +require 'bundler/setup' if File.exists?(ENV['BUNDLE_GEMFILE']) diff --git a/source/example-app/config/database.yml b/source/example-app/config/database.yml new file mode 100644 index 000000000..b8e2fea65 --- /dev/null +++ b/source/example-app/config/database.yml @@ -0,0 +1,60 @@ +# PostgreSQL. Versions 8.2 and up are supported. +# +# Install the pg driver: +# gem install pg +# On OS X with Homebrew: +# gem install pg -- --with-pg-config=/usr/local/bin/pg_config +# On OS X with MacPorts: +# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config +# On Windows: +# gem install pg +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +# +# Configure Using Gemfile +# gem 'pg' +# +development: + adapter: postgresql + encoding: unicode + database: example-app_development + pool: 5 + username: postgres + password: 123456 + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + + # The TCP port the server listens on. Defaults to 5432. + # If your server runs on a different port number, change accordingly. + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # Defaults to warning. + #min_messages: notice + +# Warning: The database defined as "test" will be erased and +# re-generated from your development database when you run "rake". +# Do not set this db to the same as development or production. +test: + adapter: postgresql + encoding: unicode + database: example-app_test + pool: 5 + username: postgres + password: 123456 + +production: + adapter: postgresql + encoding: unicode + database: example-app_production + pool: 5 + username: postgres + password: 123456 diff --git a/source/example-app/config/environment.rb b/source/example-app/config/environment.rb new file mode 100644 index 000000000..aebc109cd --- /dev/null +++ b/source/example-app/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require File.expand_path('../application', __FILE__) + +# Initialize the Rails application. +ExampleApp::Application.initialize! diff --git a/source/example-app/config/environments/development.rb b/source/example-app/config/environments/development.rb new file mode 100644 index 000000000..b4c8454f9 --- /dev/null +++ b/source/example-app/config/environments/development.rb @@ -0,0 +1,29 @@ +ExampleApp::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 +end diff --git a/source/example-app/config/environments/production.rb b/source/example-app/config/environments/production.rb new file mode 100644 index 000000000..9b343689c --- /dev/null +++ b/source/example-app/config/environments/production.rb @@ -0,0 +1,80 @@ +ExampleApp::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 thread 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 Rails's static asset server (Apache or nginx will already do this). + config.serve_static_assets = false + + # 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 + + # Generate digests for assets URLs. + config.assets.digest = true + + # Version of your assets, change this if you want to expire all your assets. + config.assets.version = '1.0' + + # 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 + + # Set to :debug to see everything in the log. + config.log_level = :info + + # 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" + + # Precompile additional assets. + # application.js, application.css, and all non-JS/CSS in app/assets folder are already added. + # config.assets.precompile += %w( search.js ) + + # 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 can not be found). + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners. + config.active_support.deprecation = :notify + + # Disable automatic flushing of the log to improve performance. + # config.autoflush_log = false + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new +end diff --git a/source/example-app/config/environments/test.rb b/source/example-app/config/environments/test.rb new file mode 100644 index 000000000..4838a862a --- /dev/null +++ b/source/example-app/config/environments/test.rb @@ -0,0 +1,36 @@ +ExampleApp::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 asset server for tests with Cache-Control for performance. + config.serve_static_assets = 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 + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr +end diff --git a/source/example-app/config/initializers/backtrace_silencers.rb b/source/example-app/config/initializers/backtrace_silencers.rb new file mode 100644 index 000000000..59385cdf3 --- /dev/null +++ b/source/example-app/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/source/example-app/config/initializers/filter_parameter_logging.rb b/source/example-app/config/initializers/filter_parameter_logging.rb new file mode 100644 index 000000000..4a994e1e7 --- /dev/null +++ b/source/example-app/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/source/example-app/config/initializers/inflections.rb b/source/example-app/config/initializers/inflections.rb new file mode 100644 index 000000000..ac033bf9d --- /dev/null +++ b/source/example-app/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/source/example-app/config/initializers/mime_types.rb b/source/example-app/config/initializers/mime_types.rb new file mode 100644 index 000000000..72aca7e44 --- /dev/null +++ b/source/example-app/config/initializers/mime_types.rb @@ -0,0 +1,5 @@ +# 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 +# Mime::Type.register_alias "text/html", :iphone diff --git a/source/example-app/config/initializers/secret_token.rb b/source/example-app/config/initializers/secret_token.rb new file mode 100644 index 000000000..9d6561b4f --- /dev/null +++ b/source/example-app/config/initializers/secret_token.rb @@ -0,0 +1,12 @@ +# 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 your secret_key_base is kept private +# if you're sharing your code publicly. +ExampleApp::Application.config.secret_key_base = '99be1c8a58f1aa9437bf98c0bc5c3c92ea7acb4373605129b25065db1dd66a7380ce89db5cc7487ee2b9b6f6272c7b0a177a76ebf2dd110bc0901adf759c0787' diff --git a/source/example-app/config/initializers/session_store.rb b/source/example-app/config/initializers/session_store.rb new file mode 100644 index 000000000..e9614dabe --- /dev/null +++ b/source/example-app/config/initializers/session_store.rb @@ -0,0 +1,3 @@ +# Be sure to restart your server when you modify this file. + +ExampleApp::Application.config.session_store :cookie_store, key: '_example-app_session' diff --git a/source/example-app/config/initializers/wrap_parameters.rb b/source/example-app/config/initializers/wrap_parameters.rb new file mode 100644 index 000000000..33725e95f --- /dev/null +++ b/source/example-app/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/source/example-app/config/locales/en.yml b/source/example-app/config/locales/en.yml new file mode 100644 index 000000000..065395716 --- /dev/null +++ b/source/example-app/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/source/example-app/config/routes.rb b/source/example-app/config/routes.rb new file mode 100644 index 000000000..bf48e892d --- /dev/null +++ b/source/example-app/config/routes.rb @@ -0,0 +1,60 @@ +ExampleApp::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". + + # 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/source/example-app/db/migrate/20210420134753_create_customers.rb b/source/example-app/db/migrate/20210420134753_create_customers.rb new file mode 100644 index 000000000..9dec5cbe7 --- /dev/null +++ b/source/example-app/db/migrate/20210420134753_create_customers.rb @@ -0,0 +1,11 @@ +class CreateCustomers < ActiveRecord::Migration + def change + create_table :customers do |t| + t.string :first_name, null: false, limit: 50 + t.string :last_name, null: false, limit: 50 + + # I didn't see in the specifications these columns, but I'm going to keep them + t.timestamps + end + end +end diff --git a/source/example-app/db/migrate/20210420134839_create_charges.rb b/source/example-app/db/migrate/20210420134839_create_charges.rb new file mode 100644 index 000000000..c7baa789c --- /dev/null +++ b/source/example-app/db/migrate/20210420134839_create_charges.rb @@ -0,0 +1,25 @@ +class CreateCharges < ActiveRecord::Migration + def change + # some of the default values and constraints might not apply for this case + # need more information about them, just guessed + + create_table :charges do |t| + t.integer :created, null: false + t.boolean :paid, null: false, default: false + t.decimal :amount + t.string :currency, null: false + t.boolean :refunded, null: false, default: false + t.references :customer, index: true, null: false + + t.timestamps + end + reversible do |dir| + dir.up do + execute <<-SQL + ALTER TABLE charges + ALTER COLUMN created SET DEFAULT date_part('epoch', current_timestamp); + SQL + end + end + end +end diff --git a/source/example-app/db/schema.rb b/source/example-app/db/schema.rb new file mode 100644 index 000000000..4ea05f28a --- /dev/null +++ b/source/example-app/db/schema.rb @@ -0,0 +1,39 @@ +# 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: 20210420134839) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "charges", force: true do |t| + t.integer "created", null: false + t.boolean "paid", default: false, null: false + t.decimal "amount" + t.string "currency", null: false + t.boolean "refunded", default: false, null: false + t.integer "customer_id", null: false + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "charges", ["customer_id"], name: "index_charges_on_customer_id", using: :btree + + create_table "customers", force: true do |t| + t.string "first_name", limit: 50, null: false + t.string "last_name", limit: 50, null: false + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/source/example-app/db/seeds.rb b/source/example-app/db/seeds.rb new file mode 100644 index 000000000..d5d132d05 --- /dev/null +++ b/source/example-app/db/seeds.rb @@ -0,0 +1,51 @@ +# 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) + +Customer.create(first_name: "Johny", last_name: "Flow") +Customer.create(first_name: "Raj", last_name: "Jamnis") +Customer.create(first_name: "Andrew", last_name: "Chung") +Customer.create(first_name: "Mike", last_name: "Smith") + +# 10 Should be successful Charges: +# - 5 Should be linked to Customer 1 +# - 3 Should be linked to Customer 2 +# - 1 Should be linked to Customer 3 +# - 1 Should be linked to Customer 4 + +Charge.create(paid: true, amount: 10.50, currency: 'usd', customer_id: 1) +Charge.create(paid: true, amount: 150, currency: 'usd', customer_id: 1) +Charge.create(paid: true, amount: 20, currency: 'usd', customer_id: 1) +Charge.create(paid: true, amount: 30, currency: 'usd', customer_id: 1) +Charge.create(paid: true, amount: 45, currency: 'usd', customer_id: 1) + +Charge.create(paid: true, amount: 10.50, currency: 'usd', customer_id: 2) +Charge.create(paid: true, amount: 150, currency: 'usd', customer_id: 2) +Charge.create(paid: true, amount: 20, currency: 'usd', customer_id: 2) + +Charge.create(paid: true, amount: 30, currency: 'usd', customer_id: 3) + +Charge.create(paid: true, amount: 45, currency: 'usd', customer_id: 4) + + +# 5 Should be Charges that failed: +# - 3 Should be linked to Customer 3 +# - 2 Should be linked to Customer 4 + +Charge.create(paid: false, amount: 45, currency: 'usd', refunded: true, customer_id: 3) +Charge.create(paid: false, amount: 45, currency: 'usd', refunded: true, customer_id: 3) +Charge.create(paid: false, amount: 45, currency: 'usd', refunded: true, customer_id: 3) + +Charge.create(paid: false, amount: 45, currency: 'usd', refunded: true, customer_id: 4) +Charge.create(paid: false, amount: 45, currency: 'usd', refunded: true, customer_id: 4) + +# 5 should be disputed: +# - 3 should be linked to Customer 1 +# - 2 should be linked to customer 2 + +Charge.create(paid: true, amount: 45, currency: 'usd', refunded: true, customer_id: 1) +Charge.create(paid: true, amount: 45, currency: 'usd', refunded: true, customer_id: 2) diff --git a/source/example-app/lib/assets/.keep b/source/example-app/lib/assets/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/lib/tasks/.keep b/source/example-app/lib/tasks/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/log/.keep b/source/example-app/log/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/public/404.html b/source/example-app/public/404.html new file mode 100644 index 000000000..a0daa0c15 --- /dev/null +++ b/source/example-app/public/404.html @@ -0,0 +1,58 @@ + + + + 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/source/example-app/public/422.html b/source/example-app/public/422.html new file mode 100644 index 000000000..fbb4b84d7 --- /dev/null +++ b/source/example-app/public/422.html @@ -0,0 +1,58 @@ + + + + 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/source/example-app/public/500.html b/source/example-app/public/500.html new file mode 100644 index 000000000..e9052d35b --- /dev/null +++ b/source/example-app/public/500.html @@ -0,0 +1,57 @@ + + + + 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/source/example-app/public/favicon.ico b/source/example-app/public/favicon.ico new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/public/robots.txt b/source/example-app/public/robots.txt new file mode 100644 index 000000000..1a3a5e4dd --- /dev/null +++ b/source/example-app/public/robots.txt @@ -0,0 +1,5 @@ +# See http://www.robotstxt.org/wc/norobots.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/source/example-app/spec/controllers/charges_controller_spec.rb b/source/example-app/spec/controllers/charges_controller_spec.rb new file mode 100644 index 000000000..e2a7f9c4c --- /dev/null +++ b/source/example-app/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/source/example-app/spec/controllers/customers_controller_spec.rb b/source/example-app/spec/controllers/customers_controller_spec.rb new file mode 100644 index 000000000..60980a9d4 --- /dev/null +++ b/source/example-app/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/source/example-app/spec/features/charges_spec.rb b/source/example-app/spec/features/charges_spec.rb new file mode 100644 index 000000000..4a0535b2a --- /dev/null +++ b/source/example-app/spec/features/charges_spec.rb @@ -0,0 +1,21 @@ +require "rails_helper" + +RSpec.feature "Charges index", :type => :feature do + before(:each) do + visit "/charges/" + end + + describe "User visits the index page of the transactions" do + it "Can see the lists of types of transactions" do + expect(page).to have_text("Successful Charges") + expect(page).to have_text("Failed Charges") + expect(page).to have_text("Disputed Charges") + end + + it "Can see the exact number of transactions in its corresponding list" do + page.assert_selector('#successful-charges .charge-item', count: 10) + page.assert_selector('#failed-charges .charge-item', count: 5) + page.assert_selector('#disputed-charges .charge-item') + end + end +end \ No newline at end of file diff --git a/source/example-app/spec/helpers/charges_helper_spec.rb b/source/example-app/spec/helpers/charges_helper_spec.rb new file mode 100644 index 000000000..0c25eaa8c --- /dev/null +++ b/source/example-app/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/source/example-app/spec/helpers/customers_helper_spec.rb b/source/example-app/spec/helpers/customers_helper_spec.rb new file mode 100644 index 000000000..fa160374f --- /dev/null +++ b/source/example-app/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/source/example-app/spec/models/charge_spec.rb b/source/example-app/spec/models/charge_spec.rb new file mode 100644 index 000000000..a99f4cce3 --- /dev/null +++ b/source/example-app/spec/models/charge_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Charge, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/source/example-app/spec/models/customer_spec.rb b/source/example-app/spec/models/customer_spec.rb new file mode 100644 index 000000000..3399b1f56 --- /dev/null +++ b/source/example-app/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/source/example-app/spec/rails_helper.rb b/source/example-app/spec/rails_helper.rb new file mode 100644 index 000000000..bf60770ed --- /dev/null +++ b/source/example-app/spec/rails_helper.rb @@ -0,0 +1,89 @@ +require 'database_cleaner' + +# 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 before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.check_pending! +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 = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, :type => :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") + + config.before(:suite) do + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, js: true) do + DatabaseCleaner.strategy = :truncation + end + + config.before :suite do + load "#{Rails.root}/db/seeds.rb" + end + + config.after(:all) do + DatabaseCleaner.clean + end + + config.after(:suite) do + DatabaseCleaner.clean_with(:truncation) + end +end diff --git a/source/example-app/spec/requests/charges_spec.rb b/source/example-app/spec/requests/charges_spec.rb new file mode 100644 index 000000000..504ef7846 --- /dev/null +++ b/source/example-app/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/source/example-app/spec/requests/customers_spec.rb b/source/example-app/spec/requests/customers_spec.rb new file mode 100644 index 000000000..081166fb1 --- /dev/null +++ b/source/example-app/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/source/example-app/spec/routing/charges_routing_spec.rb b/source/example-app/spec/routing/charges_routing_spec.rb new file mode 100644 index 000000000..4b2d9c92f --- /dev/null +++ b/source/example-app/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/source/example-app/spec/routing/customers_routing_spec.rb b/source/example-app/spec/routing/customers_routing_spec.rb new file mode 100644 index 000000000..622cd626a --- /dev/null +++ b/source/example-app/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/source/example-app/spec/spec_helper.rb b/source/example-app/spec/spec_helper.rb new file mode 100644 index 000000000..ce33d66df --- /dev/null +++ b/source/example-app/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/source/example-app/spec/views/charges/_charge.html.erb b/source/example-app/spec/views/charges/_charge.html.erb new file mode 100644 index 000000000..76d341137 --- /dev/null +++ b/source/example-app/spec/views/charges/_charge.html.erb @@ -0,0 +1,12 @@ + + <%= charge.created %> + <%= charge.paid %> + <%= charge.amount %> + <%= charge.currency %> + <%= charge.refunded %> + <%# full name can go in a helper %> + <%= charge.customer.first_name %> + <%= 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/source/example-app/spec/views/charges/_charges.html.erb b/source/example-app/spec/views/charges/_charges.html.erb new file mode 100644 index 000000000..14dbd36fd --- /dev/null +++ b/source/example-app/spec/views/charges/_charges.html.erb @@ -0,0 +1,18 @@ +> + + + + + + + + + + + + + + + <%= render partial: 'charge', collection: charges %> + +
CreatedPaidAmountCurrencyRefundedCustomer
\ No newline at end of file diff --git a/source/example-app/spec/views/charges/edit.html.erb_spec.rb b/source/example-app/spec/views/charges/edit.html.erb_spec.rb new file mode 100644 index 000000000..5085974db --- /dev/null +++ b/source/example-app/spec/views/charges/edit.html.erb_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +RSpec.describe "charges/edit", type: :view do + before(:each) do + @charge = assign(:charge, Charge.create!( + :created => 1, + :paid => false, + :amount => "9.99", + :currency => "MyString", + :refunded => false, + :customer => nil + )) + end + + it "renders the edit charge form" do + render + + assert_select "form[action=?][method=?]", charge_path(@charge), "post" do + + assert_select "input#charge_created[name=?]", "charge[created]" + + 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]" + + assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" + end + end +end diff --git a/source/example-app/spec/views/charges/index.html.erb b/source/example-app/spec/views/charges/index.html.erb new file mode 100644 index 000000000..c51a5c3bc --- /dev/null +++ b/source/example-app/spec/views/charges/index.html.erb @@ -0,0 +1,18 @@ + +<%# we can extract the titles from the scope to assign them to the titles in the partial %> + +

Failed Charges

+ +<%= render partial: 'charges', locals: {charges: @charges.failed} %> + +

Disputed Charges

+ +<%= render partial: 'charges', locals: {charges: @charges.disputed} %> + +

Successful Charges

+ +<%= render partial: 'charges', locals: {charges: @charges.successful} %> + +
+ +<%= link_to 'New Charge', new_charge_path %> diff --git a/source/example-app/spec/views/charges/index.html.erb_spec.rb b/source/example-app/spec/views/charges/index.html.erb_spec.rb new file mode 100644 index 000000000..4e3adf234 --- /dev/null +++ b/source/example-app/spec/views/charges/index.html.erb_spec.rb @@ -0,0 +1,34 @@ +require 'rails_helper' + +RSpec.describe "charges/index", type: :view do + before(:each) do + assign(:charges, [ + Charge.create!( + :created => 2, + :paid => false, + :amount => "9.99", + :currency => "Currency", + :refunded => false, + :customer => nil + ), + Charge.create!( + :created => 2, + :paid => false, + :amount => "9.99", + :currency => "Currency", + :refunded => false, + :customer => nil + ) + ]) + end + + it "renders a list of charges" do + render + assert_select "tr>td", :text => 2.to_s, :count => 2 + assert_select "tr>td", :text => false.to_s, :count => 2 + assert_select "tr>td", :text => "9.99".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 "tr>td", :text => nil.to_s, :count => 2 + end +end diff --git a/source/example-app/spec/views/charges/new.html.erb_spec.rb b/source/example-app/spec/views/charges/new.html.erb_spec.rb new file mode 100644 index 000000000..acf7bdb14 --- /dev/null +++ b/source/example-app/spec/views/charges/new.html.erb_spec.rb @@ -0,0 +1,33 @@ +require 'rails_helper' + +RSpec.describe "charges/new", type: :view do + before(:each) do + assign(:charge, Charge.new( + :created => 1, + :paid => false, + :amount => "9.99", + :currency => "MyString", + :refunded => false, + :customer => nil + )) + end + + it "renders new charge form" do + render + + assert_select "form[action=?][method=?]", charges_path, "post" do + + assert_select "input#charge_created[name=?]", "charge[created]" + + 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]" + + assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" + end + end +end diff --git a/source/example-app/spec/views/charges/show.html.erb_spec.rb b/source/example-app/spec/views/charges/show.html.erb_spec.rb new file mode 100644 index 000000000..ba2c49779 --- /dev/null +++ b/source/example-app/spec/views/charges/show.html.erb_spec.rb @@ -0,0 +1,24 @@ +require 'rails_helper' + +RSpec.describe "charges/show", type: :view do + before(:each) do + @charge = assign(:charge, Charge.create!( + :created => 2, + :paid => false, + :amount => "9.99", + :currency => "Currency", + :refunded => false, + :customer => nil + )) + end + + it "renders attributes in

" do + render + expect(rendered).to match(/2/) + expect(rendered).to match(/false/) + expect(rendered).to match(/9.99/) + expect(rendered).to match(/Currency/) + expect(rendered).to match(/false/) + expect(rendered).to match(//) + end +end diff --git a/source/example-app/spec/views/customers/edit.html.erb_spec.rb b/source/example-app/spec/views/customers/edit.html.erb_spec.rb new file mode 100644 index 000000000..35f127c2c --- /dev/null +++ b/source/example-app/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/source/example-app/spec/views/customers/index.html.erb_spec.rb b/source/example-app/spec/views/customers/index.html.erb_spec.rb new file mode 100644 index 000000000..76a42155b --- /dev/null +++ b/source/example-app/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/source/example-app/spec/views/customers/new.html.erb_spec.rb b/source/example-app/spec/views/customers/new.html.erb_spec.rb new file mode 100644 index 000000000..bb26fd937 --- /dev/null +++ b/source/example-app/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/source/example-app/spec/views/customers/show.html.erb_spec.rb b/source/example-app/spec/views/customers/show.html.erb_spec.rb new file mode 100644 index 000000000..28ce03fd9 --- /dev/null +++ b/source/example-app/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 diff --git a/source/example-app/vendor/assets/javascripts/.keep b/source/example-app/vendor/assets/javascripts/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/example-app/vendor/assets/stylesheets/.keep b/source/example-app/vendor/assets/stylesheets/.keep new file mode 100644 index 000000000..e69de29bb