Skip to content

Commit

Permalink
Merge pull request #421 from mumuki/cleanup
Browse files Browse the repository at this point in the history
Cleanup
  • Loading branch information
aguspina committed Jan 22, 2016
2 parents 0da954c + ef043cf commit f1efce9
Show file tree
Hide file tree
Showing 24 changed files with 54 additions and 118 deletions.
23 changes: 1 addition & 22 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ module ApplicationHelper
include WithLinksRendering
include WithIcons
include WithNavigation
include WithStatusRendering

def contact_email
Book.current.contact_email
Expand Down Expand Up @@ -32,16 +33,6 @@ def time_ago_in_words_or_never(date)
date ? time_ago_in_words(date) : t(:never)
end

def tab_list(tabs)
('<ul class="nav nav-tabs" role="tablist">' +
tabs.map do |tab|
%Q{<li role="presentation" class="#{'active' if tab == tabs.first }">
<a href="##{tab}-panel" aria-controls="#{tab}" role="tab" data-toggle="tab">#{t(tab)}</a>
</li>}
end.join("\n") +
'</ul>').html_safe
end

def chapter_finished(guide)
t :chapter_finished_html, chapter: link_to_path_element(@guide.chapter) if @guide.chapter
end
Expand All @@ -51,16 +42,4 @@ def corollary_box(with_corollary)
"<div><h3>#{t :corollary}</h3><p>#{with_corollary.corollary_html}</p></div>".html_safe
end
end

def with_classifications(classifiable)
classifications = [
(classification_label('success', :certificate, :new) if classifiable.new?),
(classification_label('info', :university, :learning) if classifiable.learning?),
(classification_label('warning', :warning, :beta) if classifiable.beta)]
classifications.compact.join(' ').html_safe
end

def classification_label(style, icon, key)
%Q{<span class="label label-#{style}">#{fa_icon icon} #{t key}</span>}
end
end
21 changes: 4 additions & 17 deletions app/helpers/concerns/with_breadcrumbs.rb
Original file line number Diff line number Diff line change
@@ -1,26 +1,13 @@
module WithBreadcrumbs
def exercise_breadcrumb(e)
def breadcrumbs(e)
base = link_to_path_element e
if e.guide
"#{guide_breadcrumb(e.guide)} <li>#{base}</li>".html_safe
if e.respond_to?(:orphan?) && !e.orphan?
"#{breadcrumbs(e.parent)} <li>#{base}</li>".html_safe
else
base
"#{home_breadcrumb}<li>#{base}</li>".html_safe
end
end

def guide_breadcrumb(g)
base = link_to_path_element g
if g.chapter
"#{path_breadcrumb(g.chapter)} <li>#{base}</li>".html_safe
else
base
end
end

def path_breadcrumb(p)
"#{home_breadcrumb}<li>#{link_to_path_element p}</li>".html_safe
end

def home_breadcrumb
"<li><span class=\"ahahamojimoji\">#{link_to 'ム', root_path }</span></li>".html_safe
end
Expand Down
8 changes: 0 additions & 8 deletions app/helpers/concerns/with_icons.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ def exercise_status_fa_icon(exercise)
fa_icon(*icon_for_status(exercise.status_for(current_user)))
end

def class_for_status(s)
s.iconize[:class].to_s
end

def icon_type_for_status(s)
s.iconize[:type].to_s
end

def icon_for_status(s)
iconized = s.iconize
[iconized[:type], class: "text-#{iconized[:class]} status-icon"]
Expand Down
19 changes: 2 additions & 17 deletions app/helpers/concerns/with_progress_bar.rb
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
module WithProgressBar

def class_for_bar(status)
case status.to_s
when 'passed'
'success'
when 'PassedWithWarnings'
'warning'
when 'failed'
'danger'
else
''
end
def active_class(exercise, actual)
exercise == actual ? 'active' : ''
end

def active_class exercise, actual
exercise.id == actual.id ? 'active' : ''
end

end
6 changes: 0 additions & 6 deletions app/helpers/concerns/with_stats_chart.rb

This file was deleted.

4 changes: 0 additions & 4 deletions app/helpers/concerns/with_stats_rendering.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
module WithStatsRendering
def stats_html(stats, k)
"#{stats.send k} #{status_icon(k)} ".html_safe
end

def practice_key_for(stats)
if stats && stats.started?
:continue_practicing
Expand Down
9 changes: 9 additions & 0 deletions app/helpers/concerns/with_status_rendering.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module WithStatusRendering
def class_for_status(s)
Status.coerce(s).iconize[:class].to_s
end

def icon_type_for_status(s)
Status.coerce(s).iconize[:type].to_s
end
end
1 change: 0 additions & 1 deletion app/helpers/guides_helper.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
module GuidesHelper
include WithStatsRendering
include WithStatsChart
end
3 changes: 0 additions & 3 deletions app/models/concerns/exercise_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module ExerciseNavigation
extend ActiveSupport::Concern

included do
include Navigable
include WithParent

belongs_to :guide
end

Expand Down
13 changes: 7 additions & 6 deletions app/models/concerns/guide_navigation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ module GuideNavigation
extend ActiveSupport::Concern

included do
include Navigable
include WithParent

has_one :chapter_guide
has_one :chapter, through: :chapter_guide
end
Expand All @@ -15,16 +12,20 @@ def positionate!(chapter, number) #FIXME stop doing position logic by hand
self.chapter_guide
end

def done_for?(user)
stats_for(user).done?
end

def number
chapter_guide.try(&:number)
end

def siblings_for(user) #FIXME duplicated code
chapter.try { |it| it.pending_guides(user) } || []
def siblings_for(user)
chapter.defaulting([]) { |it| it.pending_guides(user) }
end

def siblings
chapter.try(&:guides) || []
chapter.defaulting([], &:guides)
end

def parent
Expand Down
3 changes: 2 additions & 1 deletion app/models/concerns/navigable.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
module Navigable

## Plain Navigation

def next
Expand All @@ -24,6 +23,8 @@ def first_for(user)
siblings_for(user).sort_by(&:number).first
end

# Names

def navigable_name
"#{number}. #{name}"
end
Expand Down
10 changes: 3 additions & 7 deletions app/models/concerns/with_parent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,13 @@ def orphan?
end

def navigable_name
with_parent_name { "#{number}. #{name}" }
defaulting_name { super }
end

private

def with_parent_name
if orphan?
name
else
yield
end
def defaulting_name(&block)
parent.defaulting(name, &block)
end
end

8 changes: 4 additions & 4 deletions app/models/exercise.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ class Exercise < ActiveRecord::Base
include WithSearch,
WithTeaser,
WithAssignments,
ExerciseNavigation,
WithLocale,
WithLanguage,
WithLayout,
Submittable,
Queriable,
FriendlyName

include Submittable, Queriable
include Navigable, WithParent, ExerciseNavigation

after_initialize :defaults, if: :new_record?

validates_presence_of :name, :description, :language,
Expand All @@ -35,7 +35,7 @@ def extra
end

def friendly
with_parent_name { "#{parent.friendly} - #{name}" }
defaulting_name { "#{parent.friendly} - #{name}" }
end

def new_solution
Expand Down
17 changes: 4 additions & 13 deletions app/models/guide.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,20 @@ class Guide < ActiveRecord::Base
include WithSearch,
WithTeaser,
WithLocale,
GuideNavigation,
WithExercises,
WithStats,
WithExpectations,
WithExercises,
WithLanguage,
FriendlyName

belongs_to :language
include WithParent, Navigable, GuideNavigation

validates_presence_of :slug

markdown_on :description, :teaser, :corollary

has_one :chapter_guide

#Deactivate STI, so I can use type attribute
self.inheritance_column = nil

enum type: [ :learning, :practice ]
Expand All @@ -37,16 +36,8 @@ def new?
created_at > 7.days.ago
end

def done_for?(user)
stats_for(user).done?
end

def friendly
with_parent_name { "#{parent.friendly}: #{name}" }
end

def position
chapter_guide.try &:number
defaulting_name { "#{parent.friendly}: #{name}" }
end

def import!
Expand Down
2 changes: 1 addition & 1 deletion app/views/chapters/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= content_for :breadcrumbs do %>
<%= path_breadcrumb @chapter %>
<%= breadcrumbs @chapter %>
<% end %>

<div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/exercises/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= content_for :breadcrumbs do %>
<%= exercise_breadcrumb @exercise %>
<%= breadcrumbs @exercise %>
<% end %>
<% @stats = @exercise.guide.stats_for(current_user) if current_user && @exercise.guide %>
Expand Down
1 change: 0 additions & 1 deletion app/views/guides/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
<p class="text-left">
<%# exercise_status_icon(it) %>
<strong><%= link_to_path_element it %></strong>
<%= with_classifications(it) %>
</p>
</div>
<div class="col-md-2">
Expand Down
2 changes: 1 addition & 1 deletion app/views/guides/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<%= content_for :breadcrumbs do %>
<%= guide_breadcrumb @guide %>
<%= breadcrumbs @guide %>
<% end %>

<div class="row">
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/_progress_bar.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="progress-list-flex">
<% guide.exercises.each do |e| %>
<a href="<%= exercise_path(e)%>" class="text-center <%= class_for_bar(e.status_for(current_user))%> <%= active_class(e, actual)%>">
<a href="<%= exercise_path(e)%>" class="text-center <%= class_for_status(e.status_for(current_user))%> <%= active_class(e, actual)%>">
</a>
<% end %>
</div>
5 changes: 5 additions & 0 deletions config/initializers/defaulting.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class Object
def defaulting(value, &block)
try(&block) || value
end
end
4 changes: 1 addition & 3 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
require_relative './seeds/languages'
require_relative './seeds/guides'

Book.find_or_create_by(name: 'central') do |it|
it.locale = 'es'
end.switch!
Book.find_or_create_by(name: 'central').switch!

elsif Apartment::Tenant.on? 'central'

Expand Down
6 changes: 6 additions & 0 deletions db/seeds/book_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ class BookBuilder
def initialize
@chapters=[]
@preface = ''
@locale = 'en'
end

def locale(locale)
@locale = locale
end

def preface(preface)
Expand All @@ -17,6 +22,7 @@ def chapter(name)
def build!
book = Book.current
book.preface = @preface
book.locale = @locale
book.rebuild! @chapters
end

Expand Down
1 change: 1 addition & 0 deletions db/seeds/central.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
BookBuilder.build! do |b|
b.locale 'es'
b.preface "\
¿Alguna vez te sucedió estar haciendo una tarea tediosa y repetitiva? ¿Te descrubiste a vos mismo cometiendo\
errores tontos por cansancio o distracción? ¿Te diste cuenta de que estabas perdiendo tiempo valioso en cosas que... podría\
Expand Down
2 changes: 1 addition & 1 deletion spec/helpers/with_breadcrumbs_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
helper WithBreadcrumbs
helper WithLinksRendering

let(:breadcrumb) { exercise_breadcrumb(exercise) }
let(:breadcrumb) { breadcrumbs(exercise) }

context 'standalone exercise' do
let(:exercise) { create(:exercise, name: 'my exercise') }
Expand Down

0 comments on commit f1efce9

Please sign in to comment.