Made some tweaks!

Created: 2020-04-06, Last Updated: 2023-02-04

It took some time, but I got around to put post previews on the homepage and getting twitter cards set up. So here are some learnings to take away.

Post Previews Learnings

I wasn't able to find a method that would just cut up a post neatly. I needed something that wouldn't cut a work in half, so I had to roll my own. What I ended up with was

def post_snippet(char, char_count)
  return self.text if self.text.count(char) <= char_count
  break_point = (0..self.text.size).select {|i| self.text[i] == char}[char_count-1]
  self.text[0..break_point - 1] + "..."
end

This is a model method on my Post model that is taking a character to break on, the occurrence count of that character that the post should be broken on. If the post is too short the post body is returned unaltered. If it is long enough then we pull only the part we want and append "..." to the end.

At first I hardcoded the break character, but then I realized I might want to pull the first sentence or two, so I made it so that I could pass in a period in addition to a space. Also at first I had this in a helper method, but I moved it into a model instance method so that I could use it in the controller as well as the view.

Twitter Cards

This is straight forward. Add the following to your header

<meta name="twitter:card" content="summary" />
<meta name="twitter:creator" content="@danclark" />
<meta name="twitter:title" content="BruisedThumb">
<meta name="twitter:description" content="Learnings through experience">

Those are tags you need, and don't forget to use quotes!

Back