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

Introduce Quizzes #1289

Merged
merged 1 commit into from May 14, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/assets/stylesheets/_questions-show.scss
@@ -0,0 +1,5 @@
.questions-show {
.hidden {
display: none;
}
}
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.css.scss
Expand Up @@ -46,6 +46,7 @@
@import "tile";
@import "trail-titles";
@import "trails-show";
@import "questions-show";
@import "repos";
@import "resources-index";
@import "topic-label";
Expand Down
16 changes: 16 additions & 0 deletions app/controllers/questions_controller.rb
@@ -0,0 +1,16 @@
class QuestionsController < ApplicationController
def show
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to continue to pass both objects in here, or would it make sense to create a facade?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the template, nearly all the methods are called directly on the @question object so it feels a wee bit premature to extract a facade now, but I expect that will change soon.

@quiz = find_quiz
@question = find_question
end

private

def find_question
Question.find(params[:id])
end

def find_quiz
Quiz.find(params[:quiz_id])
end
end
11 changes: 11 additions & 0 deletions app/controllers/quizzes_controller.rb
@@ -0,0 +1,11 @@
class QuizzesController < ApplicationController
def show
redirect_to quiz_question_path(quiz, quiz.first_question)
end

private

def quiz
Quiz.find(params[:id])
end
end
12 changes: 12 additions & 0 deletions app/models/question.rb
@@ -0,0 +1,12 @@
class Question < ActiveRecord::Base
validates :prompt, presence: true
validates :answer, presence: true

belongs_to :quiz

acts_as_list scope: :quiz

def next
lower_item
end
end
9 changes: 9 additions & 0 deletions app/models/quiz.rb
@@ -0,0 +1,9 @@
class Quiz < ActiveRecord::Base
validates :title, presence: true

has_many :questions, -> { order(position: :asc) }, dependent: :destroy
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this order isn't tested.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for it.


def first_question
questions.first
end
end
6 changes: 5 additions & 1 deletion app/models/user.rb
Expand Up @@ -57,7 +57,11 @@ def has_active_subscription?
end

def has_access_to?(feature)
subscription.present? && subscription.has_access_to?(feature)
if feature == :quizzes
admin?
else
subscription.present? && subscription.has_access_to?(feature)
end
end

def subscribed_at
Expand Down
4 changes: 4 additions & 0 deletions app/services/practice.rb
Expand Up @@ -19,6 +19,10 @@ def in_progress_trails
trails.select(&:in_progress?)
end

def quizzes
Quiz.all
end

private

attr_reader :user
Expand Down
6 changes: 6 additions & 0 deletions app/views/practice/_quizzes.html.erb
@@ -0,0 +1,6 @@
<% if current_user.has_access_to?(:quizzes) %>
<h2><%= t('.quizzes-list-title') %></h2>
<section class="quizzes">
<%= render quizzes %>
</section>
<% end %>
2 changes: 2 additions & 0 deletions app/views/practice/show.html.erb
Expand Up @@ -5,6 +5,8 @@
<%= render "products/locked_features" %>
<% end %>

<%= render "practice/quizzes", quizzes: @practice.quizzes %>

<section class="root-trails">
<div class="content">
<section class="trails-progress">
Expand Down
36 changes: 36 additions & 0 deletions app/views/questions/show.html.erb
@@ -0,0 +1,36 @@
<div class="question">
<h1>
<%= t('.numbered-title', position: @question.position) %>
</h1>

<div class="prompt">
<%= format_markdown @question.prompt %>
</div>

<h3><%= t('.answer') %></h3>

<%= link_to t('.reveal-answer'), "javascript:;", class: "reveal-answer" %>

<div class="answer hidden">
<%= format_markdown @question.answer %>

<% if @question.next.present? %>
<%= link_to t('.next-question'), [@question.quiz, @question.next] %>
<% else %>
<p>No more questions.</p>
<%= link_to t('.return-to-dashboard'), practice_path %>
<% end %>
</div>

<% content_for :javascript do -%>
<script type="text/javascript">
$(function() {
$revealAnswerLink = $(".reveal-answer");
$revealAnswerLink.on("click", function(event) {
$(".answer").show();
$revealAnswerLink.hide();
});
});
</script>
<% end %>
</div>
3 changes: 3 additions & 0 deletions app/views/quizzes/_quiz.html.erb
@@ -0,0 +1,3 @@
<div class="quiz">
<span class="title"><%= link_to quiz.title, quiz %></span>
</div>