Skip to content

Instantly share code, notes, and snippets.

@thiagoa
Last active December 20, 2016 02:14
Show Gist options
  • Save thiagoa/f20613faf98d1c39fe8c1a41bee9fae5 to your computer and use it in GitHub Desktop.
Save thiagoa/f20613faf98d1c39fe8c1a41bee9fae5 to your computer and use it in GitHub Desktop.
class Imageable < Module
def initialize(has_one: %i(main_image))
has_one.each { |image_kind| define_image_getter(image_kind) }
end
private
def included(model_class)
model_class.has_many :images
end
def define_image_getter(image_kind)
define_method image_kind do
images.find_by(kind: image_kind)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment