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

URL: warn if key doesn't exist in url drop #5524

Merged
merged 4 commits into from Nov 9, 2016
Merged

URL: warn if key doesn't exist in url drop #5524

merged 4 commits into from Nov 9, 2016

Conversation

ghost
Copy link

@ghost ghost commented Nov 1, 2016

fixes #5332

the tests only cover not throwing a NoMethodError because i can't figure out how to capture the logged message to check if it logged the correct message (minitest's capture_io only works with kernel puts, weirdly enough)

@DirtyF DirtyF added the tests label Nov 2, 2016
@DirtyF
Copy link
Member

DirtyF commented Nov 2, 2016

/cc @jekyll/core

replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze))
begin
replacement = @placeholders.public_send(match.sub(":".freeze, "".freeze))
rescue NoMethodError
Copy link
Member

Choose a reason for hiding this comment

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

Instead of a begin .. rescue, what about checking for the key first, like

template.gsub(%r!:([a-z_]+)!) do |match|
  key = match.sub(":".freeze, "".freeze)
  raise NoMethodError, "No URL template key #{key} exists!" unless @placeholders.key?(key)
  if @placeholders[key].nil?
    "".freeze
  else
    self.class.escape_path(@placeholders[key])
  end
end.gsub(%r!//!, "/".freeze)

Copy link
Author

Choose a reason for hiding this comment

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

hmm, that makes more sense, yeah

Copy link
Author

@ghost ghost Nov 2, 2016

Choose a reason for hiding this comment

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

hmm, by using this approach, it looks like the URLDrop (at least the one i use in my tests) throws an unrelated error because it can't find fallback_data when using key? or #[]

Copy link
Member

Choose a reason for hiding this comment

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

Ah, ok. No problem. Define def fallback_data in UrlDrop as an empty hash.

class UrlDrop
  private
  def fallback_data
    {}
  end
end

@ghost
Copy link
Author

ghost commented Nov 2, 2016

updated according to @parkr's comments

Copy link
Member

@parkr parkr left a comment

Choose a reason for hiding this comment

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

LGTM!

@ghost
Copy link
Author

ghost commented Nov 6, 2016

@jekyll/core is this ok to merge? its been sitting for a while

@parkr
Copy link
Member

parkr commented Nov 9, 2016

@jekyllbot: merge +minor

@jekyllbot jekyllbot merged commit f10c914 into jekyll:master Nov 9, 2016
jekyllbot added a commit that referenced this pull request Nov 9, 2016
@ghost ghost deleted the nomethoderror branch November 9, 2016 22:43
@jekyll jekyll locked and limited conversation to collaborators Jul 11, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

URL: check to see if the key exists or rescue NoMethodError
3 participants