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

final eval question rails #43

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
6 changes: 6 additions & 0 deletions app/callbacks/activable_callbacks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ def self.before_validation(model_object)
end
end

def self.before_save(model_object)
if model_object.active? && !model_object.publishable?
self.active = false
end
end

end
2 changes: 1 addition & 1 deletion app/controllers/concerns/controllers/activable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Controllers::Activable
end

def activate
if @model_instance.activate
if @model_instance.activate && @model_instance.active?
flash[:alert] = "#{controller_name.classify} was successfully activated."
else
flash[:alert] = "#{controller_name.classify} cannot be activated."
Expand Down
15 changes: 15 additions & 0 deletions app/models/question.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ class Question < ApplicationRecord
validate :only_one_correct_option

before_validation ActivableCallbacks, on: :update
before_save ActivableCallbacks
after_commit :update_quiz, on: [:update, :destroy]

scope :active, -> { where(active: true) }

Expand All @@ -35,6 +37,10 @@ def self.ransackable_associations(auth_object = nil)
[]
end

def publishable?
question_options.length >= 4
end

private

def only_one_correct_option
Expand All @@ -51,4 +57,13 @@ def options_count
errors.add :base, 'enter exactly 4 options' unless count == 4
end

def update_quiz
quiz.inactivate unless quiz.publishable?

if quiz_id_before_last_save && quiz_id_before_last_save != quiz_id
previous_quiz = Quiz.find_by_id(quiz_id_before_last_save)
previous_quiz.inactivate unless previous_quiz.publishable?
end
end

end
12 changes: 11 additions & 1 deletion app/models/question_option.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ class QuestionOption < ApplicationRecord
validate :presence_of_either_data_or_image

before_validation :set_type
after_commit :update_question, on: [:update, :destroy]

scope :correct, -> { where(correct: true) }

private

def presence_of_either_data_or_image
unless option_image.present? && data.present?
if option_image.blank? && data.blank?
errors.add :base, 'Both data and image can\'t be blank'
end
end
Expand All @@ -28,4 +29,13 @@ def set_type
end
end

def update_question
question.inactivate unless question.publishable?

if question_id_before_last_save && question_id_before_last_save != question_id
previous_question = Question.find_by_id(question_id_before_last_save)
previous_question.inactivate unless previous_question.publishable?
end
end

end
5 changes: 5 additions & 0 deletions app/models/quiz.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class Quiz < ApplicationRecord

before_validation :set_time_limit_in_seconds, if: -> { time_limit_in_minutes.present? }
before_validation ActivableCallbacks, on: :update
before_save ActivableCallbacks
after_save_commit :schedule_mail_if_featured, if: :featured?

scope :active, -> { where(active: true) }
Expand Down Expand Up @@ -59,6 +60,10 @@ def self.ransackable_associations(auth_object = nil)
[]
end

def publishable?
questions.length >= 10 && questions.length == questions.map(&:publishable?).count(true)
end

private

def set_time_limit_in_seconds
Expand Down