Skip to content

Commit

Permalink
Merge pull request nodunayo#567 from hilarysk/hilarysk/issue-147-grac…
Browse files Browse the repository at this point in the history
…efully-handle-non-existent-tags

Issue nodunayo#147 - Gracefully handle non-existent tags
  • Loading branch information
nodunayo committed May 8, 2024
2 parents 7ff5e76 + 0c7f535 commit 1f84348
Show file tree
Hide file tree
Showing 9 changed files with 82 additions and 3 deletions.
4 changes: 4 additions & 0 deletions app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class TagsController < ApplicationController
def index
@tags = ActsAsTaggableOn::Tag.order(name: :asc)
end

def show
@tag = ActsAsTaggableOn::Tag.find_by(name: params[:id])
@proposals = Proposal.tagged_with(@tag.name)
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<li><%= link_to 'Speakers', speakers_path %></li>
<li><%= link_to 'Proposals', proposals_path %></li>
<li><%= link_to 'Events', events_path %></li>
<li><%= link_to 'Tags', tags_path %></li>
</div>
</ul>
</div>
Expand Down
29 changes: 29 additions & 0 deletions app/views/tags/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<% tags_columns = @tags.in_groups(4, false) %>

<p>New tags are created by adding them to a proposal.</p>

<div class="row">
<div class="three columns">
<% tags_columns[0].each do |tag| %>
<p><%= link_to "##{tag.name}", tag_path(tag.name) %></p>
<% end %>
</div>

<div class="three columns">
<% tags_columns[1].each do |tag| %>
<p><%= link_to "##{tag.name}", tag_path(tag.name) %></p>
<% end %>
</div>

<div class="three columns">
<% tags_columns[2].each do |tag| %>
<p><%= link_to "##{tag.name}", tag_path(tag.name) %></p>
<% end %>
</div>

<div class="three columns">
<% tags_columns[3].each do |tag| %>
<p><%= link_to "##{tag.name}", tag_path(tag.name) %></p>
<% end %>
</div>
</div>
8 changes: 7 additions & 1 deletion app/views/tags/show.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
<h5>Proposals tagged with #<%= @tag.name.gsub(" ", "") %></h5>
<% tag_name = "##{@tag.name.gsub(" ", "")}" %>

<h5>Proposals tagged with <%= tag_name %></h5>

<% if @proposals.length.zero? %>
<p>There are no proposals tagged with <%= tag_name %>. <%= link_to "Add yours now!", new_proposal_path %></p>
<% end %>
<% @proposals.each do |proposal| %>
<p><%= link_to proposal.title, proposal %></p>
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
resources :submissions
resources :events, only: [:index, :show]
resources :event_instances, only: [:new, :create, :show]
resources :tags, only: [:show]
resources :tags, only: [:index, :show]
end
2 changes: 1 addition & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2022_12_16_163229) do
ActiveRecord::Schema[7.1].define(version: 2022_12_16_163229) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down
15 changes: 15 additions & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
Event.destroy_all
EventInstance.destroy_all
Speaker.destroy_all
ActsAsTaggableOn::Tag.destroy_all

20.times { Speaker.create!(name: Faker::Name.unique.name) }
puts "20 speakers added..."
Expand All @@ -28,11 +29,25 @@
end
puts "20 proposals added..."

["ruby", "javascript", "bookclub", "interactive", "oop", "objects", "rails", "junior", "culture", "management"].each do |tag|
ActsAsTaggableOn::Tag.create!(name: tag)
end
puts "10 tags added..."

tag_names = ActsAsTaggableOn::Tag.pluck(:name)

Proposal.all.each do | proposal |
proposal.tag_list.add(tag_names.sample)
proposal.save!
end
puts "Proposals each tagged with one tag..."

Proposal.ids.each do | id |
event_instance = EventInstance.ids.sample(2)
Submission.create!(result: [ 0, 1, 2 ].sample, proposal_id: id, event_instance_id: event_instance[0])
Submission.create!(result: [ 0, 1, 2 ].sample, proposal_id: id, event_instance_id: event_instance[1])
end
puts "2 submissions for each proposal added..."

puts "Finished seeding the development database!"
end
19 changes: 19 additions & 0 deletions spec/controllers/tags_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
require 'rails_helper'

RSpec.describe TagsController do
describe 'GET #index' do
context 'when viewing the Tags#index page' do
before do
create(:tag, name: "ruby")
create(:tag, name: "python")
create(:tag, name: "activerecord")

get :index
end

it 'lists the tags in alphabetical order' do
expect(assigns(:tags).pluck(:name)).to eq ["activerecord", "python", "ruby"]
end
end
end
end
5 changes: 5 additions & 0 deletions spec/factories/tag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
FactoryBot.define do
factory :tag, class: ActsAsTaggableOn::Tag do
name { Faker::Name.name }
end
end

0 comments on commit 1f84348

Please sign in to comment.