Love Models

Posted by BadReligion Tue, 14 Jul 2009 12:11:00 GMT

I wanted a DRY way to set the published_at attribute of a Blog. Remembering the advice of my peers to keep a fat model I decided to stuff the logic there. This could be used for any similar attribute. First decide on a method name that we will call on our instance variable, I decided to use "publish?". So for instance I can call "@blog.publish?", now I know I will need to know whether or not to publish the blog and I am just directly passing in the value of my published check box from params. Be sure to sanitize anything an end user can control, in this case only I use this behind an admin.

Now we need to define this method in the model, in my case "blog.rb". Its easier to show what this method looks like, then explain it.

Class Blog << ActiveRecord::Base
  ...
  def publish?(bool)
    bool ? self.published_at = Time.now : self.published_at = nil
  end
end

We define this method just like any other, we expect a boolean as an input. First we simply set the published_at to Time.now if 1 is passed in (check box is checked) otherwise set published_at to nil, effectively un-publishing the blog. However, we do not want to update the published_at with a new timestamp if the article has already been published. Although this could be dependent on the expected behavior of the application, I personally don't think it makes sense. Reason is I sort articles by published_at, I do not want them popping back up if I go back and edit them. That is what I have an updated_at attribute for.

I often found myself wondering why bother with all this...just slap another conditional in the controller, copy paste, no big deal. However as you add more and more code, the controller quickly gets out of hand and turns into complete anarchy. Refactoring your code and continually enforcing the "fat model" theory will make your controllers (and code) much easier to manage later on. Take it from someone who as learned the hard way!

Thanks for reading... Sean

Posted in  | no comments

Microsoft Mail User Agents

Posted by BadReligion Fri, 10 Jul 2009 20:41:00 GMT

An excerpt from Mailing List Etiquette on FreeBSD.


Please use a standards-compliant Mail User Agent (MUA). A lot of badly formatted messages come from bad mailers or badly configured mailers. The following mailers are known to send out badly formatted messages without you finding out about them:

  • * cc:Mail
  • * Eudora� (older versions)
  • * exmh
  • * Microsoft� Exchange
  • * Microsoft Internet Mail
  • * Microsoft Outlook�
  • * Netscape� (older versions)

As you can see, the mailers in the Microsoft world are frequent offenders. If at all possible, use a UNIX� mailer. If you must use a mailer under Microsoft environments, make sure it is set up correctly. Try not to use MIME: a lot of people use mailers which do not get on very well with MIME.


I just find this hilarious. I mean what exactly defines user friendly? Software that has shiny buttons or that formats its data in a way that complies with standards and is compatible with other users *standard compliant* software?!

Sean

Posted in , ,  | no comments