From a690d0b05661e61c0512304280aa430cb670f514 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Thu, 15 Jul 2021 10:54:56 +0300 Subject: [PATCH] added models: questions, quiz, categories --- .gitignore | 1 + .rspec | 1 + .ruby-version | 2 +- Gemfile | 16 ++- Gemfile.lock | 65 ++++++++- app/assets/stylesheets/home.scss | 3 + app/assets/stylesheets/questions.scss | 3 + app/assets/stylesheets/scaffolds.scss | 65 +++++++++ app/controllers/home_controller.rb | 4 + app/controllers/questions_controller.rb | 69 ++++++++++ app/helpers/home_helper.rb | 2 + app/helpers/questions_helper.rb | 2 + app/models/category.rb | 3 + app/models/question.rb | 4 + app/models/quiz.rb | 2 + app/views/home/index.html.erb | 2 + app/views/questions/_form.html.erb | 17 +++ app/views/questions/_question.json.jbuilder | 2 + app/views/questions/edit.html.erb | 6 + app/views/questions/index.html.erb | 25 ++++ app/views/questions/index.json.jbuilder | 1 + app/views/questions/new.html.erb | 5 + app/views/questions/show.html.erb | 4 + app/views/questions/show.json.jbuilder | 1 + config/database.yml | 79 +++++++++-- config/routes.rb | 2 + db/migrate/20210714125036_create_questions.rb | 8 ++ ...10714131823_fix_column_text_in_question.rb | 5 + .../20210714140730_create_categories.rb | 9 ++ ...0210714141144_add_reference_to_question.rb | 5 + db/migrate/20210715073937_create_quizzes.rb | 9 ++ ...0210715074035_add_reference_to_category.rb | 5 + db/schema.rb | 40 ++++++ spec/factories/categories.rb | 5 + spec/factories/questions.rb | 5 + spec/factories/quizzes.rb | 5 + spec/helpers/home_helper_spec.rb | 15 ++ spec/helpers/questions_helper_spec.rb | 15 ++ spec/models/category_spec.rb | 5 + spec/models/question_spec.rb | 14 ++ spec/models/quiz_spec.rb | 5 + spec/rails_helper.rb | 101 ++++++++++++++ spec/requests/home_spec.rb | 11 ++ spec/requests/questions_spec.rb | 130 ++++++++++++++++++ spec/routing/questions_routing_spec.rb | 38 +++++ spec/spec_helper.rb | 96 +++++++++++++ spec/support/factory_bot.rb | 3 + spec/views/home/index.html.erb_spec.rb | 5 + spec/views/questions/edit.html.erb_spec.rb | 14 ++ spec/views/questions/index.html.erb_spec.rb | 14 ++ spec/views/questions/new.html.erb_spec.rb | 14 ++ spec/views/questions/show.html.erb_spec.rb | 11 ++ 52 files changed, 954 insertions(+), 19 deletions(-) create mode 100644 .rspec create mode 100644 app/assets/stylesheets/home.scss create mode 100644 app/assets/stylesheets/questions.scss create mode 100644 app/assets/stylesheets/scaffolds.scss create mode 100644 app/controllers/home_controller.rb create mode 100644 app/controllers/questions_controller.rb create mode 100644 app/helpers/home_helper.rb create mode 100644 app/helpers/questions_helper.rb create mode 100644 app/models/category.rb create mode 100644 app/models/question.rb create mode 100644 app/models/quiz.rb create mode 100644 app/views/home/index.html.erb create mode 100644 app/views/questions/_form.html.erb create mode 100644 app/views/questions/_question.json.jbuilder create mode 100644 app/views/questions/edit.html.erb create mode 100644 app/views/questions/index.html.erb create mode 100644 app/views/questions/index.json.jbuilder create mode 100644 app/views/questions/new.html.erb create mode 100644 app/views/questions/show.html.erb create mode 100644 app/views/questions/show.json.jbuilder create mode 100644 db/migrate/20210714125036_create_questions.rb create mode 100644 db/migrate/20210714131823_fix_column_text_in_question.rb create mode 100644 db/migrate/20210714140730_create_categories.rb create mode 100644 db/migrate/20210714141144_add_reference_to_question.rb create mode 100644 db/migrate/20210715073937_create_quizzes.rb create mode 100644 db/migrate/20210715074035_add_reference_to_category.rb create mode 100644 db/schema.rb create mode 100644 spec/factories/categories.rb create mode 100644 spec/factories/questions.rb create mode 100644 spec/factories/quizzes.rb create mode 100644 spec/helpers/home_helper_spec.rb create mode 100644 spec/helpers/questions_helper_spec.rb create mode 100644 spec/models/category_spec.rb create mode 100644 spec/models/question_spec.rb create mode 100644 spec/models/quiz_spec.rb create mode 100644 spec/rails_helper.rb create mode 100644 spec/requests/home_spec.rb create mode 100644 spec/requests/questions_spec.rb create mode 100644 spec/routing/questions_routing_spec.rb create mode 100644 spec/spec_helper.rb create mode 100644 spec/support/factory_bot.rb create mode 100644 spec/views/home/index.html.erb_spec.rb create mode 100644 spec/views/questions/edit.html.erb_spec.rb create mode 100644 spec/views/questions/index.html.erb_spec.rb create mode 100644 spec/views/questions/new.html.erb_spec.rb create mode 100644 spec/views/questions/show.html.erb_spec.rb diff --git a/.gitignore b/.gitignore index 072bcce6..db05bd5b 100644 --- a/.gitignore +++ b/.gitignore @@ -38,3 +38,4 @@ /yarn-error.log yarn-debug.log* .yarn-integrity +/coverage/* diff --git a/.rspec b/.rspec new file mode 100644 index 00000000..c99d2e73 --- /dev/null +++ b/.rspec @@ -0,0 +1 @@ +--require spec_helper diff --git a/.ruby-version b/.ruby-version index 2c9b4ef4..cb2b00e4 100644 --- a/.ruby-version +++ b/.ruby-version @@ -1 +1 @@ -2.7.3 +3.0.1 diff --git a/Gemfile b/Gemfile index 558e538a..b2476c2a 100644 --- a/Gemfile +++ b/Gemfile @@ -1,12 +1,12 @@ source 'https://rubygems.org' git_source(:github) { |repo| "https://github.com/#{repo}.git" } -ruby '2.7.3' +ruby '3.0.1' # Bundle edge Rails instead: gem 'rails', github: 'rails/rails', branch: 'main' gem 'rails', '~> 6.1.4' # Use sqlite3 as the database for Active Record -gem 'sqlite3', '~> 1.4' +gem 'pg' # Use Puma as the app server gem 'puma', '~> 5.0' # Use SCSS for stylesheets @@ -27,10 +27,14 @@ gem 'jbuilder', '~> 2.7' # Reduces boot times through caching; required in config/boot.rb gem 'bootsnap', '>= 1.4.4', require: false +gem 'devise' 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] + gem 'byebug', platforms: %i[mri mingw x64_mingw] + gem 'database_cleaner' + gem 'factory_bot_rails' + gem 'rspec-rails', '~> 5.0.0' end group :development do @@ -38,8 +42,8 @@ group :development do gem 'web-console', '>= 4.1.0' # Display performance information such as SQL time and flame graphs for each request in your browser. # Can be configured to work on production as well see: https://github.com/MiniProfiler/rack-mini-profiler/blob/master/README.md - gem 'rack-mini-profiler', '~> 2.0' gem 'listen', '~> 3.3' + gem 'rack-mini-profiler', '~> 2.0' # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring gem 'spring' end @@ -49,8 +53,10 @@ group :test do gem 'capybara', '>= 3.26' gem 'selenium-webdriver' # Easy installation and use of web drivers to run system tests with browsers + gem 'faker' + gem 'simplecov', '0.17.1', require: false # CC last supported v0.17 gem 'webdrivers' end # Windows does not include zoneinfo files, so bundle the tzinfo-data gem -gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby] +gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby] diff --git a/Gemfile.lock b/Gemfile.lock index b64aeee8..0aabec90 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -62,6 +62,7 @@ GEM zeitwerk (~> 2.3) addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) + bcrypt (3.1.16) bindex (0.8.1) bootsnap (1.7.5) msgpack (~> 1.0) @@ -78,7 +79,28 @@ GEM childprocess (3.0.0) concurrent-ruby (1.1.9) crass (1.0.6) + database_cleaner (2.0.1) + database_cleaner-active_record (~> 2.0.0) + database_cleaner-active_record (2.0.1) + activerecord (>= 5.a) + database_cleaner-core (~> 2.0.0) + database_cleaner-core (2.0.1) + devise (4.8.0) + bcrypt (~> 3.0) + orm_adapter (~> 0.1) + railties (>= 4.1.0) + responders + warden (~> 1.2.3) + diff-lcs (1.4.4) + docile (1.4.0) erubi (1.10.0) + factory_bot (6.2.0) + activesupport (>= 5.0.0) + factory_bot_rails (6.2.0) + factory_bot (~> 6.2.0) + railties (>= 5.0.0) + faker (2.18.0) + i18n (>= 1.6, < 2) ffi (1.15.3) globalid (0.4.2) activesupport (>= 4.2.0) @@ -86,6 +108,7 @@ GEM concurrent-ruby (~> 1.0) jbuilder (2.11.2) activesupport (>= 5.0.0) + json (2.5.1) listen (3.5.1) rb-fsevent (~> 0.10, >= 0.10.3) rb-inotify (~> 0.9, >= 0.9.10) @@ -104,6 +127,8 @@ GEM nokogiri (1.11.7) mini_portile2 (~> 2.5.0) racc (~> 1.4) + orm_adapter (0.5.0) + pg (1.2.3) public_suffix (4.0.6) puma (5.3.2) nio4r (~> 2.0) @@ -146,6 +171,26 @@ GEM rb-inotify (0.10.1) ffi (~> 1.0) regexp_parser (2.1.1) + responders (3.0.1) + actionpack (>= 5.0) + railties (>= 5.0) + rspec-core (3.10.1) + rspec-support (~> 3.10.0) + rspec-expectations (3.10.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-mocks (3.10.2) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.10.0) + rspec-rails (5.0.1) + actionpack (>= 5.2) + activesupport (>= 5.2) + railties (>= 5.2) + rspec-core (~> 3.10) + rspec-expectations (~> 3.10) + rspec-mocks (~> 3.10) + rspec-support (~> 3.10) + rspec-support (3.10.2) rubyzip (2.3.2) sass-rails (6.0.0) sassc-rails (~> 2.1, >= 2.1.1) @@ -161,6 +206,11 @@ GEM childprocess (>= 0.5, < 4.0) rubyzip (>= 1.2.2) semantic_range (3.0.0) + simplecov (0.17.1) + docile (~> 1.1) + json (>= 1.8, < 3) + simplecov-html (~> 0.10.0) + simplecov-html (0.10.2) spring (2.1.1) sprockets (4.0.2) concurrent-ruby (~> 1.0) @@ -169,7 +219,6 @@ GEM actionpack (>= 4.0) activesupport (>= 4.0) sprockets (>= 3.0.0) - sqlite3 (1.4.2) thor (1.1.0) tilt (2.0.10) turbolinks (5.2.1) @@ -177,6 +226,8 @@ GEM turbolinks-source (5.2.0) tzinfo (2.0.4) concurrent-ruby (~> 1.0) + warden (1.2.9) + rack (>= 2.0.9) web-console (4.1.0) actionview (>= 6.0.0) activemodel (>= 6.0.0) @@ -205,15 +256,21 @@ DEPENDENCIES bootsnap (>= 1.4.4) byebug capybara (>= 3.26) + database_cleaner + devise + factory_bot_rails + faker jbuilder (~> 2.7) listen (~> 3.3) + pg puma (~> 5.0) rack-mini-profiler (~> 2.0) rails (~> 6.1.4) + rspec-rails (~> 5.0.0) sass-rails (>= 6) selenium-webdriver + simplecov (= 0.17.1) spring - sqlite3 (~> 1.4) turbolinks (~> 5) tzinfo-data web-console (>= 4.1.0) @@ -221,7 +278,7 @@ DEPENDENCIES webpacker (~> 5.0) RUBY VERSION - ruby 2.7.3p183 + ruby 3.0.1p64 BUNDLED WITH - 2.1.4 + 2.2.15 diff --git a/app/assets/stylesheets/home.scss b/app/assets/stylesheets/home.scss new file mode 100644 index 00000000..9e167d00 --- /dev/null +++ b/app/assets/stylesheets/home.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Home controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/questions.scss b/app/assets/stylesheets/questions.scss new file mode 100644 index 00000000..d7cdb83f --- /dev/null +++ b/app/assets/stylesheets/questions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Questions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/assets/stylesheets/scaffolds.scss b/app/assets/stylesheets/scaffolds.scss new file mode 100644 index 00000000..bb2597f3 --- /dev/null +++ b/app/assets/stylesheets/scaffolds.scss @@ -0,0 +1,65 @@ +body { + background-color: #fff; + color: #333; + margin: 33px; } + +body, p, ol, ul, td { + font-family: verdana, arial, helvetica, sans-serif; + font-size: 13px; + line-height: 18px; } + +pre { + background-color: #eee; + padding: 10px; + font-size: 11px; } + +a { + color: #000; } + +a:visited { + color: #666; } + +a:hover { + color: #fff; + background-color: #000; } + +th { + padding-bottom: 5px; } + +td { + padding: 0 5px 7px; } + +div.field, +div.actions { + margin-bottom: 10px; } + +#notice { + color: green; } + +.field_with_errors { + padding: 2px; + background-color: red; + display: table; } + +#error_explanation { + width: 450px; + border: 2px solid red; + padding: 7px 7px 0; + margin-bottom: 20px; + background-color: #f0f0f0; } + +#error_explanation h2 { + text-align: left; + font-weight: bold; + padding: 5px 5px 5px 15px; + font-size: 12px; + margin: -7px -7px 0; + background-color: #c00; + color: #fff; } + +#error_explanation ul li { + font-size: 12px; + list-style: square; } + +label { + display: block; } diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb new file mode 100644 index 00000000..95f29929 --- /dev/null +++ b/app/controllers/home_controller.rb @@ -0,0 +1,4 @@ +class HomeController < ApplicationController + def index + end +end diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb new file mode 100644 index 00000000..c8cb22f0 --- /dev/null +++ b/app/controllers/questions_controller.rb @@ -0,0 +1,69 @@ +class QuestionsController < ApplicationController + before_action :set_question, only: %i[ show edit update destroy ] + + # GET /questions or /questions.json + def index + @questions = Question.all + end + + # GET /questions/1 or /questions/1.json + def show + end + + # GET /questions/new + def new + @question = Question.new + end + + # GET /questions/1/edit + def edit + end + + # POST /questions or /questions.json + def create + @question = Question.new(question_params) + + respond_to do |format| + if @question.save + format.html { redirect_to @question, notice: "Question was successfully created." } + format.json { render :show, status: :created, location: @question } + else + format.html { render :new, status: :unprocessable_entity } + format.json { render json: @question.errors, status: :unprocessable_entity } + end + end + end + + # PATCH/PUT /questions/1 or /questions/1.json + def update + respond_to do |format| + if @question.update(question_params) + format.html { redirect_to @question, notice: "Question was successfully updated." } + format.json { render :show, status: :ok, location: @question } + else + format.html { render :edit, status: :unprocessable_entity } + format.json { render json: @question.errors, status: :unprocessable_entity } + end + end + end + + # DELETE /questions/1 or /questions/1.json + def destroy + @question.destroy + respond_to do |format| + format.html { redirect_to questions_url, notice: "Question was successfully destroyed." } + format.json { head :no_content } + end + end + + private + # Use callbacks to share common setup or constraints between actions. + def set_question + @question = Question.find(params[:id]) + end + + # Only allow a list of trusted parameters through. + def question_params + params.fetch(:question, {}) + end +end diff --git a/app/helpers/home_helper.rb b/app/helpers/home_helper.rb new file mode 100644 index 00000000..23de56ac --- /dev/null +++ b/app/helpers/home_helper.rb @@ -0,0 +1,2 @@ +module HomeHelper +end diff --git a/app/helpers/questions_helper.rb b/app/helpers/questions_helper.rb new file mode 100644 index 00000000..2eaab4ab --- /dev/null +++ b/app/helpers/questions_helper.rb @@ -0,0 +1,2 @@ +module QuestionsHelper +end diff --git a/app/models/category.rb b/app/models/category.rb new file mode 100644 index 00000000..27ea36ce --- /dev/null +++ b/app/models/category.rb @@ -0,0 +1,3 @@ +class Category < ApplicationRecord + has_many :questions +end diff --git a/app/models/question.rb b/app/models/question.rb new file mode 100644 index 00000000..802fcabe --- /dev/null +++ b/app/models/question.rb @@ -0,0 +1,4 @@ +class Question < ApplicationRecord + validates :title, presence: true + belongs_to :category +end diff --git a/app/models/quiz.rb b/app/models/quiz.rb new file mode 100644 index 00000000..a9f5acc8 --- /dev/null +++ b/app/models/quiz.rb @@ -0,0 +1,2 @@ +class Quiz < ApplicationRecord +end diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb new file mode 100644 index 00000000..2085730c --- /dev/null +++ b/app/views/home/index.html.erb @@ -0,0 +1,2 @@ +

Home#index

+

Find me in app/views/home/index.html.erb

diff --git a/app/views/questions/_form.html.erb b/app/views/questions/_form.html.erb new file mode 100644 index 00000000..eef8c3f9 --- /dev/null +++ b/app/views/questions/_form.html.erb @@ -0,0 +1,17 @@ +<%= form_with(model: question) do |form| %> + <% if question.errors.any? %> +
+

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

+ + +
+ <% end %> + +
+ <%= form.submit %> +
+<% end %> diff --git a/app/views/questions/_question.json.jbuilder b/app/views/questions/_question.json.jbuilder new file mode 100644 index 00000000..998b8f8c --- /dev/null +++ b/app/views/questions/_question.json.jbuilder @@ -0,0 +1,2 @@ +json.extract! question, :id, :created_at, :updated_at +json.url question_url(question, format: :json) diff --git a/app/views/questions/edit.html.erb b/app/views/questions/edit.html.erb new file mode 100644 index 00000000..8140146c --- /dev/null +++ b/app/views/questions/edit.html.erb @@ -0,0 +1,6 @@ +

Editing Question

+ +<%= render 'form', question: @question %> + +<%= link_to 'Show', @question %> | +<%= link_to 'Back', questions_path %> diff --git a/app/views/questions/index.html.erb b/app/views/questions/index.html.erb new file mode 100644 index 00000000..b152266f --- /dev/null +++ b/app/views/questions/index.html.erb @@ -0,0 +1,25 @@ +

<%= notice %>

+ +

Questions

+ + + + + + + + + + <% @questions.each do |question| %> + + + + + + <% end %> + +
<%= link_to 'Show', question %><%= link_to 'Edit', edit_question_path(question) %><%= link_to 'Destroy', question, method: :delete, data: { confirm: 'Are you sure?' } %>
+ +
+ +<%= link_to 'New Question', new_question_path %> diff --git a/app/views/questions/index.json.jbuilder b/app/views/questions/index.json.jbuilder new file mode 100644 index 00000000..83754956 --- /dev/null +++ b/app/views/questions/index.json.jbuilder @@ -0,0 +1 @@ +json.array! @questions, partial: "questions/question", as: :question diff --git a/app/views/questions/new.html.erb b/app/views/questions/new.html.erb new file mode 100644 index 00000000..2d2d9b47 --- /dev/null +++ b/app/views/questions/new.html.erb @@ -0,0 +1,5 @@ +

New Question

+ +<%= render 'form', question: @question %> + +<%= link_to 'Back', questions_path %> diff --git a/app/views/questions/show.html.erb b/app/views/questions/show.html.erb new file mode 100644 index 00000000..97d55e4e --- /dev/null +++ b/app/views/questions/show.html.erb @@ -0,0 +1,4 @@ +

<%= notice %>

+ +<%= link_to 'Edit', edit_question_path(@question) %> | +<%= link_to 'Back', questions_path %> diff --git a/app/views/questions/show.json.jbuilder b/app/views/questions/show.json.jbuilder new file mode 100644 index 00000000..5bf43b19 --- /dev/null +++ b/app/views/questions/show.json.jbuilder @@ -0,0 +1 @@ +json.partial! "questions/question", question: @question diff --git a/config/database.yml b/config/database.yml index 4a8a1b26..8cb1d17d 100644 --- a/config/database.yml +++ b/config/database.yml @@ -1,25 +1,86 @@ -# SQLite. Versions 3.8.0 and up are supported. -# gem install sqlite3 +# PostgreSQL. Versions 9.3 and up are supported. # -# Ensure the SQLite 3 gem is defined in your Gemfile -# gem 'sqlite3' +# Install the pg driver: +# gem install pg +# On macOS with Homebrew: +# gem install pg -- --with-pg-config=/usr/local/bin/pg_config +# On macOS with MacPorts: +# gem install pg -- --with-pg-config=/opt/local/lib/postgresql84/bin/pg_config +# On Windows: +# gem install pg +# Choose the win32 build. +# Install PostgreSQL and put its /bin directory on your path. +# +# Configure Using Gemfile +# gem 'pg' # default: &default - adapter: sqlite3 + adapter: postgresql + encoding: unicode + # For details on connection pooling, see Rails configuration guide + # https://guides.rubyonrails.org/configuring.html#database-pooling pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %> - timeout: 5000 development: <<: *default - database: db/development.sqlite3 + database: accreditation_development + + # The specified database role being used to connect to postgres. + # To create additional roles in postgres see `$ createuser --help`. + # When left blank, postgres will use the default role. This is + # the same name as the operating system user running Rails. + #username: tort_ee + + # The password associated with the postgres role (username). + #password: + + # Connect on a TCP socket. Omitted by default since the client uses a + # domain socket that doesn't need configuration. Windows does not have + # domain sockets, so uncomment these lines. + #host: localhost + + # The TCP port the server listens on. Defaults to 5432. + # If your server runs on a different port number, change accordingly. + #port: 5432 + + # Schema search path. The server defaults to $user,public + #schema_search_path: myapp,sharedapp,public + + # Minimum log levels, in increasing order: + # debug5, debug4, debug3, debug2, debug1, + # log, notice, warning, error, fatal, and panic + # Defaults to warning. + #min_messages: notice # Warning: The database defined as "test" will be erased and # re-generated from your development database when you run "rake". # Do not set this db to the same as development or production. test: <<: *default - database: db/test.sqlite3 + database: accreditation_development_test +# As with config/credentials.yml, you never want to store sensitive information, +# like your database password, in your source code. If your source code is +# ever seen by anyone, they now have access to your database. +# +# Instead, provide the password or a full connection URL as an environment +# variable when you boot the app. For example: +# +# DATABASE_URL="postgres://myuser:mypass@localhost/somedatabase" +# +# If the connection URL is provided in the special DATABASE_URL environment +# variable, Rails will automatically merge its configuration values on top of +# the values provided in this file. Alternatively, you can specify a connection +# URL environment variable explicitly: +# +# production: +# url: <%= ENV['MY_APP_DATABASE_URL'] %> +# +# Read https://guides.rubyonrails.org/configuring.html#configuring-a-database +# for a full overview on how database connection configuration can be specified. +# production: <<: *default - database: db/production.sqlite3 + database: accreditation_development_production + username: accreditation_development + password: <%= ENV['ACCREDITATION_DATABASE_PASSWORD'] %> diff --git a/config/routes.rb b/config/routes.rb index c06383a1..a1fb8271 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,3 +1,5 @@ Rails.application.routes.draw do + root 'home#index' + resources :questions # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html end diff --git a/db/migrate/20210714125036_create_questions.rb b/db/migrate/20210714125036_create_questions.rb new file mode 100644 index 00000000..014e6e12 --- /dev/null +++ b/db/migrate/20210714125036_create_questions.rb @@ -0,0 +1,8 @@ +class CreateQuestions < ActiveRecord::Migration[6.1] + def change + create_table :questions do |t| + t.string :text + t.timestamps + end + end +end diff --git a/db/migrate/20210714131823_fix_column_text_in_question.rb b/db/migrate/20210714131823_fix_column_text_in_question.rb new file mode 100644 index 00000000..2c63b2b1 --- /dev/null +++ b/db/migrate/20210714131823_fix_column_text_in_question.rb @@ -0,0 +1,5 @@ +class FixColumnTextInQuestion < ActiveRecord::Migration[6.1] + def change + rename_column :questions, :text, :title + end +end diff --git a/db/migrate/20210714140730_create_categories.rb b/db/migrate/20210714140730_create_categories.rb new file mode 100644 index 00000000..6d1d2fe9 --- /dev/null +++ b/db/migrate/20210714140730_create_categories.rb @@ -0,0 +1,9 @@ +class CreateCategories < ActiveRecord::Migration[6.1] + def change + create_table :categories do |t| + t.string :title + + t.timestamps + end + end +end diff --git a/db/migrate/20210714141144_add_reference_to_question.rb b/db/migrate/20210714141144_add_reference_to_question.rb new file mode 100644 index 00000000..877a092b --- /dev/null +++ b/db/migrate/20210714141144_add_reference_to_question.rb @@ -0,0 +1,5 @@ +class AddReferenceToQuestion < ActiveRecord::Migration[6.1] + def change + add_reference :questions, :category, index: true + end +end diff --git a/db/migrate/20210715073937_create_quizzes.rb b/db/migrate/20210715073937_create_quizzes.rb new file mode 100644 index 00000000..85438202 --- /dev/null +++ b/db/migrate/20210715073937_create_quizzes.rb @@ -0,0 +1,9 @@ +class CreateQuizzes < ActiveRecord::Migration[6.1] + def change + create_table :quizzes do |t| + t.string :title + + t.timestamps + end + end +end diff --git a/db/migrate/20210715074035_add_reference_to_category.rb b/db/migrate/20210715074035_add_reference_to_category.rb new file mode 100644 index 00000000..78cde4aa --- /dev/null +++ b/db/migrate/20210715074035_add_reference_to_category.rb @@ -0,0 +1,5 @@ +class AddReferenceToCategory < ActiveRecord::Migration[6.1] + def change + add_reference :categories, :quiz, index: true + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..9c42aef6 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,40 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `bin/rails +# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2021_07_15_074035) do + + # These are extensions that must be enabled in order to support this database + enable_extension "plpgsql" + + create_table "categories", force: :cascade do |t| + t.string "title" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.bigint "quiz_id" + t.index ["quiz_id"], name: "index_categories_on_quiz_id" + end + + create_table "questions", force: :cascade do |t| + t.string "title" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + t.bigint "category_id" + t.index ["category_id"], name: "index_questions_on_category_id" + end + + create_table "quizzes", force: :cascade do |t| + t.string "title" + t.datetime "created_at", precision: 6, null: false + t.datetime "updated_at", precision: 6, null: false + end + +end diff --git a/spec/factories/categories.rb b/spec/factories/categories.rb new file mode 100644 index 00000000..2a511f6e --- /dev/null +++ b/spec/factories/categories.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :category do + title { "MyString" } + end +end diff --git a/spec/factories/questions.rb b/spec/factories/questions.rb new file mode 100644 index 00000000..e1816747 --- /dev/null +++ b/spec/factories/questions.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :question do + title { "How are doing?" } + end +end diff --git a/spec/factories/quizzes.rb b/spec/factories/quizzes.rb new file mode 100644 index 00000000..aff1e057 --- /dev/null +++ b/spec/factories/quizzes.rb @@ -0,0 +1,5 @@ +FactoryBot.define do + factory :quiz do + title { "MyString" } + end +end diff --git a/spec/helpers/home_helper_spec.rb b/spec/helpers/home_helper_spec.rb new file mode 100644 index 00000000..e537d8d9 --- /dev/null +++ b/spec/helpers/home_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the HomeHelper. For example: +# +# describe HomeHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe HomeHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/questions_helper_spec.rb b/spec/helpers/questions_helper_spec.rb new file mode 100644 index 00000000..c9ba916c --- /dev/null +++ b/spec/helpers/questions_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the QuestionsHelper. For example: +# +# describe QuestionsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +RSpec.describe QuestionsHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/category_spec.rb b/spec/models/category_spec.rb new file mode 100644 index 00000000..0c9fefae --- /dev/null +++ b/spec/models/category_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Category, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb new file mode 100644 index 00000000..fb7c7d4c --- /dev/null +++ b/spec/models/question_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe Question, type: :model do + include Rack::Test::Methods + context 'check validation' do + let(:question) { build(:question, title: "Hey") } + + it 'check presence of title field' do + question.title = nil + + expect(question.save).to be false + end + end +end diff --git a/spec/models/quiz_spec.rb b/spec/models/quiz_spec.rb new file mode 100644 index 00000000..fc9291dd --- /dev/null +++ b/spec/models/quiz_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Quiz, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/rails_helper.rb b/spec/rails_helper.rb new file mode 100644 index 00000000..a4c6258a --- /dev/null +++ b/spec/rails_helper.rb @@ -0,0 +1,101 @@ +# This file is copied to spec/ when you run 'rails generate rspec:install' +require 'simplecov' +SimpleCov.start 'rails' +puts "required simplecov" + + +require 'spec_helper' +ENV['RAILS_ENV'] ||= 'test' +require File.expand_path('../config/environment', __dir__) +# Prevent database truncation if the environment is production +abort("The Rails environment is running in production mode!") if Rails.env.production? +require 'rspec/rails' +require 'capybara/rspec' +require 'database_cleaner' +Dir[Rails.root.join('spec/support/**/*.rb')].each { |f| require f } +# Add additional requires below this line. Rails is not loaded until this point! + +# Requires supporting ruby files with custom matchers and macros, etc, in +# spec/support/ and its subdirectories. Files matching `spec/**/*_spec.rb` are +# run as spec files by default. This means that files in spec/support that end +# in _spec.rb will both be required and run as specs, causing the specs to be +# run twice. It is recommended that you do not name files matching this glob to +# end with _spec.rb. You can configure this pattern with the --pattern +# option on the command line or in ~/.rspec, .rspec or `.rspec-local`. +# +# The following line is provided for convenience purposes. It has the downside +# of increasing the boot-up time by auto-requiring all files in the support +# directory. Alternatively, in the individual `*_spec.rb` files, manually +# require only the support files necessary. +# +# Dir[Rails.root.join('spec', 'support', '**', '*.rb')].sort.each { |f| require f } + +# Checks for pending migrations and applies them before tests are run. +# If you are not using ActiveRecord, you can remove these lines. +begin + ActiveRecord::Migration.maintain_test_schema! +rescue ActiveRecord::PendingMigrationError => e + puts e.to_s.strip + exit 1 +end +RSpec.configure do |config| + # Remove this line if you're not using ActiveRecord or ActiveRecord fixtures + config.include FactoryBot::Syntax::Methods + config.include Devise::Test::ControllerHelpers, type: :controller + + # If you're not using ActiveRecord, or you'd prefer not to run each of your + # examples within a transaction, remove the following line or assign false + # instead of true. + config.use_transactional_fixtures = false + + config.before(:suite) do + DatabaseCleaner.clean_with(:truncation) + end + + config.before(:each) do + DatabaseCleaner.strategy = :transaction + end + + config.before(:each, js: true) do + DatabaseCleaner.strategy = :truncation + end + + config.before(:each) do + DatabaseCleaner.start + end + + config.after(:each) do + DatabaseCleaner.clean + end + + config.before(:all) do + DatabaseCleaner.start + end + + config.after(:all) do + DatabaseCleaner.clean + end + + # You can uncomment this line to turn off ActiveRecord support entirely. + # config.use_active_record = false + + # RSpec Rails can automatically mix in different behaviours to your tests + # based on their file location, for example enabling you to call `get` and + # `post` in specs under `spec/controllers`. + # + # You can disable this behaviour by removing the line below, and instead + # explicitly tag your specs with their type, e.g.: + # + # RSpec.describe UsersController, type: :controller do + # # ... + # end + # + # The different available types are documented in the features, such as in + # https://relishapp.com/rspec/rspec-rails/docs + config.infer_spec_type_from_file_location! + + # Filter lines from Rails gems in backtraces. + config.filter_rails_from_backtrace! + # arbitrary gems may also be filtered via: + # config.filter_gems_from_backtrace("gem name") +end diff --git a/spec/requests/home_spec.rb b/spec/requests/home_spec.rb new file mode 100644 index 00000000..fdbd6423 --- /dev/null +++ b/spec/requests/home_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "Homes", type: :request do + describe "GET /index" do + it "returns http success" do + get "/home/index" + expect(response).to have_http_status(:success) + end + end + +end diff --git a/spec/requests/questions_spec.rb b/spec/requests/questions_spec.rb new file mode 100644 index 00000000..654adb09 --- /dev/null +++ b/spec/requests/questions_spec.rb @@ -0,0 +1,130 @@ + require 'rails_helper' + +# This spec was generated by rspec-rails when you ran the scaffold generator. +# It demonstrates how one might use RSpec to test 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. + +RSpec.describe "/questions", type: :request do + + # Question. As you add validations to Question, be sure to + # adjust the attributes here as well. + let(:valid_attributes) { + skip("Add a hash of attributes valid for your model") + } + + let(:invalid_attributes) { + skip("Add a hash of attributes invalid for your model") + } + + describe "GET /index" do + it "renders a successful response" do + Question.create! valid_attributes + get questions_url + expect(response).to be_successful + end + end + + describe "GET /show" do + it "renders a successful response" do + question = Question.create! valid_attributes + get question_url(question) + expect(response).to be_successful + end + end + + describe "GET /new" do + it "renders a successful response" do + get new_question_url + expect(response).to be_successful + end + end + + describe "GET /edit" do + it "render a successful response" do + question = Question.create! valid_attributes + get edit_question_url(question) + expect(response).to be_successful + end + end + + describe "POST /create" do + context "with valid parameters" do + it "creates a new Question" do + expect { + post questions_url, params: { question: valid_attributes } + }.to change(Question, :count).by(1) + end + + it "redirects to the created question" do + post questions_url, params: { question: valid_attributes } + expect(response).to redirect_to(question_url(Question.last)) + end + end + + context "with invalid parameters" do + it "does not create a new Question" do + expect { + post questions_url, params: { question: invalid_attributes } + }.to change(Question, :count).by(0) + end + + it "renders a successful response (i.e. to display the 'new' template)" do + post questions_url, params: { question: invalid_attributes } + expect(response).to be_successful + end + end + end + + describe "PATCH /update" do + context "with valid parameters" do + let(:new_attributes) { + skip("Add a hash of attributes valid for your model") + } + + it "updates the requested question" do + question = Question.create! valid_attributes + patch question_url(question), params: { question: new_attributes } + question.reload + skip("Add assertions for updated state") + end + + it "redirects to the question" do + question = Question.create! valid_attributes + patch question_url(question), params: { question: new_attributes } + question.reload + expect(response).to redirect_to(question_url(question)) + end + end + + context "with invalid parameters" do + it "renders a successful response (i.e. to display the 'edit' template)" do + question = Question.create! valid_attributes + patch question_url(question), params: { question: invalid_attributes } + expect(response).to be_successful + end + end + end + + describe "DELETE /destroy" do + it "destroys the requested question" do + question = Question.create! valid_attributes + expect { + delete question_url(question) + }.to change(Question, :count).by(-1) + end + + it "redirects to the questions list" do + question = Question.create! valid_attributes + delete question_url(question) + expect(response).to redirect_to(questions_url) + end + end +end diff --git a/spec/routing/questions_routing_spec.rb b/spec/routing/questions_routing_spec.rb new file mode 100644 index 00000000..ebed2543 --- /dev/null +++ b/spec/routing/questions_routing_spec.rb @@ -0,0 +1,38 @@ +require "rails_helper" + +RSpec.describe QuestionsController, type: :routing do + describe "routing" do + it "routes to #index" do + expect(get: "/questions").to route_to("questions#index") + end + + it "routes to #new" do + expect(get: "/questions/new").to route_to("questions#new") + end + + it "routes to #show" do + expect(get: "/questions/1").to route_to("questions#show", id: "1") + end + + it "routes to #edit" do + expect(get: "/questions/1/edit").to route_to("questions#edit", id: "1") + end + + + it "routes to #create" do + expect(post: "/questions").to route_to("questions#create") + end + + it "routes to #update via PUT" do + expect(put: "/questions/1").to route_to("questions#update", id: "1") + end + + it "routes to #update via PATCH" do + expect(patch: "/questions/1").to route_to("questions#update", id: "1") + end + + it "routes to #destroy" do + expect(delete: "/questions/1").to route_to("questions#destroy", id: "1") + end + end +end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 00000000..ce33d66d --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1,96 @@ +# This file was generated by the `rails generate rspec:install` command. Conventionally, all +# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`. +# The generated `.rspec` file contains `--require spec_helper` which will cause +# this file to always be loaded, without a need to explicitly require it in any +# files. +# +# Given that it is always loaded, you are encouraged to keep this file as +# light-weight as possible. Requiring heavyweight dependencies from this file +# will add to the boot time of your test suite on EVERY test run, even for an +# individual file that may not need all of that loaded. Instead, consider making +# a separate helper file that requires the additional dependencies and performs +# the additional setup, and require it from the spec files that actually need +# it. +# +# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration +RSpec.configure do |config| + # rspec-expectations config goes here. You can use an alternate + # assertion/expectation library such as wrong or the stdlib/minitest + # assertions if you prefer. + config.expect_with :rspec do |expectations| + # This option will default to `true` in RSpec 4. It makes the `description` + # and `failure_message` of custom matchers include text for helper methods + # defined using `chain`, e.g.: + # be_bigger_than(2).and_smaller_than(4).description + # # => "be bigger than 2 and smaller than 4" + # ...rather than: + # # => "be bigger than 2" + expectations.include_chain_clauses_in_custom_matcher_descriptions = true + end + + # rspec-mocks config goes here. You can use an alternate test double + # library (such as bogus or mocha) by changing the `mock_with` option here. + config.mock_with :rspec do |mocks| + # Prevents you from mocking or stubbing a method that does not exist on + # a real object. This is generally recommended, and will default to + # `true` in RSpec 4. + mocks.verify_partial_doubles = true + end + + # This option will default to `:apply_to_host_groups` in RSpec 4 (and will + # have no way to turn it off -- the option exists only for backwards + # compatibility in RSpec 3). It causes shared context metadata to be + # inherited by the metadata hash of host groups and examples, rather than + # triggering implicit auto-inclusion in groups with matching metadata. + config.shared_context_metadata_behavior = :apply_to_host_groups + +# The settings below are suggested to provide a good initial experience +# with RSpec, but feel free to customize to your heart's content. +=begin + # This allows you to limit a spec run to individual examples or groups + # you care about by tagging them with `:focus` metadata. When nothing + # is tagged with `:focus`, all examples get run. RSpec also provides + # aliases for `it`, `describe`, and `context` that include `:focus` + # metadata: `fit`, `fdescribe` and `fcontext`, respectively. + config.filter_run_when_matching :focus + + # Allows RSpec to persist some state between runs in order to support + # the `--only-failures` and `--next-failure` CLI options. We recommend + # you configure your source control system to ignore this file. + config.example_status_persistence_file_path = "spec/examples.txt" + + # Limits the available syntax to the non-monkey patched syntax that is + # recommended. For more details, see: + # - http://rspec.info/blog/2012/06/rspecs-new-expectation-syntax/ + # - http://www.teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/ + # - http://rspec.info/blog/2014/05/notable-changes-in-rspec-3/#zero-monkey-patching-mode + config.disable_monkey_patching! + + # Many RSpec users commonly either run the entire suite or an individual + # file, and it's useful to allow more verbose output when running an + # individual spec file. + if config.files_to_run.one? + # Use the documentation formatter for detailed output, + # unless a formatter has already been configured + # (e.g. via a command-line flag). + config.default_formatter = "doc" + end + + # Print the 10 slowest examples and example groups at the + # end of the spec run, to help surface which specs are running + # particularly slow. + config.profile_examples = 10 + + # Run specs in random order to surface order dependencies. If you find an + # order dependency and want to debug it, you can fix the order by providing + # the seed, which is printed after each run. + # --seed 1234 + config.order = :random + + # Seed global randomization in this process using the `--seed` CLI option. + # Setting this allows you to use `--seed` to deterministically reproduce + # test failures related to randomization by passing the same `--seed` value + # as the one that triggered the failure. + Kernel.srand config.seed +=end +end diff --git a/spec/support/factory_bot.rb b/spec/support/factory_bot.rb new file mode 100644 index 00000000..195f20aa --- /dev/null +++ b/spec/support/factory_bot.rb @@ -0,0 +1,3 @@ +RSpec.configure do |config| + config.include FactoryBot::Syntax::Methods +end \ No newline at end of file diff --git a/spec/views/home/index.html.erb_spec.rb b/spec/views/home/index.html.erb_spec.rb new file mode 100644 index 00000000..75bb045b --- /dev/null +++ b/spec/views/home/index.html.erb_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe "home/index.html.erb", type: :view do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/questions/edit.html.erb_spec.rb b/spec/views/questions/edit.html.erb_spec.rb new file mode 100644 index 00000000..ab54af5b --- /dev/null +++ b/spec/views/questions/edit.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "questions/edit", type: :view do + before(:each) do + @question = assign(:question, Question.create!()) + end + + # it "renders the edit question form" do + # render + + # assert_select "form[action=?][method=?]", question_path(@question), "post" do + # end + # end +end diff --git a/spec/views/questions/index.html.erb_spec.rb b/spec/views/questions/index.html.erb_spec.rb new file mode 100644 index 00000000..b6d0b04c --- /dev/null +++ b/spec/views/questions/index.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "questions/index", type: :view do + before(:each) do + assign(:questions, [ + Question.create!(), + Question.create!() + ]) + end + + # it "renders a list of questions" do + # render + # end +end diff --git a/spec/views/questions/new.html.erb_spec.rb b/spec/views/questions/new.html.erb_spec.rb new file mode 100644 index 00000000..fa276c5b --- /dev/null +++ b/spec/views/questions/new.html.erb_spec.rb @@ -0,0 +1,14 @@ +require 'rails_helper' + +RSpec.describe "questions/new", type: :view do + before(:each) do + assign(:question, Question.new()) + end + + it "renders new question form" do + render + + assert_select "form[action=?][method=?]", questions_path, "post" do + end + end +end diff --git a/spec/views/questions/show.html.erb_spec.rb b/spec/views/questions/show.html.erb_spec.rb new file mode 100644 index 00000000..94ea18f9 --- /dev/null +++ b/spec/views/questions/show.html.erb_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "questions/show", type: :view do + before(:each) do + @question = assign(:question, Question.create!()) + end + + # it "renders attributes in

" do + # render + # end +end