diff --git a/Sample_App/Gemfile b/Sample_App/Gemfile new file mode 100644 index 0000000..12c6b9e --- /dev/null +++ b/Sample_App/Gemfile @@ -0,0 +1,71 @@ +source 'https://rubygems.org' +git_source(:github) { |repo| "https://github.com/#{repo}.git" } + +ruby '2.6.3' + +gem 'faker', '1.7.3' +gem 'bootstrap-sass', '3.4.1' +gem 'rails-controller-testing' +gem 'minitest', group: :test +gem 'minitest-reporters', group: :test +gem 'will_paginate', '3.1.7' +gem 'bootstrap-will_paginate', '1.0.0' +# Bundle edge Rails instead: gem 'rails', github: 'rails/rails' +gem 'rails', '~> 5.2.3' +# Use sqlite3 as the database for Active Record +gem 'sqlite3' +# Use Puma as the app server +gem 'puma', '~> 3.11' +# Use SCSS for stylesheets +gem 'sass-rails', '~> 5.0' +# Use Uglifier as compressor for JavaScript assets +gem 'uglifier', '>= 1.3.0' +# See https://github.com/rails/execjs#readme for more supported runtimes +# gem 'mini_racer', platforms: :ruby + +gem 'jquery-rails' + +# Use CoffeeScript for .coffee assets and views +gem 'coffee-rails', '~> 4.2' +# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks +gem 'turbolinks', '~> 5' +# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder +gem 'jbuilder', '~> 2.5' +# Use Redis adapter to run Action Cable in production +# gem 'redis', '~> 4.0' +# Use ActiveModel has_secure_password +gem 'bcrypt', '~> 3.1.12' + +# Use ActiveStorage variant +# gem 'mini_magick', '~> 4.8' + +# Use Capistrano for deployment +# gem 'capistrano-rails', group: :development + +# Reduces boot times through caching; required in config/boot.rb +gem 'bootsnap', '>= 1.1.0', require: false + +group :development, :test do + # Call 'byebug' anywhere in the code to stop execution and get a debugger console + gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] +end + +group :development do + # Access an interactive console on exception pages or by calling 'console' anywhere in the code. + gem 'web-console', '>= 3.3.0' + gem 'listen', '>= 3.0.5', '< 3.2' + # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring + gem 'spring' + gem 'spring-watcher-listen', '~> 2.0.0' +end + +group :test do + # Adds support for Capybara system testing and selenium driver + gem 'capybara', '>= 2.15' + gem 'selenium-webdriver' + # Easy installation and use of chromedriver to run system tests with Chrome + gem 'chromedriver-helper' +end + +# Windows does not include zoneinfo files, so bundle the tzinfo-data gem +gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] diff --git a/Sample_App/Gemfile.lock b/Sample_App/Gemfile.lock new file mode 100644 index 0000000..3563516 --- /dev/null +++ b/Sample_App/Gemfile.lock @@ -0,0 +1,253 @@ +GEM + remote: https://rubygems.org/ + specs: + actioncable (5.2.4.1) + actionpack (= 5.2.4.1) + nio4r (~> 2.0) + websocket-driver (>= 0.6.1) + actionmailer (5.2.4.1) + actionpack (= 5.2.4.1) + actionview (= 5.2.4.1) + activejob (= 5.2.4.1) + mail (~> 2.5, >= 2.5.4) + rails-dom-testing (~> 2.0) + actionpack (5.2.4.1) + actionview (= 5.2.4.1) + activesupport (= 5.2.4.1) + rack (~> 2.0, >= 2.0.8) + rack-test (>= 0.6.3) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.2) + actionview (5.2.4.1) + activesupport (= 5.2.4.1) + builder (~> 3.1) + erubi (~> 1.4) + rails-dom-testing (~> 2.0) + rails-html-sanitizer (~> 1.0, >= 1.0.3) + activejob (5.2.4.1) + activesupport (= 5.2.4.1) + globalid (>= 0.3.6) + activemodel (5.2.4.1) + activesupport (= 5.2.4.1) + activerecord (5.2.4.1) + activemodel (= 5.2.4.1) + activesupport (= 5.2.4.1) + arel (>= 9.0) + activestorage (5.2.4.1) + actionpack (= 5.2.4.1) + activerecord (= 5.2.4.1) + marcel (~> 0.3.1) + activesupport (5.2.4.1) + concurrent-ruby (~> 1.0, >= 1.0.2) + i18n (>= 0.7, < 2) + minitest (~> 5.1) + tzinfo (~> 1.1) + addressable (2.7.0) + public_suffix (>= 2.0.2, < 5.0) + ansi (1.5.0) + archive-zip (0.12.0) + io-like (~> 0.3.0) + arel (9.0.0) + autoprefixer-rails (9.7.3) + execjs + bcrypt (3.1.13) + bindex (0.8.1) + bootsnap (1.4.5) + msgpack (~> 1.0) + bootstrap-sass (3.4.1) + autoprefixer-rails (>= 5.2.1) + sassc (>= 2.0.0) + bootstrap-will_paginate (1.0.0) + will_paginate + builder (3.2.4) + byebug (11.0.1) + capybara (3.29.0) + 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) + childprocess (3.0.0) + chromedriver-helper (2.1.1) + archive-zip (~> 0.10) + nokogiri (~> 1.8) + coffee-rails (4.2.2) + coffee-script (>= 2.2.0) + railties (>= 4.0.0) + coffee-script (2.4.1) + coffee-script-source + execjs + coffee-script-source (1.12.2) + concurrent-ruby (1.1.5) + crass (1.0.5) + erubi (1.9.0) + execjs (2.7.0) + faker (1.7.3) + i18n (~> 0.5) + ffi (1.11.3) + globalid (0.4.2) + activesupport (>= 4.2.0) + i18n (0.9.5) + concurrent-ruby (~> 1.0) + io-like (0.3.0) + 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) + listen (3.1.5) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + ruby_dep (~> 1.2) + loofah (2.4.0) + crass (~> 1.0.2) + nokogiri (>= 1.5.9) + mail (2.7.1) + mini_mime (>= 0.1.1) + marcel (0.3.3) + mimemagic (~> 0.3.2) + method_source (0.9.2) + mimemagic (0.3.3) + mini_mime (1.0.2) + mini_portile2 (2.4.0) + minitest (5.13.0) + minitest-reporters (1.4.2) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + msgpack (1.3.1) + nio4r (2.5.2) + nokogiri (1.10.7) + mini_portile2 (~> 2.4.0) + public_suffix (4.0.1) + puma (3.12.2) + rack (2.0.8) + rack-test (1.1.0) + rack (>= 1.0, < 3) + rails (5.2.4.1) + actioncable (= 5.2.4.1) + actionmailer (= 5.2.4.1) + actionpack (= 5.2.4.1) + actionview (= 5.2.4.1) + activejob (= 5.2.4.1) + activemodel (= 5.2.4.1) + activerecord (= 5.2.4.1) + activestorage (= 5.2.4.1) + activesupport (= 5.2.4.1) + bundler (>= 1.3.0) + railties (= 5.2.4.1) + sprockets-rails (>= 2.0.0) + rails-controller-testing (1.0.4) + actionpack (>= 5.0.1.x) + actionview (>= 5.0.1.x) + activesupport (>= 5.0.1.x) + rails-dom-testing (2.0.3) + activesupport (>= 4.2.0) + nokogiri (>= 1.6) + rails-html-sanitizer (1.3.0) + loofah (~> 2.3) + railties (5.2.4.1) + actionpack (= 5.2.4.1) + activesupport (= 5.2.4.1) + method_source + rake (>= 0.8.7) + thor (>= 0.19.0, < 2.0) + rake (13.0.1) + rb-fsevent (0.10.3) + rb-inotify (0.10.0) + ffi (~> 1.0) + regexp_parser (1.6.0) + ruby-progressbar (1.10.1) + ruby_dep (1.5.0) + rubyzip (2.0.0) + sass (3.7.4) + sass-listen (~> 4.0.0) + sass-listen (4.0.0) + rb-fsevent (~> 0.9, >= 0.9.4) + rb-inotify (~> 0.9, >= 0.9.7) + sass-rails (5.1.0) + railties (>= 5.2.0) + sass (~> 3.1) + sprockets (>= 2.8, < 4.0) + sprockets-rails (>= 2.0, < 4.0) + tilt (>= 1.1, < 3) + sassc (2.2.1) + ffi (~> 1.9) + selenium-webdriver (3.142.6) + childprocess (>= 0.5, < 4.0) + rubyzip (>= 1.2.2) + spring (2.1.0) + spring-watcher-listen (2.0.1) + listen (>= 2.7, < 4.0) + spring (>= 1.2, < 3.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.4.2) + thor (1.0.1) + thread_safe (0.3.6) + tilt (2.0.10) + turbolinks (5.2.1) + turbolinks-source (~> 5.2) + turbolinks-source (5.2.0) + tzinfo (1.2.5) + thread_safe (~> 0.1) + uglifier (4.2.0) + execjs (>= 0.3.0, < 3) + web-console (3.7.0) + actionview (>= 5.0) + activemodel (>= 5.0) + bindex (>= 0.4.0) + railties (>= 5.0) + websocket-driver (0.7.1) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.4) + will_paginate (3.1.7) + xpath (3.2.0) + nokogiri (~> 1.8) + +PLATFORMS + ruby + +DEPENDENCIES + bcrypt (~> 3.1.12) + bootsnap (>= 1.1.0) + bootstrap-sass (= 3.4.1) + bootstrap-will_paginate (= 1.0.0) + byebug + capybara (>= 2.15) + chromedriver-helper + coffee-rails (~> 4.2) + faker (= 1.7.3) + jbuilder (~> 2.5) + jquery-rails + listen (>= 3.0.5, < 3.2) + minitest + minitest-reporters + puma (~> 3.11) + rails (~> 5.2.3) + rails-controller-testing + sass-rails (~> 5.0) + selenium-webdriver + spring + spring-watcher-listen (~> 2.0.0) + sqlite3 + turbolinks (~> 5) + tzinfo-data + uglifier (>= 1.3.0) + web-console (>= 3.3.0) + will_paginate (= 3.1.7) + +RUBY VERSION + ruby 2.6.3p62 + +BUNDLED WITH + 2.0.2 diff --git a/Sample_App/README.md b/Sample_App/README.md new file mode 100644 index 0000000..7db80e4 --- /dev/null +++ b/Sample_App/README.md @@ -0,0 +1,24 @@ +# 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 + +* ... diff --git a/Sample_App/Rakefile b/Sample_App/Rakefile new file mode 100644 index 0000000..e85f913 --- /dev/null +++ b/Sample_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_relative 'config/application' + +Rails.application.load_tasks diff --git a/Sample_App/app/assets/config/manifest.js b/Sample_App/app/assets/config/manifest.js new file mode 100644 index 0000000..b16e53d --- /dev/null +++ b/Sample_App/app/assets/config/manifest.js @@ -0,0 +1,3 @@ +//= link_tree ../images +//= link_directory ../javascripts .js +//= link_directory ../stylesheets .css diff --git a/Sample_App/app/assets/images/.keep b/Sample_App/app/assets/images/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/app/assets/images/dog.jpg b/Sample_App/app/assets/images/dog.jpg new file mode 100644 index 0000000..8d46df2 Binary files /dev/null and b/Sample_App/app/assets/images/dog.jpg differ diff --git a/Sample_App/app/assets/javascripts/application.js b/Sample_App/app/assets/javascripts/application.js new file mode 100644 index 0000000..df3de38 --- /dev/null +++ b/Sample_App/app/assets/javascripts/application.js @@ -0,0 +1,18 @@ +// 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, 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. JavaScript code in this file should be added after the last require_* statement. +// +// Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details +// about supported directives. +// +//= require jquery +//= require bootstrap +//= require rails-ujs +//= require activestorage +//= require turbolinks +//= require_tree . diff --git a/Sample_App/app/assets/javascripts/cable.js b/Sample_App/app/assets/javascripts/cable.js new file mode 100644 index 0000000..739aa5f --- /dev/null +++ b/Sample_App/app/assets/javascripts/cable.js @@ -0,0 +1,13 @@ +// Action Cable provides the framework to deal with WebSockets in Rails. +// You can generate new channels where WebSocket features live using the `rails generate channel` command. +// +//= require action_cable +//= require_self +//= require_tree ./channels + +(function() { + this.App || (this.App = {}); + + App.cable = ActionCable.createConsumer(); + +}).call(this); diff --git a/Sample_App/app/assets/javascripts/channels/.keep b/Sample_App/app/assets/javascripts/channels/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/app/assets/javascripts/sessions.coffee b/Sample_App/app/assets/javascripts/sessions.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/Sample_App/app/assets/javascripts/sessions.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/Sample_App/app/assets/javascripts/static_pages.coffee b/Sample_App/app/assets/javascripts/static_pages.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/Sample_App/app/assets/javascripts/static_pages.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/Sample_App/app/assets/javascripts/users.coffee b/Sample_App/app/assets/javascripts/users.coffee new file mode 100644 index 0000000..24f83d1 --- /dev/null +++ b/Sample_App/app/assets/javascripts/users.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/Sample_App/app/assets/stylesheets/application.css b/Sample_App/app/assets/stylesheets/application.css new file mode 100644 index 0000000..d05ea0f --- /dev/null +++ b/Sample_App/app/assets/stylesheets/application.css @@ -0,0 +1,15 @@ +/* + * This is a manifest file that'll be compiled into application.css, which will include all the files + * listed below. + * + * Any CSS and SCSS file within this directory, lib/assets/stylesheets, 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 other CSS/SCSS + * files in this directory. Styles in this file should be added after the last require_* statement. + * It is generally better to create a new file per style scope. + * + *= require_tree . + *= require_self + */ diff --git a/Sample_App/app/assets/stylesheets/custom.scss b/Sample_App/app/assets/stylesheets/custom.scss new file mode 100644 index 0000000..5fdc08e --- /dev/null +++ b/Sample_App/app/assets/stylesheets/custom.scss @@ -0,0 +1,206 @@ +@import "bootstrap-sprockets"; +@import "bootstrap"; + +/* mixins, variables, etc. */ + +$gray-medium-light: #eaeaea; + +/* universal */ + +body { + padding-top: 60px; +} + +section { + overflow: auto; +} + +textarea { + resize: vertical; +} + +.center { + text-align: center; + h1 { + margin-bottom: 10px; + } +} + +/* typography */ + +h1, h2, h3, h4, h5, h6 { + line-height: 1; +} + +h1 { + font-size: 3em; + letter-spacing: -2px; + margin-bottom: 30px; + text-align: center; +} + +h2 { + font-size: 1.2em; + letter-spacing: -1px; + margin-bottom: 30px; + text-align: center; + font-weight: normal; + color: $gray-light; +} + +p { + font-size: 1.1em; + line-height: 1.7em; +} + + +/* header */ + +#logo { + float: left; + margin-right: 10px; + font-size: 1.7em; + color: white; + text-transform: uppercase; + letter-spacing: -1px; + padding-top: 9px; + font-weight: bold; + &:hover { + color: white; + text-decoration: none; + } +} + +/* footer */ + +footer { + margin-top: 45px; + padding-top: 5px; + border-top: 1px solid $gray-medium-light; + color: $gray-light; + a { + color: $gray; + &:hover { + color: $gray-darker; + } + } + small { + float: left; + } + ul { + float: right; + list-style: none; + li { + float: left; + margin-left: 15px; + } + } +} + + +@mixin box_sizing { + -moz-box-sizing: border-box; + -webkit-box-sizing: border-box; + box-sizing: border-box; +} + +/* miscellaneous */ + +.debug_dump { + clear: both; + float: left; + width: 100%; + margin-top: 45px; + @include box_sizing; +} + +/* sidebar */ + +aside { + section.user_info { + margin-top: 20px; + } + section { + padding: 10px 0; + margin-top: 20px; + &:first-child { + border: 0; + padding-top: 0; + } + span { + display: block; + margin-bottom: 3px; + line-height: 1; + } + h1 { + font-size: 1.4em; + text-align: left; + letter-spacing: -1px; + margin-bottom: 3px; + margin-top: 0px; + } + } +} + +.gravatar { + float: left; + margin-right: 10px; +} + +.gravatar_edit { + margin-top: 15px; +} + +/* forms */ + +input, textarea, select, .uneditable-input { + border: 1px solid #bbb; + width: 100%; + margin-bottom: 15px; + @include box_sizing; +} + +input { + height: auto !important; +} + +#error_explanation { + color: red; + ul { + color: red; + margin: 0 0 30px 0; + } +} + +.field_with_errors { + @extend .has-error; + .form-control { + color: $state-danger-text; + } +} + +.checkbox { + margin-top: -10px; + margin-bottom: 10px; + span { + margin-left: 20px; + font-weight: normal; + } +} + +#session_remember_me { + width: auto; + margin-left: 0; +} + +/* Users index */ + +.users { + list-style: none; + margin: 0; + li { + overflow: auto; + padding: 10px 0; + border-bottom: 1px solid $gray-lighter; + } +} \ No newline at end of file diff --git a/Sample_App/app/assets/stylesheets/sessions.scss b/Sample_App/app/assets/stylesheets/sessions.scss new file mode 100644 index 0000000..ccb1ed2 --- /dev/null +++ b/Sample_App/app/assets/stylesheets/sessions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Sessions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/Sample_App/app/assets/stylesheets/static_pages.scss b/Sample_App/app/assets/stylesheets/static_pages.scss new file mode 100644 index 0000000..d55836c --- /dev/null +++ b/Sample_App/app/assets/stylesheets/static_pages.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the StaticPages controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/Sample_App/app/assets/stylesheets/users.scss b/Sample_App/app/assets/stylesheets/users.scss new file mode 100644 index 0000000..31a2eac --- /dev/null +++ b/Sample_App/app/assets/stylesheets/users.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Users controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/Sample_App/app/channels/application_cable/channel.rb b/Sample_App/app/channels/application_cable/channel.rb new file mode 100644 index 0000000..d672697 --- /dev/null +++ b/Sample_App/app/channels/application_cable/channel.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Channel < ActionCable::Channel::Base + end +end diff --git a/Sample_App/app/channels/application_cable/connection.rb b/Sample_App/app/channels/application_cable/connection.rb new file mode 100644 index 0000000..0ff5442 --- /dev/null +++ b/Sample_App/app/channels/application_cable/connection.rb @@ -0,0 +1,4 @@ +module ApplicationCable + class Connection < ActionCable::Connection::Base + end +end diff --git a/Sample_App/app/controllers/application_controller.rb b/Sample_App/app/controllers/application_controller.rb new file mode 100644 index 0000000..6bc3e3f --- /dev/null +++ b/Sample_App/app/controllers/application_controller.rb @@ -0,0 +1,4 @@ +class ApplicationController < ActionController::Base + protect_from_forgery with: :exception + include SessionsHelper +end diff --git a/Sample_App/app/controllers/concerns/.keep b/Sample_App/app/controllers/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/app/controllers/sessions_controller.rb b/Sample_App/app/controllers/sessions_controller.rb new file mode 100644 index 0000000..87ad4d1 --- /dev/null +++ b/Sample_App/app/controllers/sessions_controller.rb @@ -0,0 +1,21 @@ +class SessionsController < ApplicationController + def new + end + + def create + user = User.find_by(email: params[:session][:email].downcase) + if user && user.authenticate(params[:session][:password]) + log_in user + params[:session][:remember_me] == '1' ? remember(user) : forget(user) + redirect_back_or user + else + flash.now[:danger] = 'Invalid email/password combination' + render 'new' + end + end + + def destroy + log_out if logged_in? + redirect_to root_url + end +end diff --git a/Sample_App/app/controllers/static_pages_controller.rb b/Sample_App/app/controllers/static_pages_controller.rb new file mode 100644 index 0000000..9cc8f8f --- /dev/null +++ b/Sample_App/app/controllers/static_pages_controller.rb @@ -0,0 +1,14 @@ +class StaticPagesController < ApplicationController + def home + end + + def help + end + + def about + end + + def contact + end + +end diff --git a/Sample_App/app/controllers/users_controller.rb b/Sample_App/app/controllers/users_controller.rb new file mode 100644 index 0000000..fa056a9 --- /dev/null +++ b/Sample_App/app/controllers/users_controller.rb @@ -0,0 +1,73 @@ +class UsersController < ApplicationController + before_action :logged_in_user, only: [:edit, :update, :index, :destroy] + before_action :correct_user, only: [:edit, :update] + before_action :admin_user, only: :destroy + + def index + @users = User.paginate(page: params[:page]) + end + + def show + @user = User.find(params[:id]) + # debugger + end + + def new + @user = User.new + end + + def create + @user = User.new(user_params) + if @user.save + log_in @user + flash[:success] = "Welcome to my Rails App" + redirect_to @user + else + render 'new' + end + end + + def edit + @user = User.find(params[:id]) + end + + def update + @user = User.find(params[:id]) + if @user.update_attributes(user_params) + flash[:success] = "Profile updated" + redirect_to @user + else + render 'edit' + end + end + + def destroy + User.find(params[:id]).destroy + flash[:success] = "User deleted" + redirect_to users_url + end + + + private + + def user_params + params.require(:user).permit(:name, :email, :password, :password_confirmation) + end + + def logged_in_user + unless logged_in? + store_location + flash[:danger] = "Please log in." + redirect_to login_url + end + end + + def correct_user + @user = User.find(params[:id]) + redirect_to(root_url) unless current_user?(@user) + end + + def admin_user + redirect_to(root_url) unless current_user.admin? + end +end diff --git a/Sample_App/app/helpers/application_helper.rb b/Sample_App/app/helpers/application_helper.rb new file mode 100644 index 0000000..31c5bb1 --- /dev/null +++ b/Sample_App/app/helpers/application_helper.rb @@ -0,0 +1,10 @@ +module ApplicationHelper + def full_title(page_title = '') + base_title = "Ruby on Rails Tutorial Sample App" + if page_title.empty? + base_title + else + page_title + " | " + base_title + end + end +end diff --git a/Sample_App/app/helpers/sessions_helper.rb b/Sample_App/app/helpers/sessions_helper.rb new file mode 100644 index 0000000..463096b --- /dev/null +++ b/Sample_App/app/helpers/sessions_helper.rb @@ -0,0 +1,55 @@ +module SessionsHelper + def log_in(user) + session[:user_id] = user.id + end + + def remember(user) + user.remember + cookies.permanent.signed[:user_id] = user.id + cookies.permanent[:remember_token] = user.remember_token + # remember_digest? + end + + def current_user?(user) + user == current_user + end + + def current_user + if (user_id = session[:user_id]) + @current_user ||= User.find_by(id: user_id) + elsif (user_id = cookies.signed[:user_id]) + user = User.find_by(id: user_id) + if user && user.authenticated?(cookies[:remember_token]) + log_in user + @current_user = user + end + end + end + + def logged_in? + !current_user.nil? + end + + def log_out + forget(current_user) + session.delete(:user_id) + @current_user = nil + end + + def forget(user) + user.forget + cookies.delete(:user_id) + cookies.delete(:remember_token) + end + + # Redirects to stored location (or to the default). + def redirect_back_or(default) + redirect_to(session[:forwarding_url] || default) + session.delete(:forwarding_url) + end + + # Stores the URL trying to be accessed. + def store_location + session[:forwarding_url] = request.original_url if request.get? + end +end diff --git a/Sample_App/app/helpers/static_pages_helper.rb b/Sample_App/app/helpers/static_pages_helper.rb new file mode 100644 index 0000000..2d63e79 --- /dev/null +++ b/Sample_App/app/helpers/static_pages_helper.rb @@ -0,0 +1,2 @@ +module StaticPagesHelper +end diff --git a/Sample_App/app/helpers/users_helper.rb b/Sample_App/app/helpers/users_helper.rb new file mode 100644 index 0000000..3f3f18f --- /dev/null +++ b/Sample_App/app/helpers/users_helper.rb @@ -0,0 +1,8 @@ +module UsersHelper + def gravatar_for(user, options = { size: 80 }) + gravatar_id = Digest::MD5::hexdigest(user.email.downcase) + size = options[:size] + gravatar_url = "https://secure.gravatar.com/avatar/#{gravatar_id}?s=#{size}" + image_tag(gravatar_url, alt: user.name, class: "gravatar") + end +end diff --git a/Sample_App/app/jobs/application_job.rb b/Sample_App/app/jobs/application_job.rb new file mode 100644 index 0000000..a009ace --- /dev/null +++ b/Sample_App/app/jobs/application_job.rb @@ -0,0 +1,2 @@ +class ApplicationJob < ActiveJob::Base +end diff --git a/Sample_App/app/mailers/application_mailer.rb b/Sample_App/app/mailers/application_mailer.rb new file mode 100644 index 0000000..286b223 --- /dev/null +++ b/Sample_App/app/mailers/application_mailer.rb @@ -0,0 +1,4 @@ +class ApplicationMailer < ActionMailer::Base + default from: 'from@example.com' + layout 'mailer' +end diff --git a/Sample_App/app/models/application_record.rb b/Sample_App/app/models/application_record.rb new file mode 100644 index 0000000..10a4cba --- /dev/null +++ b/Sample_App/app/models/application_record.rb @@ -0,0 +1,3 @@ +class ApplicationRecord < ActiveRecord::Base + self.abstract_class = true +end diff --git a/Sample_App/app/models/concerns/.keep b/Sample_App/app/models/concerns/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/app/models/user.rb b/Sample_App/app/models/user.rb new file mode 100644 index 0000000..c0054b2 --- /dev/null +++ b/Sample_App/app/models/user.rb @@ -0,0 +1,37 @@ +class User < ApplicationRecord + attr_accessor :remember_token + + before_save { email.downcase! } + VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-]+(\.[a-z\d\-]+)*\.[a-z]+\z/i + + validates :name, presence: true, length: { maximum: 50 } + validates :email, presence: true, length: { maximum: 255}, + format: { with: VALID_EMAIL_REGEX }, + uniqueness: { case_sensitive: false } + has_secure_password + validates :password, presence: true, length: { minimum: 6 }, allow_nil: true + + def User.digest(string) + cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : + BCrypt::Engine.cost + BCrypt::Password.create(string, cost: cost) + end + + def User.new_token + SecureRandom.urlsafe_base64 + end + + def remember + self.remember_token = User.new_token + update_attribute(:remember_digest, User.digest(remember_token)) + end + + def authenticated?(remember_token) + return false if remember_digest.nil? + BCrypt::Password.new(remember_digest).is_password?(remember_token) + end + + def forget + update_attribute(:remember_digest, nil) + end +end diff --git a/Sample_App/app/views/layouts/_footer.html.erb b/Sample_App/app/views/layouts/_footer.html.erb new file mode 100644 index 0000000..ee3a4eb --- /dev/null +++ b/Sample_App/app/views/layouts/_footer.html.erb @@ -0,0 +1,13 @@ + \ No newline at end of file diff --git a/Sample_App/app/views/layouts/_header.html.erb b/Sample_App/app/views/layouts/_header.html.erb new file mode 100644 index 0000000..7155319 --- /dev/null +++ b/Sample_App/app/views/layouts/_header.html.erb @@ -0,0 +1,29 @@ + \ No newline at end of file diff --git a/Sample_App/app/views/layouts/application.html.erb b/Sample_App/app/views/layouts/application.html.erb new file mode 100644 index 0000000..8e2a434 --- /dev/null +++ b/Sample_App/app/views/layouts/application.html.erb @@ -0,0 +1,23 @@ + + + + <%= yield(:title) %> | Ruby on Rails Tutorial Sample App + <%= csrf_meta_tags %> + <%= stylesheet_link_tag 'application', media: 'all', + 'data-turbolinks-track': 'reload' %> + <%= javascript_include_tag 'application', + 'data-turbolinks-track': 'reload' %> + + + + <%= render 'layouts/header' %> +
+ <% flash.each do |message_type, message| %> + <%= content_tag(:div, message, class: "alert alert-#{message_type}") %> + <% end %> + <%= yield %> + <%= render 'layouts/footer' %> + <%= debug(params) if Rails.env.development? %> +
+ + \ No newline at end of file diff --git a/Sample_App/app/views/layouts/mailer.html.erb b/Sample_App/app/views/layouts/mailer.html.erb new file mode 100644 index 0000000..cbd34d2 --- /dev/null +++ b/Sample_App/app/views/layouts/mailer.html.erb @@ -0,0 +1,13 @@ + + + + + + + + + <%= yield %> + + diff --git a/Sample_App/app/views/layouts/mailer.text.erb b/Sample_App/app/views/layouts/mailer.text.erb new file mode 100644 index 0000000..37f0bdd --- /dev/null +++ b/Sample_App/app/views/layouts/mailer.text.erb @@ -0,0 +1 @@ +<%= yield %> diff --git a/Sample_App/app/views/sessions/new.html.erb b/Sample_App/app/views/sessions/new.html.erb new file mode 100644 index 0000000..de6e273 --- /dev/null +++ b/Sample_App/app/views/sessions/new.html.erb @@ -0,0 +1,24 @@ +<% provide(:title, "Log in") %> +

Log in

+ +
+
+ <%= form_for(:session, url: login_path) do |f| %> + + <%= f.label :email %> + <%= f.email_field :email, class: 'form-control' %> + + <%= f.label :password %> + <%= f.password_field :password, class: 'form-control' %> + + <%= f.label :remember_me, class: "checkbox inline" do %> + <%= f.check_box :remember_me %> + Remember me on this computer + <% end %> + + <%= f.submit "Log in", class: "btn btn-primary" %> + <% end %> + +

New user? <%= link_to "Sign up now!", signup_path %>

+
+
\ No newline at end of file diff --git a/Sample_App/app/views/shared/_error_messages.html.erb b/Sample_App/app/views/shared/_error_messages.html.erb new file mode 100644 index 0000000..2474cb6 --- /dev/null +++ b/Sample_App/app/views/shared/_error_messages.html.erb @@ -0,0 +1,12 @@ +<% if @user.errors.any? %> +
+
+ The form contains <%= pluralize(@user.errors.count, "error") %>. +
+ +
+<% end %> \ No newline at end of file diff --git a/Sample_App/app/views/static_pages/about.html.erb b/Sample_App/app/views/static_pages/about.html.erb new file mode 100644 index 0000000..ad0b68d --- /dev/null +++ b/Sample_App/app/views/static_pages/about.html.erb @@ -0,0 +1,21 @@ +<% provide(:title, "About") %> + + + + <%= yield(:title) %> | Ruby on Rails Tutorial Sample App + + +

About

+

+ The Ruby on Rails + Tutorial, part of the + Learn Enough family of + tutorials, is a + book and + screencast series + to teach web development with + Ruby on Rails. + This is the sample app for the tutorial. +

+ + \ No newline at end of file diff --git a/Sample_App/app/views/static_pages/contact.html.erb b/Sample_App/app/views/static_pages/contact.html.erb new file mode 100644 index 0000000..7b5e134 --- /dev/null +++ b/Sample_App/app/views/static_pages/contact.html.erb @@ -0,0 +1,6 @@ +<% provide(:title, "Contact") %> +

Contact

+

+ Contact the Ruby on Rails Tutorial about the sample app at the + contact page. +

\ No newline at end of file diff --git a/Sample_App/app/views/static_pages/help.html.erb b/Sample_App/app/views/static_pages/help.html.erb new file mode 100644 index 0000000..4ec6145 --- /dev/null +++ b/Sample_App/app/views/static_pages/help.html.erb @@ -0,0 +1,18 @@ +<% provide(:title, "Help") %> + + + + <%= yield(:title) %> | Ruby on Rails Tutorial Sample App + + +

Help

+

+ Get help on the Ruby on Rails Tutorial at the + Rails Tutorial help + section. + To get help on this sample app, see the + Ruby on Rails + Tutorial book. +

