diff --git a/Makefile b/Makefile new file mode 100755 index 000000000..a38a46b0b --- /dev/null +++ b/Makefile @@ -0,0 +1,84 @@ +SHELL:=/bin/bash + +wipe-all: down wipe-volumes wipe-images wipe-containers + +wipe-volumes: + @$$(cd gt-app/tmp && ls -A | grep -v '\.keep' | xargs sudo rm -rf) + @if [[ -n "$$(docker volume ls -qf dangling=true)" ]]; then\ + docker volume rm -f $$(docker volume ls -qf dangling=true);\ + fi + @docker volume ls -qf dangling=true | xargs -r docker volume rm + +wipe-images: + @if [[ -n "$$(docker images --filter "dangling=true" -q --no-trunc)" ]]; then\ + docker rmi -f $$(docker images --filter "dangling=true" -q --no-trunc);\ + fi + @if [[ -n "$$(docker images | grep "none" | awk '/ / { print $3 }')" ]]; then\ + docker rmi -f $$(docker images | grep "none" | awk '/ / { print $3 }');\ + fi + +wipe-containers: + @if [[ -n "$$(docker ps -qa --no-trunc --filter "status=exited")" ]]; then\ + docker rm -f $$(docker ps -qa --no-trunc --filter "status=exited");\ + fi + +wipe-postgres-redis-data: + @sudo rm -rf tmp + +down: + @docker-compose down + @docker-compose kill + +install-docker: + @echo "Installing Docker" + + @sudo apt-get update + + @sudo apt-get install \ + apt-transport-https \ + ca-certificates \ + curl \ + software-properties-common -y + + @curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - + + @sudo add-apt-repository \ + "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ + $$(lsb_release -cs) \ + stable" + + @sudo apt-get update + + @sudo apt-get install docker-ce + + @sudo curl -L https://github.com/docker/compose/releases/download/1.21.0/docker-compose-$(uname -s)-$(uname -m) \ + -o /usr/local/bin/docker-compose + + @sudo chmod +x /usr/local/bin/docker-compose + @sleep 5 + @echo "Docker Installed successfully" + +install-docker-if-not-already-installed: + @if [ -z "$$(which docker)" ]; then\ + make install-docker;\ + fi + +build-all-docker-images: + @echo "Building docker images." + @echo "Grab a coffe and wait." + @docker-compose build + @echo "Docker images built" + +pull: + @git pull origin $$(git branch | grep \* | cut -d ' ' -f2) --rebase + +push: + @git push origin $$(git branch | grep \* | cut -d ' ' -f2) --force-with-lease + +up: + @docker-compose up -d + +set-up: install-docker-if-not-already-installed down build-all-docker-images + +reset: wipe-all set-up + diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100755 index 000000000..7534d5500 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,14 @@ +version: '3.7' + +services: + web: + stdin_open: true + tty: true + build: + context: ./source + target: rails_app + volumes: + - ./source:/home/rails/myapp + ports: + - "3000:3000" + diff --git a/source/.gitignore b/source/.gitignore new file mode 100644 index 000000000..050c9d95c --- /dev/null +++ b/source/.gitignore @@ -0,0 +1,17 @@ +# See https://help.github.com/articles/ignoring-files for more about ignoring files. +# +# If you find yourself ignoring temporary files generated by your text editor +# or operating system, you probably want to add a global ignore instead: +# git config --global core.excludesfile '~/.gitignore_global' + +# Ignore bundler config. +/.bundle + +# Ignore the default SQLite database. +/db/*.sqlite3 +/db/*.sqlite3-journal + +# Ignore all logfiles and tempfiles. +/log/* +!/log/.keep +/tmp diff --git a/source/.rubocop-excludes.yml b/source/.rubocop-excludes.yml new file mode 100644 index 000000000..cbb10298a --- /dev/null +++ b/source/.rubocop-excludes.yml @@ -0,0 +1,26 @@ +AllCops: + Exclude: + # Rubocop defaults + - Gemfile + - vendor/**/* + - tmp/**/* + # Framework generated configuration files + - config/boot.rb + - config/application.rb + - config/environment.rb + - config/environments/* + - config/initializers/* + - bin/**/* + - script/**/* + - db/schema.rb + - Rakefile + # rspec generated files + - spec/spec_helper.rb + - spec/rails_helper.rb +# auto genrate scafold files + - spec/controllers/* + - spec/routing/* + - spec/requests/* + - spec/views/**/* + - app/controllers/* + - app/views/**/* \ No newline at end of file diff --git a/source/.rubocop.yml b/source/.rubocop.yml new file mode 100644 index 000000000..e77891306 --- /dev/null +++ b/source/.rubocop.yml @@ -0,0 +1,61 @@ +AllCops: + TargetRubyVersion: 2.5.1 + +Bundler/OrderedGems: + Enabled: false + +Layout/ParameterAlignment: + EnforcedStyle: with_fixed_indentation + +Metrics/AbcSize: + Max: 20 + +Metrics/BlockLength: + Exclude: + - config/routes.rb + - spec/**/*.rb + - config/routes/*.rb + +Layout/LineLength: + Max: 120 + Exclude: + - config/routes.rb + IgnoreCopDirectives: true + +Metrics/MethodLength: + Max: 50 + Exclude: + - db/migrate/*.rb + + +Style/ClassAndModuleChildren: + Enabled: false + +Style/Documentation: + Enabled: false + +Style/FrozenStringLiteralComment: + Enabled: false + +Layout/SpaceAroundMethodCallOperator: + Enabled: true + +Lint/RaiseException: + Enabled: true + +Lint/StructNewOverride: + Enabled: true + +Style/ExponentialNotation: + Enabled: true + +Style/HashEachMethods: + Enabled: true + +Style/HashTransformKeys: + Enabled: true + +Style/HashTransformValues: + Enabled: true + +inherit_from: .rubocop-excludes.yml diff --git a/source/Dockerfile b/source/Dockerfile new file mode 100644 index 000000000..a70506a2d --- /dev/null +++ b/source/Dockerfile @@ -0,0 +1,29 @@ +FROM ruby:2.5.1 AS base + +RUN apt-get update -qq && apt-get install -y build-essential libpq-dev nodejs + +RUN useradd -ms /bin/bash rails + +USER rails + +RUN mkdir /home/rails/myapp + +WORKDIR /home/rails/myapp + +COPY Gemfile* ./ + +RUN bundle install + +COPY . . + +FROM base AS rails_app + +COPY ./docker-entrypoint.sh /usr/local/bin/ + +ENTRYPOINT ["docker-entrypoint.sh"] + +CMD [ "bundle", "exec", "rails s -p 3000 -b '0.0.0.0'"] + +FROM base AS sidekiq_app + +CMD ["bundle", "exec", "sidekiq"] diff --git a/source/Gemfile b/source/Gemfile new file mode 100644 index 000000000..26435c5b6 --- /dev/null +++ b/source/Gemfile @@ -0,0 +1,59 @@ +source 'https://rubygems.org' + + +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '4.2.11' +# Use sqlite3 as the database for Active Record +gem 'sqlite3', '1.3.13' +# 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' + gem 'rspec-rails', '~> 3.7' + gem 'pry-byebug' + gem 'shoulda-matchers' + gem 'database_cleaner' + gem 'factory_bot_rails' + gem 'faker' + gem 'rubocop' + gem 'capybara' +end +gem 'simplecov', require: false, group: :test + +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 +gem "therubyracer" +gem "less-rails" +gem "twitter-bootstrap-rails" +gem 'bootstrap-generators', '~> 3.3.4' diff --git a/source/Gemfile.lock b/source/Gemfile.lock new file mode 100644 index 000000000..c3fc89512 --- /dev/null +++ b/source/Gemfile.lock @@ -0,0 +1,270 @@ +GEM + remote: https://rubygems.org/ + specs: + actionmailer (4.2.11) + actionpack (= 4.2.11) + actionview (= 4.2.11) + activejob (= 4.2.11) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 1.0, >= 1.0.5) + actionpack (4.2.11) + actionview (= 4.2.11) + activesupport (= 4.2.11) + 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) + activesupport (= 4.2.11) + 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) + activesupport (= 4.2.11) + globalid (>= 0.3.0) + activemodel (4.2.11) + activesupport (= 4.2.11) + builder (~> 3.1) + activerecord (4.2.11) + activemodel (= 4.2.11) + activesupport (= 4.2.11) + arel (~> 6.0) + activesupport (4.2.11) + i18n (~> 0.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + arel (6.0.4) + ast (2.4.0) + binding_of_caller (0.8.0) + debug_inspector (>= 0.0.1) + bootstrap-generators (3.3.4) + railties (>= 3.1.0) + builder (3.2.4) + byebug (11.1.3) + capybara (3.32.1) + addressable + mini_mime (>= 0.1.3) + nokogiri (~> 1.8) + rack (>= 1.6.0) + rack-test (>= 0.6.3) + regexp_parser (~> 1.5) + xpath (~> 3.2) + coderay (1.1.2) + 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) + commonjs (0.2.7) + concurrent-ruby (1.1.6) + crass (1.0.6) + database_cleaner (1.8.5) + debug_inspector (0.0.3) + diff-lcs (1.3) + docile (1.3.2) + erubis (2.7.0) + 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.12.2) + globalid (0.4.2) + activesupport (>= 4.2.0) + i18n (0.9.5) + concurrent-ruby (~> 1.0) + jaro_winkler (1.5.4) + jbuilder (2.9.1) + activesupport (>= 4.2.0) + jquery-rails (4.3.5) + rails-dom-testing (>= 1, < 3) + railties (>= 4.2.0) + thor (>= 0.14, < 2.0) + json (1.8.6) + less (2.6.0) + commonjs (~> 0.2.7) + less-rails (4.0.0) + actionpack (>= 4) + less (~> 2.6.0) + sprockets (>= 2) + libv8 (3.16.14.19) + loofah (2.5.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + 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.0) + nokogiri (1.10.9) + mini_portile2 (~> 2.4.0) + parallel (1.19.1) + parser (2.7.1.2) + ast (~> 2.4.0) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) + pry-byebug (3.9.0) + byebug (~> 11.0) + pry (~> 0.13.0) + public_suffix (4.0.4) + rack (1.6.13) + rack-test (0.6.3) + rack (>= 1.0) + rails (4.2.11) + actionmailer (= 4.2.11) + actionpack (= 4.2.11) + actionview (= 4.2.11) + activejob (= 4.2.11) + activemodel (= 4.2.11) + activerecord (= 4.2.11) + activesupport (= 4.2.11) + bundler (>= 1.3.0, < 2.0) + railties (= 4.2.11) + 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) + actionpack (= 4.2.11) + activesupport (= 4.2.11) + 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) + ref (2.0.0) + regexp_parser (1.7.0) + rexml (3.2.4) + rspec-core (3.9.2) + rspec-support (~> 3.9.3) + rspec-expectations (3.9.1) + 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.82.0) + jaro_winkler (~> 1.5.1) + parallel (~> 1.10) + parser (>= 2.7.0.1) + rainbow (>= 2.2.2, < 4.0) + rexml + ruby-progressbar (~> 1.7) + unicode-display_width (>= 1.4.0, < 2.0) + ruby-progressbar (1.10.1) + 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) + shoulda-matchers (4.3.0) + activesupport (>= 4.2.0) + simplecov (0.18.5) + docile (~> 1.1) + simplecov-html (~> 0.11) + simplecov-html (0.12.2) + spring (2.1.0) + sprockets (3.7.2) + concurrent-ruby (~> 1.0) + rack (> 1, < 3) + sprockets-rails (3.2.1) + actionpack (>= 4.0) + activesupport (>= 4.0) + sprockets (>= 3.0.0) + sqlite3 (1.3.13) + therubyracer (0.12.3) + libv8 (~> 3.16.14.15) + ref + 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) + twitter-bootstrap-rails (3.2.2) + actionpack (>= 3.1) + execjs (>= 2.2.2, >= 2.2) + less-rails (>= 2.5.0) + railties (>= 3.1) + tzinfo (1.2.7) + 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) + railties (>= 4.0) + sprockets-rails (>= 2.0, < 4.0) + xpath (3.2.0) + nokogiri (~> 1.8) + +PLATFORMS + ruby + +DEPENDENCIES + bootstrap-generators (~> 3.3.4) + byebug + capybara + coffee-rails (~> 4.1.0) + database_cleaner + factory_bot_rails + faker + jbuilder (~> 2.0) + jquery-rails + less-rails + pry-byebug + rails (= 4.2.11) + rspec-rails (~> 3.7) + rubocop + sass-rails (~> 5.0) + sdoc (~> 0.4.0) + shoulda-matchers + simplecov + spring + sqlite3 (= 1.3.13) + therubyracer + turbolinks + twitter-bootstrap-rails + uglifier (>= 1.3.0) + web-console (~> 2.0) + +BUNDLED WITH + 1.17.3 diff --git a/source/README.rdoc b/source/README.rdoc new file mode 100644 index 000000000..7cbcba0dc --- /dev/null +++ b/source/README.rdoc @@ -0,0 +1,22 @@ +# README + +* Ruby version 2.5.1 + +* Rails version 4.2.11 + +#### Instriction to run app +###### To setup DB Run: +* `rake db:create` +* `rake db:migrate` + +###### To seed customers and charges: +* `rake db:seed` + +###### Run rails app using +* `rails s` + +###### Run Test case using +* `rspec` + +##### run app using docker +* `make up` \ No newline at end of file diff --git a/source/Rakefile b/source/Rakefile new file mode 100644 index 000000000..ba6b733dd --- /dev/null +++ b/source/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/source/app/assets/images/.keep b/source/app/assets/images/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/app/assets/javascripts/application.js b/source/app/assets/javascripts/application.js new file mode 100644 index 000000000..bcb49bf38 --- /dev/null +++ b/source/app/assets/javascripts/application.js @@ -0,0 +1,17 @@ +// 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 twitter/bootstrap +//= require turbolinks +//= require_tree . diff --git a/source/app/assets/javascripts/bootstrap.js.coffee b/source/app/assets/javascripts/bootstrap.js.coffee new file mode 100644 index 000000000..944067987 --- /dev/null +++ b/source/app/assets/javascripts/bootstrap.js.coffee @@ -0,0 +1,3 @@ +jQuery -> + $("a[rel~=popover], .has-popover").popover() + $("a[rel~=tooltip], .has-tooltip").tooltip() diff --git a/source/app/assets/javascripts/charges.coffee b/source/app/assets/javascripts/charges.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/source/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/source/app/assets/javascripts/customers.coffee b/source/app/assets/javascripts/customers.coffee new file mode 100644 index 000000000..24f83d18b --- /dev/null +++ b/source/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/source/app/assets/stylesheets/application.css b/source/app/assets/stylesheets/application.css new file mode 100644 index 000000000..11392b975 --- /dev/null +++ b/source/app/assets/stylesheets/application.css @@ -0,0 +1,27 @@ +/* + * 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 + */ + +table, th, td { + padding: 2px; +} + +tr.failed { + background-color: #FF0000; +} + +tr.disputed { + background-color: #FF5400; +} diff --git a/source/app/assets/stylesheets/bootstrap_and_overrides.css.less b/source/app/assets/stylesheets/bootstrap_and_overrides.css.less new file mode 100644 index 000000000..d7b6f58b5 --- /dev/null +++ b/source/app/assets/stylesheets/bootstrap_and_overrides.css.less @@ -0,0 +1,32 @@ +@import "twitter/bootstrap/bootstrap"; + +// Set correct font paths +@glyphiconsEotPath: font-url("glyphicons-halflings-regular.eot"); +@glyphiconsEotPath_iefix: font-url("glyphicons-halflings-regular.eot?#iefix"); +@glyphiconsWoffPath: font-url("glyphicons-halflings-regular.woff"); +@glyphiconsTtfPath: font-url("glyphicons-halflings-regular.ttf"); +@glyphiconsSvgPath: font-url("glyphicons-halflings-regular.svg#glyphicons_halflingsregular"); + +// Set the Font Awesome (Font Awesome is default. You can disable by commenting below lines) +@fontAwesomeEotPath: font-url("fontawesome-webfont.eot"); +@fontAwesomeEotPath_iefix: font-url("fontawesome-webfont.eot?#iefix"); +@fontAwesomeWoffPath: font-url("fontawesome-webfont.woff"); +@fontAwesomeTtfPath: font-url("fontawesome-webfont.ttf"); +@fontAwesomeSvgPath: font-url("fontawesome-webfont.svg#fontawesomeregular"); + +// Font Awesome +@import "fontawesome/font-awesome"; + +// Glyphicons +//@import "twitter/bootstrap/glyphicons.less"; + +// Your custom LESS stylesheets goes here +// +// Since bootstrap was imported above you have access to its mixins which +// you may use and inherit here +// +// If you'd like to override bootstrap's own variables, you can do so here as well +// See http://twitter.github.com/bootstrap/customize.html#variables for their names and documentation +// +// Example: +// @link-color: #ff0000; diff --git a/source/app/controllers/application_controller.rb b/source/app/controllers/application_controller.rb new file mode 100644 index 000000000..d83690e1b --- /dev/null +++ b/source/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/app/controllers/charges_controller.rb b/source/app/controllers/charges_controller.rb new file mode 100644 index 000000000..c3d1eb3e2 --- /dev/null +++ b/source/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.includes(:customer) + 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(:created, :paid, :amount, :refunded, :currency, :customer_id) + end +end diff --git a/source/app/controllers/concerns/.keep b/source/app/controllers/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/app/controllers/customers_controller.rb b/source/app/controllers/customers_controller.rb new file mode 100644 index 000000000..c5cf7e989 --- /dev/null +++ b/source/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/source/app/helpers/application_helper.rb b/source/app/helpers/application_helper.rb new file mode 100644 index 000000000..de6be7945 --- /dev/null +++ b/source/app/helpers/application_helper.rb @@ -0,0 +1,2 @@ +module ApplicationHelper +end diff --git a/source/app/helpers/charges_helper.rb b/source/app/helpers/charges_helper.rb new file mode 100644 index 000000000..414ee900f --- /dev/null +++ b/source/app/helpers/charges_helper.rb @@ -0,0 +1,2 @@ +module ChargesHelper +end diff --git a/source/app/helpers/customers_helper.rb b/source/app/helpers/customers_helper.rb new file mode 100644 index 000000000..a07ce2943 --- /dev/null +++ b/source/app/helpers/customers_helper.rb @@ -0,0 +1,2 @@ +module CustomersHelper +end diff --git a/source/app/mailers/.keep b/source/app/mailers/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/app/models/.keep b/source/app/models/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/app/models/charge.rb b/source/app/models/charge.rb new file mode 100644 index 000000000..033ea6f2c --- /dev/null +++ b/source/app/models/charge.rb @@ -0,0 +1,15 @@ +class Charge < ActiveRecord::Base + belongs_to :customer, required: true + + scope :successful, -> { where(paid: true, refunded: false) } + scope :failed, -> { where(paid: false) } + scope :disputed, -> { where(paid: true, refunded: true) } + + def formated_amount + "$ #{amount / 1000.0}" + end + + def formated_date + Time.at(created).to_datetime.strftime('%c') + end +end diff --git a/source/app/models/concerns/.keep b/source/app/models/concerns/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/app/models/customer.rb b/source/app/models/customer.rb new file mode 100644 index 000000000..107b58c99 --- /dev/null +++ b/source/app/models/customer.rb @@ -0,0 +1,8 @@ +class Customer < ActiveRecord::Base + validates :first_name, presence: true + validates :last_name, presence: true + has_many :charges + def name + "#{first_name} #{last_name}" + end +end diff --git a/source/app/views/charges/_charge.json.jbuilder b/source/app/views/charges/_charge.json.jbuilder new file mode 100644 index 000000000..18eb8e4e9 --- /dev/null +++ b/source/app/views/charges/_charge.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! charge, :id, :created, :paid, :amount, :refunded, :currency, :customer_id, :created_at, :updated_at +json.url charge_url(charge, format: :json) diff --git a/source/app/views/charges/_charges.html.erb b/source/app/views/charges/_charges.html.erb new file mode 100644 index 000000000..c14e32f36 --- /dev/null +++ b/source/app/views/charges/_charges.html.erb @@ -0,0 +1,24 @@ +

<%= "#{title} Charges" %>

+
+ + + + + + + + + + + <% charges.each do |charge| %> + + + + + + <% end %> + +
Customer NameAmountDate
<%= charge.customer.name %><%= charge.formated_amount %><%= charge.formated_date %>
+
+ +
diff --git a/source/app/views/charges/_form.html.erb b/source/app/views/charges/_form.html.erb new file mode 100644 index 000000000..4ce5a8a03 --- /dev/null +++ b/source/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.number_field :amount %> +
+
+ <%= f.label :refunded %>
+ <%= f.check_box :refunded %> +
+
+ <%= f.label :currency %>
+ <%= f.text_field :currency %> +
+
+ <%= f.label :customer_id %>
+ <%= f.text_field :customer_id %> +
+
+ <%= f.submit %> +
+<% end %> diff --git a/source/app/views/charges/edit.html.erb b/source/app/views/charges/edit.html.erb new file mode 100644 index 000000000..8ed23b109 --- /dev/null +++ b/source/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/app/views/charges/index.html.erb b/source/app/views/charges/index.html.erb new file mode 100644 index 000000000..95e611764 --- /dev/null +++ b/source/app/views/charges/index.html.erb @@ -0,0 +1,6 @@ +

<%= notice %>

+
+ <%= render partial: 'charges', locals: { title: 'Failed', charges: @charges.failed } %> + <%= render partial: 'charges', locals: { title: 'Disputed', charges: @charges.disputed } %> + <%= render partial: 'charges', locals: { title: 'Successful', charges: @charges.successful } %> +
diff --git a/source/app/views/charges/index.json.jbuilder b/source/app/views/charges/index.json.jbuilder new file mode 100644 index 000000000..bf20b255c --- /dev/null +++ b/source/app/views/charges/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @charges, partial: "charges/charge", as: :charge diff --git a/source/app/views/charges/new.html.erb b/source/app/views/charges/new.html.erb new file mode 100644 index 000000000..55047d4e1 --- /dev/null +++ b/source/app/views/charges/new.html.erb @@ -0,0 +1,5 @@ +

