Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Order events and reduce N+1 on rich text fields #58

Merged
merged 5 commits into from
Oct 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/controllers/admin/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class EventsController < BaseController
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

def index
@events = Event.all.order(created_at: :desc)
@events = Event.with_all_rich_text.all.order(created_at: :desc)
end

def new
Expand Down Expand Up @@ -63,7 +63,7 @@ def destroy

# Use callbacks to share common setup or constraints between actions.
def set_event
@event = Event.find_by!(slug: params[:id])
@event = Event.with_all_rich_text.find_by!(slug: params[:id])
end

# Only allow a list of trusted parameters through.
Expand Down
14 changes: 7 additions & 7 deletions app/controllers/events_controller.rb
Original file line number Diff line number Diff line change
@@ -1,32 +1,32 @@
class EventsController < ApplicationController
# GET /events
def index
@events = Event.upcoming
@events = Event.with_all_rich_text.upcoming
end

# GET /events/all or /events/all.ics
def all
@events = Event.published
@events = Event.with_all_rich_text.published.order(start_at: :desc)

respond_to do |format|
format.html { render :index }
format.ics { render plain: Calendar.new(@events.order(start_at: :asc)).to_ical }
format.ics { render plain: Calendar.new(@events.reorder(start_at: :asc)).to_ical }
end
end

def show
@event = Event.find_by!(slug: params[:slug])
@event = Event.with_all_rich_text.find_by!(slug: params[:slug])
rescue ActiveRecord::RecordNotFound
redirect_to all_events_path, error: "Event not found"
redirect_to all_events_path, error: 'Event not found'
end

def past
@events = Event.past
@events = Event.with_all_rich_text.past
render :index
end

def next
@events = Event.upcoming.first
@events = Event.with_all_rich_text.upcoming.first
render :index
end
end
14 changes: 12 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,19 @@
module ApplicationHelper
def title
"#{content_for(:page_title)} | Toronto Ruby" || 'Toronto Ruby'
text = content_for(:page_title)
if text.present?
"#{text} | Toronto Ruby"
else
'Toronto Ruby'
end
end

def description
content_for(:page_description) || 'Toronto Ruby Meetups'
text = content_for(:page_description)
if text.present?
text
else
'Toronto Ruby Meetups'
end
end
end
4 changes: 4 additions & 0 deletions app/views/admin/events/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,7 @@
<div>
<%= link_to "Back to events", admin_events_path %>
</div>

<% content_for :page_title do %>
Editing <%= @event.name %>
<% end %>
2 changes: 1 addition & 1 deletion app/views/admin/events/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@
</div>

<% content_for :page_title do %>
Admin Home
Admin Home
<% end %>

3 changes: 3 additions & 0 deletions app/views/admin/events/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@
<div>
<%= link_to "Back to events", events_path %>
</div>
<% content_for :page_title do %>
Create new event
<% end %>
4 changes: 4 additions & 0 deletions app/views/admin/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,7 @@
<% end %>

<%= render partial: "layouts/event_layout" %>

<% content_for :page_title do %>
<%= @event.name %>
<% end %>
1 change: 0 additions & 1 deletion app/views/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
<%= @event.name %>
<% end %>


<%= content_for :event_content do %>
<%= render partial: "events/event", locals: { event: @event } %>
<% end %>
Expand Down