Created
September 19, 2011 23:41
-
-
Save nicholasjhenry/1227926 to your computer and use it in GitHub Desktop.
Streamlined Modeling Examples
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class User < ActiveRecord::Base | |
# Collaboration Rules | |
def can_like?(like) | |
user.plugins.include?(:page_like) | |
end | |
# Business Service | |
def like(page) | |
Like.create!(:page => page, :user => self) | |
end | |
end | |
class Page < ActiveRecord::Base | |
# Collaboration Rules | |
def likable?(like) | |
# this could be overridden in a subclasses | |
true | |
end | |
end | |
class Like < ActiveRecord::Base | |
belongs_to :user | |
belongs_to :page | |
validate :user_can_like?, :page_is_likeable? | |
private | |
def user_can_like? | |
if user && !user.can_like?(self) | |
errors.add(:user, "does not have permission to like") | |
end | |
end | |
def page_is_likeable? | |
if page && !page.likeable?(self) | |
errors.add(:page, "cannot be liked") | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment