Skip to content

Commit bdd4958

Browse files
committed
removed version checks for older rails
1 parent 3dfada0 commit bdd4958

21 files changed

+227
-356
lines changed

app/controllers/rapidfire/attempts_controller.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ def find_survey!
4242

4343
def attempt_params
4444
answer_params = { params: (params[:attempt] || {}) }
45-
answer_params.merge(user: rapidfire_current_scoped, survey: @survey, attempt_id: params[:id])
45+
answer_params.merge(
46+
user: rapidfire_current_scoped,
47+
survey: @survey,
48+
attempt_id: params[:id],
49+
)
4650
end
4751

4852
def attempt_params_for_find
@@ -68,7 +72,7 @@ def after_answer_path_for
6872
end
6973

7074
def rapidfire_current_scoped
71-
send 'current_' + rapidfire_scoped.to_s
75+
send "current_" + rapidfire_scoped.to_s
7276
end
7377
end
7478
end

app/models/rapidfire/answer.rb

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@ class Answer < ApplicationRecord
44
belongs_to :attempt, inverse_of: :answers
55

66
validates :question, :attempt, presence: true
7-
validate :verify_answer_text
7+
validate :verify_answer_text
88

9-
if "#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}" >= "5.2"
10-
has_one_attached :file
11-
has_many_attached :files
12-
end
9+
has_one_attached :file
10+
has_many_attached :files
1311

1412
private
1513

app/models/rapidfire/attempt.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
module Rapidfire
22
class Attempt < ApplicationRecord
33
belongs_to :survey
4-
has_many :answers, inverse_of: :attempt, autosave: true
5-
6-
if Rails::VERSION::MAJOR >= 5
7-
belongs_to :user, polymorphic: true, optional: true
8-
else
9-
belongs_to :user, polymorphic: true
10-
end
4+
has_many :answers, dependent: :destroy, inverse_of: :attempt
5+
belongs_to :user, polymorphic: true, optional: true
116
end
127
end

app/models/rapidfire/question.rb

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
module Rapidfire
22
class Question < ApplicationRecord
33
belongs_to :survey, :inverse_of => :questions
4-
has_many :answers
5-
6-
has_many_attached :files
4+
has_many :answers
75

6+
has_many_attached :files
87

98
default_scope { order(:position) }
109

@@ -36,20 +35,12 @@ def validate_answer(answer)
3635
if rules[:presence] == "1"
3736
case self
3837
when Rapidfire::Questions::File
39-
if Rails::VERSION::MAJOR >= 6
40-
answer.validates_presence_of :file
41-
else
42-
if !answer.file.attached?
43-
answer.errors.add(:file, :blank)
44-
end
38+
if !answer.file.attached?
39+
answer.errors.add(:file, :blank)
4540
end
4641
when Rapidfire::Questions::MultiFile
47-
if Rails::VERSION::MAJOR >= 6
48-
answer.validates_presence_of :files
49-
else
50-
if !answer.files.attached?
51-
answer.errors.add(:files, :blank)
52-
end
42+
if !answer.files.attached?
43+
answer.errors.add(:files, :blank)
5344
end
5445
else
5546
answer.validates_presence_of :answer_text

app/models/rapidfire/survey.rb

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
1-
require 'csv'
1+
require "csv"
2+
23
module Rapidfire
34
class Survey < ApplicationRecord
4-
has_many :attempts
5-
has_many :questions
6-
7-
validates :name, :presence => true
5+
has_many :attempts, dependent: :destroy
6+
has_many :questions, dependent: :destroy
7+
has_many :answers, through: :attempts, dependent: :destroy
88

9-
10-
if Rails::VERSION::MAJOR == 3
11-
attr_accessible :name, :introduction, :after_survey_content
12-
end
9+
validates :name, presence: true
1310

1411
def self.csv_user_attributes=(attributes)
1512
@@csv_user_attributes = Array(attributes)
@@ -24,24 +21,21 @@ def results_to_csv(filter)
2421
header = []
2522
header += Rapidfire::Survey.csv_user_attributes
2623
questions.each do |question|
27-
header << ActionView::Base.full_sanitizer.sanitize(question.question_text, :tags => [], :attributes => [])
24+
header << ActionView::Base.full_sanitizer.sanitize(question.question_text, tags: [], attributes: [])
2825
end
2926
header << "results updated at"
3027
csv << header
31-
attempts.where(SurveyResults.filter(filter, 'id')).each do |attempt|
28+
attempts.where(SurveyResults.filter(filter, "id")).each do |attempt|
3229
this_attempt = []
3330

3431
Survey.csv_user_attributes.each do |attribute|
3532
this_attempt << attempt.user.try(attribute)
3633
end
3734

3835
questions.each do |question|
39-
answer = attempt.answers.detect{|a| a.question_id == question.id }.try(:answer_text)
36+
answer = attempt.answers.detect { |a| a.question_id == question.id }.try(:answer_text)
4037
this_attempt << answer
4138
end
42-
43-
this_attempt << attempt.updated_at
44-
csv << this_attempt
4539
end
4640
end
4741
end

