开发者

STI + namespaces

I need to be able to schedule reminders for users. So I thought I could just create a base model Reminder and use STI to make descendant classes which override the function fire(), called when reminder fires. But different user roles have similar types of reminders. So they need to be namespaced, e.g. Adult::BrushTeethReminder sends an email to user, Kid::BrushTeethReminder posts on kid's FB wall.

Is it possible with STI and how if yes? Other way I see is to开发者_开发技巧 just prefix model names like KidBrushTeethReminder. Or go even deeper - write a factory method which creates objects according to type. Or is there a cleaner way?


I see two types of reminders, one for adult and one for kid. Personally, I would use an STI called Reminder and have one model called BrushTeeth inherit from Reminder. In the BrushTeeth model, I would have two Boolean columns named for_adult and for_kid.

In your Namespace for Adult, you can check BrushTeeth by querying whether the for_adult column is set to TRUE and base your logic / implementation from there. And for the Kid Namespace, you check the for_kid column.

Having Reminder as the base, gives you the option to have other reminders easily (i.e. Shower, Bath, Nails, etc). The Reminder model also comes with a Type column, since this is an STI model.

Hope that helps.


I found that you do can use STI for models in different namespaces. You just need to place them in appropriate subfolders. For example, Kid::BrushTeethReminder should be placed under app/models/kid in file brush_teeth_reminder.rb

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