Last active
August 29, 2015 14:03
-
-
Save phillipoertel/3368c920e4b352ea01b1 to your computer and use it in GitHub Desktop.
This file contains 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
class PortfolioItem::Image | |
dragonfly_accessor :image_original # store original | |
dragonfly_accessor :image_medium # 660px | |
end | |
class PortfolioItem | |
include PortfolioItem::ImagesConcern | |
end | |
module PortfolioItem::ImagesConcern | |
extend ActiveSupport::Concern | |
class NoSuchStyleError < StandardError; end | |
included do | |
has_many :images, dependent: :destroy # , class_name: 'PortfolioItem::Image' # class name may not be necessary | |
private :images # only use images through the methods #tile_image, #tile_image=, etc. | |
end | |
def tile_image | |
images.find_by_slot(:tile).image_original | |
end | |
def tile_image=(file) | |
association_object = images.find_or_initialize_by_slot(:tile) | |
association_object.image = file | |
association_object.save! | |
# TODO verify return value of tile_image with Paperclip. return the same as paperclip would. | |
association_object.image | |
end | |
# | |
# current method name: #teaser | |
# | |
# How is it currently used? | |
# | |
# teaser.path # no arg means default, :original | |
# => replace with #tile_widget_image.path | |
# | |
# teaser.path(:preview) | |
# => replace with #tile_widget_image(:medium).path | |
# | |
# teaser.url(:preview) | |
# => replace with #tile_widget_image(:medium).url | |
# | |
# teaser.file? (was a file uploaded or is the image a default image) | |
# => replace with #tile_widget_image.present? | |
# | |
def tile_widget_image(style = :original) | |
association_object = images.find_by_origin(:tile_widget) | |
image_attribute = "image_#{style}".to_sym | |
raise NoSuchStyleError.new(style) unless association_object.respond_to?(image_attribute) | |
association_object.send(image_attribute) | |
end | |
# | |
# DISCUSS | |
# it may be possible to replace all of PortfolioItem::CollectDetailImageCandidates with a method like this: | |
# | |
# def detail_image_candidates | |
# images | |
# .where(slot: nil) | |
# .where(origin: [:embedly, :tile_widget_upload, :detail_widget_upload, :initial_upload]) | |
# .map(:&image_medium) # or return value object instead of the dragonfly object | |
# end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment