Modules

2010/05/09 at 1:12 am (Hobo, Programming, Ruby on Rails, WebApplication)

Today I noticed duplication in my models (two models were sharing most of their code and I was about to make a third really similar to them). Instead of making a third model I decided to get rid of duplication.

First I moved permissions to a module and it was easy. After that I wasted some time trying to move lifecycle to module, only to realize that it’s possible but ugly and tricky. I realized I was abusing lifecycles(but I liked it ;) ) and decided to use something else.

I wanted some actions available used two things only to owner and there are two things in Hobo that have names suggesting they are the right things to use for that: permissions and owner actions. I abandoned lifecycles and wrote clean, 36 lines short OwnedModel module. I’m not completely satisfied with it yet because:

  1. I use owner, but I don’t validate presence of one. It’s little problem I’ll fix it with Module::included callback in five minutes.
  2. I can’t get edit and view permissions right. Edit and view must be permitted when creating new record. But when creating new record it’s owner gets changed. So I need to allow viewing records after changing owner. And I’m a bit worried it may be exploited.

But it’s enough for now.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: