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

Chore rendering optimizations #1561

Draft
wants to merge 13 commits into
base: master
Choose a base branch
from
Draft
4 changes: 3 additions & 1 deletion app/helpers/breadcrumbs_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ module BreadcrumbsHelper
include DiscussionsHelper

def breadcrumbs(e, extra=nil)
breadcrumbs0(e.navigable_name, e, extra, 'last')
Rails.cache.fetch [:breadcrumb, e, extra, Organization.current] do
breadcrumbs0(e.navigable_name, e, extra, 'last')
end
end

def header_breadcrumbs(link_for_organization: true)
Expand Down
14 changes: 3 additions & 11 deletions app/helpers/icons_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ def fixed_fa_icon(name, options = {})
fa_icon name, options.merge(class: 'fa-fw fixed-icon')
end

def exercise_status_icon(exercise)
link_to exercise_status_fa_icon(exercise),
exercise_path(exercise) if current_user?
def assignment_status_icon(assignment)
link_to contextualization_fa_icon(assignment),
exercise_path(assignment.exercise) if current_user?
end

def language_icon(language)
Expand All @@ -22,10 +22,6 @@ def contextualization_fa_icon(contextualization)
fa_icon(*icon_for(contextualization))
end

def exercise_status_fa_icon(exercise)
contextualization_fa_icon(exercise.assignment_for(current_user))
end

def discussion_status_fa_icon(discussion)
contextualization_fa_icon(discussion)
end
Expand All @@ -35,10 +31,6 @@ def icon_for(iconizable)
[iconized[:type], class: "text-#{iconized[:class]} status-icon"]
end

def class_for_exercise(exercise)
icon_class_for(exercise.assignment_for(current_user))
end

def icon_class_for(iconizable)
iconizable.iconize[:class].to_s
end
Expand Down
17 changes: 6 additions & 11 deletions app/helpers/links_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
module LinksHelper

def link_to_path_element(element, options={})
name = extract_name element, options
link_to name, element, options
mode = options[:mode]
Rails.cache.fetch [:link, element, mode, Organization.current] do
link_to extract_name(element, mode), element
end
end

def link_to_error_404
Expand Down Expand Up @@ -65,15 +67,8 @@ def turbolinks_enable_for(exercise)

private

def extract_name(named, options)
case options.delete(:mode)
when :plain
named.name
when :friendly
named.friendly
else
named.navigable_name
end
def extract_name(named, mode )
mode == :plain ? named.name : named.navigable_name
end

def edit_link_to_bibliotheca
Expand Down
4 changes: 2 additions & 2 deletions app/helpers/progress_bar_helper.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module ProgressBarHelper
include IconsHelper

def class_for_progress_list_item(exercise, active)
"progress-list-item text-center #{class_for_exercise(exercise)} #{active ? 'active' : ''}"
def class_for_progress_list_item(assignment, active)
"progress-list-item text-center #{icon_class_for(assignment)} #{active ? 'active' : ''}"
end

end
4 changes: 2 additions & 2 deletions app/views/chapters/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
<div>
<h3><%= t(:lessons) %></h3>

<% @chapter.lessons.each do |lesson| %>
<% @chapter.lessons.includes(guide: :exercises).each do |lesson| %>
<h4><%= lesson.number %>. <%= link_to_path_element lesson, mode: :plain %></h4>
<%= render partial: 'layouts/progress_listing', locals: { exercises: lesson.exercises } %>
<%= render partial: 'layouts/progress_listing', locals: { guide: lesson.guide } %>
<% end %>
</div>
<% end %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/exercises/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

<div>
<% if @stats && standalone_mode? %>
<%= render partial: 'layouts/progress_bar', locals: {actual: @exercise, guide: @exercise.guide, stats: @stats} %>
<%= render partial: 'layouts/progress_bar', locals: {current: @exercise, guide: @exercise.guide, stats: @stats} %>
<% end %>
</div>

Expand Down Expand Up @@ -65,6 +65,6 @@
<%= render partial: 'layouts/modals/level_up' %>
<% end %>

<%= render partial: 'layouts/modals/guide_corollary', locals: {guide: @guide} %>
<%= render partial: 'layouts/modals/guide_corollary', locals: {guide: @guide} if @stats.almost_done? %>
<%= render partial: 'layouts/modals/new_message', locals: {exercise: @exercise} if should_render_message_input?(@exercise) %>
<% end if current_user? %>
6 changes: 3 additions & 3 deletions app/views/layouts/_guide.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@

