Skip to content

Commit

Permalink
Do not cache dynamic attachment styles
Browse files Browse the repository at this point in the history
  • Loading branch information
francocatena authored and Jon Yurek committed Apr 20, 2011
1 parent 3b17082 commit 7ff4120
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/paperclip/attachment.rb
Expand Up @@ -64,7 +64,7 @@ def initialize name, instance, options = {}
end

def styles
unless @normalized_styles
if @styles.respond_to?(:call) || !@normalized_styles
@normalized_styles = {}
(@styles.respond_to?(:call) ? @styles.call(self) : @styles).each do |name, args|
@normalized_styles[name] = Paperclip::Style.new(name, args.dup, self)
Expand Down
18 changes: 18 additions & 0 deletions test/attachment_test.rb
Expand Up @@ -287,6 +287,24 @@ def thumb; "-thumb"; end
assert_equal "50x50#", @attachment.styles[:thumb][:geometry]
end
end

context "An attachment with conditional :styles that is a proc" do
setup do
rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} }

@dummy = Dummy.new(:other => 'a')
end

should "have the correct styles for the assigned instance values" do
assert_equal "50x50#", @dummy.avatar.styles[:thumb][:geometry]
assert_nil @dummy.avatar.styles[:large]

@dummy.other = 'b'

assert_equal "400x400", @dummy.avatar.styles[:large][:geometry]
assert_nil @dummy.avatar.styles[:thumb]
end
end

context "An attachment with :url that is a proc" do
setup do
Expand Down

0 comments on commit 7ff4120

Please sign in to comment.