Module: ModelAttachment::ClassMethods
- Defined in:
- lib/model_attachment.rb
Instance Method Summary collapse
-
#attachment_options ⇒ Object
Returns attachment options defined by each call to acts_as_attachment.
-
#has_attachment(options = {}) ⇒ Object
has_attachmentadds the ability to upload files and make thumbnails of images *path: the path format for saving the documents *types: a hash of thumbnails to create for images :types => { :small => { :command => ‘convert -geometry 100x100’ }, :large => { :command => ‘convert -geometry 500x500’ } } *aws: the path to the aws config file or :default for rails config/amazon.yml access_key_id: secret_access_key: *logging: set to true to see logging. -
#validates_attachment_presence(name, options = {}) ⇒ Object
Places ActiveRecord-style validations on the presence of a file.
-
#validates_attachment_size(name, options = {}) ⇒ Object
Places ActiveRecord-style validations on the size of the file assigned.
Instance Method Details
#attachment_options ⇒ Object
Returns attachment options defined by each call to acts_as_attachment.
114 115 116 117 |
# File 'lib/model_attachment.rb', line 114 def # read_inheritable_attribute(:attachment_options) self. end |
#has_attachment(options = {}) ⇒ Object
has_attachment adds the ability to upload files and make thumbnails of images
-
path: the path format for saving the documents -
types: a hash of thumbnails to create for images:types => { :small => { :command => 'convert -geometry 100x100' }, :large => { :command => 'convert -geometry 500x500' } } -
aws: the path to the aws config file or :default for rails config/amazon.ymlaccess_key_id: secret_access_key: -
logging: set to true to see logging
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/model_attachment.rb', line 39 def ( = {}) include InstanceMethods if [:aws] begin require 'aws/s3' rescue LoadError => e e. << "You man need to install the aws-s3 gem" raise e end end if [:aws] == :default config_file = File.join(Rails.root, "config", "amazon.yml") if File.exist?(config_file) [:aws] = config_file include AmazonInstanceMethods else raise("You must provide a config/amazon.yml setup file") end elsif ![:aws].nil? && File.exist?([:aws]) include AmazonInstanceMethods end class_attribute :attachment_options self. = # write_inheritable_attribute(:attachment_options, options) # must be before the save to save the attributes before_save :save_attributes # must be after save to get the id for the path after_save :save_attached_files before_destroy :destroy_attached_files end |
#validates_attachment_presence(name, options = {}) ⇒ Object
Places ActiveRecord-style validations on the presence of a file. Options:
-
if: A lambda or name of a method on the instance. Validation will only be run is this lambda or method returns true. -
unless: Same asifbut validates if lambda or method returns false.
105 106 107 108 109 110 111 |
# File 'lib/model_attachment.rb', line 105 def name, = {} = [:message] || "must be set." validates_presence_of name, :message => , :if => [:if], :unless => [:unless] end |
#validates_attachment_size(name, options = {}) ⇒ Object
Places ActiveRecord-style validations on the size of the file assigned. The possible options are:
-
in: a Range of bytes (i.e.1..1.megabyte), -
less_than: equivalent to :in => 0..options -
greater_than: equivalent to :in => options..Infinity -
message: error message to display, use :min and :max as replacements -
if: A lambda or name of a method on the instance. Validation will only be run is this lambda or method returns true. -
unless: Same asifbut validates if lambda or method returns false.
86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/model_attachment.rb', line 86 def name, = {} min = [:greater_than] || ([:in] && [:in].first) || 0 max = [:less_than] || ([:in] && [:in].last) || (1.0/0) range = (min..max) = [:message] || "file size must be between :min and :max bytes." = .gsub(/:min/, min.to_s).gsub(/:max/, max.to_s) validates_inclusion_of name, :in => range, :message => , :if => [:if], :unless => [:unless] end |