# Give me a heads-up on my article teaser code

**URL:** <https://www.ruby-forum.com/t/give-me-a-heads-up-on-my-article-teaser-code/82366>\
**Category:** Rails\
**Created:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/give-me-a-heads-up-on-my-article-teaser-code/82366 "2007-01-19T16:29:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [January 19, 2007, 4:29pm UTC](https://www.ruby-forum.com/t/give-me-a-heads-up-on-my-article-teaser-code/82366/1 "2007-01-19T16:29:47Z")

</div>

I’m creating article teasers for my blogs main page and I am simple  
adding  
in my post.rb file the following…

class Post \< ActiveRecord::Base  
has\_many :comments

# Before the form is saved we update the time to be more accurate

# and format post\_text to post\_html using RedCloth/textile

# \*\*\*No need to add text to post\_html field in the form, this happens

automatically  
def before\_save  
self.created\_at = Time.now  
self.author\_name = ‘Nevyn’ if self.author\_name.nil? or  
self.author\_name.empty?  
self.post\_html = RedCloth.new(self.post\_text).to\_html  
end

def teaser  
#return article teaser first paragraph  
teaser = self.post\_html.slice(0, (self.post\_html =~  
/\r?\</p\>\r?$/)).rstrip  
return teaser  
end

end

my db schema has a ‘post\_text’ and ‘post\_html’ column, you can see above  
how  
I am using RedCloth.

But I am wondering since I am new to Ruby, if there is a cleaner way to  
do  
this.

Thanks,

–  
-mike

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [January 19, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/give-me-a-heads-up-on-my-article-teaser-code/82366/2 "2007-01-19T16:30:44Z")

</div>

Michael S. wrote:

> def before\_save  
> return teaser  
> end
> 
> end
> 
> my db schema has a ‘post\_text’ and ‘post\_html’ column, you can see above how  
> I am using RedCloth.
> 
> But I am wondering since I am new to Ruby, if there is a cleaner way to do  
> this.

A few comments:

- You don’t need the line to set the created\_at time - Rails does this  
automatically.
- You can use: if self.author\_name.blank? - nil.blank? is true, and so  
is “”.blank? and " ".blank?
- Instead of self.post\_html.slice(0, regex-match-value), do  
self.post\_html.slice(0…regex-match-value). This way if the regular  
expression is not matched and returns -1, you’ll get the whole content  
of the post instead of nil.
- Instead of doing teaser = …; return teaser, you can just have the  
slice method:  
def teaser  
self.post\_html.slice(0,whatever)  
end

Hope this helps,

Dan M.

---

<div class="post-metadata">

**Author:** ![wickie](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/w/ba9def/32.png) [@wickie](https://www.ruby-forum.com/u/wickie)\
**Post date:** [January 19, 2007, 4:31pm UTC](https://www.ruby-forum.com/t/give-me-a-heads-up-on-my-article-teaser-code/82366/3 "2007-01-19T16:31:25Z")

</div>

On 1/14/07, Dan M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > # Before the form is saved we update the time to be more accurate
> > 
> > def teaser  
> > I am using RedCloth.
> > 
> > But I am wondering since I am new to Ruby, if there is a cleaner way to  
> > do  
> > this.
> 
> A few comments:
> 
> - You don’t need the line to set the created\_at time - Rails does this  
> automatically.

Well, the reason I am using that is if it takes me 10 minutes to write  
the  
article, I don’t get the time since I clicked “Create New Post” I get  
the  
time that the post was actually submitted.  
Guess that really Isn’t a big deal.

- You can use: if self.author\_name.blank? - nil.blank? is true, and so

> is “”.blank? and " ".blank?

- Instead of self.post\_html.slice(0, regex-match-value), do

> self.post\_html.slice(0…regex-match-value). This way if the regular  
> expression is not matched and returns -1, you’ll get the whole content  
> of the post instead of nil.

Thanks for the tip! I should hope that it will always be matched, I mean  
if  
a post is created self.post\_html should have at at least one set of

elements.

- Instead of doing teaser = …; return teaser, you can just have the

> slice method:  
> def teaser  
> self.post\_html.slice(0,whatever)  
> end

Yeah the return teaser part comes from my python background 🙂  
And yes it was a help, Thank you!

Hope this helps,

> Dan M.
> 
> >

–  
-mike
