Skip to content

Commit

Permalink
Merge pull request technoweenie#14 from noosfero/master
Browse files Browse the repository at this point in the history
Fix crash with protected_attributes and rails 4.2
  • Loading branch information
pothoven committed Jul 15, 2015
2 parents c4c8921 + 2715bb1 commit f79f416
Showing 1 changed file with 14 additions and 20 deletions.
34 changes: 14 additions & 20 deletions lib/technoweenie/attachment_fu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -319,17 +319,12 @@ def create_or_update_thumbnail(temp_file, file_name_suffix, *size)
thumbnailable? || raise(ThumbnailError.new("Can't create a thumbnail if the content type is not an image or there is no parent_id column"))
find_or_initialize_thumbnail(file_name_suffix).tap do |thumb|
thumb.temp_paths.unshift temp_file
assign_attributes_args = []
assign_attributes_args << {
:content_type => content_type,
:filename => thumbnail_name_for(file_name_suffix),
:thumbnail_resize_options => size
attributes = {
content_type: content_type,
filename: thumbnail_name_for(file_name_suffix),
thumbnail_resize_options: size
}
if defined?(Rails) && Rails::VERSION::MAJOR == 3
# assign_attributes API in Rails 2.3 doesn't take a second argument
assign_attributes_args << { :without_protection => true }
end
thumb.send(:assign_attributes, *assign_attributes_args)
attributes.each{ |a, v| thumb.send "#{a}=", v }
callback_with_args :before_thumbnail_saved, thumb
thumb.save!
end
Expand Down Expand Up @@ -436,8 +431,8 @@ def with_image(&block)
# Write out the temporary data if it is not present
if temp_data.nil?
self.temp_data = current_data
end
end

self.class.with_image(temp_path, &block)
end

Expand Down Expand Up @@ -480,15 +475,14 @@ def attachment_attributes_valid?

# Initializes a new thumbnail with the given suffix.
def find_or_initialize_thumbnail(file_name_suffix)
if defined?(Rails) && Rails::VERSION::MAJOR >= 4
respond_to?(:parent_id) ?
thumbnail_class.find_or_initialize_by(:thumbnail => file_name_suffix.to_s, :parent_id => id) :
thumbnail_class.find_or_initialize_by(:thumbnail => file_name_suffix.to_s)
else
respond_to?(:parent_id) ?
thumbnail_class.find_or_initialize_by_thumbnail_and_parent_id(file_name_suffix.to_s, id) :
thumbnail_class.find_or_initialize_by_thumbnail(file_name_suffix.to_s)
attrs = {thumbnail: file_name_suffix.to_s}
attrs[:parent_id] = id if respond_to? :parent_id
thumb = thumbnail_class.where(attrs).first
unless thumb
thumb = thumbnail_class.new
attrs.each{ |a, v| thumb[a] = v }
end
thumb
end

# Stub for a #process_attachment method in a processor
Expand Down

0 comments on commit f79f416

Please sign in to comment.