Skip to content

Instantly share code, notes, and snippets.

@nicholasjhenry
Created September 19, 2011 23:41
Show Gist options
  • Save nicholasjhenry/1227926 to your computer and use it in GitHub Desktop.
Save nicholasjhenry/1227926 to your computer and use it in GitHub Desktop.
Streamlined Modeling Examples
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