From fd1c61ae61c5bfe85b24f2f431cecf97f9ca2a54 Mon Sep 17 00:00:00 2001 From: Eduardo Martinez Echevarria Date: Sun, 11 Jun 2017 13:19:12 +0200 Subject: [PATCH] Add resource_relation to Polymorphic concern This allows suggest action to send search to a relation instead of directly the model. Doing so, the suggested items can be previously filtered. --- app/controllers/concerns/commentable_actions.rb | 2 +- app/controllers/concerns/polymorphic.rb | 4 ++++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/concerns/commentable_actions.rb b/app/controllers/concerns/commentable_actions.rb index 287f2d418..489d1857c 100644 --- a/app/controllers/concerns/commentable_actions.rb +++ b/app/controllers/concerns/commentable_actions.rb @@ -34,7 +34,7 @@ module CommentableActions def suggest @limit = 5 - @resources = @search_terms.present? ? resource_model.search(@search_terms) : nil + @resources = @search_terms.present? ? resource_relation.search(@search_terms) : nil end def create diff --git a/app/controllers/concerns/polymorphic.rb b/app/controllers/concerns/polymorphic.rb index 10d64698d..6ac4b5c1c 100644 --- a/app/controllers/concerns/polymorphic.rb +++ b/app/controllers/concerns/polymorphic.rb @@ -10,6 +10,10 @@ module Polymorphic @resource_name ||= resource_model.to_s.downcase end + def resource_relation + @resource_relation ||= resource_model.all + end + def set_resource_instance instance_variable_set("@#{resource_name}", @resource) end