Skip to content

Commit

Permalink
Merge pull request #273 from mumuki/feature-path-progress
Browse files Browse the repository at this point in the history
Feature path progress
  • Loading branch information
flbulgarelli committed Aug 17, 2015
2 parents 420cd62 + 5d97e8c commit bb05f6a
Show file tree
Hide file tree
Showing 39 changed files with 320 additions and 124 deletions.
2 changes: 1 addition & 1 deletion app/controllers/guides_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def create

def show
if current_user?
@stats = @guide.stats(current_user)
@stats = @guide.stats_for(current_user)
@next_exercise = @guide.next_exercise(current_user)
else
@next_exercise = @guide.first_exercise
Expand Down
7 changes: 7 additions & 0 deletions app/controllers/paths_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class PathsController < ApplicationController
before_action :authenticate!

def show
@path = Path.find(params[:id])
end
end
6 changes: 4 additions & 2 deletions app/controllers/submissions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
class SubmissionsController < ApplicationController #FIXME remove
class SubmissionsController < ApplicationController
include NestedInUser

before_action :authenticate!
def index
@submissions = paginated current_user.submissions
@submissions = paginated @user.submissions.order(updated_at: :desc), 50
end
end
10 changes: 4 additions & 6 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ def tab_list(tabs)
end

def next_guides_box(guide, options={})
return if guide.path.blank?

suggested = guide.next_guides
if suggested.empty?
nil
elsif suggested.size == 1 || options[:only_first]
link_to t(:next_guide), suggested.first, class: 'btn btn-success'
t :path_finished, path: link_to_path(guide.path)
else
('<ul class="list-group text-center">' +
suggested.map {|it| "<li class=\"list-group-item\">#{link_to_guide(it)}</li>"}.join("\n") +
'</ul>').html_safe
link_to t(:next_guide, name: suggested.first.name), suggested.first, class: 'btn btn-success'
end
end

Expand Down
23 changes: 23 additions & 0 deletions app/helpers/concerns/with_breadcrumbs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module WithBreadcrumbs
def exercise_breadcrumb(e)
base = link_to_exercise e
if e.guide
"#{guide_breadcrumb(e.guide)}/#{base}".html_safe
else
base
end
end

def guide_breadcrumb(g)
base = link_to_guide g
if g.path
"#{path_breadcrumb(g.path)}/#{base}".html_safe
else
base
end
end

def path_breadcrumb(p)
link_to_path p
end
end
3 changes: 3 additions & 0 deletions app/helpers/concerns/with_links_rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,7 @@ def link_to_user(user)
link_to user.name, user
end

def link_to_path(path)
link_to path.name, path
end
end
2 changes: 1 addition & 1 deletion app/helpers/concerns/with_stats_rendering.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module WithStatsRendering
def stats(stats, k)
def stats_html(stats, k)
"#{stats.send k} #{status_icon(k)} ".html_safe
end

Expand Down
3 changes: 3 additions & 0 deletions app/helpers/user_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module UserHelper
include WithBreadcrumbs
end
2 changes: 1 addition & 1 deletion app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class Category < ActiveRecord::Base

validates_presence_of :name, :description, :image_url

markup_on :description
markup_on :description, :long_description, :links

def single_path?
paths.size == 1
Expand Down
8 changes: 8 additions & 0 deletions app/models/concerns/with_siblings.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,13 @@ def previous
def orphan?
parent.nil?
end

def contextualized_name
if parent
"#{position}. #{name}"
else
name
end
end
end

12 changes: 4 additions & 8 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,6 @@ def generate_original_id!
update!(original_id: id) unless original_id
end

def contextualized_title
if guide
"#{position}. #{title}"
else
title
end
end

def collaborator?(user)
guide.present? && guide.authored_by?(user)
end
Expand All @@ -76,4 +68,8 @@ def defaults
def self.default_layout
layouts.keys[0]
end

def name #FIXME remove
title
end
end
7 changes: 2 additions & 5 deletions app/models/guide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class Guide < ActiveRecord::Base
WithLocale,
WithCollaborators,
WithPath,
WithExercises
WithExercises,
WithStats

#TODO rename name to title. This helps building also generic link_to compoenetns
has_many :imports, -> { order(created_at: :desc)}
Expand All @@ -32,10 +33,6 @@ def search_tags
exercises.flat_map(&:search_tags).uniq
end

def stats(user)
Stats.from_statuses exercises.map { |it| it.status_for(user) }
end

def github_url
"https://github.com/#{github_repository}"
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/language.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Language < ActiveRecord::Base

validates :name, presence: true, uniqueness: {case_sensitive: false}

markup_on :test_syntax_hint
markup_on :test_syntax_hint, :description

def run_tests!(request)
bridge.run_tests!(request)
Expand Down
4 changes: 3 additions & 1 deletion app/models/path.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
class Path < ActiveRecord::Base
include WithGuides
include WithGuides, WithStats

belongs_to :category
belongs_to :language

has_many :exercises, through: :guides

def name
if category.single_path?
category.name
Expand Down
16 changes: 13 additions & 3 deletions app/models/stats.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,20 @@ def started?
submitted > 0
end

def resolved_ratio
ratio resolved
end

def passed_ratio
100 * passed / total.to_f
ratio passed
end

def passed_with_warnings_ratio
100 * passed_with_warnings / total.to_f
ratio passed_with_warnings
end

def failed_ratio
100 * failed / total.to_f
ratio failed
end

def to_h(&key)
Expand All @@ -60,4 +64,10 @@ def self.from_statuses(statuses)
accum
end)
end

private

