Module: Cms::Behaviors::Attaching::Validations

Defined in:
lib/cms/behaviors/attaching.rb

Overview

NOTE: Assets should be validated when created individually.

Instance Method Summary collapse

Instance Method Details

#handle_setting_attachment_pathObject

Define at :set_attachment_path if you would like to override the way file_path is set



144
145
146
147
148
149
150
# File 'lib/cms/behaviors/attaching.rb', line 144

def handle_setting_attachment_path
  if self.respond_to? :set_attachment_path
    set_attachment_path
  else
    use_default_attachment_path
  end
end

#validates_attachment_content_type(name, options = {}) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/cms/behaviors/attaching.rb', line 130

def validates_attachment_content_type(name, options = {})
  validation_options = options.dup
  allowed_types = [validation_options[:content_type]].flatten
  validate(validation_options) do |record|
    attachments.each do |a|
      if !allowed_types.any? { |t| t === a.data_content_type } && !(a.data_content_type.nil? || a.data_content_type.blank?)
        record.add_to_base(options[:message] || "is not one of #{allowed_types.join(', ')}")
      end
    end

  end
end

#validates_attachment_presence(name, options = {}) ⇒ Object



120
121
122
123
124
125
126
127
128
# File 'lib/cms/behaviors/attaching.rb', line 120

def validates_attachment_presence(name, options = {})
  message = options[:message] || "Must provide at least one #{name}"
  validate(options) do |record|
    return if record.deleted?
    unless record.attachments.any? { |a| a.attachment_name == name.to_s }
      record.errors.add(:attachment, message)
    end
  end
end

#validates_attachment_size(name, options = {}) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/cms/behaviors/attaching.rb', line 102

def validates_attachment_size(name, options = {})

  min = options[:greater_than] || (options[:in] && options[:in].first) || 0
  max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0)
  range = (min..max)
  message = options[:message] || "#{name.to_s.capitalize} file size must be between :min and :max bytes."
  message = message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)

  #options[:unless] = Proc.new {|r| r.a.asset_name != name.to_s}

  validate(options) do |record|
    record.attachments.each do |attachment|
      next unless attachment.attachment_name == name.to_s
      record.errors.add_to_base(message) unless range.include?(attachment.data_file_size)
    end
  end
end