Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active December 20, 2016 02:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thiagoa/1e4504fb2018551f934cc4ab372c5504 to your computer and use it in GitHub Desktop.
Save thiagoa/1e4504fb2018551f934cc4ab372c5504 to your computer and use it in GitHub Desktop.
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