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 the #set_attachment_path method if you would like to override the way file_path is set. A path input will be rendered for content types having #set_attachment_path.



112
113
114
115
116
117
118
# File 'lib/cms/behaviors/attaching.rb', line 112

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



97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/cms/behaviors/attaching.rb', line 97

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



87
88
89
90
91
92
93
94
95
# File 'lib/cms/behaviors/attaching.rb', line 87

def validates_attachment_presence(name, options = {})
  message = options.delete(: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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cms/behaviors/attaching.rb', line 69

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