Skip to content
This repository has been archived by the owner on Feb 10, 2022. It is now read-only.

Commit

Permalink
Addded validates_attachment_width and validates_attachment_height
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeff Berg committed Oct 27, 2008
1 parent dc1d241 commit c7ec8d4
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 1 deletion.
58 changes: 58 additions & 0 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,64 @@ def has_attached_file name, options = {}
value.send(:flush_errors) unless value.valid?
end
end


# Places ActiveRecord-style validations on the height 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[:less_than]
# * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
# * +message+: error message to display, use :min and :max as replacements
def validates_attachment_height name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance|
unless options[:greater_than].nil?
options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity
end
unless options[:less_than].nil?
options[:in] = (0..options[:less_than])
end

if attachment.file? && !options[:in].include?(instance[:"#{name}_height"].to_i)
min = options[:in].first
max = options[:in].last

if options[:message]
options[:message].gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
else
"height is not between #{min} and #{max} bytes."
end
end
end
end


# Places ActiveRecord-style validations on the width 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[:less_than]
# * +greater_than+: equivalent to :in => options[:greater_than]..Infinity
# * +message+: error message to display, use :min and :max as replacements
def validates_attachment_width name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance|
unless options[:greater_than].nil?
options[:in] = (options[:greater_than]..(1/0)) # 1/0 => Infinity
end
unless options[:less_than].nil?
options[:in] = (0..options[:less_than])
end

if attachment.file? && !options[:in].include?(instance[:"#{name}_width"].to_i)
min = options[:in].first
max = options[:in].last

if options[:message]
options[:message].gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
else
"width is not between #{min} and #{max} bytes."
end
end
end
end

# Places ActiveRecord-style validations on the size of the file assigned. The
# possible options are:
Expand Down
4 changes: 3 additions & 1 deletion test/paperclip_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,10 @@ class ::SubDummy < Dummy; end
end

[[:presence, nil, "5k.png", nil],
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:size, {:in => 1..10240}, "5k.png", "12k.png"],
[:size2, {:in => 1..10240}, nil, "12k.png"],
[:width, {:in => 400..500}, "5k.png", "50x50.png"],
[:height, {:in => 60..70}, "5k.png", "50x50.png"],
[:content_type1, {:content_type => "image/png"}, "5k.png", "text.txt"],
[:content_type2, {:content_type => "text/plain"}, "text.txt", "5k.png"],
[:content_type3, {:content_type => %r{image/.*}}, "5k.png", "text.txt"],
Expand Down

0 comments on commit c7ec8d4

Please sign in to comment.