classAssetType# The Asset Type encapsulates a type of attachment.# Conventionally this would a sensible category like 'image' or 'video'# that should be processed and presented in a particular way.# An AssetType currently provides:# * processor definitions for paperclip# * styles definitions for paperclip# * mime type list for file recognition# * selectors and scopes for retrieving this (or not this) category of asset# * radius tags for those subsets of assets (temporarily removed pending discussion of interface)@@types=[]@@type_lookup={}@@extension_lookup={}@@mime_lookup={}@@default_type=nilattr_reader:name,:processors,:styles,:icon_name,:catchall,:default_radius_tagdefinitialize(name,options={})options=options.symbolize_keys@name=name@icon_name=options[:icon]||name@processors=options[:processors]||[]@styles=options[:styles]||{}@styles=standard_stylesif@styles==:standard@default_radius_tag=options[:default_radius_tag]||'link'@extensions=options[:extensions]||[]@extensions.each{|ext|@@extension_lookup[ext]||=self}@mimes=options[:mime_types]||[]@mimes.each{|mimetype|@@mime_lookup[mimetype]||=self}this=selfAsset.send:define_method,"#{name}?".interndothis.mime_types.include?(asset_content_type)endAsset.send:define_class_method,"#{name}_condition".interndothis.condition;endAsset.send:define_class_method,"not_#{name}_condition".interndothis.non_condition;endAsset.send:scope,plural.to_sym,->{where(:conditions=>condition)}Asset.send:scope,"not_#{plural}".to_sym,->{where(:conditions=>non_condition)}self.define_radius_tags@@types.pushself@@type_lookup[@name]=selfenddefpluralname.to_s.pluralizeenddeficon(style_name='icon')ifFile.exist?(Rails.root+"public/images/admin/assets/#{icon_name}_#{style_name.to_s}.png")return"/assets/admin/#{icon_name}_#{style_name.to_s}.png"elsereturn"/assets/admin/#{icon_name}_icon.png"endenddeficon_path(style_name='icon')Rails.root+"public#{icon(style_name)}"enddefconditionif@mimes.any?["asset_content_type IN (#{@mimes.map{'?'}.join(',')})",*@mimes]elseself.class.other_conditionendenddefsanitized_conditionActiveRecord::Base.send:sanitize_sql_array,conditionenddefnon_conditionif@mimes.any?["NOT asset_content_type IN (#{@mimes.map{'?'}.join(',')})",*@mimes]elseself.class.non_other_conditionendenddefsanitized_non_conditionActiveRecord::Base.send:sanitize_sql_array,non_conditionenddefmime_types@mimesenddefpaperclip_processorsTrustyCms.config["assets.create_#{name}_thumbnails?"]?processors:[]end# Parses and combines the various ways in which paperclip styles can be defined, and normalises them into# the format that paperclip expects. Note that :styles => :standard has already been replaced with the# results of a call to standard_styles.# Styles are passed to paperclip as a hash and arbitrary keys can be passed through from configuration.#defpaperclip_styles# Styles are not relevant if processors are not defined.@paperclip_styles||=ifpaperclip_processors.any?normalize_style_rules(configured_styles.merge(styles))else{}end@paperclip_stylesend# Takes a motley collection of differently-defined styles and renders them into the standard hash-of-hashes format.# Solitary strings are assumed to be #defnormalize_style_rules(styles={})styles.each_pairdo|name,rule|unlessrule.is_a?Hashifrule=~/\=/parameters=rule.split(',').collect{|parameter|parameter.split('=')}# array of pairsrule=Hash[parameters].symbolize_keys# into hash of :first => lastelserule={:geometry=>rule}# simplest case: name:geom|name:geomendendrule[:geometry]||=rule.delete(:size)styles[name.to_sym]=ruleendstylesenddefstandard_styles{:thumbnail=>{:geometry=>'100x100#',:format=>:png}}end# Paperclip styles are defined in the config entry `assets.thumbnails.asset_type`, with the format:# foo:key-x,key=y,key=z|bar:key-x,key=y,key=z# where 'key' can be any parameter understood by your paperclip processors. Usually they include :geometry and :format.# A typical entry would be:## standard:geometry=640x640>,format=jpg## This method parses that string and returns the defined styles as a hash of style-defining strings that will later be normalized into hashes.#defconfigured_styles@configured_styles||=ifstyle_definitions=TrustyCms.config["assets.thumbnails.#{name}"]style_definitions.split('|').each_with_object({})do|definition,styles|name,rule=definition.split(':')styles[name.strip.to_sym]=rule.to_s.stripendelse{}endenddeflegacy_stylesTrustyCms::config["assets.additional_thumbnails"].to_s.gsub(' ','').split(',').collect{|s|s.split('=')}.inject({}){|ha,(k,v)|ha[k.to_sym]=v;ha}enddefstyle_dimensions(style_name)ifstyle=paperclip_styles[style_name.to_sym]style[:size]endenddefdefine_radius_tagstype=self.namePage.class_eval{tag"asset:if_#{type}"do|tag|tag.expandiffind_asset(tag,tag.attr.dup).send("#{type}?".to_sym)endtag"asset:unless_#{type}"do|tag|tag.expandunlessfind_asset(tag,tag.attr.dup).send("#{type}?".to_sym)end}end# class methodsdefself.for(attachment)extension=File.extname(attachment.original_filename).sub(/^\.+/,"")from_extension(extension)||from_mimetype(attachment.instance_read(:content_type))||catchallenddefself.from_extension(extension)@@extension_lookup[extension]enddefself.from_mimetype(mimetype)@@mime_lookup[mimetype]enddefself.catchall@@default_type||=self.find(:other)enddefself.known?(name)!self.find(name).nil?enddefself.slice(*types)@@type_lookup.slice(*types.map(&:to_sym)).valuesiftypes# Hash#slice is provided by will_paginateenddefself.find(type)@@type_lookup[type]iftypeenddefself.[](type)find(type)enddefself.all@@typesenddefself.known_types@@types.map(&:name)# to preserve orderenddefself.known_mimetypes@@mime_lookup.keysenddefself.mime_types_for(*names)names.collect{|name|find(name).mime_types}.flattenenddefself.conditions_for(*names)names.collect{|name|self.find(name).sanitized_condition}.join(' OR ')enddefself.non_other_condition["asset_content_type IN (#{known_mimetypes.map{'?'}.join(',')})",*known_mimetypes]enddefself.other_condition["NOT asset_content_type IN (#{known_mimetypes.map{'?'}.join(',')})",*known_mimetypes]endend