<%= render partial: 'layouts/authoring', locals: {guide: @guide} %>

<% if subject.timed? && subject.started?(current_user) && !current_user.teacher? %>
<% if subject.timed? && @stats.started? && !current_user.teacher? %>
<%= render partial: 'layouts/timer', locals: {end_time: subject.real_end_time(current_user)} %>
<% end %>

<%= yield if block_given? %>

<h3>
<%= t :exercises %>
<%= restart_guide_link(@guide) if current_user && @guide.started?(current_user) && @guide.resettable? %>
<%= restart_guide_link(@guide) if current_user && @stats.started? && @guide.resettable? %>
</h3>

<%= render partial: 'layouts/progress_listing', locals: {exercises: @guide.exercises} %>
<%= render partial: 'layouts/progress_listing', locals: { guide: @guide } %>

<% if @stats&.done? %>
<div class="text-box">
Expand Down
24 changes: 14 additions & 10 deletions app/views/layouts/_progress_bar.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<div class="progress-list-flex">
<% guide.exercises.each do |e| %>
<a
<%= turbolinks_enable_for e %>
href="<%= exercise_path(e)%>"
aria-label="<%= e.navigable_name %>"
title="<%= e.navigable_name %>"
data-mu-exercise-id="<%= e.id %>"
class="<%= class_for_progress_list_item(e, e == actual)%>">
</a>
<% assignments = guide.assignments_for(current_user) %>
<% assignments.each do |assignment| %>
<% exercise = assignment.exercise %>
<% cache [:progress_list_item, exercise, assignment.status, Organization.current, exercise == current] do %>
<a
<%= turbolinks_enable_for exercise %>
href="<%= exercise_path(exercise)%>"
aria-label="<%= exercise.navigable_name %>"
title="<%= exercise.navigable_name %>"
data-mu-exercise-id="<%= exercise.id %>"
class="<%= class_for_progress_list_item(assignment, exercise == current)%>">
</a>
<% end %>
<% end %>
</div>
</div>
21 changes: 14 additions & 7 deletions app/views/layouts/_progress_listing.html.erb
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
<ul class="progress-listing">
<% exercises.each do |exercise| %>
<% cache [exercise, exercise.status_for(current_user), Organization.current] do %>
<li <%= turbolinks_enable_for(exercise) %>>
<%= exercise_status_icon exercise %>
<%= link_to_path_element(exercise) %>
</li>
<% end %>
<%
assignments = guide.assignments_for(current_user)
statuses = assignments.map(&:status)
%>
<% cache_if statuses.chunk_while(&:==).count <= 2, [guide, statuses, Organization.current] do %>
<% assignments.each do |assignment| %>
<% cache [assignment.exercise, assignment.status, Organization.current] do %>
<li <%= turbolinks_enable_for(assignment.exercise) %>>
<%= assignment_status_icon assignment %>
<%= link_to_path_element(assignment.exercise) %>
</li>
<% end %>
<% end %>
<% end %>

</ul>
2 changes: 1 addition & 1 deletion mumuki-laboratory.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ Gem::Specification.new do |s|

s.add_dependency "rails", "~> 5.1.6"

s.add_dependency 'mumuki-domain', '~> 8.3.0'
s.add_dependency 'mumuki-domain', '~> 8.4.0'
s.add_dependency 'mumukit-bridge', '~> 4.1'
s.add_dependency 'mumukit-login', '~> 7.3'
s.add_dependency 'mumukit-nuntius', '~> 6.1'
Expand Down
1 change: 1 addition & 0 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@
t.datetime "legal_terms_accepted_at"
t.datetime "forum_terms_accepted_at"
t.boolean "banned_from_forum"
t.boolean "uppercase_mode"
t.index ["avatar_type", "avatar_id"], name: "index_users_on_avatar_type_and_avatar_id"
t.index ["disabled_at"], name: "index_users_on_disabled_at"
t.index ["last_organization_id"], name: "index_users_on_last_organization_id"
Expand Down
1 change: 0 additions & 1 deletion spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
before { reindex_current_organization! }

it { expect(link_to_path_element(exercise, mode: :plain)).to eq '<a href="/exercises/30-c1-bar-foo3">foo3</a>' }
it { expect(link_to_path_element(exercise, mode: :friendly)).to eq '<a href="/exercises/30-c1-bar-foo3">C1: bar - foo3</a>' }
it { expect(link_to_path_element(exercise)).to eq '<a href="/exercises/30-c1-bar-foo3">3. foo3</a>' }
end

Expand Down