Skip to content

Commit

Permalink
Don't use magic finders on Classifications
Browse files Browse the repository at this point in the history
The friendly_id magic find override module doesn't play nice with Rails STI,
which is a problem for Topics and TopicalEvents. For these models, use the new
recommended method of finding instances based on their slug, which is to go via
the `friendly` scope.

Eventually, all code should be updated to use this instead of the magic find
override module as it isn't very stable and keeps breaking with new releases of
Rails[1]. Doing this is before the upgrade to Rails 4 is complete is going to be
hard however, so deferring until later.

[1] norman/friendly_id#607
  • Loading branch information
tekin committed Feb 11, 2015
1 parent 6919525 commit f1ee01d
Show file tree
Hide file tree
Showing 7 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/admin/about_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def human_friendly_model_name

private
def find_topical_event
@topical_event = TopicalEvent.find(params[:topical_event_id])
@topical_event = TopicalEvent.friendly.find(params[:topical_event_id])
end

def find_page
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/admin/classifications_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def edit
end

def update
@classification = model_class.find(params[:id])
@classification = model_class.friendly.find(params[:id])
if @classification.update_attributes(object_params)
redirect_to [:admin, @classification], notice: "#{human_friendly_model_name} updated"
else
Expand All @@ -33,7 +33,7 @@ def update
end

def destroy
@classification = model_class.find(params[:id])
@classification = model_class.friendly.find(params[:id])
@classification.delete!
if @classification.deleted?
redirect_to [:admin, model_class], notice: "#{human_friendly_model_name} destroyed"
Expand All @@ -51,7 +51,7 @@ def build_object
end

def load_object
@classification = model_class.find(params[:id])
@classification = model_class.friendly.find(params[:id])
end

def model_name
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/offsite_links_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def show
def load_parent
@parent = WorldLocation.find(params[:world_location_id]) if params[:world_location_id]
@parent = Organisation.find(params[:organisation_id]) if params[:organisation_id]
@parent = Topic.find(params[:topic_id]) if params[:topic_id]
@parent = TopicalEvent.find(params[:topical_event_id]) if params[:topical_event_id]
@parent = Topic.friendly.find(params[:topic_id]) if params[:topic_id]
@parent = TopicalEvent.friendly.find(params[:topical_event_id]) if params[:topical_event_id]
end

def load_offsite_link
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/admin/topical_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ class Admin::TopicalEventsController < Admin::ClassificationsController
before_filter :destroy_blank_social_media_accounts, only: [:create, :update]

def update
@classification = model_class.find(params[:id])
@classification = TopicalEvent.friendly.find(params[:id])
if @classification.update_attributes(object_params)
if object_params[:classification_featurings_attributes]
redirect_to [:admin, @classification, :classification_featurings], notice: "Order of featured items updated"
else
redirect_to [:admin, model_class.new], notice: "#{human_friendly_model_name} updated"
redirect_to [:admin, TopicalEvent.new], notice: "#{human_friendly_model_name} updated"
end
else
render action: "edit"
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/topical_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def index
end

def show
@classification = TopicalEvent.find(params[:id])
@classification = TopicalEvent.friendly.find(params[:id])
@policies = @classification.published_policies.includes(:translations, :document)
@publications = fetch_associated(:published_publications, PublicationesquePresenter)
@consultations = fetch_associated(:published_consultations, PublicationesquePresenter)
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/topics_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class TopicsController < ClassificationsController
enable_request_formats show: :atom

def show
@classification = Topic.find(params[:id])
@classification = Topic.friendly.find(params[:id])

respond_to do |format|
format.html do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ebola_response = TopicalEvent.find('ebola-government-response')
ebola_response = TopicalEvent.friendly.find('ebola-government-response')

old_url = ebola_response.search_link
ebola_response.remove_from_search_index
Expand Down

0 comments on commit f1ee01d

Please sign in to comment.