has_media 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +54 -4
- data/VERSION +1 -1
- data/examples/models/audio.rb +0 -5
- data/examples/models/image.rb +0 -5
- data/examples/models/pdf.rb +0 -5
- data/examples/models/video.rb +0 -5
- data/has_media.gemspec +2 -2
- data/lib/generators/active_record/has_media_generator.rb +1 -1
- data/lib/generators/active_record/templates/migration.rb +2 -1
- data/lib/generators/templates/has_media.rb +9 -3
- data/lib/has_media/models/medium.rb +2 -17
- data/lib/has_media/uploaders/medium_uploader.rb +6 -2
- data/lib/has_media.rb +1 -59
- data/spec/has_media_spec.rb +21 -4
- metadata +4 -4
data/README.rdoc
CHANGED
@@ -70,8 +70,13 @@ Configuration take place in config/initializers/has_media.rb
|
|
70
70
|
# Set custom error messages
|
71
71
|
HasMedia.errors_messages = {:type_error => I18n.t('has_media.errors.type_error')}
|
72
72
|
|
73
|
-
# Set the allowed medium types
|
74
|
-
|
73
|
+
# Set the allowed medium types (used if no :only option given) for you application
|
74
|
+
# Set the allowed mime types for each medium type
|
75
|
+
HasMedia.medium_types = {
|
76
|
+
"Image" => ["image/jpeg", "image/png"],
|
77
|
+
"Video" => ["video/mp4"],
|
78
|
+
"Audio" => ["audio/mp3"]
|
79
|
+
}
|
75
80
|
|
76
81
|
# Set the extension of encoded files to use for each medium types (used in file_uri and file_path)
|
77
82
|
HasMedia.encoded_extensions = {
|
@@ -85,6 +90,53 @@ Configuration take place in config/initializers/has_media.rb
|
|
85
90
|
require uploader
|
86
91
|
end
|
87
92
|
|
93
|
+
== Encode with resque
|
94
|
+
|
95
|
+
Just add a callback on media class you want to encode via resque.
|
96
|
+
|
97
|
+
Example :
|
98
|
+
|
99
|
+
file : app/models/my_model_with_media.rb
|
100
|
+
|
101
|
+
class MyModelWithMedia < ActiveRecord::Base
|
102
|
+
has_one_medium :avatar, :only => :my_media
|
103
|
+
end
|
104
|
+
|
105
|
+
file : app/models/my_media.rb
|
106
|
+
|
107
|
+
class MyMedia < Medium
|
108
|
+
mount_uploader :file, MyMediaUploader
|
109
|
+
after_save :enqueue_encoding
|
110
|
+
|
111
|
+
def enqueue_encoding
|
112
|
+
Resque.enqueue(EncodeMyMedia, File.join(Rails.root, self.original_file_path))
|
113
|
+
end
|
114
|
+
end
|
115
|
+
|
116
|
+
file : resque/tasks/encode_my_media.rb
|
117
|
+
|
118
|
+
class EncodeMyMedia
|
119
|
+
@queue = :my_media
|
120
|
+
include Magick
|
121
|
+
|
122
|
+
def self.perform(file)
|
123
|
+
img = Image.read(file).first
|
124
|
+
img.thumbnail(50, 50).write(file.gsub(/\.[^.]+$/, "") + "_thumb.png")
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
Then you can manage your media like this :
|
129
|
+
|
130
|
+
@model = MyModelWithMedia.new
|
131
|
+
@model.avatar = File.open("my_file.jpg")
|
132
|
+
@model.save
|
133
|
+
@model.avatar.original_file_uri => "/media/my_media/:id/my_file.jpg"
|
134
|
+
@model.avatar.file_uri('thumb') => "/media/my_media/:id/my_file_thumb.jpg"
|
135
|
+
|
136
|
+
== Example application
|
137
|
+
|
138
|
+
https://github.com/klacointe/has_media_example
|
139
|
+
|
88
140
|
== Testing
|
89
141
|
|
90
142
|
Factory example using factory_girl :
|
@@ -100,9 +152,7 @@ Factory example using factory_girl :
|
|
100
152
|
|
101
153
|
== Todo
|
102
154
|
- Doc for testing (ActionController::TestUploadedFile is now obsolete in rails 3), add HasMediaTestHelper with stub_temp_file method
|
103
|
-
- Generator for initializer
|
104
155
|
- Add controller and views examples with overload system (form fields, display, ...)
|
105
|
-
- Fix problem with migrations generator timestamps
|
106
156
|
- Generator for models and uploaders (Type in Image, Video, Audio, Pdf) : rails generate has_media_model Type Name
|
107
157
|
- Document code
|
108
158
|
- add example app on github
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.2
|
data/examples/models/audio.rb
CHANGED
data/examples/models/image.rb
CHANGED
data/examples/models/pdf.rb
CHANGED
data/examples/models/video.rb
CHANGED
data/has_media.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{has_media}
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["klacointe", "spk"]
|
12
|
-
s.date = %q{2010-11-
|
12
|
+
s.date = %q{2010-11-05}
|
13
13
|
s.description = %q{Media Managment Library for ActiveRecord and Carrierwave}
|
14
14
|
s.email = %q{kevinlacointe@gmail.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -9,7 +9,7 @@ module ActiveRecord
|
|
9
9
|
source_root File.expand_path("../templates", __FILE__)
|
10
10
|
|
11
11
|
def copy_has_media_migration
|
12
|
-
migration_template "migration.rb", "db/migrate/
|
12
|
+
migration_template "migration.rb", "db/migrate/create_has_media"
|
13
13
|
end
|
14
14
|
|
15
15
|
# Implement the required interface for Rails::Generators::Migration.
|
@@ -1,4 +1,4 @@
|
|
1
|
-
class
|
1
|
+
class CreateHasMedia < ActiveRecord::Migration
|
2
2
|
def self.up
|
3
3
|
create_table :media do |t|
|
4
4
|
t.integer :width
|
@@ -12,6 +12,7 @@ class HasMedia < ActiveRecord::Migration
|
|
12
12
|
t.string :type # required
|
13
13
|
t.string :status
|
14
14
|
t.string :context # required
|
15
|
+
t.string :file # used by carrierwave
|
15
16
|
t.timestamps
|
16
17
|
end
|
17
18
|
add_index :media, :encode_status
|
@@ -4,10 +4,16 @@ HasMedia.directory_path = "media"
|
|
4
4
|
# Set the base uri to access media
|
5
5
|
HasMedia.directory_uri = "/media"
|
6
6
|
|
7
|
-
# Set the allowed medium types for
|
8
|
-
#
|
7
|
+
# Set the allowed medium types for you application
|
8
|
+
# Set the allowed mime types for each medium type
|
9
|
+
#HasMedia.medium_types = {
|
10
|
+
# "Image" => ["image/jpeg", "image/png"],
|
11
|
+
# "Video" => ["video/mp4"],
|
12
|
+
# "Audio" => ["audio/mp3"]
|
13
|
+
#}
|
9
14
|
|
10
|
-
# Set the extension of encoded files to use for each medium types
|
15
|
+
# Set the extension of encoded files to use for each medium types
|
16
|
+
# This is used in file_uri and file_path
|
11
17
|
#HasMedia.encoded_extensions = {
|
12
18
|
# :image => 'png',
|
13
19
|
# :audio => 'ogg',
|
@@ -15,22 +15,9 @@ class Medium < ActiveRecord::Base
|
|
15
15
|
ENCODE_NOT_READY = 4
|
16
16
|
NO_ENCODING = 5
|
17
17
|
|
18
|
-
# Allowed MIME types for upload
|
19
|
-
# need custom configuration
|
20
|
-
# TODO: add errors if not type of file
|
21
|
-
@@mime_types = {
|
22
|
-
:video => HasMedia.videos_content_types,
|
23
|
-
:image => HasMedia.images_content_types,
|
24
|
-
:audio => ['audio/mpeg', 'audio/x-ms-wma', 'audio/x-wav'],
|
25
|
-
:flash => ['application/x-shockwave-flash'],
|
26
|
-
:pdf => ['application/pdf'],
|
27
|
-
}
|
28
|
-
|
29
|
-
# TODO : check that carrierwave destroy files on after detroy hook
|
30
18
|
after_destroy :remove_file_from_fs
|
31
19
|
after_initialize :set_default_encoding_status
|
32
20
|
|
33
|
-
|
34
21
|
scope :with_context, lambda {|context|
|
35
22
|
{ :conditions => { :context => context.to_s} }
|
36
23
|
}
|
@@ -51,14 +38,12 @@ class Medium < ActiveRecord::Base
|
|
51
38
|
mime_type = MIME::Types.type_for(value.path).first.content_type
|
52
39
|
end
|
53
40
|
only ||= ""
|
54
|
-
medium_types = HasMedia.medium_types
|
41
|
+
medium_types = HasMedia.medium_types.keys.collect{|c| Kernel.const_get(c)}
|
55
42
|
if only != "" and klass = Kernel.const_get(only.camelize)
|
56
43
|
medium_types = [klass]
|
57
44
|
end
|
58
45
|
klass = medium_types.find do |k|
|
59
|
-
|
60
|
-
k.handle_content_type?(mime_type)
|
61
|
-
end
|
46
|
+
HasMedia.medium_types[k.to_s].include?(mime_type)
|
62
47
|
end
|
63
48
|
if klass.nil?
|
64
49
|
object.media_errors = [HasMedia.errors_messages[:type_error]]
|
@@ -2,9 +2,7 @@
|
|
2
2
|
|
3
3
|
class MediumUploader < CarrierWave::Uploader::Base
|
4
4
|
|
5
|
-
# Choose what kind of storage to use for this uploader
|
6
5
|
storage :file
|
7
|
-
# storage :s3
|
8
6
|
|
9
7
|
# Override the directory where uploaded files will be stored
|
10
8
|
# This is a sensible default for uploaders that are meant to be mounted:
|
@@ -12,4 +10,10 @@ class MediumUploader < CarrierWave::Uploader::Base
|
|
12
10
|
type = ActiveSupport::Inflector.underscore(model.class.to_s)
|
13
11
|
"#{HasMedia.directory_path}/#{type}/#{model.id}"
|
14
12
|
end
|
13
|
+
|
14
|
+
# see https://gist.github.com/519484
|
15
|
+
def root
|
16
|
+
CarrierWave.root
|
17
|
+
end
|
18
|
+
|
15
19
|
end
|
data/lib/has_media.rb
CHANGED
@@ -8,50 +8,10 @@ module HasMedia
|
|
8
8
|
|
9
9
|
VERSION = "0.0.1"
|
10
10
|
|
11
|
-
@@medium_types =
|
11
|
+
@@medium_types = {}
|
12
12
|
@@store_dir = '/tmp'
|
13
13
|
@@directory_uri = ''
|
14
|
-
@@custom_models_path = nil
|
15
14
|
@@errors_messages = {:type_error => 'Wrong type'}
|
16
|
-
@@images_content_types = [
|
17
|
-
'image/jpeg',
|
18
|
-
'image/pjpeg',
|
19
|
-
'image/jpg',
|
20
|
-
'image/gif',
|
21
|
-
'image/png',
|
22
|
-
'image/x-png',
|
23
|
-
'image/jpg',
|
24
|
-
'image/x-ms-bmp',
|
25
|
-
'image/bmp',
|
26
|
-
'image/x-bmp',
|
27
|
-
'image/x-bitmap',
|
28
|
-
'image/x-xbitmap',
|
29
|
-
'image/x-win-bitmap',
|
30
|
-
'image/x-windows-bmp',
|
31
|
-
'image/ms-bmp',
|
32
|
-
'application/bmp',
|
33
|
-
'application/x-bmp',
|
34
|
-
'application/x-win-bitmap',
|
35
|
-
'application/preview',
|
36
|
-
'image/jp_',
|
37
|
-
'application/jpg',
|
38
|
-
'application/x-jpg',
|
39
|
-
'image/pipeg',
|
40
|
-
'image/vnd.swiftview-jpeg',
|
41
|
-
'image/x-xbitmap',
|
42
|
-
'application/png',
|
43
|
-
'application/x-png',
|
44
|
-
'image/gi_',
|
45
|
-
'image/x-citrix-pjpeg'
|
46
|
-
]
|
47
|
-
@@videos_content_types = [
|
48
|
-
'video/mpeg',
|
49
|
-
'video/mp4',
|
50
|
-
'video/quicktime',
|
51
|
-
'video/x-ms-wmv',
|
52
|
-
'video/x-flv',
|
53
|
-
'video/x-msvideo'
|
54
|
-
]
|
55
15
|
@@encoded_extensions = {
|
56
16
|
:image => 'png',
|
57
17
|
:audio => 'mp3',
|
@@ -83,24 +43,6 @@ module HasMedia
|
|
83
43
|
def self.directory_uri
|
84
44
|
@@directory_uri
|
85
45
|
end
|
86
|
-
def self.custom_models_path
|
87
|
-
@@custom_models_path
|
88
|
-
end
|
89
|
-
def self.custom_models_path=(value)
|
90
|
-
unless value.blank?
|
91
|
-
@@custom_models_path = value
|
92
|
-
Dir.glob(self.custom_models_path + '/*.rb').each do |model|
|
93
|
-
require model
|
94
|
-
end
|
95
|
-
end
|
96
|
-
end
|
97
|
-
# taken from http://github.com/technoweenie/attachment_fu/blob/master/lib/technoweenie/attachment_fu.rb
|
98
|
-
def self.images_content_types
|
99
|
-
@@images_content_types
|
100
|
-
end
|
101
|
-
def self.videos_content_types
|
102
|
-
@@videos_content_types
|
103
|
-
end
|
104
46
|
def self.errors_messages
|
105
47
|
@@errors_messages
|
106
48
|
end
|
data/spec/has_media_spec.rb
CHANGED
@@ -24,12 +24,17 @@ describe "HasMedia" do
|
|
24
24
|
before :all do
|
25
25
|
HasMedia.directory_path = 'tmp'
|
26
26
|
HasMedia.directory_uri = '/media'
|
27
|
+
HasMedia.medium_types = {
|
28
|
+
"Image" => ["image/jpeg"],
|
29
|
+
"Audio" => ["audio/wav"],
|
30
|
+
"Pdf" => ["application/pdf"],
|
31
|
+
"Video" => ["video/mp4"]
|
32
|
+
}
|
27
33
|
end
|
28
34
|
|
29
35
|
describe "fonctionalities" do
|
30
36
|
|
31
37
|
before :each do
|
32
|
-
HasMedia.medium_types = [Image, Video, Pdf, Audio]
|
33
38
|
@medium = MediumRelatedTest.new
|
34
39
|
@image = stub_temp_file('image.jpg', 'image/jpeg')
|
35
40
|
@audio = stub_temp_file('audio.wav', 'audio/wav')
|
@@ -180,7 +185,21 @@ describe "HasMedia" do
|
|
180
185
|
|
181
186
|
describe "Configuration" do
|
182
187
|
it "should check allowed medium types if no :only option given" do
|
183
|
-
HasMedia.medium_types =
|
188
|
+
HasMedia.medium_types = {
|
189
|
+
"Image" => ["image/jpeg"],
|
190
|
+
}
|
191
|
+
@pdf = stub_temp_file('Conversational_Capital _Explained.pdf', 'application/pdf')
|
192
|
+
@medium = MediumRelatedTest.new
|
193
|
+
@medium.pdf = @pdf
|
194
|
+
@medium.valid?
|
195
|
+
@medium.should_not be_valid
|
196
|
+
@medium.save.should be_false
|
197
|
+
@medium.errors.full_messages.include?(HasMedia.errors_messages[:type_error])
|
198
|
+
end
|
199
|
+
it "should check allowed mime type" do
|
200
|
+
HasMedia.medium_types = {
|
201
|
+
"Pdf" => ["image/jpeg"],
|
202
|
+
}
|
184
203
|
@pdf = stub_temp_file('Conversational_Capital _Explained.pdf', 'application/pdf')
|
185
204
|
@medium = MediumRelatedTest.new
|
186
205
|
@medium.pdf = @pdf
|
@@ -189,8 +208,6 @@ describe "HasMedia" do
|
|
189
208
|
@medium.save.should be_false
|
190
209
|
@medium.errors.full_messages.include?(HasMedia.errors_messages[:type_error])
|
191
210
|
end
|
192
|
-
|
193
|
-
|
194
211
|
end
|
195
212
|
|
196
213
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: has_media
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 19
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 2
|
9
|
-
-
|
10
|
-
version: 0.2.
|
9
|
+
- 2
|
10
|
+
version: 0.2.2
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- klacointe
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-11-
|
19
|
+
date: 2010-11-05 00:00:00 +01:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|