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

Recommended resources #2

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open

Recommended resources #2

wants to merge 43 commits into from

Conversation

S-Warmenhoven
Copy link
Member

Description

Phase 1 complete of adding Resource recommendations to Moments show page based on all Moment attributes, matching with Resource tags.

Corresponding Issue

[ifmeorg#1713]

Screenshots


Reviewing this pull request? Check out our Code Review Practices guide if you haven't already!

Copy link

@guialbuk guialbuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can see a clear and concise solution being built for a complex problem 👍

The files on public/assets folder must be deleted. For some reason this folder is not in .gitignore. You can delete those files then add public/assets to .gitignore

@@ -7,6 +7,7 @@ class MomentsController < ApplicationController
include MomentsFormHelper
include Shared
include TagsHelper
# include PagesConcern

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the commented out code

@@ -0,0 +1,66 @@
# frozen_string_literal: true
class ResourceRecommendation
Copy link

@guialbuk guialbuk Apr 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

resources and initialize (used by .new) are the only methods exposed to other classes, so all other methods must be private.


def match_keywords
all_resources.each do |resource|
resource_tags = resource['tags'].map do |tag|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When you map something and then flats it, you can replace map with flat_map

https://ruby-doc.org/core-2.7.0/Enumerable.html#method-i-flat_map

def modify_keywords
@moment_keywords = @moment_keywords.flatten
@moment_keywords.each do |keyword|
keyword.gsub!(%r{([_@#!%()\-=;><,{}\~\[\]\./\?\"\*\^\$\+\-]+)}, '')

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
keyword.gsub!(%r{([_@#!%()\-=;><,{}\~\[\]\./\?\"\*\^\$\+\-]+)}, '')
keyword.gsub!(/[^a-z]+/i, '')

Instead of passing several special characters, you can match any character that is not a letter

^ menas not
[a-z] means any letter
+ mens one or more
The i at the end means case-insensitive

This is a kind of a JSbin for Relugar expressions. You can test that regex there:
https://rubular.com/r/8NkkIIHtxUBUxv

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like your simpler way better, especially since it would cover any special characters not present in my current regex expression. However, if we only allow a-z characters, would that not affect special alphabetical characters such as é or ñ? This project is available in several languages, including Spanish, which could pose a problem for matching words.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's a good point. Fortunately Ruby has regex support for Unicode characters, such as [[:alpha:]]

https://ruby-doc.org/core-2.7.0/Regexp.html#class-Regexp-label-Character+Classes

app/services/resource_recommendation.rb Outdated Show resolved Hide resolved
@@ -4571,7 +4571,7 @@ debug@=3.1.0:
dependencies:

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This file must be out of the PR. To revert it to the same version on master, you can run:

git checkout origin/master client/yarn.lock

Comment on lines 36 to 40
get_keywords(@moment.categories)
get_keywords(@moment.moods)
get_keywords(@moment.strategies)
@moment_keywords.push(moment_name, moment_why, moment_fix)
modify_keywords

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can move all methods related to extracting keywords from a moment to a separated class. That way the extracting step will be separated from matching. This will make the codebase easier to test and maintain.

Copy link

@guialbuk guialbuk left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nive work 👍

You also need to revert client/yarn.lock to the version in master and remove all files from public/assets

@@ -0,0 +1,45 @@

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty line

Suggested change

Comment on lines 4 to 7
let(:categories) {[FactoryBot.build(:category,
name: "category name",
description: "category description."
)]}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For multiline blocks, the most accepted style is to use do...end

Suggested change
let(:categories) {[FactoryBot.build(:category,
name: "category name",
description: "category description."
)]}
let(:categories) do
FactoryBot.build([
:category,
name: "category name",
description: "category description."
)]
end

name: "strategy-name",
description: "strategy-description."
)]}
subject(:moment) {FactoryBot.build(:moment,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it refers to a class other than the one being tests (MomentKeywords), we usually use let here

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that's what you said over Zoom! Sorry, the audio wasn't good at the time. :)

moods: moods,
strategies: strategies
) }
let(:keywords) { MomentKeywords.new(moment).extract_keywords }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the class being tested, so you can use subject here

Suggested change
let(:keywords) { MomentKeywords.new(moment).extract_keywords }
subject(:keywords) { MomentKeywords.new(moment).extract_keywords }

"self care", "text", "tested", "is", "teachers"]
)
end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the final new line is missing

why: "More testing content self-care.",
fix: "Text tested is @Teachers!!") }

let(:recommended_resources) { ResourceRecommendation.new(moment).resources }

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here. recommended_resources should use subject and moment use let

Suggested change
let(:recommended_resources) { ResourceRecommendation.new(moment).resources }
subject(:recommended_resources) { ResourceRecommendation.new(moment).resources }

end

end
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing newline

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants