Navigation Menu

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

distance_of_time_in_words returns a string instead of text element #781

Merged
merged 9 commits into from Jun 1, 2019

Conversation

edwardloveall
Copy link
Member

Purpose

closes #780

Description

Returning a text element (which uses the IO class) can cause weird bugs, like the entire HTML source of the page being printed out into the page. This alters the distance_of_time_in_words method to return a string instead which can be inserted into a text element, as an attribute of a different element, or even outside of a view entirely!

Checklist

  • - An issue already exists detailing the issue/or feature request that this PR fixes
  • - All specs are formatted with crystal tool format spec src
  • - Inline documentation has been added and/or updated
  • - Lucky builds on docker with ./script/setup
  • - All builds and specs pass on docker with ./script/test

@paulcsmith
Copy link
Member

I think before merging this in wed also want to change a bunch of the other helpers methods that write directly to the IO. Otherwise some will work differently than others. Stuff like pluralize https://github.com/luckyframework/lucky/blob/master/src/lucky/page_helpers/text_helpers.cr

Some use raw so it doesn’t make sense to convert. Those should be documented that they write directly to IO though and should probably use a Nil return type so people don’t try to use them

@edwardloveall
Copy link
Member Author

Yeah, makes sense. I'll do that in a bit 👍

@paulcsmith
Copy link
Member

Nice branch name 😃

CHANGELOG.md Outdated
@@ -1,6 +1,7 @@
### Changes since v0.13

- Change `Lucky::BaseApp` to `Lucky::BaseAppServer`
- `time_ago_in_words` returns a `String` instead of an `text` element (IO)
Copy link
Member

Choose a reason for hiding this comment

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

0.14 has already been released, so this would more likely go under ### Changes since v0.14. However, with that said, maybe all changelog changes can just go in a single commit? Kind of a pain to update all at once, but it helps to know what docs will need to be updated and such..

The goal is to make these easier to use inside other tags. The problem
was that these would all output to the IO, so if you tried to use these
in an element, it would print out the whole page HTML.
@edwardloveall
Copy link
Member Author

I made some progress on this. However, I'm not sure how to approach truncate because of its block overload. I'm not sure how to get it to run the block and then safely add the result onto the end of the truncated string. Here's my test case from crystal play:

require "./src/lucky"
require "./spec/support/context_helper"
include ContextHelper

class TextHelperTestPage
  include Lucky::HTMLPage

  def render
  end
end

view = TextHelperTestPage.new(build_context)

view.link "#" do
  text = view.truncate("Here is a long test and I need a continue to read link", length: 27) do
    view.span "Continue"
  end.to_s
  view.text text
end.to_s

This returns some extra HTML because it's grabbing the existing HTML in the truncate method. I'd love an assist.

@paulcsmith
Copy link
Member

I think that is a pretty tricky edge case. Maybe for now we make the block version a different method. Like raw_truncate or something. What do you think?

@edwardloveall
Copy link
Member Author

Yeah I like that. There's a private method called truncate_text that I think will do what we want. I'll make that public and write some docs.

@edwardloveall
Copy link
Member Author

Cool, should be ready to go unless you want me to move the changelog stuff.

Copy link
Member

@paulcsmith paulcsmith left a comment

Choose a reason for hiding this comment

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

2 small things actually :D then ready to go!

# outputs:
# ```html
# You're such a <strong>nice</strong> and <strong>attractive</strong> person.
# ```
def highlight(text : String, phrases : Array(String | Regex), highlighter : Proc | String = "<mark>\\1</mark>")
Copy link
Member

Choose a reason for hiding this comment

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

Actually, could you make the return types : Nil for any of the methods that are meant to write HTML directly to the IO so that it won't ever accidentally be used as a String? Might also be worth adding a note like: This method writes HTML directly to the page. It does not return a String or something like that. Thoughts?

CHANGELOG.md Outdated
@@ -1,6 +1,7 @@
### Changes since v0.13

- Change `Lucky::BaseApp` to `Lucky::BaseAppServer`
- Many text helpers now return a `String` instead of appending to the view (`cycle`, `excerpt`, `highlight`, `pluralize`, `time_ago_in_words`, `to_sentence`, `word_wrap`) [#781](https://github.com/luckyframework/lucky/pull/781)
Copy link
Member

Choose a reason for hiding this comment

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

Might be worth putting in bold **Breaking change** - Many text... since this will definitely break stuff using it

This change also made the tests have to change since we can no longer
grab the output of the text helper to a string and then compare it with
another string.
@edwardloveall
Copy link
Member Author

@paulcsmith done in 537ee85

@paulcsmith paulcsmith merged commit 740b716 into master Jun 1, 2019
@paulcsmith
Copy link
Member

Perfect! Thank you

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.

Consider returning a string from time_ago_in_words instead of a text element
3 participants