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
-
#handle_setting_attachment_path ⇒ Object
Define at :set_attachment_path if you would like to override the way file_path is set.
- #validates_attachment_content_type(name, options = {}) ⇒ Object
- #validates_attachment_presence(name, options = {}) ⇒ Object
- #validates_attachment_size(name, options = {}) ⇒ Object
Instance Method Details
#handle_setting_attachment_path ⇒ Object
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 if self.respond_to? :set_attachment_path else 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 (name, = {}) = .dup allowed_types = [[:content_type]].flatten validate() do |record| .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([: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 (name, = {}) = [:message] || "Must provide at least one #{name}" validate() do |record| return if record.deleted? unless record..any? { |a| a. == name.to_s } record.errors.add(:attachment, ) 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 (name, = {}) min = [:greater_than] || ([:in] && [:in].first) || 0 max = [:less_than] || ([:in] && [:in].last) || (1.0/0) range = (min..max) = [:message] || "#{name.to_s.capitalize} file size must be between :min and :max bytes." = .gsub(/:min/, min.to_s).gsub(/:max/, max.to_s) #options[:unless] = Proc.new {|r| r.a.asset_name != name.to_s} validate() do |record| record..each do || next unless . == name.to_s record.errors.add_to_base() unless range.include?(.data_file_size) end end end |