Skip to content

Commit

Permalink
[ifmeorg#1713] refactored code to shorten resources method
Browse files Browse the repository at this point in the history
  • Loading branch information
S-Warmenhoven committed Apr 9, 2020
1 parent 319fab3 commit 391ca8a
Showing 1 changed file with 24 additions and 14 deletions.
38 changes: 24 additions & 14 deletions app/services/resource_recommendation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand All @@ -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
Expand Down

0 comments on commit 391ca8a

Please sign in to comment.