Sha256: e9e8f5ad017e17200b4edfdea643fbe614f94744288697c8959b6cd9bd583ca4

Contents?: true

Size: 1.88 KB

Versions: 27

Compression:

Stored size: 1.88 KB

Contents

class Asset < ActiveRecord::Base
  has_attachment :storage => :file_system,
                 :path_prefix => 'public/assets',
                 :max_size => 5.megabytes,
                 :resize_to => '900',
                 :thumbnails => { :large => '480x480', :medium => '230x230', :thumb => '184', :square => '75x75!' }
                 
  has_many :assetings, :dependent => :delete_all
  belongs_to :assetable, :polymorphic => true
  
  named_scope :not_thumbnails, :conditions => 'parent_id IS NULL'
  named_scope :images, :conditions => "content_type LIKE 'image%'"
  named_scope :documents, :conditions => "content_type NOT LIKE 'image%'"
                 
  validates_as_attachment
  # validates_presence_of :description, :unless => :is_thumbnail?
  
  attr_accessor :position
  attr_accessible :category, :description, :uploaded_data
  
  def description
    read_attribute('description').blank? ? 'No Description' : super
  end
  
  def category
    read_attribute('category').blank? ? 'No Folder' : super
  end
  
  def is_thumbnail?
    !parent_id.nil?
  end
  
  def file_type
    self.content_type.split("/")[1]
  end
  
  def is_image?
    content_type =~ /image/
  end
  
  def self.grouped_by_content_type
    self.not_thumbnails.group_by(&:content_type).sort
  end
  
  def self.grouped_by_category
    self.not_thumbnails.group_by(&:category).sort
  end
  
  def self.categories
    self.find(:all, :group => "category", :select => "category", :order => 'category')
  end
  
  def sizes
    sizes = []
    if is_image? and not is_thumbnail?
      sizes << [ 'fullsize', public_filename ]
      attachment_options[:thumbnails].each_key do |key|
        sizes << [key.to_s, public_filename(key)]
      end
    end
    sizes
  end
  
  def to_json(options = {})
    methods = [:public_filename]
    methods << :sizes if is_image?
    options.reverse_merge! :methods => methods
    super options
  end


end

Version data entries

27 entries across 27 versions & 1 rubygems

Version Path
beef-has_assets-0.2.10 app/models/asset.rb
beef-has_assets-0.2.8 app/models/asset.rb
beef-has_assets-0.2.9 app/models/asset.rb
beef-has_assets-0.3.1 app/models/asset.rb
beef-has_assets-0.3.2 app/models/asset.rb
beef-has_assets-0.3.3 app/models/asset.rb
beef-has_assets-0.3.4 app/models/asset.rb
beef-has_assets-0.3.5 app/models/asset.rb
beef-has_assets-0.3.6 app/models/asset.rb
beef-has_assets-0.3.7 app/models/asset.rb
beef-has_assets-0.3.8 app/models/asset.rb
beef-has_assets-0.3.9 app/models/asset.rb
beef-has_assets-0.4.0 app/models/asset.rb
beef-has_assets-0.4.1 app/models/asset.rb
beef-has_assets-0.5.0 app/models/asset.rb
beef-has_assets-0.7.3 app/models/asset.rb
beef-has_assets-0.7.2 app/models/asset.rb
beef-has_assets-0.7.1 app/models/asset.rb
beef-has_assets-0.7.0 app/models/asset.rb
beef-has_assets-0.6.8 app/models/asset.rb