Module: SeoAttachment::InstanceMethods

Defined in:
lib/seo_attachment/seo_attachment.rb

Instance Method Summary collapse

Instance Method Details

#directoryObject



27
28
29
30
31
32
33
# File 'lib/seo_attachment/seo_attachment.rb', line 27

def directory
  data_name = seo_attachment_options[:data_name]
  return if self.send(data_name).path.nil?
  data_files = self.send(data_name).path.split("/")
  data_files.delete_at(data_files.size-1)
  data_files.push("*.*").join('/')
end

#interpolates_file_namesObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/seo_attachment/seo_attachment.rb', line 35

def interpolates_file_names
  data_name = seo_attachment_options[:data_name]
  return if self.send("#{data_name}_file_size").blank?
  normally_file_name = self.send(seo_attachment_options[:normally_method].to_s)
  extension = File.extname(self.send("#{data_name}_file_name")).downcase rescue nil
  old_file = self.send("#{data_name}_file_name")
  new_file = "#{normally_file_name}#{extension}"
  self.send(data_name).instance_write(:file_name, new_file)
  files = Dir[directory]
  files.each do |file|
    dir_array = file.split("/")
    dir_array.pop
    dir = dir_array.join('/')
    prefix = file.split("/").last.split("-")[0]
    was = File.join(dir, "#{prefix}-#{old_file}")
    build = File.join(dir, "#{prefix}-#{new_file}")
    if File.exist?(was) && was != build
      FileUtils.mv was, build
    end
  end
end