Last active
December 20, 2016 02:13
-
-
Save thiagoa/1e4504fb2018551f934cc4ab372c5504 to your computer and use it in GitHub Desktop.
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
module Imageable | |
def self.included(model_class) | |
model_class.has_many :images, polymorphic: true | |
model_class.extend ClassMethods | |
end | |
module ClassMethods | |
def has_one_image(*names) | |
names.each do |image_kind| | |
define_method image_kind do | |
images.find { |image| image.kind == image_kind } | |
end | |
end | |
end | |
end | |
end | |
class Project | |
include Imageable | |
has_one_image :main_image, :secondary_image | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment