Skip to content

Commit 8440517

Browse files
authored
Merge pull request codetriage#1796 from codetriage/avcwisesa-topics_view
Update hacktoberfest logic
2 parents e164a72 + cc84a7e commit 8440517

File tree

9 files changed

+68
-82
lines changed

9 files changed

+68
-82
lines changed

app/controllers/pages_controller.rb

Lines changed: 3 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -51,46 +51,7 @@ def index
5151
end
5252
end
5353

54-
def topic
55-
topic_name = params['topic']
56-
57-
if not ['hacktoberfest'].include?(topic_name)
58-
redirect_back fallback_location: root_path
59-
return
60-
end
61-
62-
set_title("Open Source Project Topic: #{topic_name}")
63-
set_description(description)
64-
65-
label = Label.find_by(name: topic_name)
66-
67-
@repos = Repo.with_some_issues
68-
.includes(:repo_labels)
69-
.where(repo_labels: { label_id: label.id })
70-
.select(:id, :updated_at, :issues_count, :language, :full_name, :name, :description)
71-
72-
topic_repo_ids = @repos.map { |repo| repo.id }
73-
74-
@repos = @repos.without_user_subscriptions(current_user.id) if user_signed_in?
75-
@repos = @repos.order_by_issue_count.page(valid_params[:page]).per_page(valid_params[:per_page] || 50)
76-
77-
if user_signed_in?
78-
@repos_subs = current_user.repo_subscriptions.page(valid_params[:page]).per_page(valid_params[:per_page] || 50).includes(:repo)
79-
@repos_subs = @repos_subs.where(repo_id: topic_repo_ids)
80-
end
81-
82-
respond_to do |format|
83-
format.html {}
84-
format.json do
85-
htmlForPage = render_to_string(partial: "repos_with_pagination", locals: { repos: @repos }, formats: ['html'])
86-
render json: { html: htmlForPage }.to_json
87-
end
88-
end
89-
end
90-
91-
private
92-
93-
def description
54+
private def description
9455
Rails.cache.fetch("pages#index/description", expires_in: 1.hour) do
9556
"Discover the easiest way to get started contributing to open source. " \
9657
"Over #{number_with_delimiter(User.count, delimiter: ',')} devs are " \
@@ -99,11 +60,11 @@ def description
9960
end
10061
end
10162

102-
def valid_params
63+
private def valid_params
10364
params.permit(:language, :per_page, :page)
10465
end
10566

106-
def set_cache_headers
67+
private def set_cache_headers
10768
response.headers["Cache-Control"] = "no-cache, no-store"
10869
response.headers["Pragma"] = "no-cache"
10970
response.headers["Expires"] = "Fri, 01 Jan 1990 00:00:00 GMT"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
class TopicsController < ApplicationController
4+
TOPICS = ["hacktoberfest".html_safe].freeze
5+
before_action :set_topic
6+
7+
def show
8+
set_title("Open Source Project Topic: #{@topic}")
9+
set_description("Open source repos labeled #{@topic}")
10+
11+
label = Label.find_by(name: @topic)
12+
@repos = Repo.with_some_issues
13+
.joins(:repo_labels)
14+
.where(repo_labels: { label_id: label.id })
15+
.select(:id, :issues_count, :language, :full_name, :name, :description)
16+
17+
@repos = @repos.order_by_issue_count.page(valid_params[:page]).per_page(valid_params[:per_page] || 50)
18+
end
19+
20+
private def set_topic
21+
topic_name = valid_params['id'].downcase
22+
@topic = TOPICS.detect { |t| t == topic_name }
23+
24+
if @topic.blank?
25+
redirect_back fallback_location: root_path, notice: "Invalid topic #{topic_name.inspect} not found in #{TOPICS.map(&:inspect).join(", ")}"
26+
end
27+
end
28+
29+
private def valid_params
30+
params.permit(:id, :per_page, :page)
31+
end
32+
end

app/views/application/_nav.html.slim

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,13 @@ nav.application-navigation
1212
li.application-navigation-list-item= image_tag current_user.avatar_url, class: 'user-profile-image', alt: 'user avatar'
1313
li.application-navigation-list-item= link_to current_user.github, current_user
1414
li.application-navigation-list-item= link_to 'University', university_index_path
15-
li.application-navigation-list-item
16-
div.dropdown
17-
= content_tag 'a', 'Topics'
18-
div.dropdown-content
19-
=link_to 'Hacktoberfest', '/topics/hacktoberfest'
15+
li.application-navigation-list-item= link_to 'Hacktoberfest', topic_path(id: "hacktoberfest")
2016
li.application-navigation-list-item= link_to "Submit Repo", new_repo_path
2117
li.application-navigation-list-item= link_to "Sign Out", destroy_user_session_path, method: :delete
2218
- else
2319
li.application-navigation-list-item= link_to 'About', what_path
2420
li.application-navigation-list-item= link_to 'University', university_index_path
25-
li.application-navigation-list-item
26-
div.dropdown
27-
= content_tag 'a', 'Topics'
28-
div.dropdown-content
29-
=link_to 'Hacktoberfest', '/topics/hacktoberfest'
21+
li.application-navigation-list-item= link_to 'Hacktoberfest', topic_path(id: "hacktoberfest")
3022
li.application-navigation-list-item= link_to 'Log in', user_github_omniauth_authorize_path, method: :post
3123
li.application-navigation-list-item= link_to 'Sign Up', user_github_omniauth_authorize_path, method: :post
3224

@@ -35,7 +27,6 @@ div.application-logo
3527
span.application-logo-image
3628
span.hide-visually Code Triage Home
3729

38-
3930
scss class="class":
4031

4132
.dropdown:hover .dropdown-content {

app/views/pages/topic.html.slim

Lines changed: 0 additions & 22 deletions
This file was deleted.

app/views/topics/show.html.slim

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
.hero
2+
h1.hero-title-primary Repos with label: #{@topic}
3+
4+
- unless user_signed_in?
5+
= link_to user_github_omniauth_authorize_path, class: 'button', method: :post
6+
| Sign up with GitHub
7+
8+
hr.section-break
9+
10+
.repos-list-wrapper
11+
h2.repos-list-title All repos with with label #{@topic}.
12+
13+
section.repo-list-with-pagination
14+
= render "pages/repos_with_pagination", repos: @repos

config/routes.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@
2727
end
2828

2929
root to: "pages#index"
30-
31-
get "/topics/:topic", to: "pages#topic"
30+
resources :topics, only: [:show]
3231

3332
get 'what' => "pages#what"
3433
get 'privacy' => "pages#privacy"

db/seeds.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434

3535
raise "DocMethods not saved" if DocMethod.count.zero?
3636

37+
hacktoberfest_label = Label.where(name: "hacktoberfest").first_or_create!
38+
3739
100.times do
3840
printf "."
3941
begin
@@ -56,6 +58,8 @@
5658
)
5759
repo.save(validate: false)
5860

61+
repo.repo_labels.where(label: hacktoberfest_label).first_or_create!
62+
5963
repo.subscribers << user
6064

6165
rand(10).times do |i|

test/functional/pages_controller_test.rb

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,4 @@ class PagesControllerTest < ActionController::TestCase
2626
assert_equal("no-cache", response.headers["Pragma"])
2727
assert_equal("Fri, 01 Jan 1990 00:00:00 GMT", response.headers["Expires"])
2828
end
29-
30-
test "redirect invalid topic" do
31-
get :topic, params: { topic: 'test' }
32-
assert_redirected_to root_url
33-
end
3429
end
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# frozen_string_literal: true
2+
3+
require 'test_helper'
4+
5+
class TopicsControllerTest < ActionController::TestCase
6+
include Devise::Test::ControllerHelpers
7+
8+
test "redirect invalid topic" do
9+
get :show, params: { id: 'test' }
10+
assert_redirected_to root_url
11+
end
12+
end

0 commit comments

Comments
 (0)