has_media 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -19,3 +19,4 @@ rdoc
19
19
  pkg
20
20
 
21
21
  ## PROJECT::SPECIFIC
22
+ uploads
data/README.rdoc CHANGED
@@ -1,8 +1,10 @@
1
- = has_media
1
+ = Has Media
2
2
 
3
3
  Media Managment Library for ActiveRecord and Carrierwave
4
4
 
5
- Easy way to link media (ie image, audio, pdf for now) on other activerecords models
5
+ Easy way to link media (ie image, audio, video, pdf, ...) on other activerecords models
6
+
7
+ Use branch rails2.3 for Rails 2.3.x and rails3 for Rails 3
6
8
 
7
9
  === Create migrations
8
10
 
@@ -27,8 +29,7 @@ Examples are in the /examples directory...
27
29
 
28
30
  == Configuration
29
31
 
30
- * Set custom uploaders/models path to overload has_media ones
31
- HasMedia.custom_models_path = File.dirname(__FILE__) + '/../../app/models/has_media'
32
+ * Configuration take place in config/initializers/has_media.rb
32
33
 
33
34
  * Set the directory path to use to store media
34
35
  HasMedia.directory_path = "media"
@@ -39,6 +40,21 @@ Examples are in the /examples directory...
39
40
  * Set custom error messages
40
41
  HasMedia.errors_messages = {:type_error => I18n.t('has_media.errors.type_error')}
41
42
 
43
+ * Set the allowed medium types for your application (used if no :only option given)
44
+ HasMedia.medium_types = [Image, Video, Audio]
45
+
46
+ * Set the extension of encoded files to use for each medium types (used in file_uri and file_path)
47
+ HasMedia.encoded_extensions = {
48
+ :image => 'png',
49
+ :audio => 'ogg',
50
+ :video => 'flv'
51
+ }
52
+
53
+ * Require you uploaders
54
+ Dir.glob(File.dirname(__FILE__) + '/../app/uploaders/*.rb').each do |uploader|
55
+ require uploader
56
+ end
57
+
42
58
  == Testing
43
59
 
44
60
  Factory example using factory_girl :
@@ -52,9 +68,13 @@ Factory example using factory_girl :
52
68
  f.file {ActionController::TestUploadedFile.new(File.join(Rails.root, '/spec/factories/image.jpg'), 'image/jpeg')}
53
69
  end
54
70
 
55
- == TODO
56
-
57
- * Add a video class
71
+ == TODO
72
+ * Doc for testing (ActionController::TestUploadedFile is now obsolete in rails 3), add HasMediaTestHelper with stub_temp_file method
73
+ * Generator for initializer
74
+ * Add controller and views examples with overload system (form fields, display, ...)
75
+ * Fix problem with migrations generator timestamps
76
+ * Generator for models and uploaders (Type in Image, Video, Audio, Pdf) :
77
+ rails generate has_media_model Type Name
58
78
 
59
79
  == Contributors
60
80
 
data/Rakefile CHANGED
@@ -1,5 +1,10 @@
1
- require 'rake'
2
- require 'rubygems'
1
+ #require 'rake'
2
+ #require 'rake/rdoctask'
3
+ #require 'rubygems'
4
+ #require 'rspec'
5
+ #require 'rspec/core'
6
+ #require 'rspec/core/rake_task'
7
+ #Spec::Core::RakeTask.new(:spec)
3
8
 
4
9
  begin
5
10
  require 'jeweler'
@@ -8,40 +13,39 @@ begin
8
13
  gem.summary = %Q{Media Managment Library for ActiveRecord and Carrierwave}
9
14
  gem.description = %Q{Media Managment Library for ActiveRecord and Carrierwave}
10
15
  gem.email = "kevinlacointe@gmail.com"
11
- gem.homepage = "http://github.com/AF83/has_media"
16
+ gem.homepage = "http://github.com/klacointe/has_media"
12
17
  gem.authors = ["klacointe", "spk"]
13
- gem.add_development_dependency "rspec", ">= 1.2.9"
14
- gem.add_dependency('carrierwave', '>=0.4.2')
15
- gem.add_dependency('activerecord', '>=2.3.4')
16
- gem.add_dependency('activesupport', '>=2.3.4')
18
+ gem.add_development_dependency "rspec", "<=1.3.0"
19
+ gem.add_dependency('carrierwave', '=0.4.5')
20
+ gem.add_dependency('activerecord', '=2.3.8')
21
+ gem.add_dependency('activesupport', '=2.3.8')
22
+ gem.add_dependency('mime-types', '>=1.16')
17
23
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
18
24
  end
19
25
  rescue LoadError
20
26
  puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
21
27
  end
22
28
 
23
- require 'spec/rake/spectask'
24
- Spec::Rake::SpecTask.new(:spec) do |spec|
25
- spec.libs << 'lib' << 'spec'
26
- spec.spec_files = FileList['spec/**/*_spec.rb']
27
- end
28
-
29
- Spec::Rake::SpecTask.new(:rcov) do |spec|
30
- spec.libs << 'lib' << 'spec'
31
- spec.pattern = 'spec/**/*_spec.rb'
32
- spec.rcov = true
33
- end
34
-
35
- task :spec => :check_dependencies
36
-
37
- task :default => :spec
38
-
39
- require 'rake/rdoctask'
40
- Rake::RDocTask.new do |rdoc|
41
- version = File.exist?('VERSION') ? File.read('VERSION') : ""
42
-
43
- rdoc.rdoc_dir = 'rdoc'
44
- rdoc.title = "has_media #{version}"
45
- rdoc.rdoc_files.include('README*')
46
- rdoc.rdoc_files.include('lib/**/*.rb')
47
- end
29
+ #Spec::Rake::SpecTask.new(:spec) do |spec|
30
+ # spec.libs << 'lib' << 'spec'
31
+ # spec.spec_files = FileList['spec/**/*_spec.rb']
32
+ #end
33
+ #
34
+ #Spec::Rake::SpecTask.new(:rcov) do |spec|
35
+ # spec.libs << 'lib' << 'spec'
36
+ # spec.pattern = 'spec/**/*_spec.rb'
37
+ # spec.rcov = true
38
+ #end
39
+ #
40
+ #task :spec => :check_dependencies
41
+ #
42
+ #task :default => :spec
43
+ #
44
+ #Rake::RDocTask.new do |rdoc|
45
+ # version = File.exist?('VERSION') ? File.read('VERSION') : ""
46
+ #
47
+ # rdoc.rdoc_dir = 'rdoc'
48
+ # rdoc.title = "has_media #{version}"
49
+ # rdoc.rdoc_files.include('README*')
50
+ # rdoc.rdoc_files.include('lib/**/*.rb')
51
+ #end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.2
1
+ 0.1.3
data/has_media.gemspec ADDED
@@ -0,0 +1,99 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE DIRECTLY
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run the gemspec command
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{has_media}
8
+ s.version = "0.1.3"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["klacointe", "spk"]
12
+ s.date = %q{2010-10-06}
13
+ s.description = %q{Media Managment Library for ActiveRecord and Carrierwave}
14
+ s.email = %q{kevinlacointe@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ ".rspec",
23
+ "LICENSE",
24
+ "README.rdoc",
25
+ "Rakefile",
26
+ "VERSION",
27
+ "examples/models/audio.rb",
28
+ "examples/models/image.rb",
29
+ "examples/models/pdf.rb",
30
+ "examples/models/video.rb",
31
+ "examples/uploaders/audio_uploader.rb",
32
+ "examples/uploaders/image_uploader.rb",
33
+ "examples/uploaders/pdf_uploader.rb",
34
+ "examples/uploaders/video_uploader.rb",
35
+ "generators/has_media/has_media_generator.rb",
36
+ "generators/has_media/templates/create_media.rb",
37
+ "generators/has_media/templates/create_media_links.rb",
38
+ "has_media.gemspec",
39
+ "lib/has_media.rb",
40
+ "lib/has_media/models/media_link.rb",
41
+ "lib/has_media/models/medium.rb",
42
+ "lib/has_media/uploaders/medium_uploader.rb",
43
+ "lib/tasks/rspec.rake",
44
+ "spec/fixtures/media/Conversational_Capital _Explained.pdf",
45
+ "spec/fixtures/media/audio.wav",
46
+ "spec/fixtures/media/image.jpg",
47
+ "spec/fixtures/media/image_bis.jpg",
48
+ "spec/fixtures/media/lc_pdf_overview_format.pdf",
49
+ "spec/fixtures/models/image.rb",
50
+ "spec/fixtures/uploaders/uploader_with_exception.rb",
51
+ "spec/has_media_spec.rb",
52
+ "spec/spec_helper.rb"
53
+ ]
54
+ s.homepage = %q{http://github.com/klacointe/has_media}
55
+ s.rdoc_options = ["--charset=UTF-8"]
56
+ s.require_paths = ["lib"]
57
+ s.rubygems_version = %q{1.3.7}
58
+ s.summary = %q{Media Managment Library for ActiveRecord and Carrierwave}
59
+ s.test_files = [
60
+ "spec/spec_helper.rb",
61
+ "spec/fixtures/models/image.rb",
62
+ "spec/fixtures/uploaders/uploader_with_exception.rb",
63
+ "spec/has_media_spec.rb",
64
+ "examples/models/image.rb",
65
+ "examples/models/pdf.rb",
66
+ "examples/models/audio.rb",
67
+ "examples/models/video.rb",
68
+ "examples/uploaders/pdf_uploader.rb",
69
+ "examples/uploaders/audio_uploader.rb",
70
+ "examples/uploaders/video_uploader.rb",
71
+ "examples/uploaders/image_uploader.rb"
72
+ ]
73
+
74
+ if s.respond_to? :specification_version then
75
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
76
+ s.specification_version = 3
77
+
78
+ if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
79
+ s.add_development_dependency(%q<rspec>, ["<= 1.3.0"])
80
+ s.add_runtime_dependency(%q<carrierwave>, ["= 0.4.5"])
81
+ s.add_runtime_dependency(%q<activerecord>, ["= 2.3.8"])
82
+ s.add_runtime_dependency(%q<activesupport>, ["= 2.3.8"])
83
+ s.add_runtime_dependency(%q<mime-types>, [">= 1.16"])
84
+ else
85
+ s.add_dependency(%q<rspec>, ["<= 1.3.0"])
86
+ s.add_dependency(%q<carrierwave>, ["= 0.4.5"])
87
+ s.add_dependency(%q<activerecord>, ["= 2.3.8"])
88
+ s.add_dependency(%q<activesupport>, ["= 2.3.8"])
89
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
90
+ end
91
+ else
92
+ s.add_dependency(%q<rspec>, ["<= 1.3.0"])
93
+ s.add_dependency(%q<carrierwave>, ["= 0.4.5"])
94
+ s.add_dependency(%q<activerecord>, ["= 2.3.8"])
95
+ s.add_dependency(%q<activesupport>, ["= 2.3.8"])
96
+ s.add_dependency(%q<mime-types>, [">= 1.16"])
97
+ end
98
+ end
99
+
@@ -15,13 +15,6 @@ class Medium < ActiveRecord::Base
15
15
  ENCODE_NOT_READY = 4
16
16
  NO_ENCODING = 5
17
17
 
18
- EXTENSIONS = {
19
- :image => 'png',
20
- :audio => 'mp3',
21
- :pdf => 'pdf',
22
- :video => 'flv'
23
- }
24
-
25
18
  # Allowed MIME types for upload
26
19
  # need custom configuration
27
20
  # TODO: add errors if not type of file
@@ -51,16 +44,20 @@ class Medium < ActiveRecord::Base
51
44
  return name.downcase
52
45
  end
53
46
 
54
- # FIXME : get medium types from available classes, use has_media.medium_types
55
47
  def self.new_from_value(object, value, context, encode, only)
48
+ if value.respond_to?(:content_type)
49
+ mime_type = value.content_type
50
+ else
51
+ mime_type = MIME::Types.type_for(value.path).first.content_type
52
+ end
56
53
  only ||= ""
57
54
  medium_types = HasMedia.medium_types
58
- if only != "" and klass = Kernel.const_get(only.capitalize)
55
+ if only != "" and klass = Kernel.const_get(only.camelize)
59
56
  medium_types = [klass]
60
57
  end
61
58
  klass = medium_types.find do |k|
62
59
  if k.respond_to?(:handle_content_type?)
63
- k.handle_content_type?(value.content_type)
60
+ k.handle_content_type?(mime_type)
64
61
  end
65
62
  end
66
63
  if klass.nil?
@@ -68,9 +65,13 @@ class Medium < ActiveRecord::Base
68
65
  return
69
66
  end
70
67
  medium = klass.new
71
- medium.filename = self.sanitize(value.original_filename)
68
+ if value.respond_to?(:original_filename)
69
+ medium.filename = self.sanitize(value.original_filename)
70
+ else
71
+ medium.filename = self.sanitize(File.basename(value.path))
72
+ end
72
73
  medium.file = value
73
- medium.content_type = value.content_type
74
+ medium.content_type = mime_type
74
75
  medium.context = context
75
76
  medium.encode_status = (encode == "false" ? NO_ENCODING : ENCODE_WAIT)
76
77
  medium.save
@@ -121,18 +122,22 @@ class Medium < ActiveRecord::Base
121
122
  self.file.store_dir
122
123
  end
123
124
  # system path for a medium
124
- def file_path(thumbnail = nil)
125
- final_name = filename.gsub /\.[^.]+$/, '.' + file_extension
126
- final_name[-4,0] = "_#{thumbnail}" if thumbnail
127
- File.join(directory_path, final_name)
125
+ def file_path(version = nil)
126
+ File.join(directory_path, encoded_file_name(version))
128
127
  end
129
128
 
130
129
  # http uri for a medium
131
- def file_uri(thumbnail = nil)
132
- final_name = filename.gsub /\.[^.]+$/, '.' + file_extension
133
- final_name[-4,0] = "_#{thumbnail}" if thumbnail
134
- File.join(directory_uri, final_name)
130
+ def file_uri(version = nil)
131
+ File.join(directory_uri, encoded_file_name(version))
135
132
  end
133
+
134
+ def encoded_file_name(version = nil)
135
+ # remove original extension and add the encoded extension
136
+ final_name = filename.gsub(/\.[^.]{1,4}$/, "") + '.' + file_extension
137
+ final_name[-4,0] = "_#{version}" if version
138
+ final_name
139
+ end
140
+
136
141
  # http uri of directory which stores media
137
142
  def directory_uri
138
143
  File.join(HasMedia.directory_uri,
@@ -145,7 +150,11 @@ class Medium < ActiveRecord::Base
145
150
  end
146
151
 
147
152
  def file_extension
148
- EXTENSIONS[type.to_s.downcase.to_sym]
153
+ sym = type.underscore.to_sym
154
+ unless HasMedia.encoded_extensions.keys.include?(sym)
155
+ raise Exception.new("You need to add encoded extension configuration for :#{sym}")
156
+ end
157
+ HasMedia.encoded_extensions[sym]
149
158
  end
150
159
 
151
160
  private
@@ -161,6 +170,6 @@ private
161
170
  # TODO : remove all files, not only the original one
162
171
  def remove_file_from_fs
163
172
  require 'fileutils'
164
- FileUtils.rm_rf(self.original_file_path)
173
+ FileUtils.rm_rf(self.directory_path)
165
174
  end
166
175
  end
data/lib/has_media.rb CHANGED
@@ -2,6 +2,7 @@ require 'rubygems'
2
2
  require 'active_record'
3
3
  require 'active_support'
4
4
  require 'carrierwave'
5
+ require 'mime/types'
5
6
 
6
7
  module HasMedia
7
8
 
@@ -51,6 +52,12 @@ module HasMedia
51
52
  'video/x-flv',
52
53
  'video/x-msvideo'
53
54
  ]
55
+ @@encoded_extensions = {
56
+ :image => 'png',
57
+ :audio => 'mp3',
58
+ :pdf => 'pdf',
59
+ :video => 'flv'
60
+ }
54
61
 
55
62
  def self.medium_types=(value)
56
63
  @@medium_types = value
@@ -58,6 +65,12 @@ module HasMedia
58
65
  def self.medium_types
59
66
  @@medium_types
60
67
  end
68
+ def self.encoded_extensions=(value)
69
+ @@encoded_extensions = value
70
+ end
71
+ def self.encoded_extensions
72
+ @@encoded_extensions
73
+ end
61
74
  def self.directory_path=(value)
62
75
  @@store_dir = value
63
76
  end
@@ -0,0 +1,39 @@
1
+ begin
2
+ require 'rspec/core'
3
+ require 'rspec/core/rake_task'
4
+ rescue MissingSourceFile
5
+ module RSpec
6
+ module Core
7
+ class RakeTask
8
+ def initialize(name)
9
+ task name do
10
+ # if rspec-rails is a configured gem, this will output helpful material and exit ...
11
+ require File.expand_path(File.dirname(__FILE__) + "/../../config/environment")
12
+
13
+ # ... otherwise, do this:
14
+ raise <<-MSG
15
+
16
+ #{"*" * 80}
17
+ * You are trying to run an rspec rake task defined in
18
+ * #{__FILE__},
19
+ * but rspec can not be found in vendor/gems, vendor/plugins or system gems.
20
+ #{"*" * 80}
21
+ MSG
22
+ end
23
+ end
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ Rake.application.instance_variable_get('@tasks').delete('default')
30
+
31
+ task :default => :spec
32
+
33
+ namespace :spec do
34
+ desc "Run the code examples in spec"
35
+ RSpec::Core::RakeTask.new(:spec) do |t|
36
+ t.pattern = "./spec/*_spec.rb"
37
+ end
38
+ end
39
+
@@ -1,182 +1,196 @@
1
1
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
2
 
3
+ # load models and uploaders examples
4
+ Dir.glob(File.dirname(__FILE__) + '/../examples/uploaders/*.rb').each do |uploader|
5
+ require uploader
6
+ end
7
+
8
+ Dir.glob(File.dirname(__FILE__) + '/../examples/models/*.rb').each do |model|
9
+ require model
10
+ end
11
+ class MediumRelatedTest < ActiveRecord::Base
12
+ include HasMedia
13
+ has_one_medium :image, :only => :image
14
+ has_many_media :images, :only => :image
15
+ has_one_medium :audio, :only => :audio
16
+ has_many_media :audios, :only => :audio
17
+ has_one_medium :image_no_encode, :only => :image, :encode => false
18
+ has_one_medium :pdf, :encode => false
19
+ end
20
+
21
+
3
22
  describe "HasMedia" do
4
23
 
5
24
  before :all do
6
- # load models and uploaders examples
7
- Dir.glob(File.dirname(__FILE__) + '/../examples/uploaders/*.rb').each do |uploader|
8
- require uploader
9
- end
10
-
11
- Dir.glob(File.dirname(__FILE__) + '/../examples/models/*.rb').each do |model|
12
- require model
13
- end
14
-
15
- class MediumRelatedTest < ActiveRecord::Base
16
- include HasMedia
17
- has_one_medium :image, :only => :image
18
- has_many_media :images, :only => :image
19
- has_one_medium :audio, :only => :audio
20
- has_many_media :audios, :only => :audio
21
- has_one_medium :image_no_encode, :only => :image, :encode => false
22
- has_one_medium :pdf, :encode => false
23
- end
24
25
  HasMedia.directory_path = 'tmp'
25
26
  HasMedia.directory_uri = '/media'
26
- HasMedia.medium_types = [Image, Video, Pdf, Audio]
27
27
  end
28
28
 
29
- before :each do
30
- @medium = MediumRelatedTest.new
31
- @image = ActionController::TestUploadedFile.new('spec/fixtures/media/image.jpg', 'image/jpeg')
32
- @audio = ActionController::TestUploadedFile.new('spec/fixtures/media/audio.wav', 'audio/wav')
33
- @image_bis = ActionController::TestUploadedFile.new('spec/fixtures/media/image_bis.jpg', 'image/jpeg')
34
- @pdf = ActionController::TestUploadedFile.new('spec/fixtures/media/Conversational_Capital _Explained.pdf', 'application/pdf')
35
- end
29
+ describe "fonctionalities" do
36
30
 
37
- it 'should not have 2 has_one_medium with same context' do
38
- lambda {
39
- class Prout < ActiveRecord::Base
40
- include HasMedia
41
- has_one_medium :prout, :only => :image
42
- has_one_medium :prout, :only => :audio
31
+ before :each do
32
+ HasMedia.medium_types = [Image, Video, Pdf, Audio]
33
+ @medium = MediumRelatedTest.new
34
+ @image = stub_temp_file('image.jpg', 'image/jpeg')
35
+ @audio = stub_temp_file('audio.wav', 'audio/wav')
36
+ @image_bis = stub_temp_file('image_bis.jpg', 'image/jpeg')
37
+ @pdf = stub_temp_file('Conversational_Capital _Explained.pdf', 'application/pdf')
43
38
  end
44
- }.should raise_error(Exception)
45
- end
46
39
 
47
- it "should define accessors" do
48
- @medium.methods.should include("image")
49
- @medium.methods.should include("images")
50
- @medium.methods.should include("audio")
51
- @medium.methods.should include("audios")
52
- @medium.methods.should include("pdf")
53
- end
40
+ it 'should not have 2 has_one_medium with same context' do
41
+ lambda {
42
+ class Prout < ActiveRecord::Base
43
+ include HasMedia
44
+ has_one_medium :prout, :only => :image
45
+ has_one_medium :prout, :only => :audio
46
+ end
47
+ }.should raise_error(Exception)
48
+ end
54
49
 
55
- it "should define setters" do
56
- @medium.methods.should include("image=")
57
- @medium.methods.should include("images=")
58
- @medium.methods.should include("audio=")
59
- @medium.methods.should include("audios=")
60
- @medium.methods.should include("pdf=")
61
- end
50
+ it "should define accessors" do
51
+ @medium.methods.should include("image")
52
+ @medium.methods.should include("images")
53
+ @medium.methods.should include("audio")
54
+ @medium.methods.should include("audios")
55
+ @medium.methods.should include("pdf")
56
+ end
62
57
 
63
- it "should associate image to mediated object" do
64
- @medium.image = @image
65
- @medium.save!
66
- @medium.image.should_not be_nil
67
- @medium.image.class.should == Image
68
- end
58
+ it "should define setters" do
59
+ @medium.methods.should include("image=")
60
+ @medium.methods.should include("images=")
61
+ @medium.methods.should include("audio=")
62
+ @medium.methods.should include("audios=")
63
+ @medium.methods.should include("pdf=")
64
+ end
69
65
 
70
- it "should associate audio to mediated object" do
71
- @medium.audio = @audio
72
- @medium.save!
73
- @medium.audio.should_not be_nil
74
- @medium.audio.class.should == Audio
75
- end
66
+ it "should associate image to mediated object" do
67
+ @medium.image = @image
68
+ @medium.save!
69
+ @medium.image.should_not be_nil
70
+ @medium.image.class.should == Image
71
+ end
76
72
 
77
- it "should associate pdf to mediated object" do
78
- @medium.pdf = @pdf
79
- @medium.save!
80
- @medium.pdf.should_not be_nil
81
- @medium.pdf.class.should == Pdf
82
- end
73
+ it "should associate audio to mediated object" do
74
+ @medium.audio = @audio
75
+ @medium.save!
76
+ @medium.audio.should_not be_nil
77
+ @medium.audio.class.should == Audio
78
+ end
83
79
 
84
- it "should add both audio and image " do
85
- @medium.image = @image
86
- @medium.audio = @audio
87
- @medium.save!
88
- @medium.audio.should_not be_nil
89
- @medium.audio.class.should == Audio
90
- @medium.audio.filename.should == @audio.original_filename
91
- @medium.audio.content_type.should == "audio/wav"
92
- @medium.audio
93
- @medium.image.should_not be_nil
94
- @medium.image.class.should == Image
95
- @medium.image.filename.should == @image.original_filename
96
- @medium.image.content_type.should == "image/jpeg"
97
- end
80
+ it "should associate pdf to mediated object" do
81
+ @medium.pdf = @pdf
82
+ @medium.save!
83
+ @medium.pdf.should_not be_nil
84
+ @medium.pdf.class.should == Pdf
85
+ end
98
86
 
99
- it "should replace media when in has_one_medium relations" do
100
- @medium.image = @image_bis
101
- @medium.save
102
- @medium.image.filename.should == @image_bis.original_filename
103
- @medium.image = @image
104
- @medium.save
105
- @medium.image.filename.should == @image.original_filename
106
- end
87
+ it "should add both audio and image " do
88
+ @medium.image = @image
89
+ @medium.audio = @audio
90
+ @medium.save!
91
+ @medium.audio.should_not be_nil
92
+ @medium.audio.class.should == Audio
93
+ @medium.audio.filename.should == @audio.original_filename
94
+ @medium.audio.content_type.should == "audio/wav"
95
+ @medium.audio
96
+ @medium.image.should_not be_nil
97
+ @medium.image.class.should == Image
98
+ @medium.image.filename.should == @image.original_filename
99
+ @medium.image.content_type.should == "image/jpeg"
100
+ end
107
101
 
108
- it "should destroy files from fs when Related Model is destroyed" do
109
- @medium.image = @image
110
- @medium.save!
111
- path = @medium.image.original_file_path
112
- File.exist?(path).should be_true
113
- @medium.destroy
114
- File.exist?(path).should be_false
115
- end
102
+ it "should replace media when in has_one_medium relations" do
103
+ @medium.image = @image_bis
104
+ @medium.save
105
+ @medium.image.filename.should == @image_bis.original_filename
106
+ @medium.reload
107
+ @medium.image = @image
108
+ @medium.save
109
+ @medium.image.filename.should == @image.original_filename
110
+ end
116
111
 
117
- [:encoding?, :ready?, :failed?].each do |method|
118
- [:image, :audio, :images, :audios].each do |context|
119
- class_eval %{
120
- it "should responds to #{context}.#{method}" do
121
- @medium.send("#{context}=", @#{context})
122
- @medium.save!
123
- media = @medium.send("#{context}")
124
- if media.is_a? Array
125
- media.each do |medium|
126
- [true, false].include?(medium.send("#{method}"))
112
+ it "should destroy files from fs when Related Model is destroyed" do
113
+ @medium.image = @image
114
+ @medium.save!
115
+ path = @medium.image.original_file_path
116
+ File.exist?(path).should be_true
117
+ @medium.destroy
118
+ File.exist?(path).should be_false
119
+ end
120
+
121
+ [:encoding?, :ready?, :failed?].each do |method|
122
+ [:image, :audio, :images, :audios].each do |context|
123
+ class_eval %{
124
+ it "should responds to #{context}.#{method}" do
125
+ @medium.send("#{context}=", @#{context})
126
+ @medium.save!
127
+ media = @medium.send("#{context}")
128
+ if media.is_a? Array
129
+ media.each do |medium|
130
+ [true, false].include?(medium.send("#{method}"))
131
+ end
132
+ else
133
+ [true, false].include?(media.send("#{method}"))
127
134
  end
128
- else
129
- [true, false].include?(media.send("#{method}"))
130
135
  end
131
- end
132
- }
136
+ }
137
+ end
133
138
  end
134
- end
135
139
 
136
- it "should add several has_many_media for the same context" do
137
- @medium.images = [@image, @image_bis]
138
- @medium.save!
139
- @medium.media.size.should == 2
140
- end
140
+ it "should add several has_many_media for the same context" do
141
+ @medium.images = [@image, @image_bis]
142
+ @medium.save!
143
+ @medium.media.size.should == 2
144
+ end
141
145
 
142
- it "should have the right encode status" do
143
- @medium.image_no_encode = @image
144
- @medium.save!
145
- @medium.image_no_encode.encode_status.should == Medium::NO_ENCODING
146
- @medium.image = @image_bis
147
- @medium.save!
148
- @medium.image.encode_status.should == Medium::ENCODE_WAIT
149
- end
146
+ it "should have the right encode status" do
147
+ @medium.image_no_encode = @image
148
+ @medium.save!
149
+ @medium.image_no_encode.encode_status.should == Medium::NO_ENCODING
150
+ @medium.image = @image_bis
151
+ @medium.save!
152
+ @medium.image.encode_status.should == Medium::ENCODE_WAIT
153
+ end
150
154
 
151
- it "should have an original uri" do
152
- @medium.image = @image
153
- @medium.save!
154
- @medium.image.original_file_uri.should == File.join(HasMedia.directory_uri,
155
- ActiveSupport::Inflector.underscore(@medium.image.type),
156
- @medium.image.id.to_s,
157
- @medium.image.filename)
158
- end
155
+ it "should have an original uri" do
156
+ @medium.image = @image
157
+ @medium.save!
158
+ @medium.image.original_file_uri.should == File.join(HasMedia.directory_uri,
159
+ ActiveSupport::Inflector.underscore(@medium.image.type),
160
+ @medium.image.id.to_s,
161
+ @medium.image.filename)
162
+ end
159
163
 
160
- it "pdf should exist" do
161
- @medium.pdf = @pdf
162
- @medium.save!
163
- path = @medium.pdf.file_path
164
- File.exist?(path).should be_true
165
- end
164
+ it "pdf should exist" do
165
+ @medium.pdf = @pdf
166
+ @medium.save!
167
+ path = @medium.pdf.file_path
168
+ File.exist?(path).should be_true
169
+ end
166
170
 
167
- it "should add errors on parent model if type is not allowed" do
168
- @image = ActionController::TestUploadedFile.new('spec/fixtures/media/image.jpg', 'image/jpeg')
169
- @medium.audio = @image
170
- @medium.valid?
171
- @medium.should_not be_valid
172
- @medium.save.should be_false
173
- @medium.errors.full_messages.include?(HasMedia.errors_messages[:type_error])
171
+ it "should add errors on parent model if type is not allowed" do
172
+ @image = stub_temp_file('image.jpg', 'image/jpeg')
173
+ @medium.audio = @image
174
+ @medium.valid?
175
+ @medium.should_not be_valid
176
+ @medium.save.should be_false
177
+ @medium.errors.full_messages.include?(HasMedia.errors_messages[:type_error])
178
+ end
174
179
  end
175
180
 
176
- it "should load custom models" do
177
- lambda {
178
- HasMedia.custom_models_path = File.join(File.dirname(__FILE__), 'fixtures/models/')
179
- }.should raise_error(RuntimeError, "this exception is normal, useful for testing")
181
+ describe "Configuration" do
182
+ it "should check allowed medium types if no :only option given" do
183
+ HasMedia.medium_types = [Image]
184
+ @pdf = stub_temp_file('Conversational_Capital _Explained.pdf', 'application/pdf')
185
+ @medium = MediumRelatedTest.new
186
+ @medium.pdf = @pdf
187
+ @medium.valid?
188
+ @medium.should_not be_valid
189
+ @medium.save.should be_false
190
+ @medium.errors.full_messages.include?(HasMedia.errors_messages[:type_error])
191
+ end
192
+
193
+
180
194
  end
181
195
 
182
196
  end
data/spec/spec_helper.rb CHANGED
@@ -1,16 +1,20 @@
1
1
  $LOAD_PATH.unshift(File.dirname(__FILE__))
2
2
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
3
- require 'spec'
4
- require 'spec/autorun'
5
3
  require 'rubygems'
4
+ require 'rspec'
5
+ require 'rspec/core'
6
+ require 'rspec/core/rake_task'
6
7
  require 'action_controller'
7
- require 'action_controller/test_process'
8
+ #require 'action_controller/test_process'
9
+ require 'action_dispatch'
10
+ require 'action_dispatch/testing/test_process'
8
11
  require 'has_media'
9
12
 
10
13
  dbconfig = {
11
- :adapter => 'sqlite3',
12
- :database => ':memory:',
13
- # :database => 'has_media.sqlite'
14
+ :adapter => 'mysql',
15
+ :database => 'has_media',
16
+ :username => 'test',
17
+ :password => 'test'
14
18
  }
15
19
 
16
20
  ActiveRecord::Base.establish_connection(dbconfig)
@@ -46,8 +50,42 @@ class TestMigration < ActiveRecord::Migration
46
50
  end
47
51
  end
48
52
 
49
- Spec::Runner.configure do |config|
50
- config.before(:all) { TestMigration.up }
51
- config.after(:all) { TestMigration.down }
52
- config.after(:each) { Medium.destroy_all; MediaLink.destroy_all; MediumRelatedTest.destroy_all; }
53
+ RSpec.configure do |c|
54
+ c.before(:all) do
55
+ TestMigration.up
56
+ end
57
+ c.before(:each) do
58
+ @real_world = RSpec.world
59
+ RSpec.instance_variable_set(:@world, RSpec::Core::World.new)
60
+ end
61
+ c.after(:all) do
62
+ TestMigration.down
63
+ end
64
+ c.after(:each) do
65
+ RSpec.instance_variable_set(:@world, @real_world)
66
+ Medium.destroy_all
67
+ MediaLink.destroy_all
68
+ MediumRelatedTest.destroy_all
69
+ end
70
+ end
71
+
72
+
73
+ def file_path( *paths )
74
+ File.expand_path(File.join(File.dirname(__FILE__), 'fixtures', 'media', *paths))
75
+ end
76
+
77
+ def stub_temp_file(filename, mime_type=nil, fake_name=nil)
78
+ raise "#{file_path(filename)} file does not exist" unless File.exist?(file_path(filename))
79
+
80
+ t = Tempfile.new(filename)
81
+ FileUtils.copy_file(file_path(filename), t.path)
82
+
83
+ # This is stupid, but for some reason rspec won't play nice...
84
+ eval <<-EOF
85
+ def t.original_filename; '#{fake_name || filename}'; end
86
+ def t.content_type; '#{mime_type}'; end
87
+ def t.local_path; path; end
88
+ EOF
89
+
90
+ return t
53
91
  end
metadata CHANGED
@@ -1,12 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_media
3
3
  version: !ruby/object:Gem::Version
4
+ hash: 29
4
5
  prerelease: false
5
6
  segments:
6
7
  - 0
7
8
  - 1
8
- - 2
9
- version: 0.1.2
9
+ - 3
10
+ version: 0.1.3
10
11
  platform: ruby
11
12
  authors:
12
13
  - klacointe
@@ -15,65 +16,88 @@ autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2010-06-09 00:00:00 +02:00
19
+ date: 2010-10-06 00:00:00 +02:00
19
20
  default_executable:
20
21
  dependencies:
21
22
  - !ruby/object:Gem::Dependency
22
23
  name: rspec
23
24
  prerelease: false
24
25
  requirement: &id001 !ruby/object:Gem::Requirement
26
+ none: false
25
27
  requirements:
26
- - - ">="
28
+ - - <=
27
29
  - !ruby/object:Gem::Version
30
+ hash: 27
28
31
  segments:
29
32
  - 1
30
- - 2
31
- - 9
32
- version: 1.2.9
33
+ - 3
34
+ - 0
35
+ version: 1.3.0
33
36
  type: :development
34
37
  version_requirements: *id001
35
38
  - !ruby/object:Gem::Dependency
36
39
  name: carrierwave
37
40
  prerelease: false
38
41
  requirement: &id002 !ruby/object:Gem::Requirement
42
+ none: false
39
43
  requirements:
40
- - - ">="
44
+ - - "="
41
45
  - !ruby/object:Gem::Version
46
+ hash: 5
42
47
  segments:
43
48
  - 0
44
49
  - 4
45
- - 2
46
- version: 0.4.2
50
+ - 5
51
+ version: 0.4.5
47
52
  type: :runtime
48
53
  version_requirements: *id002
49
54
  - !ruby/object:Gem::Dependency
50
55
  name: activerecord
51
56
  prerelease: false
52
57
  requirement: &id003 !ruby/object:Gem::Requirement
58
+ none: false
53
59
  requirements:
54
- - - ">="
60
+ - - "="
55
61
  - !ruby/object:Gem::Version
62
+ hash: 19
56
63
  segments:
57
64
  - 2
58
65
  - 3
59
- - 4
60
- version: 2.3.4
66
+ - 8
67
+ version: 2.3.8
61
68
  type: :runtime
62
69
  version_requirements: *id003
63
70
  - !ruby/object:Gem::Dependency
64
71
  name: activesupport
65
72
  prerelease: false
66
73
  requirement: &id004 !ruby/object:Gem::Requirement
74
+ none: false
67
75
  requirements:
68
- - - ">="
76
+ - - "="
69
77
  - !ruby/object:Gem::Version
78
+ hash: 19
70
79
  segments:
71
80
  - 2
72
81
  - 3
73
- - 4
74
- version: 2.3.4
82
+ - 8
83
+ version: 2.3.8
75
84
  type: :runtime
76
85
  version_requirements: *id004
86
+ - !ruby/object:Gem::Dependency
87
+ name: mime-types
88
+ prerelease: false
89
+ requirement: &id005 !ruby/object:Gem::Requirement
90
+ none: false
91
+ requirements:
92
+ - - ">="
93
+ - !ruby/object:Gem::Version
94
+ hash: 47
95
+ segments:
96
+ - 1
97
+ - 16
98
+ version: "1.16"
99
+ type: :runtime
100
+ version_requirements: *id005
77
101
  description: Media Managment Library for ActiveRecord and Carrierwave
78
102
  email: kevinlacointe@gmail.com
79
103
  executables: []
@@ -86,6 +110,7 @@ extra_rdoc_files:
86
110
  files:
87
111
  - .document
88
112
  - .gitignore
113
+ - .rspec
89
114
  - LICENSE
90
115
  - README.rdoc
91
116
  - Rakefile
@@ -101,10 +126,12 @@ files:
101
126
  - generators/has_media/has_media_generator.rb
102
127
  - generators/has_media/templates/create_media.rb
103
128
  - generators/has_media/templates/create_media_links.rb
129
+ - has_media.gemspec
104
130
  - lib/has_media.rb
105
131
  - lib/has_media/models/media_link.rb
106
132
  - lib/has_media/models/medium.rb
107
133
  - lib/has_media/uploaders/medium_uploader.rb
134
+ - lib/tasks/rspec.rake
108
135
  - spec/fixtures/media/Conversational_Capital _Explained.pdf
109
136
  - spec/fixtures/media/audio.wav
110
137
  - spec/fixtures/media/image.jpg
@@ -113,10 +140,9 @@ files:
113
140
  - spec/fixtures/models/image.rb
114
141
  - spec/fixtures/uploaders/uploader_with_exception.rb
115
142
  - spec/has_media_spec.rb
116
- - spec/spec.opts
117
143
  - spec/spec_helper.rb
118
144
  has_rdoc: true
119
- homepage: http://github.com/AF83/has_media
145
+ homepage: http://github.com/klacointe/has_media
120
146
  licenses: []
121
147
 
122
148
  post_install_message:
@@ -125,36 +151,40 @@ rdoc_options:
125
151
  require_paths:
126
152
  - lib
127
153
  required_ruby_version: !ruby/object:Gem::Requirement
154
+ none: false
128
155
  requirements:
129
156
  - - ">="
130
157
  - !ruby/object:Gem::Version
158
+ hash: 3
131
159
  segments:
132
160
  - 0
133
161
  version: "0"
134
162
  required_rubygems_version: !ruby/object:Gem::Requirement
163
+ none: false
135
164
  requirements:
136
165
  - - ">="
137
166
  - !ruby/object:Gem::Version
167
+ hash: 3
138
168
  segments:
139
169
  - 0
140
170
  version: "0"
141
171
  requirements: []
142
172
 
143
173
  rubyforge_project:
144
- rubygems_version: 1.3.6
174
+ rubygems_version: 1.3.7
145
175
  signing_key:
146
176
  specification_version: 3
147
177
  summary: Media Managment Library for ActiveRecord and Carrierwave
148
178
  test_files:
149
179
  - spec/spec_helper.rb
150
- - spec/has_media_spec.rb
151
- - spec/fixtures/uploaders/uploader_with_exception.rb
152
180
  - spec/fixtures/models/image.rb
153
- - examples/uploaders/image_uploader.rb
154
- - examples/uploaders/pdf_uploader.rb
155
- - examples/uploaders/video_uploader.rb
156
- - examples/uploaders/audio_uploader.rb
181
+ - spec/fixtures/uploaders/uploader_with_exception.rb
182
+ - spec/has_media_spec.rb
157
183
  - examples/models/image.rb
158
184
  - examples/models/pdf.rb
159
185
  - examples/models/audio.rb
160
186
  - examples/models/video.rb
187
+ - examples/uploaders/pdf_uploader.rb
188
+ - examples/uploaders/audio_uploader.rb
189
+ - examples/uploaders/video_uploader.rb
190
+ - examples/uploaders/image_uploader.rb
File without changes