diff --git a/app/helpers/protips_helper.rb b/app/helpers/protips_helper.rb index 2aee2226..8c50f848 100644 --- a/app/helpers/protips_helper.rb +++ b/app/helpers/protips_helper.rb @@ -1,6 +1,11 @@ require 'cfm' + module ProtipsHelper + def protip_search_results_to_render(protips) + (protips.results if protips.respond_to?(:results)) || protips.try(:all) + end + def protip_summary "A protip by #{@protip.user.username} about #{@protip.topics.to_sentence}." end @@ -296,12 +301,12 @@ def protip_stat_class(protip) def formatted_best_stat_value(protip) value = - case best_stat_name(protip).to_sym - when :views - views_stat_value(protip) - else - best_stat_value(protip) - end + case best_stat_name(protip).to_sym + when :views + views_stat_value(protip) + else + best_stat_value(protip) + end number_to_human(value, units: {unit: "", thousand: "k"}) end diff --git a/app/views/protips/_grid.html.haml b/app/views/protips/_grid.html.haml index abba375a..2ce04229 100644 --- a/app/views/protips/_grid.html.haml +++ b/app/views/protips/_grid.html.haml @@ -1,30 +1,31 @@ --reset_protip_result_index --show_job = defined?(opportunity) && !opportunity.nil? +- reset_protip_result_index +- show_job = defined?(opportunity) && opportunity.present? - width = (defined?(width) && width) || 4 -- show_ad_every = width*2+width-2 -- items = show_job ? protips.each_slice(show_ad_every).map{|row| row.count == show_ad_every ? row.concat(['show-ad', nil]) : row }.flatten : protips +- show_ad_every = width * 2 + width - 2 +- items = show_job ? protips.each_slice(show_ad_every).map{ |row| row.count == show_ad_every ? row.concat(['show-ad', nil]) : row }.flatten : protips -- if collection.failure? +- logger.ap(collection, :debug) +- if collection.respond_to?(:failure?) && collection.failure? .error - ="#{collection.error} Meanwhile you can " - =link_to 'create a pro tip', new_protip_path + = "#{collection.error} Meanwhile you can " + = link_to 'create a pro tip', new_protip_path = "about #{@topic}" unless @topic.nil? --items.in_groups_of(width, nil).each_with_index do |group, row| - -if (row > 0) && blur_protips? +- items.in_groups_of(width, nil).each_with_index do |group, row| + - if (row > 0) && blur_protips? .blurred-tips - =render :partial => "sessions/join_buttons", :locals => {:message => "Join to start earning badges for your open source contributions, discover protips and connect with other developers"} - -break + = render(partial: 'sessions/join_buttons', locals: { message: 'Join to start earning badges for your open source contributions, discover protips and connect with other developers' }) + - break %ul.protips-grid.cf - -group.each do |protip| - -if protip == 'show-ad' - =render :partial => 'opportunities/mini', :locals => {:opportunity => opportunity} - -elsif !protip.nil? + - group.each do |protip| + - if protip == 'show-ad' + = render(partial: 'opportunities/mini', locals: { opportunity: opportunity }) + -elsif protip.present? - if protip = protip.load rescue nil # HACK: User deleted, protip no longer exists. Won't be found. - %li{:class => (protip.kind == 'link' ? 'ext-link' : '') } - = render :partial => 'protips/mini', :locals => {:protip => protip, :mode => mode} + %li{ class: (protip.kind == 'link' ? 'ext-link' : '') } + = render(partial: 'protips/mini', locals: { protip: protip, mode: mode }) - unless collection.nil? || !collection.respond_to?(:next_page) || collection.next_page.nil? || hide_more - - next_url = url_for(params.merge(:tags=> params[:tags], :q => params[:q], :source => params[:action], :controller =>params[:controller], :page => collection.current_page + 1, :section => (defined?(section) ? section : nil), :width => width, :mode => mode )) - %div{:class => ("#{number_to_word(width)}-cols-more" unless width.nil?)} - #more=link_to 'More', next_url, :class => 'final-more track' , :rel => 'next', :remote => true, :method => (params[:action] == "search" ? :post : :get), 'data-action' => 'more protips', 'data-from' => params[:action] + - next_url = url_for(params.merge(tags: params[:tags], q: params[:q], source: params[:action], controller:params[:controller], page: collection.current_page + 1, section: (defined?(section) ? section : nil), width: width, mode: mode )) + %div{ class: ("#{number_to_word(width)}-cols-more" unless width.nil?) } + #more= link_to('More', next_url, class: 'final-more track', rel: 'next', remote: true, method: (params[:action] == 'search' ? :post : :get), 'data-action' => 'more protips', 'data-from' => params[:action]) diff --git a/app/views/protips/topic.html.haml b/app/views/protips/topic.html.haml index 23e230c0..6ae5da79 100644 --- a/app/views/protips/topic.html.haml +++ b/app/views/protips/topic.html.haml @@ -1,33 +1,25 @@ --#-content_for :javascript do --# -if @topic_user --# %script="logUsage('viewed', 'users protips');" --# -else --# %script="logUsage('viewed', 'topics protips');" - -= render :partial => "head", :locals => {:topic => @topic} += render(partial: 'head', locals: { topic: @topic }) .secondary-notice %p - -if @topic_user - -if @topic_user && signed_in? && @topic_user == current_user + - if @topic_user + - if @topic_user && signed_in? && @topic_user == current_user These are your links, code snippets, posts and other pro tips you have created or upvoted. Have a quick pro tip to - =link_to('share?', new_protip_path, 'data-action' => 'create protip', 'data-from' => 'own protips page') + = link_to('share?', new_protip_path, 'data-action' => 'create protip', 'data-from' => 'own protips page') or - =link_to('learn more', faq_path, :class => 'track', 'data-action' => 'view faq', 'data-from' => 'own protips page') + = link_to('learn more', faq_path, class: 'track', 'data-action' => 'view faq', 'data-from' => 'own protips page') - if @topic_user.protips.reject(&:created_automagically?).count == 0 %br Pro tips you currently see here are created automagically from repos you follow/consider useful or slideshares you've created - -else - ==These are the links, code snippets, posts, and other pro tips #{@topic_user.display_name} has created. Have your own to - =link_to('share?', new_protip_path, 'data-action' => 'create protip', 'data-from' => 'user protips page') + - else + == These are the links, code snippets, posts, and other pro tips #{@topic_user.display_name} has created. Have your own to + = link_to('share?', new_protip_path, 'data-action' => 'create protip', 'data-from' => 'user protips page') or - =link_to('learn more', faq_path, :class => 'track', 'data-action' => 'view faq', 'data-from' => 'user protips page') - -elsif signed_in? - %p - =link_to('Share', new_protip_path(:topics => @topic), 'data-action' => 'create protip', 'data-from' => 'user protips page') + " a link, code snippet, post or other pro tip about #{@topic}" - -else - =link_to('Sign in', root_path) - ==to start sharing your #{@topic} pro tips or - =link_to('learn more', faq_path, :class => 'track', 'data-action' => 'view faq', 'data-from' => 'user protips page') + = link_to('learn more', faq_path, class: 'track', 'data-action' => 'view faq', 'data-from' => 'user protips page') + - elsif signed_in? + %p= link_to('Share', new_protip_path(topics: @topic), 'data-action' => 'create protip', 'data-from' => 'user protips page') + " a link, code snippet, post or other pro tip about #{@topic}" + - else + = link_to('Sign in', root_path) + == to start sharing your #{@topic} pro tips or + = link_to('learn more', faq_path, class: 'track', 'data-action' => 'view faq', 'data-from' => 'user protips page') -#browse-results - = render :partial => "grid", :locals => {:protips => (@protips.results if @protips.respond_to?(:results)) || @protips.try(:all), :collection => @protips, :url => :tagged_protips_path, :hide_more => false, :mode => 'popup'} +#browse-results= render(partial: 'grid', locals: { protips: protip_search_results_to_render(@protips), collection: @protips, url: :tagged_protips_path, hide_more: false, mode: 'popup' }) diff --git a/spec/helpers/protips_helper_spec.rb b/spec/helpers/protips_helper_spec.rb index b4d9edfd..98408129 100644 --- a/spec/helpers/protips_helper_spec.rb +++ b/spec/helpers/protips_helper_spec.rb @@ -1,6 +1,7 @@ -require 'spec_helper' - -RSpec.describe ProtipsHelper, :type => :helper do - - -end \ No newline at end of file +RSpec.describe ProtipsHelper, type: :helper do + describe ".protip_search_results_to_render" do + it 'has no protips to render' do + expect(helper.protip_search_results_to_render(nil)).to be_nil + end + end +end