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 the #set_attachment_path method 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 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 if self.respond_to? : else 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 (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
87 88 89 90 91 92 93 94 95 |
# File 'lib/cms/behaviors/attaching.rb', line 87 def (name, = {}) = .delete(: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
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 (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 |