Module: PaperclipI18n::HasManyAttachedFiles::InstanceMethods

Defined in:
lib/paperclip_i18n/has_many_attached_files.rb

Instance Method Summary collapse

Instance Method Details

#create_or_update_asset(data_item) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/paperclip_i18n/has_many_attached_files.rb', line 37

def create_or_update_asset(data_item)
  override_default_styles, normalised_styles = override_default_styles?(data_item.original_filename)
  asset = assets.where(:upload_language => current_language).first || self.has_many_attached_files_options[:model].new # is there an attachment, then delete the old
  asset.upload.instance_variable_set('@styles', normalised_styles) if override_default_styles
  asset.upload = data_item
  asset.upload_language = current_language

  if asset.new_record?
    asset.save
    assets << asset # store the asset ...
  else
    asset.save
  end
  assets(true) # reload implicitly
end

#current_languageObject

based on the Authlogic plugin (agilewebdevelopment.com/plugins/authgasm)



76
77
78
# File 'lib/paperclip_i18n/has_many_attached_files.rb', line 76

def current_language
  @current_file_language || ::I18n.locale.to_s
end

#override_default_styles?(filename) ⇒ Boolean

Returns:

  • (Boolean)


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/paperclip_i18n/has_many_attached_files.rb', line 53

def override_default_styles?(filename)
  if !self.has_many_attached_files_options[:styles].nil?
    normalised_styles = {}
    
    self.has_many_attached_files_options[:styles].each do |name, args|
      dimensions, format = [args, nil].flatten[0..1]
      format = nil if format.blank?
      
      if filename.match(/\.pdf$/) # remove crop commands if file is a PDF (this fails with Imagemagick)
        args.gsub!(/#/ , '')
        format = 'png'
      end
      
      normalised_styles[name] = { :processors => [:thumbnail], :geometry => dimensions, :format => format }
    end
    
    return true, normalised_styles
  else
    return(false)
  end
end

#save_attached_filesObject



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/paperclip_i18n/has_many_attached_files.rb', line 25

def save_attached_files
  self.has_many_attached_files_options[:model].transaction do
    if upload.is_a?(Array)
      upload.each do |data_item|
        create_or_update_asset(data_item) unless data_item.nil? || data_item.blank?
      end
    else
      create_or_update_asset(upload)
    end
  end unless upload.nil? || upload.blank?
end