app/services/rapidfire/attempt_builder.rb

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,7 @@ def save!(options = {})
2222
# strings. we will store answers as one big string separated
2323
# by delimiter.
2424
text = text.values if text.is_a?(ActionController::Parameters)
25-
answer.answer_text =
26-
if text.is_a?(Array)
25+
answer.answer_text = if text.is_a?(Array)
2726
strip_checkbox_answers(text).join(Rapidfire.answers_delimiter)
2827
else
2928
text
@@ -37,22 +36,22 @@ def save!(options = {})
3736
end
3837
end
3938

40-
if Rails::VERSION::MAJOR >= 5
41-
@attempt.save!
42-
else
43-
@attempt.save!(options)
44-
end
39+
@attempt.save!(validate: options[:validate] != false)
40+
true
4541
end
4642

4743
def save(options = {})
48-
save!(options)
49-
rescue ActiveRecord::ActiveRecordError => e
50-
errors.add(:base, e.message)
51-
# repopulate answers here in case of failure as they are not getting updated
52-
@answers = @survey.questions.collect do |question|
53-
@attempt.answers.find { |a| a.question_id == question.id }
44+
begin
45+
save!(options)
46+
rescue ActiveRecord::ActiveRecordError => e
47+
errors.add(:base, e.message)
48+
# repopulate answers here in case of failure as they are not getting updated
49+
@answers = @survey.questions.collect do |question|
50+
answer = @attempt.answers.find { |a| a.question_id == question.id }
51+
answer || @attempt.answers.build(question_id: question.id)
52+
end
53+
false
5454
end
55-
false
5655
end
5756

5857
private
Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,37 @@
1-
if Rails::VERSION::MAJOR >= 5
2-
version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f
3-
base = ActiveRecord::Migration[version]
4-
else
5-
base = ActiveRecord::Migration
6-
end
7-
8-
class CreateRapidfireTables < base
1+
class CreateRapidfireTables < ActiveRecord::Migration[7.0]
92
def change
103
create_table :rapidfire_surveys do |t|
11-
t.string :name
4+
t.string :name
125
t.text :introduction
136
t.timestamps
147
end
158

169
create_table :rapidfire_questions do |t|
17-
t.references :survey
18-
t.string :type
19-
t.string :question_text
20-
t.string :default_text
21-
t.string :placeholder
10+
t.references :survey, foreign_key: { to_table: :rapidfire_surveys }
11+
t.string :type
12+
t.string :question_text
13+
t.string :default_text
14+
t.string :placeholder
2215
t.integer :position
2316
t.text :answer_options
2417
t.text :validation_rules
2518

2619
t.timestamps
2720
end
28-
add_index :rapidfire_questions, :survey_id if Rails::VERSION::MAJOR < 5
2921

3022
create_table :rapidfire_attempts do |t|
31-
t.references :survey
32-
t.references :user, polymorphic: true
23+
t.references :survey, foreign_key: { to_table: :rapidfire_surveys }
24+
t.references :user, polymorphic: true, index: true
3325

3426
t.timestamps
3527
end
36-
add_index :rapidfire_attempts, :survey_id if Rails::VERSION::MAJOR < 5
37-
add_index :rapidfire_attempts, [:user_id, :user_type]
3828

3929
create_table :rapidfire_answers do |t|
40-
t.references :attempt
41-
t.references :question
30+
t.references :attempt, foreign_key: { to_table: :rapidfire_attempts }
31+
t.references :question, foreign_key: { to_table: :rapidfire_questions }
4232
t.text :answer_text
4333

4434
t.timestamps
4535
end
46-
if Rails::VERSION::MAJOR < 5
47-
add_index :rapidfire_answers, :attempt_id
48-
add_index :rapidfire_answers, :question_id
49-
end
5036
end
5137
end

db/migrate/20170701191411_add_after_survey_content_to_survey.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
if Rails::VERSION::MAJOR >= 5
2-
version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f
3-
base = ActiveRecord::Migration[version]
4-
else
5-
base = ActiveRecord::Migration
6-
end
7-
8-
class AddAfterSurveyContentToSurvey < base
1+
class AddAfterSurveyContentToSurvey < ActiveRecord::Migration[7.0]
92
def change
103
add_column :rapidfire_surveys, :after_survey_content, :text
114
end

db/migrate/20190701274749_add_active_to_surveys.rb

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,4 @@
1-
if Rails::VERSION::MAJOR >= 5
2-
version = [Rails::VERSION::MAJOR, Rails::VERSION::MINOR].join('.').to_f
3-
base = ActiveRecord::Migration[version]
4-
else
5-
base = ActiveRecord::Migration
6-
end
7-
8-
class AddActiveToSurveys < base
1+
class AddActiveToSurveys < ActiveRecord::Migration[7.0]
92
def change
103
add_column :rapidfire_surveys, :active, :boolean
114
end

lib/generators/rapidfire/templates/migrations/add_active_to_survey.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
class AddActiveToSurvey < ActiveRecord::Migration[6.0]
1+
class AddActiveToSurvey < ActiveRecord::Migration[7.0]
22
def change
33
add_column :rapidfire_surveys, :active, :boolean, default: 1
44
end

0 commit comments

Comments
 (0)