Module: HasMedia

Included in:
ActiveRecord::Base
Defined in:
lib/has_media.rb,
lib/generators/has_media/views_generator.rb,
lib/generators/has_media/install_generator.rb

Defined Under Namespace

Modules: ClassMethods, Generators Classes: Engine

Constant Summary collapse

VERSION =
"0.0.1"
@@medium_types =
{}
@@store_dir =
'/tmp'
@@directory_uri =
''
@@errors_messages =
{:type_error => 'Wrong type'}
@@encoded_extensions =
{
  :image => 'png',
  :audio => 'mp3',
  :pdf   => 'pdf',
  :video => 'flv'
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.directory_pathObject



80
81
82
# File 'lib/has_media.rb', line 80

def self.directory_path
  @@store_dir
end

.directory_path=(value) ⇒ Object

directory_path

Used to configure directory_path to store media on filesystem

Example :

HasMedia.directory_path = Rails.root + "media"


77
78
79
# File 'lib/has_media.rb', line 77

def self.directory_path=(value)
  @@store_dir = value
end

.directory_uriObject



95
96
97
# File 'lib/has_media.rb', line 95

def self.directory_uri
  @@directory_uri
end

.directory_uri=(value) ⇒ Object

directory_uri

Used to store www access to your media

Example :

HasMedia.directory_path = Rails.root + "media"


92
93
94
# File 'lib/has_media.rb', line 92

def self.directory_uri=(value)
  @@directory_uri = value
end

.encoded_extensionsObject



65
66
67
# File 'lib/has_media.rb', line 65

def self.encoded_extensions
  @@encoded_extensions
end

.encoded_extensions=(value) ⇒ Object

encoded_extensions

Used to configure output format if you use a custom encoder

Example :

HasMedia.encoded_extensions = {
  :image => 'png',
  :audio => 'ogg',
  :video => 'flv'
}


62
63
64
# File 'lib/has_media.rb', line 62

def self.encoded_extensions=(value)
  @@encoded_extensions = value
end

.errors_messagesObject

errors_messages

Used to store custom error messages

Example :

HasMedia.errors_messages = {:type_error => "Le format du logo n'est pas correct"}


107
108
109
# File 'lib/has_media.rb', line 107

def self.errors_messages
  @@errors_messages
end

.errors_messages=(h) ⇒ Object



110
111
112
# File 'lib/has_media.rb', line 110

def self.errors_messages=(h)
  @@errors_messages.merge!(h)
end

.included(mod) ⇒ Object



114
115
116
# File 'lib/has_media.rb', line 114

def self.included(mod)
  mod.extend ClassMethods
end

.medium_typesObject



46
47
48
# File 'lib/has_media.rb', line 46

def self.medium_types
  @@medium_types
end

.medium_types=(value) ⇒ Object

medium_types

Used to configure available medium types

Each medium type id representing with its class name and contain an Array of possible mime types. An empty Array means no limitation on mime type

Example :

HasMedia.medium_types = {
  "Image" => ["image/jpeg", "image/png"],
  "Video" => ["video/mp4"],
  "Audio" => ["audio/mp3"],
  "Document" => []
}


43
44
45
# File 'lib/has_media.rb', line 43

def self.medium_types=(value)
  @@medium_types = value
end

.sanitize_file_name(name) ⇒ String

Sanitize file name

Parameters:

  • name (String)

Returns:

  • (String)


123
124
125
126
127
128
129
130
# File 'lib/has_media.rb', line 123

def self.sanitize_file_name(name)
  name = name.gsub("\\", "/") # work-around for IE
  name = File.basename(name)
  name = name.gsub(/[^a-zA-Z0-9\.\-\+_]/,"_")
  name = "_#{name}" if name =~ /\A\.+\z/
  name = "unnamed" if name.size == 0
  return name.downcase
end

Instance Method Details

#remove_old_mediaObject

Remove old media before saving



294
295
296
297
298
# File 'lib/has_media.rb', line 294

def remove_old_media
  (@old_media || []).each do |medium|
    medium.destroy if medium
  end
end