def ratio(x)
(100 * x / total.to_f).round(2)
end
end
4 changes: 3 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ class User < ActiveRecord::Base
class_name: 'Exercise',
source: :exercise

has_many :submitted_guides, -> { uniq.order(:position) }, through: :submitted_exercises, class_name: 'Guide', source: :guide
has_many :submitted_guides, -> { uniq }, through: :submitted_exercises, class_name: 'Guide', source: :guide

has_many :submitted_paths, -> { uniq }, through: :submitted_guides, class_name: 'Path', source: :path

has_many :solved_exercises,
-> { where('submissions.status' => Status::Passed.to_i).uniq },
Expand Down
5 changes: 5 additions & 0 deletions app/models/with_stats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module WithStats
def stats_for(user)
Stats.from_statuses exercises.map { |it| it.status_for(user) }
end
end
4 changes: 2 additions & 2 deletions app/views/exercise_submissions/_results.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%
@guide = @exercise.guide
@stats = @guide.stats(current_user) if @guide
@stats = @guide.stats_for(current_user) if @guide
%>


Expand Down Expand Up @@ -56,7 +56,7 @@
class: 'text-warning warning pull-left' %>
<%= next_button(@exercise) %>
<%= next_guides_box(@guide, only_first: true) if @stats.try &:done? %>
<%= next_guides_box(@guide) if @stats.try &:done? %>
</div>

<script>
Expand Down
1 change: 0 additions & 1 deletion app/views/exercises/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@
</div>
<div class="row">
<div class="col-md-12 text-center">
<h4><%= t :see_next_guides %></h4>
<%= next_guides_box(@guide) %>
</div>
</div>
Expand Down
28 changes: 13 additions & 15 deletions app/views/guides/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,12 @@
<div class="col-md-12">
<div>
<h3><%= t :about_this_guide %></h3>

<p>
<%= @guide.description_html %>
</p>
<%= @guide.description_html %>
</div>
</div>
</div>

<% if current_user? %>
<% if current_user? && @stats.started? %>
<div class="row">
<div class="col-md-5">
<h3><%= t :stats %></h3>
Expand All @@ -27,20 +24,16 @@
</p>

<p>
<%= raw t :passed_exercises_stats, count: stats(@stats, :passed) %>
<%= raw t :failed_exercises_stats, count: stats(@stats, :failed) %>
<%= raw t :unknown_exercises_stats, count: stats(@stats, :unknown) %>
<%= raw t :passed_exercises_stats, count: stats_html(@stats, :passed) %>
<%= raw t :failed_exercises_stats, count: stats_html(@stats, :failed) %>
<%= raw t :unknown_exercises_stats, count: stats_html(@stats, :unknown) %>
</p>

<p>
<% if @stats.done? %>
¡Eso significa que <strong>ya terminaste esta guía</strong>! ¡Bien hecho!
Tenemos más guías para vos:
<%= next_guides_box @guide %>
<% elsif @stats.good_progress? %>
<%= raw t :good_progress_in_guide %>
<% elsif !@stats.started? %>
<%= raw t :not_started_guide %>
<% elsif @stats.stuck? %>
<%= raw t :stuck_in_guide %>
<% end %>
Expand All @@ -52,7 +45,7 @@
</div>
</div>

<% else %>
<% elsif !current_user? %>

<div class="row">
<div class="col-md-12">
Expand All @@ -70,11 +63,16 @@

<% end %>
<% unless @stats.try :done? %>
<% if @stats.try :done? %>
<%= corollary_box(@guide) %>
<div class="actions">
<%= next_guides_box @guide %>
</div>
<% else %>
<div class="row">
<div class="col-md-12">
<div class="actions">
<% if @next_exercise %>
<% if @next_exercise %>
<%= link_to t(practice_key_for(@stats)), exercise_path(@next_exercise), class: 'btn btn-primary' %>
<% end %>
</div>
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_exercise.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<% @stats = @exercise.guide.stats(current_user) if current_user && @exercise.guide %>
<% @stats = @exercise.guide.stats_for(current_user) if current_user && @exercise.guide %>


<div class="row">
<div class="col-md-12">
<h1 style="display: inline-block">
<%= @exercise.contextualized_title %>
<%= @exercise.contextualized_name %>
</h1>
<span style="margin-left: 5px">
<a data-toggle="collapse" href="#about-section" class="text-info">
Expand Down
4 changes: 2 additions & 2 deletions app/views/layouts/_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
<div class="row">
<div class="col-md-12">
<h1>
<%= @guide.name %>
<%= @guide.contextualized_name %>
<small> - <%= @guide.language %></small>
</h1>
<h4><%= with_classifications @guide %></h4>

<% if @guide.path %>
<p>
<%= previous_nav_button(@guide) %>
<span><%= @guide.path.name %></span>
<span><%= link_to_path @guide.path %></span>
<%= next_nav_button(@guide) %>
</p>
<% end%>
Expand Down
7 changes: 5 additions & 2 deletions app/views/layouts/_user.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,14 @@
<li <%= active_if :overview %>>
<%= link_to t(:overview), @user %>
</li>
<li <%= active_if :recent_activity %>>
<%= link_to t(:recent_activity), user_submissions_path(@user) %>
</li>
<li <%= active_if :exercises %>>
<%= link_to t(:exercise).pluralize, user_exercises_path(@user) %>
<%= link_to t(:created_exercises), user_exercises_path(@user) %>
</li>
<li <%= active_if :guides %>>
<%= link_to t(:guide).pluralize, user_guides_path(@user) %>
<%= link_to t(:created_guides), user_guides_path(@user) %>
</li>
</ul>
</div>
Expand Down
Loading

0 comments on commit bb05f6a

Please sign in to comment.