New Charge

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

<%= notice %>

+ +

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

+ +

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

+ +

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

+ +

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

+ +

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

+ +

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

+ +<%= link_to 'Edit', edit_charge_path(@charge) %> | +<%= link_to 'Back', charges_path %> diff --git a/source/app/views/charges/show.json.jbuilder b/source/app/views/charges/show.json.jbuilder new file mode 100644 index 000000000..54f867331 --- /dev/null +++ b/source/app/views/charges/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "charges/charge", charge: @charge diff --git a/source/app/views/customers/_customer.json.jbuilder b/source/app/views/customers/_customer.json.jbuilder new file mode 100644 index 000000000..9f2b36660 --- /dev/null +++ b/source/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/source/app/views/customers/_form.html.erb b/source/app/views/customers/_form.html.erb new file mode 100644 index 000000000..a6a28ec49 --- /dev/null +++ b/source/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:

+ +
    + <% @customer.errors.full_messages.each do |message| %> +
  • <%= message %>
  • + <% end %> +
+
+ <% 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/app/views/customers/edit.html.erb b/source/app/views/customers/edit.html.erb new file mode 100644 index 000000000..11a41c26e --- /dev/null +++ b/source/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/app/views/customers/index.html.erb b/source/app/views/customers/index.html.erb new file mode 100644 index 000000000..036598f77 --- /dev/null +++ b/source/app/views/customers/index.html.erb @@ -0,0 +1,30 @@ +

