module Imageable
def self.included(base)
base.extend(ClassMethods)
base.scope :owned_by, lambda { |user|
base.joins(:items).merge(Item.where(:user_id => user.id))
}
end
module ClassMethods
def xyz
puts 'class method'
end
end
def owned_by?(user)
self.items.collect(&:user_id).include?(user.id)
end
end
接下來可以用 include Imageable 在你的 model to include this module.
沒有留言:
張貼留言