+ + \ No newline at end of file diff --git a/Sample_App/app/views/static_pages/home.html.erb b/Sample_App/app/views/static_pages/home.html.erb new file mode 100644 index 0000000..fd9fe9b --- /dev/null +++ b/Sample_App/app/views/static_pages/home.html.erb @@ -0,0 +1,23 @@ +<% provide(:title, "Home") %> + + + + <%= yield(:title) %> | Ruby on Rails Tutorial Sample App + + +
+

Welcome to the Sample App

+ +

+ This is the home page for the + Ruby on Rails Tutorial + sample application. +

+ + <%= link_to "Sign up now!", signup_path, class: "btn btn-lg btn-primary" %> +
+ + <%= link_to image_tag("dog.jpg", alt: "Rails logo"), + 'http://rubyonrails.org/' %> + + \ No newline at end of file diff --git a/Sample_App/app/views/users/_form.html.erb b/Sample_App/app/views/users/_form.html.erb new file mode 100644 index 0000000..055218e --- /dev/null +++ b/Sample_App/app/views/users/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_for(@user) do |f| %> + <%= render 'shared/error_messages', object: @user %> + + <%= f.label :name %> + <%= f.text_field :name, class: 'form-control' %> + + <%= f.label :email %> + <%= f.email_field :email, class: 'form-control' %> + + <%= f.label :password %> + <%= f.password_field :password, class: 'form-control' %> + + <%= f.label :password_confirmation %> + <%= f.password_field :password_confirmation, class: 'form-control' %> + + <%= f.submit yield(:button_text), class: "btn btn-primary" %> + <% end %> \ No newline at end of file diff --git a/Sample_App/app/views/users/_user.html.erb b/Sample_App/app/views/users/_user.html.erb new file mode 100644 index 0000000..5e6adbd --- /dev/null +++ b/Sample_App/app/views/users/_user.html.erb @@ -0,0 +1,8 @@ +
  • + <%= gravatar_for user, size: 50 %> + <%= link_to user.name, user %> + <% if current_user.admin? && !current_user?(user) %> + | <%= link_to "delete", user, method: :delete, + data: { confirm: "You sure?" } %> + <% end %> +
  • \ No newline at end of file diff --git a/Sample_App/app/views/users/edit.html.erb b/Sample_App/app/views/users/edit.html.erb new file mode 100644 index 0000000..4d6b8ae --- /dev/null +++ b/Sample_App/app/views/users/edit.html.erb @@ -0,0 +1,12 @@ +<% provide(:title, 'Edit user') %> +<% provide(:button_text, 'Save changes') %> +

    Update your profile

    +
    +
    + <%= render 'form' %> +
    + <%= gravatar_for @user %> + Change +
    +
    +
    \ No newline at end of file diff --git a/Sample_App/app/views/users/index.html.erb b/Sample_App/app/views/users/index.html.erb new file mode 100644 index 0000000..cd5b789 --- /dev/null +++ b/Sample_App/app/views/users/index.html.erb @@ -0,0 +1,9 @@ +<% provide(:title, 'All users') %> +

    All users

    +<%= will_paginate %> + +<%= will_paginate %> \ No newline at end of file diff --git a/Sample_App/app/views/users/new.html.erb b/Sample_App/app/views/users/new.html.erb new file mode 100644 index 0000000..b3d68a4 --- /dev/null +++ b/Sample_App/app/views/users/new.html.erb @@ -0,0 +1,8 @@ +<% provide(:title, 'Sign up') %> +<% provide(:button_text, 'Create my account') %> +

    Sign up

    +
    +
    + <%= render 'form' %> +
    +
    \ No newline at end of file diff --git a/Sample_App/app/views/users/show.html.erb b/Sample_App/app/views/users/show.html.erb new file mode 100644 index 0000000..65b068d --- /dev/null +++ b/Sample_App/app/views/users/show.html.erb @@ -0,0 +1,11 @@ +<% provide(:title, @user.name) %> +
    + +
    \ No newline at end of file diff --git a/Sample_App/bin/bundle b/Sample_App/bin/bundle new file mode 100755 index 0000000..f19acf5 --- /dev/null +++ b/Sample_App/bin/bundle @@ -0,0 +1,3 @@ +#!/usr/bin/env ruby +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) +load Gem.bin_path('bundler', 'bundle') diff --git a/Sample_App/bin/rails b/Sample_App/bin/rails new file mode 100755 index 0000000..5badb2f --- /dev/null +++ b/Sample_App/bin/rails @@ -0,0 +1,9 @@ +#!/usr/bin/env ruby +begin + load File.expand_path('../spring', __FILE__) +rescue LoadError => e + raise unless e.message.include?('spring') +end +APP_PATH = File.expand_path('../config/application', __dir__) +require_relative '../config/boot' +require 'rails/commands' diff --git a/Sample_App/bin/rake b/Sample_App/bin/rake new file mode 100755 index 0000000..d87d5f5 --- /dev/null +++ b/Sample_App/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/Sample_App/bin/setup b/Sample_App/bin/setup new file mode 100755 index 0000000..94fd4d7 --- /dev/null +++ b/Sample_App/bin/setup @@ -0,0 +1,36 @@ +#!/usr/bin/env ruby +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = File.expand_path('..', __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +chdir APP_ROOT do + # This script is a starting point to setup your application. + # Add necessary setup steps to this file. + + puts '== Installing dependencies ==' + system! 'gem install bundler --conservative' + system('bundle check') || system!('bundle install') + + # Install JavaScript dependencies if using Yarn + # system('bin/yarn') + + # puts "\n== Copying sample files ==" + # unless File.exist?('config/database.yml') + # cp 'config/database.yml.sample', 'config/database.yml' + # end + + puts "\n== Preparing database ==" + system! 'bin/rails db:setup' + + puts "\n== Removing old logs and tempfiles ==" + system! 'bin/rails log:clear tmp:clear' + + puts "\n== Restarting application server ==" + system! 'bin/rails restart' +end diff --git a/Sample_App/bin/spring b/Sample_App/bin/spring new file mode 100755 index 0000000..d89ee49 --- /dev/null +++ b/Sample_App/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/Sample_App/bin/update b/Sample_App/bin/update new file mode 100755 index 0000000..58bfaed --- /dev/null +++ b/Sample_App/bin/update @@ -0,0 +1,31 @@ +#!/usr/bin/env ruby +require 'fileutils' +include FileUtils + +# path to your application root. +APP_ROOT = File.expand_path('..', __dir__) + +def system!(*args) + system(*args) || abort("\n== Command #{args} failed ==") +end + +chdir APP_ROOT do + # This script is a way to update your development environment automatically. + # Add necessary update steps to this file. + + puts '== Installing dependencies ==' + system! 'gem install bundler --conservative' + system('bundle check') || system!('bundle install') + + # Install JavaScript dependencies if using Yarn + # system('bin/yarn') + + puts "\n== Updating database ==" + system! 'bin/rails db:migrate' + + puts "\n== Removing old logs and tempfiles ==" + system! 'bin/rails log:clear tmp:clear' + + puts "\n== Restarting application server ==" + system! 'bin/rails restart' +end diff --git a/Sample_App/bin/yarn b/Sample_App/bin/yarn new file mode 100755 index 0000000..460dd56 --- /dev/null +++ b/Sample_App/bin/yarn @@ -0,0 +1,11 @@ +#!/usr/bin/env ruby +APP_ROOT = File.expand_path('..', __dir__) +Dir.chdir(APP_ROOT) do + begin + exec "yarnpkg", *ARGV + rescue Errno::ENOENT + $stderr.puts "Yarn executable was not detected in the system." + $stderr.puts "Download Yarn at https://yarnpkg.com/en/docs/install" + exit 1 + end +end diff --git a/Sample_App/config.ru b/Sample_App/config.ru new file mode 100644 index 0000000..f7ba0b5 --- /dev/null +++ b/Sample_App/config.ru @@ -0,0 +1,5 @@ +# This file is used by Rack-based servers to start the application. + +require_relative 'config/environment' + +run Rails.application diff --git a/Sample_App/config/application.rb b/Sample_App/config/application.rb new file mode 100644 index 0000000..e746037 --- /dev/null +++ b/Sample_App/config/application.rb @@ -0,0 +1,19 @@ +require_relative 'boot' + +require 'rails/all' + +# Require the gems listed in Gemfile, including any gems +# you've limited to :test, :development, or :production. +Bundler.require(*Rails.groups) + +module RailsApp + class Application < Rails::Application + # Initialize configuration defaults for originally generated Rails version. + config.load_defaults 5.2 + + # Settings in config/environments/* take precedence over those specified here. + # Application configuration can go into files in config/initializers + # -- all .rb files in that directory are automatically loaded after loading + # the framework and any gems in your application. + end +end diff --git a/Sample_App/config/boot.rb b/Sample_App/config/boot.rb new file mode 100644 index 0000000..b9e460c --- /dev/null +++ b/Sample_App/config/boot.rb @@ -0,0 +1,4 @@ +ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__) + +require 'bundler/setup' # Set up gems listed in the Gemfile. +require 'bootsnap/setup' # Speed up boot time by caching expensive operations. diff --git a/Sample_App/config/cable.yml b/Sample_App/config/cable.yml new file mode 100644 index 0000000..ae650bd --- /dev/null +++ b/Sample_App/config/cable.yml @@ -0,0 +1,10 @@ +development: + adapter: async + +test: + adapter: async + +production: + adapter: redis + url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %> + channel_prefix: rails_app_production diff --git a/Sample_App/config/credentials.yml.enc b/Sample_App/config/credentials.yml.enc new file mode 100644 index 0000000..2a7e0b8 --- /dev/null +++ b/Sample_App/config/credentials.yml.enc @@ -0,0 +1 @@ ++nUB47iA2I7KA59VqTZ85pCQ9M00VMZQhwcVFrUhzEiJLQW15U7yoGm+vf6QK7Lbs1yuLOVu1zkz6D+UAgcZO0npQCIV+mwkLgmSSWQPbhphAulNsoeJoGupUqt3haRKS64lmj+883sPNKv9eltdS+rhwmA5lhF2kjc2pshMYMh3NPBEp4iuaMZ1iSpcS0B0EAmK0bhXkPPxAxgqy38jntdbPv3kj3TEycU5BCn8Wk3Jwe5eSKiCmDv32azznUspk/mQJ5y02UXxhReNSbkQCUBYoqmM8A0+M40WokR5wWDEQXfjtYn1gXLce564BN95FbQ4MJdkWMvwvGI36s30NMxnS6tKxuFQIr494buZxgdY40HptIxOjAGT25AeNr6RgLEYVoXsYYCLXPPzn7o/RLZfK7c3ORNis3qY--g4IGfZW4wLxV4z9i--Ed1hlCY8KXd9JnrCOHy8kA== \ No newline at end of file diff --git a/Sample_App/config/database.yml b/Sample_App/config/database.yml new file mode 100644 index 0000000..0d02f24 --- /dev/null +++ b/Sample_App/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: <%= ENV.fetch("RAILS_MAX_THREADS") { 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/Sample_App/config/environment.rb b/Sample_App/config/environment.rb new file mode 100644 index 0000000..426333b --- /dev/null +++ b/Sample_App/config/environment.rb @@ -0,0 +1,5 @@ +# Load the Rails application. +require_relative 'application' + +# Initialize the Rails application. +Rails.application.initialize! diff --git a/Sample_App/config/environments/development.rb b/Sample_App/config/environments/development.rb new file mode 100644 index 0000000..9526974 --- /dev/null +++ b/Sample_App/config/environments/development.rb @@ -0,0 +1,60 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # In the development environment your application's code is reloaded on + # every request. This slows down response time but is perfect for development + # since you don't have to restart the web server when you make code changes. + config.cache_classes = false + + # Do not eager load code on boot. + config.eager_load = false + + # Show full error reports. + config.consider_all_requests_local = true + + # Enable/disable caching. By default caching is disabled. + # Run rails dev:cache to toggle caching. + if Rails.root.join('tmp', 'caching-dev.txt').exist? + config.action_controller.perform_caching = true + + config.cache_store = :memory_store + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{2.days.to_i}" + } + else + config.action_controller.perform_caching = false + + config.cache_store = :null_store + end + + # Store uploaded files on the local file system (see config/storage.yml for options) + config.active_storage.service = :local + + # Don't care if the mailer can't send. + config.action_mailer.raise_delivery_errors = false + + config.action_mailer.perform_caching = false + + # Print deprecation notices to the Rails logger. + config.active_support.deprecation = :log + + # Raise an error on page load if there are pending migrations. + config.active_record.migration_error = :page_load + + # Highlight code that triggered database queries in logs. + config.active_record.verbose_query_logs = true + + # 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 + + # Suppress logger output for asset requests. + config.assets.quiet = true + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true + + # Use an evented file watcher to asynchronously detect changes in source code, + # routes, locales, etc. This feature depends on the listen gem. + config.file_watcher = ActiveSupport::EventedFileUpdateChecker +end diff --git a/Sample_App/config/environments/production.rb b/Sample_App/config/environments/production.rb new file mode 100644 index 0000000..02c9dc1 --- /dev/null +++ b/Sample_App/config/environments/production.rb @@ -0,0 +1,94 @@ +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 + + # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] + # or in config/master.key. This key is used to decrypt credentials (and other encrypted files). + # config.require_master_key = true + + # Disable serving static files from the `/public` folder by default since + # Apache or NGINX already handles this. + config.public_file_server.enabled = ENV['RAILS_SERVE_STATIC_FILES'].present? + + # 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 + + # `config.assets.precompile` and `config.assets.version` have moved to config/initializers/assets.rb + + # Enable serving of images, stylesheets, and JavaScripts from an asset server. + # config.action_controller.asset_host = 'http://assets.example.com' + + # Specifies the header that your server uses for sending files. + # config.action_dispatch.x_sendfile_header = 'X-Sendfile' # for Apache + # config.action_dispatch.x_sendfile_header = 'X-Accel-Redirect' # for NGINX + + # Store uploaded files on the local file system (see config/storage.yml for options) + config.active_storage.service = :local + + # Mount Action Cable outside main process or domain + # config.action_cable.mount_path = nil + # config.action_cable.url = 'wss://example.com/cable' + # config.action_cable.allowed_request_origins = [ 'http://example.com', /http:\/\/example.*/ ] + + # Force all access to the app over SSL, use Strict-Transport-Security, and use secure cookies. + # config.force_ssl = true + + # Use the lowest log level to ensure availability of diagnostic information + # when problems arise. + config.log_level = :debug + + # Prepend all log lines with the following tags. + config.log_tags = [ :request_id ] + + # Use a different cache store in production. + # config.cache_store = :mem_cache_store + + # Use a real queuing backend for Active Job (and separate queues per environment) + # config.active_job.queue_adapter = :resque + # config.active_job.queue_name_prefix = "rails_app_#{Rails.env}" + + config.action_mailer.perform_caching = false + + # Ignore bad email addresses and do not raise email delivery errors. + # Set this to true and configure the email server for immediate delivery to raise delivery errors. + # config.action_mailer.raise_delivery_errors = false + + # Enable locale fallbacks for I18n (makes lookups for any locale fall back to + # the I18n.default_locale when a translation cannot be found). + config.i18n.fallbacks = true + + # Send deprecation notices to registered listeners. + config.active_support.deprecation = :notify + + # Use default logging formatter so that PID and timestamp are not suppressed. + config.log_formatter = ::Logger::Formatter.new + + # Use a different logger for distributed setups. + # require 'syslog/logger' + # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new 'app-name') + + if ENV["RAILS_LOG_TO_STDOUT"].present? + logger = ActiveSupport::Logger.new(STDOUT) + logger.formatter = config.log_formatter + config.logger = ActiveSupport::TaggedLogging.new(logger) + end + + # Do not dump schema after migrations. + config.active_record.dump_schema_after_migration = false +end diff --git a/Sample_App/config/environments/test.rb b/Sample_App/config/environments/test.rb new file mode 100644 index 0000000..0a38fd3 --- /dev/null +++ b/Sample_App/config/environments/test.rb @@ -0,0 +1,46 @@ +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + # The test environment is used exclusively to run your application's + # test suite. You never need to work with it otherwise. Remember that + # your test database is "scratch space" for the test suite and is wiped + # and recreated between test runs. Don't rely on the data there! + config.cache_classes = true + + # Do not eager load code on boot. This avoids loading your whole application + # just for the purpose of running a single test. If you are using a tool that + # preloads Rails for running tests, you may have to set it to true. + config.eager_load = false + + # Configure public file server for tests with Cache-Control for performance. + config.public_file_server.enabled = true + config.public_file_server.headers = { + 'Cache-Control' => "public, max-age=#{1.hour.to_i}" + } + + # Show full error reports and disable caching. + config.consider_all_requests_local = true + config.action_controller.perform_caching = false + + # Raise exceptions instead of rendering exception templates. + config.action_dispatch.show_exceptions = false + + # Disable request forgery protection in test environment. + config.action_controller.allow_forgery_protection = false + + # Store uploaded files on the local file system in a temporary directory + config.active_storage.service = :test + + config.action_mailer.perform_caching = false + + # Tell Action Mailer not to deliver emails to the real world. + # The :test delivery method accumulates sent emails in the + # ActionMailer::Base.deliveries array. + config.action_mailer.delivery_method = :test + + # Print deprecation notices to the stderr. + config.active_support.deprecation = :stderr + + # Raises error for missing translations + # config.action_view.raise_on_missing_translations = true +end diff --git a/Sample_App/config/initializers/application_controller_renderer.rb b/Sample_App/config/initializers/application_controller_renderer.rb new file mode 100644 index 0000000..89d2efa --- /dev/null +++ b/Sample_App/config/initializers/application_controller_renderer.rb @@ -0,0 +1,8 @@ +# Be sure to restart your server when you modify this file. + +# ActiveSupport::Reloader.to_prepare do +# ApplicationController.renderer.defaults.merge!( +# http_host: 'example.org', +# https: false +# ) +# end diff --git a/Sample_App/config/initializers/assets.rb b/Sample_App/config/initializers/assets.rb new file mode 100644 index 0000000..4b828e8 --- /dev/null +++ b/Sample_App/config/initializers/assets.rb @@ -0,0 +1,14 @@ +# 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 +# Add Yarn node_modules folder to the asset load path. +Rails.application.config.assets.paths << Rails.root.join('node_modules') + +# Precompile additional assets. +# application.js, application.css, and all non-JS/CSS in the app/assets +# folder are already added. +# Rails.application.config.assets.precompile += %w( admin.js admin.css ) diff --git a/Sample_App/config/initializers/backtrace_silencers.rb b/Sample_App/config/initializers/backtrace_silencers.rb new file mode 100644 index 0000000..59385cd --- /dev/null +++ b/Sample_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/Sample_App/config/initializers/content_security_policy.rb b/Sample_App/config/initializers/content_security_policy.rb new file mode 100644 index 0000000..d3bcaa5 --- /dev/null +++ b/Sample_App/config/initializers/content_security_policy.rb @@ -0,0 +1,25 @@ +# Be sure to restart your server when you modify this file. + +# Define an application-wide content security policy +# For further information see the following documentation +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy + +# Rails.application.config.content_security_policy do |policy| +# policy.default_src :self, :https +# policy.font_src :self, :https, :data +# policy.img_src :self, :https, :data +# policy.object_src :none +# policy.script_src :self, :https +# policy.style_src :self, :https + +# # Specify URI for violation reports +# # policy.report_uri "/csp-violation-report-endpoint" +# end + +# If you are using UJS then enable automatic nonce generation +# Rails.application.config.content_security_policy_nonce_generator = -> request { SecureRandom.base64(16) } + +# Report CSP violations to a specified URI +# For further information see the following documentation: +# https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Security-Policy-Report-Only +# Rails.application.config.content_security_policy_report_only = true diff --git a/Sample_App/config/initializers/cookies_serializer.rb b/Sample_App/config/initializers/cookies_serializer.rb new file mode 100644 index 0000000..5a6a32d --- /dev/null +++ b/Sample_App/config/initializers/cookies_serializer.rb @@ -0,0 +1,5 @@ +# Be sure to restart your server when you modify this file. + +# Specify a serializer for the signed and encrypted cookie jars. +# Valid options are :json, :marshal, and :hybrid. +Rails.application.config.action_dispatch.cookies_serializer = :json diff --git a/Sample_App/config/initializers/filter_parameter_logging.rb b/Sample_App/config/initializers/filter_parameter_logging.rb new file mode 100644 index 0000000..4a994e1 --- /dev/null +++ b/Sample_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/Sample_App/config/initializers/inflections.rb b/Sample_App/config/initializers/inflections.rb new file mode 100644 index 0000000..ac033bf --- /dev/null +++ b/Sample_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/Sample_App/config/initializers/mime_types.rb b/Sample_App/config/initializers/mime_types.rb new file mode 100644 index 0000000..dc18996 --- /dev/null +++ b/Sample_App/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/Sample_App/config/initializers/wrap_parameters.rb b/Sample_App/config/initializers/wrap_parameters.rb new file mode 100644 index 0000000..bbfc396 --- /dev/null +++ b/Sample_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] +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/Sample_App/config/locales/en.yml b/Sample_App/config/locales/en.yml new file mode 100644 index 0000000..decc5a8 --- /dev/null +++ b/Sample_App/config/locales/en.yml @@ -0,0 +1,33 @@ +# Files in the config/locales directory are used for internationalization +# and are automatically loaded by Rails. If you want to use locales other +# than English, add the necessary files in this directory. +# +# To use the locales, use `I18n.t`: +# +# I18n.t 'hello' +# +# In views, this is aliased to just `t`: +# +# <%= t('hello') %> +# +# To use a different locale, set it with `I18n.locale`: +# +# I18n.locale = :es +# +# This would use the information in config/locales/es.yml. +# +# The following keys must be escaped otherwise they will not be retrieved by +# the default I18n backend: +# +# true, false, on, off, yes, no +# +# Instead, surround them with single quotes. +# +# en: +# 'true': 'foo' +# +# To learn more, please read the Rails Internationalization guide +# available at http://guides.rubyonrails.org/i18n.html. + +en: + hello: "Hello world" diff --git a/Sample_App/config/puma.rb b/Sample_App/config/puma.rb new file mode 100644 index 0000000..a5eccf8 --- /dev/null +++ b/Sample_App/config/puma.rb @@ -0,0 +1,34 @@ +# Puma can serve each request in a thread from an internal thread pool. +# The `threads` method setting takes two numbers: a minimum and maximum. +# Any libraries that use thread pools should be configured to match +# the maximum value specified for Puma. Default is set to 5 threads for minimum +# and maximum; this matches the default thread size of Active Record. +# +threads_count = ENV.fetch("RAILS_MAX_THREADS") { 5 } +threads threads_count, threads_count + +# Specifies the `port` that Puma will listen on to receive requests; default is 3000. +# +port ENV.fetch("PORT") { 3000 } + +# Specifies the `environment` that Puma will run in. +# +environment ENV.fetch("RAILS_ENV") { "development" } + +# Specifies the number of `workers` to boot in clustered mode. +# Workers are forked webserver processes. If using threads and workers together +# the concurrency of the application would be max `threads` * `workers`. +# Workers do not work on JRuby or Windows (both of which do not support +# processes). +# +# workers ENV.fetch("WEB_CONCURRENCY") { 2 } + +# Use the `preload_app!` method when specifying a `workers` number. +# This directive tells Puma to first boot the application and load code +# before forking the application. This takes advantage of Copy On Write +# process behavior so workers use less memory. +# +# preload_app! + +# Allow puma to be restarted by `rails restart` command. +plugin :tmp_restart diff --git a/Sample_App/config/routes.rb b/Sample_App/config/routes.rb new file mode 100644 index 0000000..2ac94f8 --- /dev/null +++ b/Sample_App/config/routes.rb @@ -0,0 +1,15 @@ +Rails.application.routes.draw do + root 'static_pages#home' + + get '/login', to: 'sessions#new' + post 'login', to: 'sessions#create' + delete '/logout', to: 'sessions#destroy' + + get '/help', to: 'static_pages#help' + get '/about', to: 'static_pages#about' + get '/contact', to: 'static_pages#contact' + get '/signup', to: 'users#new' + post '/signup', to: 'users#create' + + resources :users +end diff --git a/Sample_App/config/spring.rb b/Sample_App/config/spring.rb new file mode 100644 index 0000000..9fa7863 --- /dev/null +++ b/Sample_App/config/spring.rb @@ -0,0 +1,6 @@ +%w[ + .ruby-version + .rbenv-vars + tmp/restart.txt + tmp/caching-dev.txt +].each { |path| Spring.watch(path) } diff --git a/Sample_App/config/storage.yml b/Sample_App/config/storage.yml new file mode 100644 index 0000000..d32f76e --- /dev/null +++ b/Sample_App/config/storage.yml @@ -0,0 +1,34 @@ +test: + service: Disk + root: <%= Rails.root.join("tmp/storage") %> + +local: + service: Disk + root: <%= Rails.root.join("storage") %> + +# Use rails credentials:edit to set the AWS secrets (as aws:access_key_id|secret_access_key) +# amazon: +# service: S3 +# access_key_id: <%= Rails.application.credentials.dig(:aws, :access_key_id) %> +# secret_access_key: <%= Rails.application.credentials.dig(:aws, :secret_access_key) %> +# region: us-east-1 +# bucket: your_own_bucket + +# Remember not to checkin your GCS keyfile to a repository +# google: +# service: GCS +# project: your_project +# credentials: <%= Rails.root.join("path/to/gcs.keyfile") %> +# bucket: your_own_bucket + +# Use rails credentials:edit to set the Azure Storage secret (as azure_storage:storage_access_key) +# microsoft: +# service: AzureStorage +# storage_account_name: your_account_name +# storage_access_key: <%= Rails.application.credentials.dig(:azure_storage, :storage_access_key) %> +# container: your_container_name + +# mirror: +# service: Mirror +# primary: local +# mirrors: [ amazon, google, microsoft ] diff --git a/Sample_App/db/migrate/20191219115633_create_users.rb b/Sample_App/db/migrate/20191219115633_create_users.rb new file mode 100644 index 0000000..5fd691b --- /dev/null +++ b/Sample_App/db/migrate/20191219115633_create_users.rb @@ -0,0 +1,10 @@ +class CreateUsers < ActiveRecord::Migration[5.2] + def change + create_table :users do |t| + t.string :name + t.string :email + + t.timestamps + end + end +end diff --git a/Sample_App/db/migrate/20191219124433_add_index_to_users_email.rb b/Sample_App/db/migrate/20191219124433_add_index_to_users_email.rb new file mode 100644 index 0000000..6ffaad8 --- /dev/null +++ b/Sample_App/db/migrate/20191219124433_add_index_to_users_email.rb @@ -0,0 +1,5 @@ +class AddIndexToUsersEmail < ActiveRecord::Migration[5.2] + def change + add_index :users, :email, unique: true + end +end diff --git a/Sample_App/db/migrate/20191219125324_add_password_digest_to_users.rb b/Sample_App/db/migrate/20191219125324_add_password_digest_to_users.rb new file mode 100644 index 0000000..713dc51 --- /dev/null +++ b/Sample_App/db/migrate/20191219125324_add_password_digest_to_users.rb @@ -0,0 +1,5 @@ +class AddPasswordDigestToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :password_digest, :string + end +end diff --git a/Sample_App/db/migrate/20191219161154_add_remember_digest_to_users.rb b/Sample_App/db/migrate/20191219161154_add_remember_digest_to_users.rb new file mode 100644 index 0000000..108729c --- /dev/null +++ b/Sample_App/db/migrate/20191219161154_add_remember_digest_to_users.rb @@ -0,0 +1,5 @@ +class AddRememberDigestToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :remember_digest, :string + end +end diff --git a/Sample_App/db/migrate/20191220030555_add_admin_to_users.rb b/Sample_App/db/migrate/20191220030555_add_admin_to_users.rb new file mode 100644 index 0000000..aa6a7e9 --- /dev/null +++ b/Sample_App/db/migrate/20191220030555_add_admin_to_users.rb @@ -0,0 +1,5 @@ +class AddAdminToUsers < ActiveRecord::Migration[5.2] + def change + add_column :users, :admin, :boolean, default: false + end +end diff --git a/Sample_App/db/schema.rb b/Sample_App/db/schema.rb new file mode 100644 index 0000000..256e250 --- /dev/null +++ b/Sample_App/db/schema.rb @@ -0,0 +1,26 @@ +# 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: 2019_12_20_030555) do + + create_table "users", force: :cascade do |t| + t.string "name" + t.string "email" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.string "password_digest" + t.string "remember_digest" + t.boolean "admin", default: false + t.index ["email"], name: "index_users_on_email", unique: true + end + +end diff --git a/Sample_App/db/seeds.rb b/Sample_App/db/seeds.rb new file mode 100644 index 0000000..3327c29 --- /dev/null +++ b/Sample_App/db/seeds.rb @@ -0,0 +1,22 @@ +# 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 rails db:seed command (or created alongside the database with db:setup). +# +# Examples: +# +# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) +# Character.create(name: 'Luke', movie: movies.first) +User.create!(name: "Example User", + email: "example@railstutorial.org", + password: "foobar", + password_confirmation: "foobar", + admin: true) + +99.times do |n| + name = Faker::Name.name + email = "example-#{n+1}@railstutorial.org" + password = "password" + User.create!(name: name, + email: email, + password: password, + password_confirmation: password) +end \ No newline at end of file diff --git a/Sample_App/lib/assets/.keep b/Sample_App/lib/assets/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/lib/tasks/.keep b/Sample_App/lib/tasks/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/log/.keep b/Sample_App/log/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/package.json b/Sample_App/package.json new file mode 100644 index 0000000..74a8e6b --- /dev/null +++ b/Sample_App/package.json @@ -0,0 +1,5 @@ +{ + "name": "rails_app", + "private": true, + "dependencies": {} +} diff --git a/Sample_App/public/404.html b/Sample_App/public/404.html new file mode 100644 index 0000000..2be3af2 --- /dev/null +++ b/Sample_App/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/Sample_App/public/422.html b/Sample_App/public/422.html new file mode 100644 index 0000000..c08eac0 --- /dev/null +++ b/Sample_App/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/Sample_App/public/500.html b/Sample_App/public/500.html new file mode 100644 index 0000000..78a030a --- /dev/null +++ b/Sample_App/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/Sample_App/public/apple-touch-icon-precomposed.png b/Sample_App/public/apple-touch-icon-precomposed.png new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/public/apple-touch-icon.png b/Sample_App/public/apple-touch-icon.png new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/public/favicon.ico b/Sample_App/public/favicon.ico new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/public/robots.txt b/Sample_App/public/robots.txt new file mode 100644 index 0000000..37b576a --- /dev/null +++ b/Sample_App/public/robots.txt @@ -0,0 +1 @@ +# See http://www.robotstxt.org/robotstxt.html for documentation on how to use the robots.txt file diff --git a/Sample_App/storage/.keep b/Sample_App/storage/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/application_system_test_case.rb b/Sample_App/test/application_system_test_case.rb new file mode 100644 index 0000000..d19212a --- /dev/null +++ b/Sample_App/test/application_system_test_case.rb @@ -0,0 +1,5 @@ +require "test_helper" + +class ApplicationSystemTestCase < ActionDispatch::SystemTestCase + driven_by :selenium, using: :chrome, screen_size: [1400, 1400] +end diff --git a/Sample_App/test/controllers/.keep b/Sample_App/test/controllers/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/controllers/sessions_controller_test.rb b/Sample_App/test/controllers/sessions_controller_test.rb new file mode 100644 index 0000000..4ebb9f0 --- /dev/null +++ b/Sample_App/test/controllers/sessions_controller_test.rb @@ -0,0 +1,9 @@ +require 'test_helper' + +class SessionsControllerTest < ActionDispatch::IntegrationTest + test "should get new" do + get login_path + assert_response :success + end + +end diff --git a/Sample_App/test/controllers/static_pages_controller_test.rb b/Sample_App/test/controllers/static_pages_controller_test.rb new file mode 100644 index 0000000..1ba3ab2 --- /dev/null +++ b/Sample_App/test/controllers/static_pages_controller_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' + +class StaticPagesControllerTest < ActionDispatch::IntegrationTest + def setup + @base_title = "Ruby on Rails Tutorial Sample App" + end + + test "should get home" do + get root_path + assert_response :success + assert_select "title", "Home | #{@base_title}" + end + + test "should get help" do + get help_path + assert_response :success + assert_select "title", "Help | #{@base_title}" + end + + test "should get about" do + get about_path + assert_response :success + assert_select "title", "About | #{@base_title}" + end + + test "should get contact" do + get contact_path + assert_response :success + assert_select "title", "Contact | #{@base_title}" + end +end diff --git a/Sample_App/test/controllers/users_controller_test.rb b/Sample_App/test/controllers/users_controller_test.rb new file mode 100644 index 0000000..7e14730 --- /dev/null +++ b/Sample_App/test/controllers/users_controller_test.rb @@ -0,0 +1,61 @@ +require 'test_helper' + +class UsersControllerTest < ActionDispatch::IntegrationTest + def setup + @user = users(:michael) + @other_user = users(:archer) + end + + test "should get new" do + get signup_path + assert_response :success + end + + test "should redirect edit when not logged in" do + get edit_user_path(@user) + assert_not flash.empty? + assert_redirected_to login_url + end + + test "should redirect update when not logged in" do + patch user_path(@user), params: { user: { name: @user.name, + email: @user.email } } + assert_not flash.empty? + assert_redirected_to login_url + end + + test "should redirect edit when logged in as wrong user" do + log_in_as(@other_user) + get edit_user_path(@user) + assert flash.empty? + assert_redirected_to root_url + end + + test "should redirect update when logged in as wrong user" do + log_in_as(@other_user) + patch user_path(@user), params: { user: { name: @user.name, + email: @user.email } } + assert flash.empty? + assert_redirected_to root_url + end + + test "should redirect index when not logged in" do + get users_path + assert_redirected_to login_url + end + + test "should redirect destroy when not logged in" do + assert_no_difference 'User.count' do + delete user_path(@user) + end + assert_redirected_to login_url + end + + test "should redirect destroy when logged in as a non-admin" do + log_in_as(@other_user) + assert_no_difference 'User.count' do + delete user_path(@user) + end + assert_redirected_to root_url + end +end diff --git a/Sample_App/test/fixtures/.keep b/Sample_App/test/fixtures/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/fixtures/files/.keep b/Sample_App/test/fixtures/files/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/fixtures/users.yml b/Sample_App/test/fixtures/users.yml new file mode 100644 index 0000000..09d5428 --- /dev/null +++ b/Sample_App/test/fixtures/users.yml @@ -0,0 +1,29 @@ +# Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html + +michael: + name: Michael Example + email: michael@example.com + password_digest: <%= User.digest('password') %> + admin: true + +archer: + name: Sterling Archer + email: duchess@example.gov + password_digest: <%= User.digest('password') %> + +lana: + name: Lana Kane + email: hands@example.gov + password_digest: <%= User.digest('password') %> + +malory: + name: Malory Archer + email: boss@example.gov + password_digest: <%= User.digest('password') %> + +<% 30.times do |n| %> +user_<%= n %>: + name: <%= "User #{n}" %> + email: <%= "user-#{n}@example.com" %> + password_digest: <%= User.digest('password') %> +<% end %> diff --git a/Sample_App/test/helpers/.keep b/Sample_App/test/helpers/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/helpers/sessions_helper_test.rb b/Sample_App/test/helpers/sessions_helper_test.rb new file mode 100644 index 0000000..500b3b8 --- /dev/null +++ b/Sample_App/test/helpers/sessions_helper_test.rb @@ -0,0 +1,19 @@ +require 'test_helper' + +class SessionsHelperTest < ActionView::TestCase + + def setup + @user = users(:michael) + remember(@user) + end + + test "current_user returns right user when session is nil" do + assert_equal @user, current_user + assert is_logged_in? + end + + test "current_user returns nil when remember digest is wrong" do + @user.update_attribute(:remember_digest, User.digest(User.new_token)) + assert_nil current_user + end +end \ No newline at end of file diff --git a/Sample_App/test/integration/.keep b/Sample_App/test/integration/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/integration/site_layout_test.rb b/Sample_App/test/integration/site_layout_test.rb new file mode 100644 index 0000000..a0f0076 --- /dev/null +++ b/Sample_App/test/integration/site_layout_test.rb @@ -0,0 +1,14 @@ +require 'test_helper' + +class SiteLayoutTest < ActionDispatch::IntegrationTest + test "layout links" do + get root_path + assert_template 'static_pages/home' + assert_select "a[href=?]", root_path, count: 2 + assert_select "a[href=?]", help_path + assert_select "a[href=?]", about_path + assert_select "a[href=?]", contact_path + get contact_path + assert_select "title", full_title("Contact") + end +end diff --git a/Sample_App/test/integration/users_edit_test.rb b/Sample_App/test/integration/users_edit_test.rb new file mode 100644 index 0000000..19687d4 --- /dev/null +++ b/Sample_App/test/integration/users_edit_test.rb @@ -0,0 +1,53 @@ +require 'test_helper' + +class UsersEditTest < ActionDispatch::IntegrationTest + def setup + @user = users(:michael) + end + + test "unsuccessful edit" do + log_in_as(@user) + get edit_user_path(@user) + assert_template 'users/edit' + patch user_path(@user), params: { user: { name: "", + email: "foo@invalid", + password: "foo", + password_confirmation: "bar" } } + + assert_template 'users/edit' + end + + test "successful edit" do + log_in_as(@user) + get edit_user_path(@user) + assert_template 'users/edit' + name = "Foo Bar" + email = "foo@bar.com" + patch user_path(@user), params: { user: { name: name, + email: email, + password: "", + password_confirmation: "" } } + assert_not flash.empty? + assert_redirected_to @user + @user.reload + assert_equal name, @user.name + assert_equal email, @user.email + end + + test "successful edit with friendly forwarding" do + get edit_user_path(@user) + log_in_as(@user) + assert_redirected_to edit_user_url(@user) + name = "Foo Bar" + email = "foo@bar.com" + patch user_path(@user), params: { user: { name: name, + email: email, + password: "", + password_confirmation: "" } } + assert_not flash.empty? + assert_redirected_to @user + @user.reload + assert_equal name, @user.name + assert_equal email, @user.email + end +end diff --git a/Sample_App/test/integration/users_index_test.rb b/Sample_App/test/integration/users_index_test.rb new file mode 100644 index 0000000..edff9cd --- /dev/null +++ b/Sample_App/test/integration/users_index_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' + +class UsersIndexTest < ActionDispatch::IntegrationTest + def setup + @admin = users(:michael) + @non_admin = users(:archer) + end + + test "index as admin including pagination and delete links" do + log_in_as(@admin) + get users_path + assert_template 'users/index' + assert_select 'div.pagination' + first_page_of_users = User.paginate(page: 1) + first_page_of_users.each do |user| + assert_select 'a[href=?]', user_path(user), text: user.name + unless user == @admin + assert_select 'a[href=?]', user_path(user), text: 'delete' + end + end + assert_difference 'User.count', -1 do + delete user_path(@non_admin) + end + end + + test "index as non-admin" do + log_in_as(@non_admin) + get users_path + assert_select 'a', text: 'delete', count: 0 + end +end diff --git a/Sample_App/test/integration/users_login_test.rb b/Sample_App/test/integration/users_login_test.rb new file mode 100644 index 0000000..220eda4 --- /dev/null +++ b/Sample_App/test/integration/users_login_test.rb @@ -0,0 +1,64 @@ +require 'test_helper' + +class UsersLoginTest < ActionDispatch::IntegrationTest + def setup + @user = users(:michael) + end + + test "login with invalid information" do + get login_path + assert_template 'sessions/new' + post login_path, params: { session: { email: "", password: "" } } + assert_template 'sessions/new' + assert_not flash.empty? + get root_path + assert flash.empty? + end + + test "login with valid information" do + get login_path + post login_path, params: { session: { email: @user.email, + password: 'password' } } + assert_redirected_to @user + follow_redirect! + assert_template 'users/show' + assert_select "a[href=?]", login_path, count: 0 + assert_select "a[href=?]", logout_path + assert_select "a[href=?]", user_path(@user) + end + + test "login with valid information followed by logout" do + get login_path + post login_path, params: { session: { email: @user.email, + password: 'password' } } + assert is_logged_in? + assert_redirected_to @user + follow_redirect! + assert_template 'users/show' + assert_select "a[href=?]", login_path, count: 0 + assert_select "a[href=?]", logout_path + assert_select "a[href=?]", user_path(@user) + delete logout_path + assert_not is_logged_in? + assert_redirected_to root_url + # Simulate a user clicking logout in a second window. + delete logout_path + follow_redirect! + assert_select "a[href=?]", login_path + assert_select "a[href=?]", logout_path, count: 0 + assert_select "a[href=?]", user_path(@user), count: 0 + end + + test "login with remembering" do + log_in_as(@user, remember_me: '1') + assert_not_empty cookies[:remember_token] + end + + test "login without remembering" do + # Log in to set the cookie. + log_in_as(@user, remember_me: '1') + # Log in again and verify that the cookie is deleted. + log_in_as(@user, remember_me: '0') + assert_empty cookies[:remember_token] + end +end diff --git a/Sample_App/test/integration/users_signup_test.rb b/Sample_App/test/integration/users_signup_test.rb new file mode 100644 index 0000000..b975745 --- /dev/null +++ b/Sample_App/test/integration/users_signup_test.rb @@ -0,0 +1,27 @@ +require 'test_helper' + +class UsersSignupTest < ActionDispatch::IntegrationTest + test "invalid signup information" do + get signup_path + assert_no_difference 'User.count' do + post users_path, params: { user: { name: "", + email: "user@invalid", + password: "foo", + password_confirmation: "bar" } } + end + assert_template 'users/new' + end + + test "valid signup information" do + get signup_path + assert_difference 'User.count', 1 do + post users_path, params: { user: { name: "Example User", + email: "user@example.com", + password: "password", + password_confirmation: "password" } } + end + follow_redirect! + assert_template 'users/show' + assert is_logged_in? + end +end diff --git a/Sample_App/test/mailers/.keep b/Sample_App/test/mailers/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/models/.keep b/Sample_App/test/models/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/models/user_test.rb b/Sample_App/test/models/user_test.rb new file mode 100644 index 0000000..19168ad --- /dev/null +++ b/Sample_App/test/models/user_test.rb @@ -0,0 +1,80 @@ +require 'test_helper' + +class UserTest < ActiveSupport::TestCase + # test "the truth" do + # assert true + # end + def setup + @user = User.new(name: "Example user", email: "user@example.com", + password: "test123", password_confirmation: "test123") + end + + test "should be valid" do + assert @user.valid? + end + + test "name should be present" do + @user.name = " " + assert_not @user.valid? + end + + test "email should be present" do + @user.email = " " + assert_not @user.valid? + end + + test "name should not be too long" do + @user.name = "a" * 51 + assert_not @user.valid? + end + + test "email should not be too long" do + @user.email = "a" * 244 + "@example.com" + assert_not @user.valid? + end + + test "email validation should accept valid addresses" do + valid_addresses = %w[user@example.com USER@foo.COM A_US-ER@foo.bar.org + first.last@foo.jp alice+bob@baz.cn] + valid_addresses.each do |valid_address| + @user.email = valid_address + assert @user.valid?, "#{valid_address.inspect} should be valid" + end + end + + test "email validation should reject invalid addresses" do + invalid_addresses = %w[user@example,com user_at_foo.org user.name@example. + foo@bar_baz.com foo@bar+baz.com] + invalid_addresses.each do |invalid_address| + @user.email = invalid_address + assert_not @user.valid?, "#{invalid_address.inspect} should be invalid" + end + end + + test "email addresses should be unique" do + duplicate_user = @user.dup + @user.save + assert_not duplicate_user.valid? + end + + test "email addresses should be saved as lower-case" do + mixed_case_email = "Foo@ExAMPle.CoM" + @user.email = mixed_case_email + @user.save + assert_equal mixed_case_email.downcase, @user.reload.email + end + + test "password should be present (nonblank)" do + @user.password = @user.password_confirmation = " " * 6 + assert_not @user.valid? + end + + test "password should have a minimum length" do + @user.password = @user.password_confirmation = "a" * 5 + assert_not @user.valid? + end + + test "authenticated? should return false for a user with nil digest" do + assert_not @user.authenticated?('') + end +end diff --git a/Sample_App/test/system/.keep b/Sample_App/test/system/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/test/test_helper.rb b/Sample_App/test/test_helper.rb new file mode 100644 index 0000000..e568227 --- /dev/null +++ b/Sample_App/test/test_helper.rb @@ -0,0 +1,33 @@ +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../../config/environment', __FILE__) +require 'rails/test_help' +require "minitest/reporters" +Minitest::Reporters.use! + +class ActiveSupport::TestCase + # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. + fixtures :all + include ApplicationHelper + + def is_logged_in? + !session[:user_id].nil? + end + + def log_in_as(user) + session[:user_id] = user.id + end + + + # Add more helper methods to be used by all tests here... +end + + +class ActionDispatch::IntegrationTest + + # Log in as a particular user. + def log_in_as(user, password: 'password', remember_me: '1') + post login_path, params: { session: { email: user.email, + password: password, + remember_me: remember_me } } + end +end \ No newline at end of file diff --git a/Sample_App/tmp/.keep b/Sample_App/tmp/.keep new file mode 100644 index 0000000..e69de29 diff --git a/Sample_App/vendor/.keep b/Sample_App/vendor/.keep new file mode 100644 index 0000000..e69de29 diff --git a/sample_app b/sample_app deleted file mode 160000 index a5b83c0..0000000 --- a/sample_app +++ /dev/null @@ -1 +0,0 @@ -Subproject commit a5b83c0709ff74aa6ffa57624a0c3e4eeb539fa6