<%= 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 %> +
\ No newline at end of file diff --git a/source/app/views/customers/index.json.jbuilder b/source/app/views/customers/index.json.jbuilder new file mode 100644 index 000000000..328edbd7a --- /dev/null +++ b/source/app/views/customers/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @customers, partial: "customers/customer", as: :customer diff --git a/source/app/views/customers/new.html.erb b/source/app/views/customers/new.html.erb new file mode 100644 index 000000000..10ca8eeb1 --- /dev/null +++ b/source/app/views/customers/new.html.erb @@ -0,0 +1,5 @@ +

New Customer

+ +<%= render 'form' %> + +<%= link_to 'Back', customers_path %> diff --git a/source/app/views/customers/show.html.erb b/source/app/views/customers/show.html.erb new file mode 100644 index 000000000..cacd47343 --- /dev/null +++ b/source/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/app/views/customers/show.json.jbuilder b/source/app/views/customers/show.json.jbuilder new file mode 100644 index 000000000..14cfc837e --- /dev/null +++ b/source/app/views/customers/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "customers/customer", customer: @customer diff --git a/source/app/views/layouts/application.html.erb b/source/app/views/layouts/application.html.erb new file mode 100644 index 000000000..914fe49a0 --- /dev/null +++ b/source/app/views/layouts/application.html.erb @@ -0,0 +1,26 @@ + + + + Source + <%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track' => true %> + <%= javascript_include_tag 'application', 'data-turbolinks-track' => true %> + <%= csrf_meta_tags %> + + +

