From 9c5b53679da3d04687a0a2c3c5b0ac58a7e236a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Geonizeli?= Date: Thu, 21 Jul 2022 09:59:20 -0300 Subject: [PATCH] add Question --- Gemfile | 8 +-- Gemfile.lock | 3 + app/models/question.rb | 55 ++++++++++++++++++ db/migrate/20220721124944_create_questions.rb | 23 ++++++++ db/schema.rb | 26 ++++++++- spec/factories/questions.rb | 58 +++++++++++++++++++ spec/models/question_spec.rb | 43 ++++++++++++++ 7 files changed, 211 insertions(+), 5 deletions(-) create mode 100644 app/models/question.rb create mode 100644 db/migrate/20220721124944_create_questions.rb create mode 100644 spec/factories/questions.rb create mode 100644 spec/models/question_spec.rb diff --git a/Gemfile b/Gemfile index 892c538..8fc90fa 100644 --- a/Gemfile +++ b/Gemfile @@ -26,6 +26,9 @@ gem "bootsnap", require: false # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images] # gem "image_processing", "~> 1.2" +gem "pundit", "~> 2.2" +gem "enumerize", "~> 2.5" + gem "activeadmin", "~> 2.13" gem "sassc-rails" @@ -37,6 +40,7 @@ group :development, :test do gem "dotenv-rails", "~> 2.7" gem "rspec-rails", "~> 5.1" gem "factory_bot_rails", "~> 6.2" + gem "faker", "~> 2.21" gem "debug", platforms: %i[ mri mingw x64_mingw ] end @@ -50,7 +54,3 @@ end group :test do gem "shoulda-matchers", "~> 5.1" end - -gem "faker", "~> 2.21" - -gem "pundit", "~> 2.2" diff --git a/Gemfile.lock b/Gemfile.lock index 9f156ef..f640a14 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -104,6 +104,8 @@ GEM dotenv-rails (2.7.6) dotenv (= 2.7.6) railties (>= 3.2) + enumerize (2.5.0) + activesupport (>= 3.2) erubi (1.10.0) factory_bot (6.2.1) activesupport (>= 5.0.0) @@ -329,6 +331,7 @@ DEPENDENCIES debug devise (~> 4.8) dotenv-rails (~> 2.7) + enumerize (~> 2.5) factory_bot_rails (~> 6.2) faker (~> 2.21) importmap-rails diff --git a/app/models/question.rb b/app/models/question.rb new file mode 100644 index 0000000..b57791a --- /dev/null +++ b/app/models/question.rb @@ -0,0 +1,55 @@ +# == Schema Information +# +# Table name: questions +# +# id :bigint not null, primary key +# alternatives :jsonb not null +# authorship :string +# authorship_year :string +# bloom_taxonomy :string +# body :text +# check_type :string +# difficulty :string +# explanation :text +# instruction :text +# intention :text +# references :text +# status :string default("draft"), not null +# support :text +# created_at :datetime not null +# updated_at :datetime not null +# subject_id :bigint +# user_id :bigint not null +# +# Indexes +# +# index_questions_on_subject_id (subject_id) +# index_questions_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (subject_id => subjects.id) +# fk_rails_... (user_id => users.id) +# +class Question < ApplicationRecord + extend Enumerize + + belongs_to :user + belongs_to :subject, optional: true + + enumerize :status, in: %i[draft waiting_review with_requested_changes approved registered] + enumerize :difficulty, in: %i[easy medium hard] + enumerize :bloom_taxonomy, in: %i[remember understand apply analyze evaluate create] + enumerize :check_type, in: %i[ + unique_answer + incomplete_affirmation + multiple_answer + negative_focus + assertion_and_reason + gap + interpretation + association + ordering_or_ranking + constant_alternatives + ] +end diff --git a/db/migrate/20220721124944_create_questions.rb b/db/migrate/20220721124944_create_questions.rb new file mode 100644 index 0000000..be0d401 --- /dev/null +++ b/db/migrate/20220721124944_create_questions.rb @@ -0,0 +1,23 @@ +class CreateQuestions < ActiveRecord::Migration[7.0] + def change + create_table :questions do |t| + t.references :user, null: false, foreign_key: true + t.references :subject, null: true, foreign_key: true + t.jsonb :alternatives, null: false, default: [] + t.string :authorship + t.string :authorship_year + t.string :bloom_taxonomy + t.text :body + t.string :check_type + t.string :difficulty + t.text :explanation + t.text :instruction + t.text :intention + t.text :references + t.string :status, null: false, default: 'draft' + t.text :support + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c645db4..967d517 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[7.0].define(version: 2022_07_21_123327) do +ActiveRecord::Schema[7.0].define(version: 2022_07_21_124944) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -42,6 +42,28 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_21_123327) do t.index ["name"], name: "index_categories_on_name", unique: true end + create_table "questions", force: :cascade do |t| + t.bigint "user_id", null: false + t.bigint "subject_id" + t.jsonb "alternatives", default: [], null: false + t.string "authorship" + t.string "authorship_year" + t.string "bloom_taxonomy" + t.text "body" + t.string "check_type" + t.string "difficulty" + t.text "explanation" + t.text "instruction" + t.text "intention" + t.text "references" + t.string "status", default: "draft", null: false + t.text "support" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["subject_id"], name: "index_questions_on_subject_id" + t.index ["user_id"], name: "index_questions_on_user_id" + end + create_table "subjects", force: :cascade do |t| t.string "name" t.bigint "category_id", null: false @@ -66,6 +88,8 @@ ActiveRecord::Schema[7.0].define(version: 2022_07_21_123327) do t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true end + add_foreign_key "questions", "subjects" + add_foreign_key "questions", "users" add_foreign_key "subjects", "axes" add_foreign_key "subjects", "categories" end diff --git a/spec/factories/questions.rb b/spec/factories/questions.rb new file mode 100644 index 0000000..5c5f6c3 --- /dev/null +++ b/spec/factories/questions.rb @@ -0,0 +1,58 @@ +# == Schema Information +# +# Table name: questions +# +# id :bigint not null, primary key +# alternatives :jsonb not null +# authorship :string +# authorship_year :string +# bloom_taxonomy :string +# body :text +# check_type :string +# difficulty :string +# explanation :text +# instruction :text +# intention :text +# references :text +# status :string default("draft"), not null +# support :text +# created_at :datetime not null +# updated_at :datetime not null +# subject_id :bigint +# user_id :bigint not null +# +# Indexes +# +# index_questions_on_subject_id (subject_id) +# index_questions_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (subject_id => subjects.id) +# fk_rails_... (user_id => users.id) +# +FactoryBot.define do + factory :question do + introduction { "question title" } + instruction { "html raw" } + support { "html raw" } + body { "html raw" } + alternatives do + [{ text: "html raw", correct: true }, + { text: "html raw", correct: false }, + { text: "html raw", correct: false }, + { text: "html raw", correct: false }, + { text: "html raw", correct: false }] + end + explanation { "html raw" } + references { "html raw" } + status { "registered" } + difficulty { "easy" } + check_type { "unique_answer" } + bloom_taxonomy { "understand" } + authorship_year { "2020" } + authorship { "UNIFESO" } + user + subject + end +end diff --git a/spec/models/question_spec.rb b/spec/models/question_spec.rb new file mode 100644 index 0000000..83a4302 --- /dev/null +++ b/spec/models/question_spec.rb @@ -0,0 +1,43 @@ +# == Schema Information +# +# Table name: questions +# +# id :bigint not null, primary key +# alternatives :jsonb not null +# authorship :string +# authorship_year :string +# bloom_taxonomy :string +# body :text +# check_type :string +# difficulty :string +# explanation :text +# instruction :text +# intention :text +# references :text +# status :string default("draft"), not null +# support :text +# created_at :datetime not null +# updated_at :datetime not null +# subject_id :bigint +# user_id :bigint not null +# +# Indexes +# +# index_questions_on_subject_id (subject_id) +# index_questions_on_user_id (user_id) +# +# Foreign Keys +# +# fk_rails_... (subject_id => subjects.id) +# fk_rails_... (user_id => users.id) +# +require 'rails_helper' + +RSpec.describe Question, type: :model do + describe "associations" do + it { is_expected.to(belong_to(:user)) } + it { is_expected.to(belong_to(:subject).optional(true)) } + # it { is_expected.to(have_many(:review_requests)) } + # it { is_expected.to(have_many(:review_messages)) } + end +end