From 71798f1638eb47421b4ca476be33a2d1319cfda7 Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Fri, 16 Jul 2021 12:11:47 +0300 Subject: [PATCH] added view for rendering categories, quizzes and questions --- Dockerfile.generic | 5 +++- Gemfile | 2 +- Gemfile.lock | 25 ++++++++++------ app/assets/stylesheets/categories.scss | 3 ++ app/assets/stylesheets/quiz.scss | 3 ++ app/controllers/categories_controller.rb | 8 +++++ app/controllers/home_controller.rb | 1 + app/controllers/quiz_controller.rb | 7 +++++ app/helpers/categories_helper.rb | 2 ++ app/helpers/quiz_helper.rb | 2 ++ app/views/categories/show.html.erb | 24 +++++++++++++++ app/views/home/index.html.erb | 18 ++++++++++-- app/views/quiz/show.html.erb | 17 +++++++++++ config/credentials.yml.enc | 2 +- config/environments/production.rb | 3 +- config/environments/staging.rb | 16 +++++++++- config/routes.rb | 3 ++ config/secrets.yml | 25 ---------------- config/webpack/staging.js | 5 ++++ config/webpacker.yml | 22 ++++++++------ ...10716071248_add_new_field_to_categories.rb | 5 ++++ db/schema.rb | 3 +- db/seeds.rb | 29 ++++++++++++++++++- docker-compose.yml | 2 +- spec/helpers/categories_helper_spec.rb | 15 ++++++++++ spec/helpers/quiz_helper_spec.rb | 15 ++++++++++ spec/requests/categories_spec.rb | 11 +++++++ spec/requests/quiz_spec.rb | 11 +++++++ spec/views/categories/show.html.erb_spec.rb | 5 ++++ spec/views/quiz/index.html.erb_spec.rb | 5 ++++ spec/views/quiz/show.html.erb_spec.rb | 5 ++++ 31 files changed, 246 insertions(+), 53 deletions(-) create mode 100644 app/assets/stylesheets/categories.scss create mode 100644 app/assets/stylesheets/quiz.scss create mode 100644 app/controllers/categories_controller.rb create mode 100644 app/controllers/quiz_controller.rb create mode 100644 app/helpers/categories_helper.rb create mode 100644 app/helpers/quiz_helper.rb create mode 100644 app/views/categories/show.html.erb create mode 100644 app/views/quiz/show.html.erb delete mode 100644 config/secrets.yml create mode 100644 config/webpack/staging.js create mode 100644 db/migrate/20210716071248_add_new_field_to_categories.rb create mode 100644 spec/helpers/categories_helper_spec.rb create mode 100644 spec/helpers/quiz_helper_spec.rb create mode 100644 spec/requests/categories_spec.rb create mode 100644 spec/requests/quiz_spec.rb create mode 100644 spec/views/categories/show.html.erb_spec.rb create mode 100644 spec/views/quiz/index.html.erb_spec.rb create mode 100644 spec/views/quiz/show.html.erb_spec.rb diff --git a/Dockerfile.generic b/Dockerfile.generic index 8fbcb0db..c0e0ee33 100644 --- a/Dockerfile.generic +++ b/Dockerfile.generic @@ -9,4 +9,7 @@ ENV SECRET_KEY_BASE "$SECRET_KEY_BASE" RUN mkdir -p /opt/webapps/app/tmp/pids WORKDIR /opt/webapps/app COPY . . -RUN gem install bundler && bundle update --bundler && bundle config set without 'development test' && bundle install +RUN gem install bundler && bundle update --bundler && bundle install +RUN yarn install --check-files +RUN bundle exec rails webpacker:install +RUN bundle exec rails assets:precompile diff --git a/Gemfile b/Gemfile index 9a854950..eebb6d0f 100644 --- a/Gemfile +++ b/Gemfile @@ -10,7 +10,7 @@ gem 'pg' # Use Puma as the app server gem 'puma', '~> 5.0' # Use SCSS for stylesheets -gem 'sass-rails', '>= 6' +gem 'sass-rails', '~> 5.1.0' # Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker gem 'webpacker', '~> 5.0' # Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks diff --git a/Gemfile.lock b/Gemfile.lock index fedc6674..6bf063db 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -146,13 +146,11 @@ GEM marcel (1.0.1) method_source (1.0.0) mini_mime (1.1.0) - mini_portile2 (2.5.3) minitest (5.14.4) msgpack (1.4.2) nested_form (0.3.2) nio4r (2.5.7) - nokogiri (1.11.7) - mini_portile2 (~> 2.5.0) + nokogiri (1.11.7-x86_64-linux) racc (~> 1.4) orm_adapter (0.5.0) pg (1.2.3) @@ -236,8 +234,17 @@ GEM 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) + 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.4.0) ffi (~> 1.9) sassc-rails (2.1.2) @@ -256,7 +263,7 @@ GEM simplecov-html (~> 0.10.0) simplecov-html (0.10.2) spring (2.1.1) - sprockets (4.0.2) + sprockets (3.7.2) concurrent-ruby (~> 1.0) rack (> 1, < 3) sprockets-rails (3.2.2) @@ -295,7 +302,7 @@ GEM zeitwerk (2.4.2) PLATFORMS - ruby + x86_64-linux DEPENDENCIES bootsnap (>= 1.4.4) @@ -315,7 +322,7 @@ DEPENDENCIES rails_admin (~> 2.0) rexml (~> 3.2, >= 3.2.4) rspec-rails (~> 5.0.0) - sass-rails (>= 6) + sass-rails (~> 5.1.0) selenium-webdriver simplecov (= 0.17.1) spring @@ -329,4 +336,4 @@ RUBY VERSION ruby 3.0.2p107 BUNDLED WITH - 2.2.23 + 2.2.24 diff --git a/app/assets/stylesheets/categories.scss b/app/assets/stylesheets/categories.scss new file mode 100644 index 00000000..068dd1a3 --- /dev/null +++ b/app/assets/stylesheets/categories.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Categories 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/quiz.scss b/app/assets/stylesheets/quiz.scss new file mode 100644 index 00000000..bbb26a88 --- /dev/null +++ b/app/assets/stylesheets/quiz.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the Quiz controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: https://sass-lang.com/ diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb new file mode 100644 index 00000000..9ef658fe --- /dev/null +++ b/app/controllers/categories_controller.rb @@ -0,0 +1,8 @@ +class CategoriesController < ApplicationController + def show + @questions = Question.where(category_id: params[:id]) + + category = Category.find(params[:id]) + @multiply = category.multiply + end +end diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index c014d20e..be0d71ac 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -2,5 +2,6 @@ class HomeController < ApplicationController skip_before_action :authenticate_user!, :only => [:index] def index + @quizzes = Quiz.all end end diff --git a/app/controllers/quiz_controller.rb b/app/controllers/quiz_controller.rb new file mode 100644 index 00000000..51f4937c --- /dev/null +++ b/app/controllers/quiz_controller.rb @@ -0,0 +1,7 @@ +class QuizController < ApplicationController + + def show + @quiz = Quiz.find(params[:id]) + @categories = Category.where(quiz_id: @quiz.id) + end +end diff --git a/app/helpers/categories_helper.rb b/app/helpers/categories_helper.rb new file mode 100644 index 00000000..e06f3155 --- /dev/null +++ b/app/helpers/categories_helper.rb @@ -0,0 +1,2 @@ +module CategoriesHelper +end diff --git a/app/helpers/quiz_helper.rb b/app/helpers/quiz_helper.rb new file mode 100644 index 00000000..918525ec --- /dev/null +++ b/app/helpers/quiz_helper.rb @@ -0,0 +1,2 @@ +module QuizHelper +end diff --git a/app/views/categories/show.html.erb b/app/views/categories/show.html.erb new file mode 100644 index 00000000..ca61bb1b --- /dev/null +++ b/app/views/categories/show.html.erb @@ -0,0 +1,24 @@ +

Questions

+ +<% if @questions.any? %> + +<%= form_with do |f| %> + + + +<% end %> +<% else %> +

No any questions

+<% end %> \ No newline at end of file diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index 2085730c..20586b25 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -1,2 +1,16 @@ -

Home#index

-

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

+

Home

+ +

Quizzes:

+<% if @quizzes.any? %> + +<% else %> +

No any quizzes

+<% end %> \ No newline at end of file diff --git a/app/views/quiz/show.html.erb b/app/views/quiz/show.html.erb new file mode 100644 index 00000000..89efce31 --- /dev/null +++ b/app/views/quiz/show.html.erb @@ -0,0 +1,17 @@ +

+ Choose your category +

+ +<% if @categories.any? %> + +<% else %> +

No Any Categories

+<% end %> diff --git a/config/credentials.yml.enc b/config/credentials.yml.enc index 7085a7a3..b1ea8dce 100644 --- a/config/credentials.yml.enc +++ b/config/credentials.yml.enc @@ -1 +1 @@ -nmmjPIYi31KQUAt4zkUVSZ+QrImBJF6b4nRsg7/J8DHuMWT0otIk4eOrZs6E2HvFSbxqx3kyjkDIjzPLPYI9/YX9z9Z5ZDGxMlPBmAbsXXtyAFp2g8e5Bjw64VzQhQmqQWkn20LWw13rcKFabn1hRgB0bx3/U12dqzD0SAslQSYpsxZWgH195Ivtt2f49VIAd0f9C86Fk/FmyUE/N3aXMtAcC9GZDZdfw3SOX/pSE+B0Zo97Cg+uKuwTne72FRQlMGRK/PiGkrtXeuedTiao7e1lwPIrqOrkvVXBuJ+4oiTzgNwWwILj/YAEWIi1BbtLYClkXiCIJy5q4xX1FfBqMl835Qi4V6AaYun7okObrVF9mji4BM1AkTQNmtjzSMP2fqu1n7gAgFPV0la6eVEVxFWNUBhXN0X8bA+P--b7GlQ5hc2O48fwW/--fyvygLf/+b1saj1gUuWRqw== \ No newline at end of file +jI2cuUFqWtfsPyA8z2Xh88MEaEOcLYeK6G9G8BAvWjdFB6vTBLo8OmVsH7fbGl+ZInR4EeUDrXUO2Xq4uZw0vcxw95TDnIvaxdWHdMIXbvXSPOSlFY8o9/tQujlU6kefhb0mJa0jXPlepgzqJ6xc8AzeVURr1geBMGvG7lazNEkDRwLgfxHAtNw3jUpe4SatYghlXsw6CQ772HvxlJtICpGT5saisELmttWXPZee2zahy2eWUQBSqYtYiqduVwY5vy8ngDRqS6dLYcYdaMikWH4rGI63ADYRkfs94guext6SjY1mriVrzCnqCI0AyMesPpHe6e6GBah1tmaz/iRSXXJ8B+aSi5iuR88WtcHiJSyPaAQryrQrKb4yZoKpTSBqNaVlUOGMrBnyCRRJ60QNapG4FDGkL9QX5CNT--hJE7dOrJqQIG4+Fm--qJLYUILiJwMR5xzEO1ofBQ== \ No newline at end of file diff --git a/config/environments/production.rb b/config/environments/production.rb index 4f99f0c1..602bab33 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -2,6 +2,7 @@ Rails.application.configure do # Settings specified here will take precedence over those in config/application.rb. + config.webpacker.check_yarn_integrity = false # Code is not reloaded between requests. config.cache_classes = true @@ -50,7 +51,7 @@ # Include generic and useful information about system operation, but avoid logging too much # information to avoid inadvertent exposure of personally identifiable information (PII). - config.log_level = :info + config.log_level = :debug # Prepend all log lines with the following tags. config.log_tags = [ :request_id ] diff --git a/config/environments/staging.rb b/config/environments/staging.rb index 1d2ee104..1b086b32 100644 --- a/config/environments/staging.rb +++ b/config/environments/staging.rb @@ -1 +1,15 @@ -require_relative 'production' \ No newline at end of file +require_relative 'production' + +Rails.application.configure do + # Settings specified here will take precedence over those in config/application.rb. + + require 'syslog/logger' + config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new(ENV['app_name'] || 'accreditation')) + + # Enable stdout logger +config.logger = Logger.new(STDOUT) + +# Set log level +config.log_level = :ERROR + +end diff --git a/config/routes.rb b/config/routes.rb index c060f1c2..22db76f8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,7 @@ Rails.application.routes.draw do + resources :quiz, only: [ :show ] + resources :categories, only: [ :show ] + devise_for :users mount RailsAdmin::Engine => '/admin', as: 'rails_admin' root 'home#index' diff --git a/config/secrets.yml b/config/secrets.yml deleted file mode 100644 index a6b6d9fc..00000000 --- a/config/secrets.yml +++ /dev/null @@ -1,25 +0,0 @@ -# Be sure to restart your server when you modify this file. - -# Your secret key is used for verifying the integrity of signed cookies. -# If you change this key, all old signed cookies will become invalid! - -# Make sure the secret is at least 30 characters and all random, -# no regular words or you'll be exposed to dictionary attacks. -# You can use `rails secret` to generate a secure secret key. - -# Make sure the secrets in this file are kept private -# if you're sharing your code publicly. - -development: - secret_key_base: 5a1644fba6573a021373273be0ad901ec9b4a82d57efdc69831c0ad94644c42290147672a1b389e13f30251163f3d90b9245ccb2ae46f7f7fe8acb54b2ef76fe - -test: - secret_key_base: 69ecc590b3de231130e3fb390df48a4eb9259722754ef5df6a9e9ab78d69149d8c16ec2d79791c5ba7a89ad7c5afaa8cec1aa794e9a6f986d3c7319a08d6bce2 - -# Do not keep production secrets in the repository, -# instead read values from the environment. -production: - secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> - -staging: - secret_key_base: <%= ENV["SECRET_KEY_BASE"] %> \ No newline at end of file diff --git a/config/webpack/staging.js b/config/webpack/staging.js new file mode 100644 index 00000000..df477912 --- /dev/null +++ b/config/webpack/staging.js @@ -0,0 +1,5 @@ +process.env.NODE_ENV = process.env.NODE_ENV || 'staging' + +const environment = require('./environment') + +module.exports = environment.toWebpackConfig() \ No newline at end of file diff --git a/config/webpacker.yml b/config/webpacker.yml index 4e885b0c..81e245d9 100644 --- a/config/webpacker.yml +++ b/config/webpacker.yml @@ -6,17 +6,23 @@ default: &default public_root_path: public public_output_path: packs cache_path: tmp/cache/webpacker - webpack_compile_output: true + + # Verifies that correct packages and versions are installed by inspecting package.json, yarn.lock, and node_modules + check_yarn_integrity: true + + webpack_compile_output: false # Additional paths webpack should lookup modules # ['app/assets', 'engine/foo/app/assets'] + resolved_paths: [] + additional_paths: [] # Reload manifest.json on all requests so we reload latest compiled packs cache_manifest: false # Extract and emit a css file - extract_css: false + extract_css: true static_assets_extensions: - .jpg @@ -37,6 +43,7 @@ default: &default - .js - .sass - .scss + - .less - .css - .module.sass - .module.scss @@ -50,7 +57,6 @@ default: &default development: <<: *default compile: true - # Reference: https://webpack.js.org/configuration/dev-server/ dev_server: https: false @@ -65,7 +71,6 @@ development: disable_host_check: true use_local_ip: false quiet: false - pretty: false headers: 'Access-Control-Allow-Origin': '*' watch_options: @@ -74,7 +79,8 @@ development: test: <<: *default - compile: true + compile: false + cache_manifest: true # Compile test packs to a separate directory public_output_path: packs-test @@ -85,9 +91,6 @@ production: # Production depends on precompilation of packs prior to booting for performance. compile: false - # Extract and emit a css file - extract_css: true - # Cache manifest.json for performance cache_manifest: true @@ -95,7 +98,8 @@ staging: <<: *default # Production depends on precompilation of packs prior to booting for performance. - compile: true + compile: false # Cache manifest.json for performance cache_manifest: true + \ No newline at end of file diff --git a/db/migrate/20210716071248_add_new_field_to_categories.rb b/db/migrate/20210716071248_add_new_field_to_categories.rb new file mode 100644 index 00000000..96687b68 --- /dev/null +++ b/db/migrate/20210716071248_add_new_field_to_categories.rb @@ -0,0 +1,5 @@ +class AddNewFieldToCategories < ActiveRecord::Migration[6.1] + def change + add_column :categories, :multiply, :boolean + end +end diff --git a/db/schema.rb b/db/schema.rb index 32d9ad84..8f3914c5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 2021_07_15_131940) do +ActiveRecord::Schema.define(version: 2021_07_16_071248) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -30,6 +30,7 @@ t.datetime "created_at", precision: 6, null: false t.datetime "updated_at", precision: 6, null: false t.bigint "quiz_id" + t.boolean "multiply" t.index ["quiz_id"], name: "index_categories_on_quiz_id" end diff --git a/db/seeds.rb b/db/seeds.rb index 87c27a78..40eb20a5 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -5,8 +5,35 @@ # # movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }]) # Character.create(name: 'Luke', movie: movies.first) +# require 'faker' + +User.destroy_all +# Quiz.destroy_all +# Category.destroy_all +# Question.destroy_all +# Answer.destroy_all + + User.create! do |u| u.email = 'super@admin.ee' u.password = 'password' u.superadmin_role = true - end \ No newline at end of file + end + +# quiz = Quiz.create(title: Faker::Lorem.words(rand(1..4)).join(' ')) + +# 3.times do +# category = Category.create(title: Faker::Lorem.words(rand(1..2)).join(' '), quiz_id: quiz.id) +# 6.times do +# question = Question.create(title: (Faker::Lorem.words(rand(2..10)) << '?').join(' '), category_id: category.id ) +# 3.times do +# Answer.create(title_en: Faker::Lorem.words(rand(2..10)).join(' '), +# title_ee: Faker::Lorem.words(rand(2..10)).join(' '), +# question_id: question.id ) +# end +# Answer.create(title_en: Faker::Lorem.words(rand(2..10)).join(' '), +# title_ee: Faker::Lorem.words(rand(2..10)).join(' '), +# question_id: question.id, +# correct: true ) +# end +# end \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 63dada71..a03b01c9 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -18,7 +18,7 @@ services: POSTGRES_PASSWORD: some-long-secure-password POSTGRES_DB: db ACCREDITATION_CENTER_DATABASE_PASSWORD: some-long-secure-password - SECRET_KEY_BASE: 5a1644fba6573a021373273be0ad901ec9b4a82d57efdc69831c0ad94644c42290147672a1b389e13f30251163f3d90b9245ccb2ae46f7f7fe8acb54b2ef76fe + SECRET_KEY_BASE: <%= ENV["SECRET_KEY_BASE"] %> command: bash -c "rm -f tmp/pids/server.pid && bundle update --bundler && bundle exec rails server -b 0.0.0.0" restart: always diff --git a/spec/helpers/categories_helper_spec.rb b/spec/helpers/categories_helper_spec.rb new file mode 100644 index 00000000..e717765a --- /dev/null +++ b/spec/helpers/categories_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the CategoriesHelper. For example: +# +# describe CategoriesHelper 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 CategoriesHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/helpers/quiz_helper_spec.rb b/spec/helpers/quiz_helper_spec.rb new file mode 100644 index 00000000..877f04d2 --- /dev/null +++ b/spec/helpers/quiz_helper_spec.rb @@ -0,0 +1,15 @@ +require 'rails_helper' + +# Specs in this file have access to a helper object that includes +# the QuizHelper. For example: +# +# describe QuizHelper 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 QuizHelper, type: :helper do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/requests/categories_spec.rb b/spec/requests/categories_spec.rb new file mode 100644 index 00000000..988ae5f8 --- /dev/null +++ b/spec/requests/categories_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "Categories", type: :request do + describe "GET /show" do + it "returns http success" do + # get "/categories/show" + # expect(response).to have_http_status(:success) + end + end + +end diff --git a/spec/requests/quiz_spec.rb b/spec/requests/quiz_spec.rb new file mode 100644 index 00000000..d422473d --- /dev/null +++ b/spec/requests/quiz_spec.rb @@ -0,0 +1,11 @@ +require 'rails_helper' + +RSpec.describe "Quizzes", type: :request do + describe "GET /show" do + it "returns http success" do + # get "/quiz/show" + # expect(response).to have_http_status(:success) + end + end + +end diff --git a/spec/views/categories/show.html.erb_spec.rb b/spec/views/categories/show.html.erb_spec.rb new file mode 100644 index 00000000..ca5c933a --- /dev/null +++ b/spec/views/categories/show.html.erb_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe "categories/show.html.erb", type: :view do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/quiz/index.html.erb_spec.rb b/spec/views/quiz/index.html.erb_spec.rb new file mode 100644 index 00000000..f5fe2ec6 --- /dev/null +++ b/spec/views/quiz/index.html.erb_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe "quiz/index.html.erb", type: :view do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/views/quiz/show.html.erb_spec.rb b/spec/views/quiz/show.html.erb_spec.rb new file mode 100644 index 00000000..ddb6d6f2 --- /dev/null +++ b/spec/views/quiz/show.html.erb_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe "quiz/show.html.erb", type: :view do + pending "add some examples to (or delete) #{__FILE__}" +end