Skip to content

Instantly share code, notes, and snippets.

@rails-hub
Last active July 14, 2017 19:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rails-hub/30dcc7b03ca08264f277 to your computer and use it in GitHub Desktop.
Save rails-hub/30dcc7b03ca08264f277 to your computer and use it in GitHub Desktop.
class ToolDecorator < Draper::Decorator
include Trials::RatingToGrade
delegate_all
def grade
rating_to_grade(model.rating)
end
def trial_grade
rating_to_grade(model.trial_grade)
end
def trial_grade_class
rating_css_class(model.trial_grade)
end
def label
self.name
end
def my_rate
model.survey_question_answers.where(i_cant_answer: false, surveys: {user_id: h.current_user.id}).average(:value)
end
def my_last_grade
rating_to_grade my_rating
end
def my_rating_class
rating_css_class my_rating
end
def my_last_grade_date
model.surveys.where(user_id: h.current_user.id).order(created_at: :desc).limit(1).first.try(:created_at)
end
def rators_count
@rators_count ||= is_answered_non_qualifying_scope.count
end
def logo_url
model.logo.nil? ? '' : model.logo.asset_url
end
def subject
@subject ||= model.subjects.min_by(&:depth).try(:name) || ''
end
def subject_link
subject.present? ? h.link_to(subject, 'javascript:void(0)') : nil
end
def screen_shots
@screen_shots ||= model.attachments.where('attachment_type IS NULL OR attachment_type <> ?', 'logo')
end
def rating_class
rating_css_class(model.rating)
end
def type
model.class.to_s
end
def my_rating
model.survey_question_answers.
where(i_cant_answer: false, surveys: {id: model.surveys.where(user_id: h.current_user.id).
order(created_at: :desc).limit(1).select(:id)}).average(:value) || 0
end
def grade_text
my_rating > 0 ? 'Grade It Again' : 'Grade it'
end
private
def is_answered_scope
model.survey_question_answers.where(survey_question_answers: {i_cant_answer: false})
end
def is_answered_non_qualifying_scope
model.survey_question_answers.where(survey_question_answers: {i_cant_answer: false}).joins(:survey_question).where(survey_questions: {is_qualifying_question: false})
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment