Skip to content

fixes#7

Open
ruslan-369 wants to merge 1 commit into
itcentralstation:masterfrom
ruslan-369:master
Open

fixes#7
ruslan-369 wants to merge 1 commit into
itcentralstation:masterfrom
ruslan-369:master

Conversation

@ruslan-369

Copy link
Copy Markdown

No description provided.

Comment thread app/models/article.rb
end No newline at end of file
# method should return hash or nil (not string)
def article_with_image
figure_matches = body.to_s.match(/<figure.*?>.*?(<img.*?>.*?<\/img>|<img.*?\/>).*?(.*?(<img.*?>.*?<\/img>|<img.*?\/>).*?)*<\/figure>/)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've used regex since it allows to extract the first image only and takes less code for string parsing

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

previous implementation returned string in case of issues - it was not handled on views and it is not the best practice to return hash and string types - or raising errors or nil.

@ruslan-369 ruslan-369 left a comment

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Added comments related to the task 2

Comment thread app/models/article.rb

private

def posting_image_params(html)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

  1. the issue in previous implementation was that if we pass here html with multiple images and some of attributes are missing for first image - then the response is inconsistent: some attr are for first image and missing ones are from next image
  2. also I've included bot '' and "" options for html values which was missing in previous implementation

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

i've removed lambda since it only adds complexity to the code and is not needed

Comment thread app/models/article.rb
end No newline at end of file
# method should return hash or nil (not string)
def article_with_image
figure_matches = body.to_s.match(/<figure.*?>.*?(<img.*?>.*?<\/img>|<img.*?\/>).*?(.*?(<img.*?>.*?<\/img>|<img.*?\/>).*?)*<\/figure>/)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

previous implementation returned string in case of issues - it was not handled on views and it is not the best practice to return hash and string types - or raising errors or nil.

Comment thread app/models/posting.rb
belongs_to :editor, class_name: 'User', foreign_key: 'editor_id'

def article_with_image
return type if type != 'Article'

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've removed this checking and moved out article methods to appropriate class

@@ -1,6 +1,6 @@
-image = posting.article_with_image
-image = posting.respond_to?(:article_with_image) ? posting.article_with_image : nil

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

it fixes the issue with strings we had before: now we have nil or hash

-image = posting.respond_to?(:article_with_image) ? posting.article_with_image : nil
-if image
figure id="img_#{posting.id}"
img src="#{image['src']}" alt="#{image['alt'] || posting.title}" data-image="#{image['data-image']}"

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

we can also use symbol keys instead of string keys in the #article_with_image method response - it is better for ruby code performance

img src="#{image['src']}" alt="#{image['alt'] || posting.title}" data-image="#{image['data-image']}"
.teaser id="teaser_#{posting.id}"
=> posting_snippet(posting) # uses HTML_Truncator gem and calls .html_safe on the output No newline at end of file
=> posting_snippet(posting) # uses HTML_Truncator gem and calls .html_safe on the output

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

posting_snippet helper is not implemented so it will not work, but we can implement it this way:

def posting_snippet(posting)
  posting.body.to_s.html_safe
end

Comment thread app/models/article.rb
end No newline at end of file
# method should return hash or nil (not string)
def article_with_image
figure_matches = body.to_s.match(/<figure.*?>.*?(<img.*?>.*?<\/img>|<img.*?\/>).*?(.*?(<img.*?>.*?<\/img>|<img.*?\/>).*?)*<\/figure>/)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've converted body.to_s to fix issue with nil body values

Comment thread specs/posting_spec.rb
"peerspot.com/image/upload/c_limit,f_auto,q_auto,w_550/bvvrzbv97pp5srg612"\
"le16pv99rg.jpg\" data-image=\"27c79574-7aa7-4eea-8515-d2a128698803.jpg\" alt=\"Spotlight"\
" #3 - a community digest\"></figure>\r\n<p><strong><br></strong></p>\r\n<h2>Trending"\
" #3 - a community digest\"/></figure>\r\n<p><strong><br></strong></p>\r\n<h2>Trending"\

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

I've added correct ending of the tag '/>' (before it was just '>')

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

btw i'm handling both tag style cases in article_with_image implementation:

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.

1 participant