Give me a heads-up on my article teaser code

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

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.

On 1/14/07, Dan M. [email protected] 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 :slight_smile:
And yes it was a help, Thank you!

Hope this helps,

Dan M.


-mike

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs