Module: Attacheable::FileNaming
- Included in:
- Attacheable
- Defined in:
- lib/attacheable/file_naming.rb
Instance Method Summary collapse
- #attachment_basename ⇒ Object
- #attachment_extname ⇒ Object
-
#base_path ⇒ Object
:nodoc:.
-
#full_filename_with_creation(thumbnail = nil) ⇒ Object
:nodoc:.
-
#full_filename_without_creation(thumbnail = nil) ⇒ Object
:nodoc:.
- #public_filename_without_creation(thumbnail = nil) ⇒ Object
- #sanitize_filename(filename) ⇒ Object
-
#thumbnail_name_for(thumbnail = nil) ⇒ Object
:nodoc:.
Instance Method Details
#attachment_basename ⇒ Object
29 30 31 |
# File 'lib/attacheable/file_naming.rb', line 29 def filename && filename.gsub(/\.[^\.]+$/, '') end |
#attachment_extname ⇒ Object
33 34 35 |
# File 'lib/attacheable/file_naming.rb', line 33 def filename && filename.gsub(/^(.*)(\.[^\.]+)$/, '\2') end |
#base_path ⇒ Object
:nodoc:
25 26 27 |
# File 'lib/attacheable/file_naming.rb', line 25 def base_path #:nodoc: @base_path ||= File.join(Attacheable.root, 'public') end |
#full_filename_with_creation(thumbnail = nil) ⇒ Object
:nodoc:
3 4 5 |
# File 'lib/attacheable/file_naming.rb', line 3 def full_filename_with_creation(thumbnail = nil) #:nodoc: create_thumbnail_if_required(thumbnail) end |
#full_filename_without_creation(thumbnail = nil) ⇒ Object
:nodoc:
7 8 9 10 |
# File 'lib/attacheable/file_naming.rb', line 7 def full_filename_without_creation(thumbnail = nil) #:nodoc: file_system_path = [:path_prefix] File.join(Attacheable.root, file_system_path, *partitioned_path(thumbnail_name_for(thumbnail))) end |
#public_filename_without_creation(thumbnail = nil) ⇒ Object
21 22 23 |
# File 'lib/attacheable/file_naming.rb', line 21 def public_filename_without_creation(thumbnail = nil) full_filename_without_creation(thumbnail).gsub %r(^#{Regexp.escape(base_path)}), '' end |
#sanitize_filename(filename) ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'lib/attacheable/file_naming.rb', line 37 def sanitize_filename(filename) returning filename.strip do |name| # NOTE: File.basename doesn't work right with Windows paths on Unix # get only the filename, not the whole path name.gsub! /^.*(\\|\/)/, '' # Finally, replace all non alphanumeric, underscore or periods with underscore name.gsub! /[^\w\.\-]/, '_' end end |
#thumbnail_name_for(thumbnail = nil) ⇒ Object
:nodoc:
12 13 14 15 16 17 18 19 |
# File 'lib/attacheable/file_naming.rb', line 12 def thumbnail_name_for(thumbnail = nil) #:nodoc: return filename if thumbnail.blank? ext = nil basename = filename.gsub /\.\w+$/ do |s| ext = s; '' end "#{basename}_#{thumbnail}.jpg" end |