From 391ca8a711c3912017eb69af8fadae66a69157ca Mon Sep 17 00:00:00 2001 From: stephanie-warmenhoven Date: Wed, 8 Apr 2020 19:15:56 -0700 Subject: [PATCH] [#1713] refactored code to shorten resources method --- app/services/resource_recommendation.rb | 38 ++++++++++++++++--------- 1 file changed, 24 insertions(+), 14 deletions(-) diff --git a/app/services/resource_recommendation.rb b/app/services/resource_recommendation.rb index 886195731a..83d7c97e36 100644 --- a/app/services/resource_recommendation.rb +++ b/app/services/resource_recommendation.rb @@ -4,34 +4,45 @@ def initialize(moment) @moment = moment end + def all_resources + JSON.parse(File.read(Rails.root.join('doc', 'pages', 'resources.json'))) + end + + def moment_name + @moment.name.split + end + + def html_clean(str) + ActionController::Base.helpers.strip_tags(str) + end + + def moment_why + html_clean(@moment.why).split + end + + def moment_fix + html_clean(@moment.fix).split + end + def resources - all_resources = JSON.parse(File.read(Rails.root.join('doc', 'pages', 'resources.json'))) matched_resources = [] moment_keywords = [] - moment_name = @moment.name.split - moment_why = ActionController::Base.helpers.strip_tags(@moment.why).split - moment_fix = ActionController::Base.helpers.strip_tags(@moment.fix).split @moment.categories.each do |category| + category_description = html_clean(category['description']) moment_keywords.push(category['name'].split) - end - @moment.categories.each do |category| - category_description = ActionController::Base.helpers.strip_tags(category['description']) moment_keywords.push(category_description.split) end @moment.moods.each do |mood| + mood_description = html_clean(mood['description']) moment_keywords.push(mood['name'].split) - end - @moment.moods.each do |mood| - mood_description = ActionController::Base.helpers.strip_tags(mood['description']) moment_keywords.push(mood_description.split) end @moment.strategies.each do |strategy| + strategy_description = html_clean(strategy['description']) moment_keywords.push(strategy['name'].split) - end - @moment.strategies.each do |strategy| - strategy_description = ActionController::Base.helpers.strip_tags(strategy['description']) moment_keywords.push(strategy_description.split) end + moment_keywords.push(moment_name, moment_why, moment_fix) moment_keywords = moment_keywords.flatten moment_keywords.each do |keyword| @@ -44,7 +55,6 @@ def resources tag.gsub!(%r{([_@#!%()\-=;><,{}\~\[\]\./\?\"\*\^\$\+\-]+)}, ' ') tag.split end - # binding.pry unless (resource_tags & moment_keywords).empty? matched_resources.push(resource) end