Class: ActiveRecord::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/attacheable.rb

Class Method Summary collapse

Class Method Details

.has_attachment(options = {}) ⇒ Object

In model write has_attachment (conflicts with acts_as_attachment) with options:

:thumbnails => list of thumbnails, i.e. => “120x”, :large => “800x600” :croppable_thumbnails => list of thumbnails, which must be cropped to center, i.e.: [:large, :preview] :path_prefix => path, where to store photos, i.e.: “public/system/photos” :replicas => [=> “user1”, :host => “host1”], list of hosts, where to clone all loaded originals :autocreate => true/false, whether to autocreate thumbnails on requesting thumbnail

After this, add to routes:

map.assets 'system/photos/*path_info', :controller => "photos", :action => "show"

and add to PhotosController: def show

photo, data = Photo.data_by_path_info(params[:path_info])
render :text => data, :content_type => photo && photo.content_type

end This will enable creation on demand

You can also add

uri "/system/photos/", :handler => Attacheable::PhotoHandler.new("/system/photos"), :in_front => true

to any mongrel scripts for nonblocking image creation

Table for this plugin should have fields:

    filename : string
content_type : string  (optional)
       width : integer (optional)
      heigth : integer (optional)


33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/attacheable.rb', line 33

def self.has_attachment(options = {})
  class_inheritable_accessor :attachment_options
  self.attachment_options = options
  
  options.with_indifferent_access
  options[:autocreate] ||= false
  options[:force_autocreate] ||= false
  options[:thumbnails] ||= {}
  options[:thumbnails].symbolize_keys!.with_indifferent_access
  options[:croppable_thumbnails] ||= []
  options[:croppable_thumbnails] = options[:croppable_thumbnails].map(&:to_sym)
  options[:path_prefix] ||= "public/system/#{table_name}"
  options[:valid_filetypes] ||= %w(jpeg gif png psd)
  include(Attacheable)
end

.validates_as_attachment(options = {}) ⇒ Object

Currently it will check valid filetype (unless option valid_filetypes set to :all)



52
53
54
55
56
57
# File 'lib/attacheable.rb', line 52

def self.validates_as_attachment(options = {})
  options[:message] ||= "Incorrect file type. Valid file types include: #{attachment_options[:valid_filetypes].to_sentence}"
  self.attachment_options[:validation_message] = options[:message]
  
  validate :valid_filetype?
end