Skip to content

Commit

Permalink
Partners have URL slugs
Browse files Browse the repository at this point in the history
Generating and finding scopes. Updated contoller/views/helpers

With tests.
  • Loading branch information
ivankocienski committed Sep 19, 2024
1 parent 563efee commit 563283f
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/controllers/partners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ def previous_events
private

def find_partner_for_param
@partner = Partner.where(id: params[:id]).first
@partner = Partner.with_slug(params[:id]).first
return if @partner.present?

@message = "Could not find partner with that ID"
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/events_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def link_to_event_instance_with_time_and_partner(event_instance)
event = event_instance.event
time_part = fancy_time_format(event_instance.starts_at)
event_name_part = link_to(event.name, event_path(event_instance.id))
partner_part = link_to("<em>#{event.organizer.name}</em>".html_safe, partner_path(event.organizer))
partner_part = link_to("<em>#{event.organizer.name}</em>".html_safe, partner_path(event.organizer.slug))
"<p><span class='time'>#{time_part}</span> #{event_name_part} &mdash; #{partner_part}</p>".html_safe
end

Expand Down
11 changes: 11 additions & 0 deletions app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class Partner < ApplicationRecord
.where(partner_keywords: { keyword_id: keyword.id })
}

scope :with_slug, lambda { |slug_text|
return none unless slug_text =~ /^(\d+)[\w\-]*$/

where id: $1
}

def slug
([ id ] + name.downcase.split(/\W+/)).join('-')
end

class PartnerAddress
attr_reader :street_address
attr_reader :post_code
Expand Down Expand Up @@ -63,4 +73,5 @@ def address
def contact
@contact ||= PartnerContact.new(self)
end

end
2 changes: 1 addition & 1 deletion app/views/events/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
on #{fancy_date_format(@event_instance.starts_at)}

%p
By #{link_to @event.organizer.name, partner_path(@event.organizer)}
By #{link_to @event.organizer.name, partner_path(@event.organizer.slug)}
- if @event.address.present?
&mdash; (#{@event.address.to_s})

Expand Down
2 changes: 1 addition & 1 deletion app/views/partners/index.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

- @partners.each do |partner|
%article.partner
%h3= link_to partner.name, partner_path(partner.id)
%h3= link_to partner.name, partner_path(partner.slug)
%p
= partner.summary
- if partner.partner_keywords.any?
Expand Down
4 changes: 1 addition & 3 deletions app/views/partners/previous_events.html.haml
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
- content_for(:title) { "Previous events by #{@partner.name} - Partner" }

%h1 Previous events by #{link_to(@partner.name, partner_path(@partner.id))}

-# %p= link_to 'Show upcoming events', partner_path(@partner.id)
%h1 Previous events by #{link_to(@partner.name, partner_path(@partner.slug))}

- if @previous_event_instances.present?
%h2 Events before #{fancy_date_format(@today)}
Expand Down
2 changes: 1 addition & 1 deletion app/views/partners/show.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,4 @@
- if @previous_events.any?
%br
%br
%h3= link_to "See #{@previous_events.count} previous events", previous_events_partner_path(@partner)
%h3= link_to "See #{@previous_events.count} previous events", previous_events_partner_path(@partner.slug)
31 changes: 31 additions & 0 deletions spec/models/partner_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,35 @@
# the use of both filters simultaneously - but this
# could be tested in an integration test

context 'slugging' do
context '#slug' do
it 'generates a slug from the name/id' do
p1 = Partner.create!(name: 'A partner name', placecal_id: 100)
expect(p1.slug).to eq "#{p1.id}-a-partner-name"

# deals with non-word-characters
p2 = Partner.create!(name: 'A?! partner123 name $^; with~bad"letters', placecal_id: 101)
expect(p2.slug).to eq "#{p2.id}-a-partner123-name-with-bad-letters"
end
end

context '::with_slug' do
it 'finds partners based on slug' do

new_partner = Partner.create!(name: 'A partner name', placecal_id: 100)

# find by full slug
found_partner_1 = Partner.with_slug("#{new_partner.id}-a-partner-name").first
expect(found_partner_1).to eq new_partner

# with just ID
found_partner_2 = Partner.with_slug(new_partner.id.to_s).first
expect(found_partner_2).to eq new_partner

# finds nothing without the ID bit
found_partner_3 = Partner.with_slug('a-partner-name').first
expect(found_partner_3).to be nil
end
end
end
end

0 comments on commit 563283f

Please sign in to comment.