Skip to content

Commit be4012b

Browse files
authored
Merge pull request #157 from n1vedhar1/remove-older-version-check
Remove version checks for older rails
2 parents 3dfada0 + b388fe8 commit be4012b

20 files changed

+224
-344
lines changed

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 & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
module Rapidfire
22
class Attempt < ApplicationRecord
33
belongs_to :survey
4-
has_many :answers, inverse_of: :attempt, autosave: true
4+
has_many :answers, inverse_of: :attempt, autosave: true
55

6-
if Rails::VERSION::MAJOR >= 5
7-
belongs_to :user, polymorphic: true, optional: true
8-
else
9-
belongs_to :user, polymorphic: true
10-
end
6+
belongs_to :user, polymorphic: true, optional: true
117
end
128
end

app/models/rapidfire/question.rb

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,12 @@
11
module Rapidfire
22
class Question < ApplicationRecord
3-
belongs_to :survey, :inverse_of => :questions
4-
has_many :answers
5-
3+
belongs_to :survey, inverse_of: :questions
4+
has_many :answers
65
has_many_attached :files
76

8-
97
default_scope { order(:position) }
108

11-
validates :survey, :question_text, :presence => true
9+
validates :survey, :question_text, presence: true
1210
validate :type_can_change
1311
serialize :validation_rules, coder: YAML
1412

@@ -36,21 +34,9 @@ def validate_answer(answer)
3634
if rules[:presence] == "1"
3735
case self
3836
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
45-
end
37+
answer.validates_presence_of :file
4638
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
53-
end
39+
answer.validates_presence_of :files
5440
else
5541
answer.validates_presence_of :answer_text
5642
end

app/models/rapidfire/survey.rb

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
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
6+
has_many :questions
87

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

1410
def self.csv_user_attributes=(attributes)
1511
@@csv_user_attributes = Array(attributes)
@@ -24,19 +20,20 @@ def results_to_csv(filter)
2420
header = []
2521
header += Rapidfire::Survey.csv_user_attributes
2622
questions.each do |question|
27-
header << ActionView::Base.full_sanitizer.sanitize(question.question_text, :tags => [], :attributes => [])
23+
header << ActionView::Base.full_sanitizer.sanitize(question.question_text, tags: [], attributes: [])
2824
end
2925
header << "results updated at"
3026
csv << header
31-
attempts.where(SurveyResults.filter(filter, 'id')).each do |attempt|
27+
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
4239

app/services/rapidfire/attempt_builder.rb

Lines changed: 2 additions & 7 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,11 +36,7 @@ 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!
4540
end
4641

4742
def save(options = {})
Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,34 @@
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|
1710
t.references :survey
18-
t.string :type
19-
t.string :question_text
20-
t.string :default_text
21-
t.string :placeholder
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
25-
2618
t.timestamps
2719
end
28-
add_index :rapidfire_questions, :survey_id if Rails::VERSION::MAJOR < 5
2920

3021
create_table :rapidfire_attempts do |t|
3122
t.references :survey
3223
t.references :user, polymorphic: true
33-
3424
t.timestamps
3525
end
36-
add_index :rapidfire_attempts, :survey_id if Rails::VERSION::MAJOR < 5
37-
add_index :rapidfire_attempts, [:user_id, :user_type]
3826

3927
create_table :rapidfire_answers do |t|
4028
t.references :attempt
4129
t.references :question
4230
t.text :answer_text
43-
4431
t.timestamps
4532
end
46-
if Rails::VERSION::MAJOR < 5
47-
add_index :rapidfire_answers, :attempt_id
48-
add_index :rapidfire_answers, :question_id
49-
end
5033
end
5134
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
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
class AddActiveToSurvey < ActiveRecord::Migration[6.0]
1+
class AddActiveToSurvey < ActiveRecord::Migration[7.0]
22
def change
3-
add_column :rapidfire_surveys, :active, :boolean, default: 1
3+
add_column :rapidfire_surveys, :active, :boolean, default: true
44
end
55
end

lib/generators/rapidfire/templates/migrations/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

0 commit comments

Comments
 (0)