From 2e05cf062cd5882f138523bfc1fb007494d85502 Mon Sep 17 00:00:00 2001 From: n1vedhar1 Date: Sat, 21 Dec 2024 11:50:38 +0530 Subject: [PATCH] Update migrations and model associations for Rails 7 compatibility --- app/models/rapidfire/answer.rb | 8 +- app/models/rapidfire/attempt.rb | 9 +- app/models/rapidfire/question.rb | 21 +--- app/models/rapidfire/survey.rb | 24 ++-- app/services/rapidfire/attempt_builder.rb | 9 +- .../20130502170733_create_rapidfire_tables.rb | 36 ++---- ...1411_add_after_survey_content_to_survey.rb | 9 +- .../20190701274749_add_active_to_surveys.rb | 9 +- .../migrations/add_active_to_survey.rb | 2 +- .../add_after_survey_content_to_survey.rb | 9 +- ...ename_answer_groups_and_question_groups.rb | 9 +- .../rapidfire/attempts_controller_spec.rb | 18 +-- spec/dummy/config/application.rb | 13 +-- .../db/migrate/20170701191422_create_users.rb | 9 +- ...te_active_storage_tables.active_storage.rb | 71 ++++------- .../rapidfire/answering_questions_spec.rb | 72 ++++++------ spec/models/rapidfire/attempt_spec.rb | 8 +- spec/models/rapidfire/questions/file_spec.rb | 54 +++++---- .../rapidfire/questions/multi_file_spec.rb | 57 ++++----- .../rapidfire/attempt_builder_spec.rb | 110 +++++++++--------- 20 files changed, 210 insertions(+), 347 deletions(-) diff --git a/app/models/rapidfire/answer.rb b/app/models/rapidfire/answer.rb index 42e0a746..e8b9caca 100644 --- a/app/models/rapidfire/answer.rb +++ b/app/models/rapidfire/answer.rb @@ -4,12 +4,10 @@ class Answer < ApplicationRecord belongs_to :attempt, inverse_of: :answers validates :question, :attempt, presence: true - validate :verify_answer_text + validate :verify_answer_text - if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2" - has_one_attached :file - has_many_attached :files - end + has_one_attached :file + has_many_attached :files private diff --git a/app/models/rapidfire/attempt.rb b/app/models/rapidfire/attempt.rb index 0c2522a5..7d72f533 100644 --- a/app/models/rapidfire/attempt.rb +++ b/app/models/rapidfire/attempt.rb @@ -1,12 +1,7 @@ module Rapidfire class Attempt < ApplicationRecord belongs_to :survey - has_many :answers, inverse_of: :attempt, autosave: true - - if Rails::VERSION::MAJOR >= 5 - belongs_to :user, polymorphic: true, optional: true - else - belongs_to :user, polymorphic: true - end + has_many :answers, dependent: :destroy, inverse_of: :attempt + belongs_to :user, polymorphic: true, optional: true end end diff --git a/app/models/rapidfire/question.rb b/app/models/rapidfire/question.rb index ba923fe5..82a3d381 100644 --- a/app/models/rapidfire/question.rb +++ b/app/models/rapidfire/question.rb @@ -1,10 +1,9 @@ module Rapidfire class Question < ApplicationRecord belongs_to :survey, :inverse_of => :questions - has_many :answers - - has_many_attached :files + has_many :answers + has_many_attached :files default_scope { order(:position) } @@ -36,20 +35,12 @@ def validate_answer(answer) if rules[:presence] == "1" case self when Rapidfire::Questions::File - if Rails::VERSION::MAJOR >= 6 - answer.validates_presence_of :file - else - if !answer.file.attached? - answer.errors.add(:file, :blank) - end + if !answer.file.attached? + answer.errors.add(:file, :blank) end when Rapidfire::Questions::MultiFile - if Rails::VERSION::MAJOR >= 6 - answer.validates_presence_of :files - else - if !answer.files.attached? - answer.errors.add(:files, :blank) - end + if !answer.files.attached? + answer.errors.add(:files, :blank) end else answer.validates_presence_of :answer_text diff --git a/app/models/rapidfire/survey.rb b/app/models/rapidfire/survey.rb index 989c4b9a..7bbcecf9 100644 --- a/app/models/rapidfire/survey.rb +++ b/app/models/rapidfire/survey.rb @@ -1,15 +1,12 @@ -require 'csv' +require "csv" + module Rapidfire class Survey < ApplicationRecord - has_many :attempts - has_many :questions - - validates :name, :presence => true + has_many :attempts, dependent: :destroy + has_many :questions, dependent: :destroy + has_many :answers, through: :attempts, dependent: :destroy - - if Rails::VERSION::MAJOR == 3 - attr_accessible :name, :introduction, :after_survey_content - end + validates :name, presence: true def self.csv_user_attributes=(attributes) @@csv_user_attributes = Array(attributes) @@ -24,11 +21,11 @@ def results_to_csv(filter) header = [] header += Rapidfire::Survey.csv_user_attributes questions.each do |question| - header << ActionView::Base.full_sanitizer.sanitize(question.question_text, :tags => [], :attributes => []) + header << ActionView::Base.full_sanitizer.sanitize(question.question_text, tags: [], attributes: []) end header << "results updated at" csv << header - attempts.where(SurveyResults.filter(filter, 'id')).each do |attempt| + attempts.where(SurveyResults.filter(filter, "id")).each do |attempt| this_attempt = [] Survey.csv_user_attributes.each do |attribute| @@ -36,12 +33,9 @@ def results_to_csv(filter) end questions.each do |question| - answer = attempt.answers.detect{|a| a.question_id == question.id }.try(:answer_text) + answer = attempt.answers.detect { |a| a.question_id == question.id }.try(:answer_text) this_attempt << answer end - - this_attempt << attempt.updated_at - csv << this_attempt end end end diff --git a/app/services/rapidfire/attempt_builder.rb b/app/services/rapidfire/attempt_builder.rb index 5aef0934..250b496a 100644 --- a/app/services/rapidfire/attempt_builder.rb +++ b/app/services/rapidfire/attempt_builder.rb @@ -22,8 +22,7 @@ def save!(options = {}) # strings. we will store answers as one big string separated # by delimiter. text = text.values if text.is_a?(ActionController::Parameters) - answer.answer_text = - if text.is_a?(Array) + answer.answer_text = if text.is_a?(Array) strip_checkbox_answers(text).join(Rapidfire.answers_delimiter) else text @@ -37,11 +36,7 @@ def save!(options = {}) end end - if Rails::VERSION::MAJOR >= 5 - @attempt.save! - else - @attempt.save!(options) - end + @attempt.save!(validate: options[:validate] != false) end def save(options = {}) diff --git a/db/migrate/20130502170733_create_rapidfire_tables.rb b/db/migrate/20130502170733_create_rapidfire_tables.rb index d5c41f1f..d99bb31f 100644 --- a/db/migrate/20130502170733_create_rapidfire_tables.rb +++ b/db/migrate/20130502170733_create_rapidfire_tables.rb @@ -1,51 +1,37 @@ -if Rails::VERSION::MAJOR >= 5 - version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f - base = ActiveRecord::Migration[version] -else - base = ActiveRecord::Migration -end - -class CreateRapidfireTables < base +class CreateRapidfireTables < ActiveRecord::Migration[7.0] def change create_table :rapidfire_surveys do |t| - t.string :name + t.string :name t.text :introduction t.timestamps end create_table :rapidfire_questions do |t| - t.references :survey - t.string :type - t.string :question_text - t.string :default_text - t.string :placeholder + t.references :survey, foreign_key: { to_table: :rapidfire_surveys } + t.string :type + t.string :question_text + t.string :default_text + t.string :placeholder t.integer :position t.text :answer_options t.text :validation_rules t.timestamps end - add_index :rapidfire_questions, :survey_id if Rails::VERSION::MAJOR < 5 create_table :rapidfire_attempts do |t| - t.references :survey - t.references :user, polymorphic: true + t.references :survey, foreign_key: { to_table: :rapidfire_surveys } + t.references :user, polymorphic: true, index: true t.timestamps end - add_index :rapidfire_attempts, :survey_id if Rails::VERSION::MAJOR < 5 - add_index :rapidfire_attempts, [:user_id, :user_type] create_table :rapidfire_answers do |t| - t.references :attempt - t.references :question + t.references :attempt, foreign_key: { to_table: :rapidfire_attempts } + t.references :question, foreign_key: { to_table: :rapidfire_questions } t.text :answer_text t.timestamps end - if Rails::VERSION::MAJOR < 5 - add_index :rapidfire_answers, :attempt_id - add_index :rapidfire_answers, :question_id - end end end diff --git a/db/migrate/20170701191411_add_after_survey_content_to_survey.rb b/db/migrate/20170701191411_add_after_survey_content_to_survey.rb index 3fe9831c..bf15efa3 100644 --- a/db/migrate/20170701191411_add_after_survey_content_to_survey.rb +++ b/db/migrate/20170701191411_add_after_survey_content_to_survey.rb @@ -1,11 +1,4 @@ -if Rails::VERSION::MAJOR >= 5 - version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f - base = ActiveRecord::Migration[version] -else - base = ActiveRecord::Migration -end - -class AddAfterSurveyContentToSurvey < base +class AddAfterSurveyContentToSurvey < ActiveRecord::Migration[7.0] def change add_column :rapidfire_surveys, :after_survey_content, :text end diff --git a/db/migrate/20190701274749_add_active_to_surveys.rb b/db/migrate/20190701274749_add_active_to_surveys.rb index 3b7c16bc..6a55968b 100644 --- a/db/migrate/20190701274749_add_active_to_surveys.rb +++ b/db/migrate/20190701274749_add_active_to_surveys.rb @@ -1,11 +1,4 @@ -if Rails::VERSION::MAJOR >= 5 - version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f - base = ActiveRecord::Migration[version] -else - base = ActiveRecord::Migration -end - -class AddActiveToSurveys < base +class AddActiveToSurveys < ActiveRecord::Migration[7.0] def change add_column :rapidfire_surveys, :active, :boolean end diff --git a/lib/generators/rapidfire/templates/migrations/add_active_to_survey.rb b/lib/generators/rapidfire/templates/migrations/add_active_to_survey.rb index cfd35c8f..feb4db06 100644 --- a/lib/generators/rapidfire/templates/migrations/add_active_to_survey.rb +++ b/lib/generators/rapidfire/templates/migrations/add_active_to_survey.rb @@ -1,4 +1,4 @@ -class AddActiveToSurvey < ActiveRecord::Migration[6.0] +class AddActiveToSurvey < ActiveRecord::Migration[7.0] def change add_column :rapidfire_surveys, :active, :boolean, default: 1 end diff --git a/lib/generators/rapidfire/templates/migrations/add_after_survey_content_to_survey.rb b/lib/generators/rapidfire/templates/migrations/add_after_survey_content_to_survey.rb index 3fe9831c..bf15efa3 100644 --- a/lib/generators/rapidfire/templates/migrations/add_after_survey_content_to_survey.rb +++ b/lib/generators/rapidfire/templates/migrations/add_after_survey_content_to_survey.rb @@ -1,11 +1,4 @@ -if Rails::VERSION::MAJOR >= 5 - version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f - base = ActiveRecord::Migration[version] -else - base = ActiveRecord::Migration -end - -class AddAfterSurveyContentToSurvey < base +class AddAfterSurveyContentToSurvey < ActiveRecord::Migration[7.0] def change add_column :rapidfire_surveys, :after_survey_content, :text end diff --git a/lib/generators/rapidfire/templates/migrations/rename_answer_groups_and_question_groups.rb b/lib/generators/rapidfire/templates/migrations/rename_answer_groups_and_question_groups.rb index 1e79b896..71c04be3 100644 --- a/lib/generators/rapidfire/templates/migrations/rename_answer_groups_and_question_groups.rb +++ b/lib/generators/rapidfire/templates/migrations/rename_answer_groups_and_question_groups.rb @@ -1,11 +1,4 @@ -if Rails::VERSION::MAJOR >= 5 - version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f - base = ActiveRecord::Migration[version] -else - base = ActiveRecord::Migration -end - -class RenameAnswerGroupsAndQuestionGroups < base +class RenameAnswerGroupsAndQuestionGroups < ActiveRecord::Migration[7.0] def change rename_table :rapidfire_answer_groups, :rapidfire_attempts rename_table :rapidfire_question_groups, :rapidfire_surveys diff --git a/spec/controllers/rapidfire/attempts_controller_spec.rb b/spec/controllers/rapidfire/attempts_controller_spec.rb index 1bbffc1d..89420cf6 100644 --- a/spec/controllers/rapidfire/attempts_controller_spec.rb +++ b/spec/controllers/rapidfire/attempts_controller_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe Rapidfire::AttemptsController do before do @@ -8,19 +8,13 @@ # this scenario is possible when there is only 1 radio button question, and # user has not selected any option. in this case, browser doesn't send # any default value. - context 'when no parameters are passed' do - it 'initializes answer builder with empty args' do + context "when no parameters are passed" do + it "initializes answer builder with empty args" do survey = FactoryBot.create(:survey) - if Rails::VERSION::MAJOR >= 5 - expect { - post :create, params: { survey_id: survey.id } - }.not_to raise_error - else - expect { - post :create, survey_id: survey.id - }.not_to raise_error - end + expect { + post :create, params: { survey_id: survey.id } + }.not_to raise_error end end end diff --git a/spec/dummy/config/application.rb b/spec/dummy/config/application.rb index 37134f79..df1a4ab5 100644 --- a/spec/dummy/config/application.rb +++ b/spec/dummy/config/application.rb @@ -1,12 +1,10 @@ -require File.expand_path('../boot', __FILE__) +require File.expand_path("../boot", __FILE__) require "rails" require "active_model/railtie" # require "active_job/railtie" require "active_record/railtie" -if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2" - require "active_storage/engine" -end +require "active_storage/engine" require "action_controller/railtie" require "action_mailer/railtie" # require "action_mailbox/engine" @@ -16,10 +14,8 @@ # require "sprockets/railtie" require "rails/test_unit/railtie" - Bundler.require(*Rails.groups) - module Dummy class Application < Rails::Application # Settings in config/environments/* take precedence over those specified here. @@ -64,9 +60,6 @@ class Application < Rails::Application # Version of your assets, change this if you want to expire all your assets # config.assets.version = '1.0' - if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.1" - config.load_defaults "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" - end + config.load_defaults "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" end end - diff --git a/spec/dummy/db/migrate/20170701191422_create_users.rb b/spec/dummy/db/migrate/20170701191422_create_users.rb index a92c9a0b..e1fda72a 100644 --- a/spec/dummy/db/migrate/20170701191422_create_users.rb +++ b/spec/dummy/db/migrate/20170701191422_create_users.rb @@ -1,11 +1,4 @@ -if Rails::VERSION::MAJOR >= 5 - version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f - base = ActiveRecord::Migration[version] -else - base = ActiveRecord::Migration -end - -class CreateUsers < base +class CreateUsers < ActiveRecord::Migration[7.0] def change create_table :users do |t| t.string :name diff --git a/spec/dummy/db/migrate/20230402174122_create_active_storage_tables.active_storage.rb b/spec/dummy/db/migrate/20230402174122_create_active_storage_tables.active_storage.rb index 66257c87..ec4719f5 100644 --- a/spec/dummy/db/migrate/20230402174122_create_active_storage_tables.active_storage.rb +++ b/spec/dummy/db/migrate/20230402174122_create_active_storage_tables.active_storage.rb @@ -1,49 +1,27 @@ -if Rails::VERSION::MAJOR >= 5 - version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f - base = ActiveRecord::Migration[version] -else - base = ActiveRecord::Migration -end - - -# This migration comes from active_storage (originally 20170806125915) -class CreateActiveStorageTables < base +class CreateActiveStorageTables < ActiveRecord::Migration[7.0] def change - # Use Active Record's configured type for primary and foreign keys primary_key_type, foreign_key_type = primary_and_foreign_key_types create_table :active_storage_blobs, id: primary_key_type do |t| - t.string :key, null: false - t.string :filename, null: false - t.string :content_type - t.text :metadata - if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "6.1" - t.string :service_name, null: false - end - t.bigint :byte_size, null: false - t.string :checksum - - if connection.supports_datetime_with_precision? - t.datetime :created_at, precision: 6, null: false - else - t.datetime :created_at, null: false - end - - t.index [ :key ], unique: true + t.string :key, null: false + t.string :filename, null: false + t.string :content_type + t.text :metadata + t.string :service_name, null: false + t.bigint :byte_size, null: false + t.string :checksum + t.datetime :created_at, precision: 6, null: false + + t.index [:key], unique: true end create_table :active_storage_attachments, id: primary_key_type do |t| - t.string :name, null: false - t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type - t.references :blob, null: false, type: foreign_key_type + t.string :name, null: false + t.references :record, null: false, polymorphic: true, index: false, type: foreign_key_type + t.references :blob, null: false, type: foreign_key_type + t.datetime :created_at, precision: 6, null: false - if connection.supports_datetime_with_precision? - t.datetime :created_at, precision: 6, null: false - else - t.datetime :created_at, null: false - end - - t.index [ :record_type, :record_id, :name, :blob_id ], name: :index_active_storage_attachments_uniqueness, unique: true + t.index [:record_type, :record_id, :name, :blob_id], name: :index_active_storage_attachments_uniqueness, unique: true t.foreign_key :active_storage_blobs, column: :blob_id end @@ -51,17 +29,18 @@ def change t.belongs_to :blob, null: false, index: false, type: foreign_key_type t.string :variation_digest, null: false - t.index [ :blob_id, :variation_digest ], name: :index_active_storage_variant_records_uniqueness, unique: true + t.index [:blob_id, :variation_digest], name: :index_active_storage_variant_records_uniqueness, unique: true t.foreign_key :active_storage_blobs, column: :blob_id end end private - def primary_and_foreign_key_types - config = Rails.configuration.generators - setting = config.options[config.orm][:primary_key_type] - primary_key_type = setting || :primary_key - foreign_key_type = setting || :bigint - [primary_key_type, foreign_key_type] - end + + def primary_and_foreign_key_types + config = Rails.configuration.generators + setting = config.options[config.orm][:primary_key_type] + primary_key_type = setting || :primary_key + foreign_key_type = setting || :bigint + [primary_key_type, foreign_key_type] + end end diff --git a/spec/features/rapidfire/answering_questions_spec.rb b/spec/features/rapidfire/answering_questions_spec.rb index cdadefe4..3291bacc 100644 --- a/spec/features/rapidfire/answering_questions_spec.rb +++ b/spec/features/rapidfire/answering_questions_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" describe "Surveys" do let!(:survey) { FactoryBot.create(:survey, name: "Question Set", introduction: "Some introduction") } @@ -10,7 +10,7 @@ end describe "Answering Questions" do - let!(:question1) { FactoryBot.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) } + let!(:question1) { FactoryBot.create(:q_long, survey: survey, question_text: "Long Question", validation_rules: { presence: "1" }) } let!(:question2) { FactoryBot.create(:q_short, survey: survey, question_text: "Short Question") } let!(:question3) { FactoryBot.create(:q_checkbox, survey: survey, question_text: "Checkbox question") } let!(:question4) { FactoryBot.create(:q_checkbox, survey: survey, question_text: "Checkbox question", validation_rules: { presence: "1" }) } @@ -89,56 +89,54 @@ end end - if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2" - describe "Answering File uploads" do - context "when the question is single file upload" do - let!(:question1) { FactoryBot.create(:q_file, survey: survey, question_text: "Avatar") } + describe "Answering File uploads" do + context "when the question is single file upload" do + let!(:question1) { FactoryBot.create(:q_file, survey: survey, question_text: "Avatar") } - it "persistes the file" do - visit rapidfire.new_survey_attempt_path(survey) + it "persistes the file" do + visit rapidfire.new_survey_attempt_path(survey) - attach_file "attempt_#{question1.id}_file", file_fixture("one.txt") - click_button "Save" + attach_file "attempt_#{question1.id}_file", file_fixture("one.txt") + click_button "Save" - answer = Rapidfire::Answer.first - expect(answer).to be_persisted - expect(answer.file.download).to eq("one\n") - end + answer = Rapidfire::Answer.first + expect(answer).to be_persisted + expect(answer.file.download).to eq("one\n") end + end - context "when the question is multi file upload" do - let!(:question1) { FactoryBot.create(:q_multifile, survey: survey, question_text: "Images") } + context "when the question is multi file upload" do + let!(:question1) { FactoryBot.create(:q_multifile, survey: survey, question_text: "Images") } - it "persistes the file" do - visit rapidfire.new_survey_attempt_path(survey) + it "persistes the file" do + visit rapidfire.new_survey_attempt_path(survey) - attach_file "attempt_#{question1.id}_files", [file_fixture("one.txt"), file_fixture("two.txt")] - click_button "Save" + attach_file "attempt_#{question1.id}_files", [file_fixture("one.txt"), file_fixture("two.txt")] + click_button "Save" - answer = Rapidfire::Answer.first - expect(answer).to be_persisted - expect(answer.files.length).to eq 2 + answer = Rapidfire::Answer.first + expect(answer).to be_persisted + expect(answer.files.length).to eq 2 - expect(answer.files[0].download).to eq("two\n") - expect(answer.files[1].download).to eq("one\n") - end + expect(answer.files[0].download).to eq("two\n") + expect(answer.files[1].download).to eq("one\n") end + end - context "when persisting a file fails" do - let!(:question1) { FactoryBot.create(:q_file, survey: survey, question_text: "Avatar") } + context "when persisting a file fails" do + let!(:question1) { FactoryBot.create(:q_file, survey: survey, question_text: "Avatar") } - it "bubbles up the error" do - visit rapidfire.new_survey_attempt_path(survey) + it "bubbles up the error" do + visit rapidfire.new_survey_attempt_path(survey) - expect_any_instance_of(Rapidfire::Answer).to receive("file_attachment=") do - raise ActiveRecord::ActiveRecordError.new("Can't save the file") - end + expect_any_instance_of(Rapidfire::Answer).to receive("file_attachment=") do + raise ActiveRecord::ActiveRecordError.new("Can't save the file") + end - attach_file "attempt_#{question1.id}_file", file_fixture("one.txt") - click_button "Save" + attach_file "attempt_#{question1.id}_file", file_fixture("one.txt") + click_button "Save" - expect(page).to have_content("Can't save the file") - end + expect(page).to have_content("Can't save the file") end end end diff --git a/spec/models/rapidfire/attempt_spec.rb b/spec/models/rapidfire/attempt_spec.rb index 20fca878..63c985aa 100644 --- a/spec/models/rapidfire/attempt_spec.rb +++ b/spec/models/rapidfire/attempt_spec.rb @@ -1,13 +1,9 @@ -require 'spec_helper' +require "spec_helper" describe Rapidfire::Attempt do describe "Associations" do it { is_expected.to belong_to(:survey) } - if Rails::VERSION::MAJOR >= 5 - it { is_expected.to belong_to(:user).optional } - else - it { is_expected.to belong_to(:user) } - end + it { is_expected.to belong_to(:user).optional } it { is_expected.to have_many(:answers) } end end diff --git a/spec/models/rapidfire/questions/file_spec.rb b/spec/models/rapidfire/questions/file_spec.rb index 64e3bf13..c27eaf80 100644 --- a/spec/models/rapidfire/questions/file_spec.rb +++ b/spec/models/rapidfire/questions/file_spec.rb @@ -1,4 +1,4 @@ -require 'spec_helper' +require "spec_helper" def fixture_file_upload(path, mime_type) Rack::Test::UploadedFile.new(Pathname.new(file_fixture_path).join(path), mime_type, false) @@ -7,49 +7,47 @@ def fixture_file_upload(path, mime_type) describe Rapidfire::Questions::File do describe "Validations" do it { is_expected.to validate_presence_of(:survey) } - it { is_expected.to validate_presence_of(:question_text) } + it { is_expected.to validate_presence_of(:question_text) } end describe "Associations" do it { is_expected.to belong_to(:survey) } end - if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2" - describe "validate_answer" do - let(:question) { FactoryBot.create(:q_file, validation_rules: validation_rules) } - let(:answer) { FactoryBot.build(:answer, question: question, file: file) } - before { answer.valid? } + describe "validate_answer" do + let(:question) { FactoryBot.create(:q_file, validation_rules: validation_rules) } + let(:answer) { FactoryBot.build(:answer, question: question, file: file) } + before { answer.valid? } - context "when there are no validation rules" do - let(:validation_rules) { {} } - let(:file) { nil } + context "when there are no validation rules" do + let(:validation_rules) { {} } + let(:file) { nil } - it "answer should pass validations" do - expect(answer.errors).to be_empty - end + it "answer should pass validations" do + expect(answer.errors).to be_empty end + end - context "when question needs a file attachment" do - let(:validation_rules) { { presence: "1" } } + context "when question needs a file attachment" do + let(:validation_rules) { { presence: "1" } } - context "when answer is empty" do - let(:file) { nil } + context "when answer is empty" do + let(:file) { nil } - it "fails validations" do - expect(answer.errors).not_to be_empty - end + it "fails validations" do + expect(answer.errors).not_to be_empty + end - it "says answer should be present" do - expect(answer.errors[:file]).to include("can't be blank") - end + it "says answer should be present" do + expect(answer.errors[:file]).to include("can't be blank") end + end - context "when answer is not empty" do - let(:file) { fixture_file_upload("one.txt", "text/plain") } + context "when answer is not empty" do + let(:file) { fixture_file_upload("one.txt", "text/plain") } - it "passes validations" do - expect(answer.errors).to be_empty - end + it "passes validations" do + expect(answer.errors).to be_empty end end end diff --git a/spec/models/rapidfire/questions/multi_file_spec.rb b/spec/models/rapidfire/questions/multi_file_spec.rb index 6c4789c1..06aaf4b5 100644 --- a/spec/models/rapidfire/questions/multi_file_spec.rb +++ b/spec/models/rapidfire/questions/multi_file_spec.rb @@ -1,58 +1,41 @@ -require 'spec_helper' - -def fixture_file_upload(path, mime_type) - Rack::Test::UploadedFile.new(Pathname.new(file_fixture_path).join(path), mime_type, false) -end +require "spec_helper" describe Rapidfire::Questions::MultiFile do describe "Validations" do it { is_expected.to validate_presence_of(:survey) } - it { is_expected.to validate_presence_of(:question_text) } + it { is_expected.to validate_presence_of(:question_text) } end describe "Associations" do it { is_expected.to belong_to(:survey) } end - if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2" - describe "validate_answer" do - let(:question) { FactoryBot.create(:q_multifile, validation_rules: validation_rules) } - let(:answer) { FactoryBot.build(:answer, question: question, files: files) } - before { answer.valid? } + describe "validate_answer" do + let(:question) { FactoryBot.create(:q_multifile, validation_rules: validation_rules) } + let(:answer) { FactoryBot.build(:answer, question: question, files: files) } + before { answer.valid? } - context "when there are no validation rules" do - let(:validation_rules) { {} } - let(:files) { [] } + context "when there are no validation rules" do + let(:validation_rules) { {} } + let(:files) { [] } - it "answer should pass validations" do - expect(answer.errors).to be_empty - end + it "answer should pass validations" do + expect(answer.errors).to be_empty end + end - context "when question needs a file attachment" do - let(:validation_rules) { { presence: "1" } } - - context "when answer is empty" do - let(:files) { [] } + context "when question needs a file attachment" do + let(:validation_rules) { { presence: "1" } } - it "fails validations" do - expect(answer.errors).not_to be_empty - end + context "when answer is empty" do + let(:files) { [] } - it "says answer should be present" do - expect(answer.errors[:files]).to include("can't be blank") - end + it "fails validations" do + expect(answer.errors).not_to be_empty end - context "when answer is not empty" do - let(:files) do - [fixture_file_upload("one.txt", "text/plain"), - fixture_file_upload("two.txt", "text/plain")] - end - - it "passes validations" do - expect(answer.errors).to be_empty - end + it "says answer should be present" do + expect(answer.errors[:files]).to include("can't be blank") end end end diff --git a/spec/services/rapidfire/attempt_builder_spec.rb b/spec/services/rapidfire/attempt_builder_spec.rb index 5748a764..3ff36904 100644 --- a/spec/services/rapidfire/attempt_builder_spec.rb +++ b/spec/services/rapidfire/attempt_builder_spec.rb @@ -1,18 +1,20 @@ -require 'spec_helper' +require "spec_helper" def fixture_file_upload(path, mime_type) Rack::Test::UploadedFile.new(Pathname.new(file_fixture_path).join(path), mime_type, false) end describe Rapidfire::AttemptBuilder do - let(:survey) { FactoryBot.create(:survey) } - let(:question1) { FactoryBot.create(:q_short, survey: survey) } - let(:question2) { FactoryBot.create(:q_long, survey: survey, - validation_rules: { presence: "1" }) } + let(:survey) { FactoryBot.create(:survey) } + let(:question1) { FactoryBot.create(:q_short, survey: survey) } + let(:question2) { + FactoryBot.create(:q_long, survey: survey, + validation_rules: { presence: "1" }) + } describe "Creation" do - let(:builder) { described_class.new(survey: survey) } - before { [question1, question2] } + let(:builder) { described_class.new(survey: survey) } + before { [question1, question2] } it "builds answer group with answers" do expect(builder.answers).not_to be_empty @@ -25,18 +27,18 @@ def fixture_file_upload(path, mime_type) end describe "#save" do - let(:question_ids) { survey.questions.map(&:id) } + let(:question_ids) { survey.questions.map(&:id) } let(:builder) do params = { params: answer_params }.merge(survey: survey) described_class.new(params) end - let(:save_answers) { builder.save } + let(:save_answers) { builder.save } context "when all the answers are valid" do let(:answer_params) do { question1.id.to_s => { :answer_text => "short answer" }, - question2.id.to_s => { :answer_text => "long answer!" } + question2.id.to_s => { :answer_text => "long answer!" }, } end @@ -57,7 +59,7 @@ def fixture_file_upload(path, mime_type) let(:answer_params) do { question1.id.to_s => { :answer_text => "short answer" }, - question2.id.to_s => { :answer_text => "" } + question2.id.to_s => { :answer_text => "" }, } end @@ -70,66 +72,62 @@ def fixture_file_upload(path, mime_type) expect(Rapidfire::Answer.count).to eq(0) end - if Rails::VERSION::MAJOR < 5 - context "when requested to save without validations" do - let(:save_answers) { builder.save(:validate => false) } + context "when requested to save without validations" do + let(:save_answers) { builder.save(:validate => false) } - it "returns true" do - expect(save_answers).to be_truthy - end + it "returns true" do + expect(save_answers).to be_truthy + end - it "saves all the answers" do - save_answers - builder.answers.each do |answer| - expect(answer).to be_persisted - expect(question_ids).to include(answer.question_id) - end + it "saves all the answers" do + save_answers + builder.answers.each do |answer| + expect(answer).to be_persisted + expect(question_ids).to include(answer.question_id) end end end end - if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2" - context "with a single file upload question" do - let(:question3) { FactoryBot.create(:q_file, survey: survey) } + context "with a single file upload question" do + let(:question3) { FactoryBot.create(:q_file, survey: survey) } - let(:answer_params) do - { - question1.id.to_s => { :answer_text => "short answer" }, - question2.id.to_s => { :answer_text => "long answer" }, - question3.id.to_s => { :file => fixture_file_upload("one.txt", "text/plain") } - } - end + let(:answer_params) do + { + question1.id.to_s => { :answer_text => "short answer" }, + question2.id.to_s => { :answer_text => "long answer" }, + question3.id.to_s => { :file => fixture_file_upload("one.txt", "text/plain") }, + } + end - it "saves the file successfully" do - expect(save_answers).to be_truthy + it "saves the file successfully" do + expect(save_answers).to be_truthy - answer = Rapidfire::Answer.last - expect(answer).to be_persisted - expect(answer.file.download).to eq("one\n") - end + answer = Rapidfire::Answer.last + expect(answer).to be_persisted + expect(answer.file.download).to eq("one\n") end + end - context "with multiple files upload question" do - let(:question3) { FactoryBot.create(:q_multifile, survey: survey) } + context "with multiple files upload question" do + let(:question3) { FactoryBot.create(:q_multifile, survey: survey) } - let(:answer_params) do - { - question1.id.to_s => { :answer_text => "short answer" }, - question2.id.to_s => { :answer_text => "long answer" }, - question3.id.to_s => { :files => [fixture_file_upload("one.txt", "text/plain"), - fixture_file_upload("two.txt", "text/plain")]} - } - end + let(:answer_params) do + { + question1.id.to_s => { :answer_text => "short answer" }, + question2.id.to_s => { :answer_text => "long answer" }, + question3.id.to_s => { :files => [fixture_file_upload("one.txt", "text/plain"), + fixture_file_upload("two.txt", "text/plain")] }, + } + end - it "saves the file successfully" do - expect(save_answers).to be_truthy + it "saves the file successfully" do + expect(save_answers).to be_truthy - answer = Rapidfire::Answer.last - expect(answer).to be_persisted - expect(answer.files[0].download).to eq("one\n") - expect(answer.files[1].download).to eq("two\n") - end + answer = Rapidfire::Answer.last + expect(answer).to be_persisted + expect(answer.files[0].download).to eq("one\n") + expect(answer.files[1].download).to eq("two\n") end end end