Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove version checks for older rails #156

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/controllers/rapidfire/attempts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ def find_survey!

def attempt_params
answer_params = { params: (params[:attempt] || {}) }
answer_params.merge(user: rapidfire_current_scoped, survey: @survey, attempt_id: params[:id])
answer_params.merge(
user: rapidfire_current_scoped,
survey: @survey,
attempt_id: params[:id],
)
end

def attempt_params_for_find
Expand All @@ -68,7 +72,7 @@ def after_answer_path_for
end

def rapidfire_current_scoped
send 'current_' + rapidfire_scoped.to_s
send "current_" + rapidfire_scoped.to_s
end
end
end
8 changes: 3 additions & 5 deletions app/models/rapidfire/answer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
9 changes: 2 additions & 7 deletions app/models/rapidfire/attempt.rb
Original file line number Diff line number Diff line change
@@ -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
21 changes: 6 additions & 15 deletions app/models/rapidfire/question.rb
Original file line number Diff line number Diff line change
@@ -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) }

Expand Down Expand Up @@ -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
Expand Down
24 changes: 9 additions & 15 deletions app/models/rapidfire/survey.rb
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -24,24 +21,21 @@ 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|
this_attempt << attempt.user.try(attribute)
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
Expand Down
27 changes: 13 additions & 14 deletions app/services/rapidfire/attempt_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,22 +36,22 @@ def save!(options = {})
end
end

if Rails::VERSION::MAJOR >= 5
@attempt.save!
else
@attempt.save!(options)
end
@attempt.save!(validate: options[:validate] != false)
true
end

def save(options = {})
save!(options)
rescue ActiveRecord::ActiveRecordError => e
errors.add(:base, e.message)
# repopulate answers here in case of failure as they are not getting updated
@answers = @survey.questions.collect do |question|
@attempt.answers.find { |a| a.question_id == question.id }
begin
save!(options)
rescue ActiveRecord::ActiveRecordError => e
errors.add(:base, e.message)
# repopulate answers here in case of failure as they are not getting updated
@answers = @survey.questions.collect do |question|
answer = @attempt.answers.find { |a| a.question_id == question.id }
answer || @attempt.answers.build(question_id: question.id)
end
false
end
false
end

private
Expand Down
36 changes: 11 additions & 25 deletions db/migrate/20130502170733_create_rapidfire_tables.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Expand Down
9 changes: 1 addition & 8 deletions db/migrate/20190701274749_add_active_to_surveys.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
18 changes: 6 additions & 12 deletions spec/controllers/rapidfire/attempts_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require 'spec_helper'
require "spec_helper"

describe Rapidfire::AttemptsController do
before do
Expand All @@ -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
13 changes: 3 additions & 10 deletions spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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.
Expand Down Expand Up @@ -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

9 changes: 1 addition & 8 deletions spec/dummy/db/migrate/20170701191422_create_users.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Loading
Loading