<%= notice %>

+

<%= alert %>

+ + +<%= yield %> + + + diff --git a/source/bin/bundle b/source/bin/bundle new file mode 100755 index 000000000..66e9889e8 --- /dev/null +++ b/source/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/bin/rails b/source/bin/rails new file mode 100755 index 000000000..0138d79b7 --- /dev/null +++ b/source/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/source/bin/rake b/source/bin/rake new file mode 100755 index 000000000..d87d5f578 --- /dev/null +++ b/source/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/source/bin/setup b/source/bin/setup new file mode 100755 index 000000000..acdb2c138 --- /dev/null +++ b/source/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/source/bin/spring b/source/bin/spring new file mode 100755 index 000000000..d89ee495f --- /dev/null +++ b/source/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/source/config.ru b/source/config.ru new file mode 100644 index 000000000..bd83b2541 --- /dev/null +++ b/source/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/config/application.rb b/source/config/application.rb new file mode 100644 index 000000000..71f0b1386 --- /dev/null +++ b/source/config/application.rb @@ -0,0 +1,35 @@ +require File.expand_path('../boot', __FILE__) + +require "rails" +# Pick the frameworks you want: +require "active_model/railtie" +require "active_job/railtie" +require "active_record/railtie" +require "action_controller/railtie" +require "action_mailer/railtie" +require "action_view/railtie" +require "sprockets/railtie" +# require "rails/test_unit/railtie" + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module Source + 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/source/config/boot.rb b/source/config/boot.rb new file mode 100644 index 000000000..6b750f00b --- /dev/null +++ b/source/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/source/config/database.yml b/source/config/database.yml new file mode 100644 index 000000000..1c1a37ca8 --- /dev/null +++ b/source/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/source/config/environment.rb b/source/config/environment.rb new file mode 100644 index 000000000..ee8d90dc6 --- /dev/null +++ b/source/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/source/config/environments/development.rb b/source/config/environments/development.rb new file mode 100644 index 000000000..b55e2144b --- /dev/null +++ b/source/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/source/config/environments/production.rb b/source/config/environments/production.rb new file mode 100644 index 000000000..5c1b32e48 --- /dev/null +++ b/source/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/source/config/environments/test.rb b/source/config/environments/test.rb new file mode 100644 index 000000000..1c19f08b2 --- /dev/null +++ b/source/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/source/config/initializers/assets.rb b/source/config/initializers/assets.rb new file mode 100644 index 000000000..01ef3e663 --- /dev/null +++ b/source/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/source/config/initializers/backtrace_silencers.rb b/source/config/initializers/backtrace_silencers.rb new file mode 100644 index 000000000..59385cdf3 --- /dev/null +++ b/source/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/config/initializers/cookies_serializer.rb b/source/config/initializers/cookies_serializer.rb new file mode 100644 index 000000000..7f70458de --- /dev/null +++ b/source/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/source/config/initializers/filter_parameter_logging.rb b/source/config/initializers/filter_parameter_logging.rb new file mode 100644 index 000000000..4a994e1e7 --- /dev/null +++ b/source/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/config/initializers/inflections.rb b/source/config/initializers/inflections.rb new file mode 100644 index 000000000..ac033bf9d --- /dev/null +++ b/source/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/config/initializers/mime_types.rb b/source/config/initializers/mime_types.rb new file mode 100644 index 000000000..dc1899682 --- /dev/null +++ b/source/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/source/config/initializers/session_store.rb b/source/config/initializers/session_store.rb new file mode 100644 index 000000000..55bfb58fe --- /dev/null +++ b/source/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: '_source_session' diff --git a/source/config/initializers/to_time_preserves_timezone.rb b/source/config/initializers/to_time_preserves_timezone.rb new file mode 100644 index 000000000..8674be322 --- /dev/null +++ b/source/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/source/config/initializers/wrap_parameters.rb b/source/config/initializers/wrap_parameters.rb new file mode 100644 index 000000000..33725e95f --- /dev/null +++ b/source/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/config/locales/en.bootstrap.yml b/source/config/locales/en.bootstrap.yml new file mode 100644 index 000000000..8d7511904 --- /dev/null +++ b/source/config/locales/en.bootstrap.yml @@ -0,0 +1,23 @@ +# Sample localization file for English. Add more files in this directory for other locales. +# See https://github.com/svenfuchs/rails-i18n/tree/master/rails%2Flocale for starting points. + +en: + breadcrumbs: + application: + root: "Index" + pages: + pages: "Pages" + helpers: + actions: "Actions" + links: + back: "Back" + cancel: "Cancel" + confirm: "Are you sure?" + destroy: "Delete" + new: "New" + edit: "Edit" + titles: + edit: "Edit %{model}" + save: "Save %{model}" + new: "New %{model}" + delete: "Delete %{model}" diff --git a/source/config/locales/en.yml b/source/config/locales/en.yml new file mode 100644 index 000000000..065395716 --- /dev/null +++ b/source/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/config/routes.rb b/source/config/routes.rb new file mode 100644 index 000000000..eb00b6b5b --- /dev/null +++ b/source/config/routes.rb @@ -0,0 +1,58 @@ +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". + + # You can have the root of your site routed with "root" + root 'charges#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/config/secrets.yml b/source/config/secrets.yml new file mode 100644 index 000000000..797fd1fd2 --- /dev/null +++ b/source/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: aac9459f85fb0b33c5cc5a4ecad812a06153629c3528d2944f2c0965bd6a8dbc3c757c17026f2bee360c7f493c525adb32fa92a6a1ca506325fffd673c611c60 + +test: + secret_key_base: c1307477fbb3e0db7f78b6dcbff7fab5df3fcb6dac792920316be8376dc89ddc086bb89ada1a7e3c358f966bafba90beb9b47861aa7e450458960653bfe98294 + +# 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/source/db/migrate/20200506041742_create_customers.rb b/source/db/migrate/20200506041742_create_customers.rb new file mode 100644 index 000000000..4a0ac76c1 --- /dev/null +++ b/source/db/migrate/20200506041742_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/source/db/migrate/20200506063703_create_charges.rb b/source/db/migrate/20200506063703_create_charges.rb new file mode 100644 index 000000000..0e0e0afb1 --- /dev/null +++ b/source/db/migrate/20200506063703_create_charges.rb @@ -0,0 +1,14 @@ +class CreateCharges < ActiveRecord::Migration + def change + create_table :charges do |t| + t.integer :created + t.boolean :paid + t.integer :amount + t.boolean :refunded + t.string :currency + t.belongs_to :customer, index: true, foreign_key: true + + t.timestamps null: false + end + end +end diff --git a/source/db/schema.rb b/source/db/schema.rb new file mode 100644 index 000000000..1375398cd --- /dev/null +++ b/source/db/schema.rb @@ -0,0 +1,36 @@ +# 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: 20200506063703) do + + create_table "charges", force: :cascade do |t| + t.integer "created" + t.boolean "paid" + t.integer "amount" + t.boolean "refunded" + t.string "currency" + t.integer "customer_id" + 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" + t.string "last_name" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + +end diff --git a/source/db/seeds.rb b/source/db/seeds.rb new file mode 100644 index 000000000..02cb3cfa3 --- /dev/null +++ b/source/db/seeds.rb @@ -0,0 +1,43 @@ +# 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) +# Seed 4 customers + +@customer_one = FactoryBot.create(:customer, first_name: 'Johny', last_name: 'Flow') +@customer_two = FactoryBot.create(:customer, first_name: 'Raj', last_name: 'Jamnis') +@customer_three = FactoryBot.create(:customer, first_name: 'Andrew', last_name: 'Chung') +@customer_four = FactoryBot.create(:customer, first_name: 'Mike', last_name: 'Smith') + +# seed 10 Successful transactions: +# successful means(paid: true, refunded: false) +# 5 successful transaction be linked to Customer 1 +5.times { FactoryBot.create(:charge, customer: @customer_one, paid: true, refunded: false) } + +# 3 Should be linked to Customer 2 +3.times { FactoryBot.create(:charge, customer: @customer_two, paid: true, refunded: false) } + +# - 1 Should be linked to Customer 3 +FactoryBot.create(:charge, customer: @customer_three, paid: true, refunded: false) + +# - 1 Should be linked to Customer 4 +FactoryBot.create(:charge, customer: @customer_four, paid: true, refunded: false) + +# seed 5 Failed transactions: +# faild means(paid: false) +# 3 Should be linked to Customer 3 +3.times { FactoryBot.create(:charge, customer: @customer_three, paid: false) } + +# - 2 Should be linked to Customer 4 +2.times { FactoryBot.create(:charge, customer: @customer_four, paid: false) } + +# 5 Disputed transactions: +# disputed means paid: true, refunded: true +# 3 should be linked to Customer 1 +3.times { FactoryBot.create(:charge, customer: @customer_one, paid: true, refunded: true) } + +# - 2 should be linked to customer 2 +2.times { FactoryBot.create(:charge, customer: @customer_two, paid: true, refunded: true) } diff --git a/source/docker-entrypoint.sh b/source/docker-entrypoint.sh new file mode 100755 index 000000000..a39fa77ab --- /dev/null +++ b/source/docker-entrypoint.sh @@ -0,0 +1,8 @@ +#!/bin/bash +set -e + +rake db:create +rake db:migrate +rake db:seed + +exec "$@" diff --git a/source/lib/assets/.keep b/source/lib/assets/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/lib/tasks/.keep b/source/lib/tasks/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/log/.keep b/source/log/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/public/404.html b/source/public/404.html new file mode 100644 index 000000000..b612547fc --- /dev/null +++ b/source/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/source/public/422.html b/source/public/422.html new file mode 100644 index 000000000..a21f82b3b --- /dev/null +++ b/source/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/source/public/500.html b/source/public/500.html new file mode 100644 index 000000000..061abc587 --- /dev/null +++ b/source/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/source/public/favicon.ico b/source/public/favicon.ico new file mode 100644 index 000000000..e69de29bb diff --git a/source/public/robots.txt b/source/public/robots.txt new file mode 100644 index 000000000..3c9c7c01f --- /dev/null +++ b/source/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/source/spec/controllers/charges_controller_spec.rb b/source/spec/controllers/charges_controller_spec.rb new file mode 100644 index 000000000..f4312cf48 --- /dev/null +++ b/source/spec/controllers/charges_controller_spec.rb @@ -0,0 +1,147 @@ +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) { + { + created: 1573170720, paid: false, amount: 4309, refunded: false, + currency: "usd", customer_id: FactoryBot.create(:customer).id + } + } + + let(:invalid_attributes) { + {created: 1573170720, paid: false, customer_id: nil } + } + + # 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) { + { + created: 1373170720, paid: false, amount: 1000, refunded: false, + currency: "usd", customer_id: FactoryBot.create(:customer).id + } + } + + it "updates the requested charge" do + charge = Charge.create! valid_attributes + put :update, {:id => charge.to_param, :charge => new_attributes}, valid_session + charge.reload + expect(charge.created).to eq(1373170720) + 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/spec/controllers/customers_controller_spec.rb b/source/spec/controllers/customers_controller_spec.rb new file mode 100644 index 000000000..1d7717370 --- /dev/null +++ b/source/spec/controllers/customers_controller_spec.rb @@ -0,0 +1,142 @@ +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) { + { first_name: Faker::Name.first_name, last_name: Faker::Name.last_name } + } + + let(:invalid_attributes) { + { first_name: Faker::Name.first_name, last_name: nil } + } + + # 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) { + { first_name: 'new first_name', last_name: 'new last_name' } + } + + it "updates the requested customer" do + customer = Customer.create! valid_attributes + put :update, {:id => customer.to_param, :customer => new_attributes}, valid_session + customer.reload + expect(customer.first_name).to eq('new first_name') + expect(customer.last_name).to eq('new last_name') + 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/spec/factories/charges.rb b/source/spec/factories/charges.rb new file mode 100644 index 000000000..96abf3135 --- /dev/null +++ b/source/spec/factories/charges.rb @@ -0,0 +1,10 @@ +FactoryBot.define do + factory :charge do + created { Time.parse(Faker::Time.between(from: 1.years.ago, to: DateTime.now, format: :long)).to_i } + paid { false } + amount { Faker::Number.between(from: 10, to: 10_000) } + refunded { false } + currency { 'usd' } + association :customer, factory: :customer, strategy: :build + end +end diff --git a/source/spec/factories/customers.rb b/source/spec/factories/customers.rb new file mode 100644 index 000000000..8136d7d8e --- /dev/null +++ b/source/spec/factories/customers.rb @@ -0,0 +1,6 @@ +FactoryBot.define do + factory :customer do + first_name { Faker::Name.first_name } + last_name { Faker::Name.first_name } + end +end diff --git a/source/spec/models/charge_spec.rb b/source/spec/models/charge_spec.rb new file mode 100644 index 000000000..03ed56eee --- /dev/null +++ b/source/spec/models/charge_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Charge, type: :model do + it { should belong_to(:customer) } +end diff --git a/source/spec/models/customer_spec.rb b/source/spec/models/customer_spec.rb new file mode 100644 index 000000000..a45c3c1d3 --- /dev/null +++ b/source/spec/models/customer_spec.rb @@ -0,0 +1,6 @@ +require 'rails_helper' + +RSpec.describe Customer, type: :model do + it { should validate_presence_of(:first_name) } + it { should validate_presence_of(:last_name) } +end diff --git a/source/spec/rails_helper.rb b/source/spec/rails_helper.rb new file mode 100644 index 000000000..6333c8773 --- /dev/null +++ b/source/spec/rails_helper.rb @@ -0,0 +1,21 @@ +# 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' + +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end + +RSpec.configure do |config| + config.fixture_path = "#{::Rails.root}/spec/fixtures" + config.use_transactional_fixtures = true + config.infer_spec_type_from_file_location! + config.filter_rails_from_backtrace! +end diff --git a/source/spec/requests/charges_spec.rb b/source/spec/requests/charges_spec.rb new file mode 100644 index 000000000..504ef7846 --- /dev/null +++ b/source/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/spec/requests/customers_spec.rb b/source/spec/requests/customers_spec.rb new file mode 100644 index 000000000..081166fb1 --- /dev/null +++ b/source/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/spec/routing/charges_routing_spec.rb b/source/spec/routing/charges_routing_spec.rb new file mode 100644 index 000000000..4b2d9c92f --- /dev/null +++ b/source/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/spec/routing/customers_routing_spec.rb b/source/spec/routing/customers_routing_spec.rb new file mode 100644 index 000000000..622cd626a --- /dev/null +++ b/source/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/spec/spec_helper.rb b/source/spec/spec_helper.rb new file mode 100644 index 000000000..c3c565650 --- /dev/null +++ b/source/spec/spec_helper.rb @@ -0,0 +1,38 @@ +require 'factory_bot_rails' +require 'shoulda-matchers' +require 'database_cleaner' +require 'capybara/rspec' + +Shoulda::Matchers.configure do |config| + config.integrate do |with| + with.test_framework :rspec + with.library :active_record + with.library :active_model + end +end + +RSpec.configure do |config| + config.expect_with :rspec do |expectations| + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + config.mock_with :rspec do |mocks| + mocks.verify_partial_doubles = true + end + + config.shared_context_metadata_behavior = :apply_to_host_groups + + config.before(:suite) do + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do |example| + DatabaseCleaner.start + end + + config.append_after(:each) do + DatabaseCleaner.clean + end + + config.include FactoryBot::Syntax::Methods +end diff --git a/source/spec/views/charges/edit.html.erb_spec.rb b/source/spec/views/charges/edit.html.erb_spec.rb new file mode 100644 index 000000000..5d8015e8b --- /dev/null +++ b/source/spec/views/charges/edit.html.erb_spec.rb @@ -0,0 +1,26 @@ +require 'rails_helper' + +RSpec.describe "charges/edit", type: :view do + before(:each) do + @charge = assign(:charge, FactoryBot.create(:charge)) + 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_refunded[name=?]", "charge[refunded]" + + assert_select "input#charge_currency[name=?]", "charge[currency]" + + assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" + end + end +end diff --git a/source/spec/views/charges/index.html.erb_spec.rb b/source/spec/views/charges/index.html.erb_spec.rb new file mode 100644 index 000000000..d0d830c22 --- /dev/null +++ b/source/spec/views/charges/index.html.erb_spec.rb @@ -0,0 +1,46 @@ +require 'rails_helper' + +RSpec.describe "charges/index", type: :view do + before(:each) do + @customer_1 = FactoryBot.create(:customer, first_name: 'Johny', last_name: 'Flow') + @customer_2 = FactoryBot.create(:customer, first_name: 'Raj', last_name: 'Jamnis') + @customer_3 = FactoryBot.create(:customer, first_name: 'Andrew', last_name: 'Chung') + @customer_4 = FactoryBot.create(:customer, first_name: 'Mike', last_name: 'Smith') + 5.times{ FactoryBot.create(:charge, customer: @customer_1, paid: true, refunded: false) } + + 3.times{ FactoryBot.create(:charge, customer: @customer_2, paid: true, refunded: false) } + FactoryBot.create(:charge, customer: @customer_3, paid: true, refunded: false) + + FactoryBot.create(:charge, customer: @customer_4, paid: true, refunded: false) + + 3.times{ FactoryBot.create(:charge, customer: @customer_3, paid: false) } + + 2.times{ FactoryBot.create(:charge, customer: @customer_4, paid: false) } + + 3.times{ FactoryBot.create(:charge, customer: @customer_1, paid: true, refunded: true) } + + 2.times{ FactoryBot.create(:charge, customer: @customer_2, paid: true, refunded: true) } + + assign(:charges, Charge.all) + end + + it "renders a list of charges" do + render + # Verify that there are three lists on the screen. + # one for successful charges, + expect(render).to have_content('Successful Charges') + # one for failed charges and + expect(render).to have_content('Failed Charges') + # one for the disputed charges. + expect(render).to have_content('Disputed Charges') + + #Verify that in the Successful charges list there are 10 line items. + expect(render).to have_selector "tr.successful", count: 10 + + # Verify that in the failed charges list there are 5 failed charges. + expect(render).to have_selector "tr.failed", count: 5 + + # Verify that in the disputed charges list there are 5 disputed charges. + expect(render).to have_selector "tr.disputed", count: 5 + end +end diff --git a/source/spec/views/charges/new.html.erb_spec.rb b/source/spec/views/charges/new.html.erb_spec.rb new file mode 100644 index 000000000..8462cf1a8 --- /dev/null +++ b/source/spec/views/charges/new.html.erb_spec.rb @@ -0,0 +1,22 @@ +require 'rails_helper' + +RSpec.describe "charges/new", type: :view do + before(:each) do + assign(:charge, FactoryBot.create(:charge)) + end + + it "renders new charge form" do + render + 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_refunded[name=?]", "charge[refunded]" + + assert_select "input#charge_currency[name=?]", "charge[currency]" + + assert_select "input#charge_customer_id[name=?]", "charge[customer_id]" + end +end diff --git a/source/spec/views/customers/edit.html.erb_spec.rb b/source/spec/views/customers/edit.html.erb_spec.rb new file mode 100644 index 000000000..35f127c2c --- /dev/null +++ b/source/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/spec/views/customers/index.html.erb_spec.rb b/source/spec/views/customers/index.html.erb_spec.rb new file mode 100644 index 000000000..76a42155b --- /dev/null +++ b/source/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/spec/views/customers/new.html.erb_spec.rb b/source/spec/views/customers/new.html.erb_spec.rb new file mode 100644 index 000000000..bb26fd937 --- /dev/null +++ b/source/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/spec/views/customers/show.html.erb_spec.rb b/source/spec/views/customers/show.html.erb_spec.rb new file mode 100644 index 000000000..28ce03fd9 --- /dev/null +++ b/source/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/vendor/assets/javascripts/.keep b/source/vendor/assets/javascripts/.keep new file mode 100644 index 000000000..e69de29bb diff --git a/source/vendor/assets/stylesheets/.keep b/source/vendor/assets/stylesheets/.keep new file mode 100644 index 000000000..e69de29bb