hoodwink.d enhanced
RSS
2.0
XHTML
1.0

RedHanded

Handfuls of Rails Nails #

by why in inspect

Once we were only wading to the knee in Rails documentation. Now that the belt buckle is submerged, we all take turns excavating.

ActiveRecord::Base#constrain

I’m in the controller. I want to limit a view to a subset.

  Article.constrain(:conditions => "blog_id = 1") do
    Article.find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1
  end

In combo with permissions, this can be great. Because constrain can take nil.

  constraints = nil
  unless @user.sees_all?
    constraints = {:conditions => ['user_id = ?', @user.id]}
  end

Has anyone coupled this with destroy to nuke a chain of associated rows?

Update: From the comments, we learn that constrain is behind the scoped database calls.

Receiving Through ActionMailer

I want messsages to a certain e-mail address to travel through Rails. The wiki has a very thorough discussion on the topic, including many setup options.

Specifically, here’s a way to handle bounces. In other words, if an e-mail bounces to an address that’s signed up for a login, you could flag the account, giving the user a better reason why their account isn’t active.

  class Inbox < ActionMailer::Base

    def receive(bounce)
      from = bounce.from.first

      if from =~ /MAILER-DAEMON/
        bounce.each_part do |part|
          if part.content_type == "text/rfc822-headers" 
            email = TMail::Mail.parse( part.body )
            user = User.find_by_email( email.to.first )
            user.update_attributes :disabled => true
            return
          end
        end
      end

    end

  end

It’d be good to have TMail in the API docs. But the above should give you an idea of how to parse attachments which contain email headers. And how to cycle attachments.

Forming XHTML from User Input

We’ve talked about this before, but I swear this is the shortest recipe and most cooperative. Assume you have incoming blog content HTML contained in the html_content accessor. Also, require 'htree' in your config/environment.rb.

 def xhtml_content
   HTree.parse( html_content ).display_xml( '' )
 end
said on 02 Nov 2005 at 15:15
Under the sheets contrain is used to do the nifty newish class-methods-on-associations.
# Scoped implicity by the user_id = 1
>> User.find(1).publications.find_by_published(true)

constrain is not so good a name though…

said on 02 Nov 2005 at 15:24

Warning: constrain will likely change to something aggravating like with_finder_options before 1.0 to avoid stepping on the established notion of relational constraints.

said on 02 Nov 2005 at 15:36

When I first read about #constrain, I remember wishing #around_filter worked more like Java’s servlet filters so that the action would execute within the block I provide. Yes, this means the action would have to be passed in and I would have to execute it manually (if I chose to). Any input?

said on 02 Nov 2005 at 15:38

jvoorhis: so basically if you yield inside a controller?

said on 02 Nov 2005 at 15:46

Nice. You know, I keep running across that:


def thing
  blah = ''
  writer(blah)
  blah
end

...pattern lately, and I keep wondering whether there is a prettier way to do it.

said on 02 Nov 2005 at 15:49

Oh, answering my own question.

HTree::Node#display_xml returns out. That means you can write:


def xhtml_content
   HTree.parse( html_content ).display_xml( '' )
end

I don’t know. Is that sufficiently readable?

said on 02 Nov 2005 at 16:04

Sure, updated the snip like so. Thanks, M.guY.

said on 02 Nov 2005 at 16:07

“Has anyone coupled this with destroy to nuke a chain of associated rows?”

That’s what referential integrity is for.

said on 02 Nov 2005 at 17:06

Referential Integrity, Smegferential Finblegrity.

Relational Databases will be first agains the wall when the revolution comes!

said on 02 Nov 2005 at 17:22

“That’s what referential integrity is for.”

And here I thought that’s what abstinence is for.

Comments are closed for this entry.