media_magick 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/.gitignore +2 -1
  2. data/.travis.yml +7 -3
  3. data/CHANGELOG.md +92 -1
  4. data/README.md +1 -1
  5. data/app/assets/javascripts/media_magick/plupload_it.js +30 -9
  6. data/app/controllers/media_magick/attach_controller.rb +16 -28
  7. data/app/helpers/media_magick/application_helper.rb +46 -5
  8. data/app/views/_file.html.erb +2 -2
  9. data/app/views/_image.html.erb +3 -3
  10. data/app/views/_upload.html.erb +3 -10
  11. data/app/views/_video.html.erb +8 -0
  12. data/gemfiles/mongoid-3.0.gemfile +7 -0
  13. data/lib/media_magick.rb +1 -5
  14. data/lib/media_magick/controller/helpers.rb +20 -0
  15. data/lib/media_magick/engine.rb +1 -1
  16. data/lib/media_magick/model.rb +41 -48
  17. data/lib/media_magick/version.rb +1 -1
  18. data/lib/media_magick/video/parser.rb +62 -0
  19. data/media_magick.gemspec +8 -4
  20. data/spec/controllers/media_magick/attach_controller_spec.rb +12 -11
  21. data/spec/dummy/app/assets/javascripts/application.js +5 -0
  22. data/spec/dummy/app/assets/javascripts/posts.js +2 -0
  23. data/spec/dummy/app/assets/stylesheets/posts.css +4 -0
  24. data/spec/dummy/app/controllers/posts_controller.rb +83 -0
  25. data/spec/dummy/app/helpers/posts_helper.rb +2 -0
  26. data/spec/dummy/app/models/album.rb +3 -1
  27. data/spec/dummy/app/models/post.rb +10 -0
  28. data/spec/dummy/app/models/track.rb +1 -0
  29. data/spec/dummy/app/uploaders/post_uploader.rb +17 -0
  30. data/spec/dummy/app/views/posts/_form.html.erb +47 -0
  31. data/spec/dummy/app/views/posts/edit.html.erb +6 -0
  32. data/spec/dummy/app/views/posts/index.html.erb +25 -0
  33. data/spec/dummy/app/views/posts/new.html.erb +5 -0
  34. data/spec/dummy/app/views/posts/show.html.erb +15 -0
  35. data/spec/dummy/app/views/users/index.html.erb +1 -1
  36. data/spec/dummy/config/mongoid.yml +65 -17
  37. data/spec/dummy/config/routes.rb +3 -0
  38. data/spec/helpers/media_magick/application_helper_spec.rb +90 -27
  39. data/spec/integration/images_spec.rb +1 -1
  40. data/spec/lib/media_magick/controller/helper_spec.rb +37 -0
  41. data/spec/lib/media_magick/model_spec.rb +41 -9
  42. data/spec/lib/media_magick/video/parser_spec.rb +67 -0
  43. data/spec/spec_helper.rb +11 -8
  44. data/spec/views/_upload.html.erb_spec.rb +26 -0
  45. metadata +76 -21
  46. data/.rvmrc +0 -1
  47. data/spec/dummy/app/helpers/users_helper.rb +0 -2
@@ -0,0 +1,6 @@
1
+ <h1>Editing post</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @post %> |
6
+ <%= link_to 'Back', posts_path %>
@@ -0,0 +1,25 @@
1
+ <h1>Listing posts</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th>Title</th>
6
+ <th>Text</th>
7
+ <th></th>
8
+ <th></th>
9
+ <th></th>
10
+ </tr>
11
+
12
+ <% @posts.each do |post| %>
13
+ <tr>
14
+ <td><%= post.title %></td>
15
+ <td><%= post.text %></td>
16
+ <td><%= link_to 'Show', post %></td>
17
+ <td><%= link_to 'Edit', edit_post_path(post) %></td>
18
+ <td><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></td>
19
+ </tr>
20
+ <% end %>
21
+ </table>
22
+
23
+ <br />
24
+
25
+ <%= link_to 'New Post', new_post_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New post</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', posts_path %>
@@ -0,0 +1,15 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+ <p>
4
+ <b>Title:</b>
5
+ <%= @post.title %>
6
+ </p>
7
+
8
+ <p>
9
+ <b>Text:</b>
10
+ <%= @post.text %>
11
+ </p>
12
+
13
+
14
+ <%= link_to 'Edit', edit_post_path(@post) %> |
15
+ <%= link_to 'Back', posts_path %>
@@ -13,7 +13,7 @@
13
13
  <td><%= user.name %></td>
14
14
  <td><%= link_to 'Show', user %></td>
15
15
  <td><%= link_to 'Edit', edit_user_path(user) %></td>
16
- <td><%= link_to 'Destroy', user, confirm: 'Are you sure?', method: :delete %></td>
16
+ <td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>
17
17
  </tr>
18
18
  <% end %>
19
19
  </table>
@@ -1,20 +1,68 @@
1
1
  development:
2
- host: localhost
3
- database: dummy_development
2
+ # Configure available database sessions. (required)
3
+ sessions:
4
+ # Defines the default session. (required)
5
+ default:
6
+ # Defines the name of the default database that Mongoid can connect to.
7
+ # (required).
8
+ database: dummy_development
9
+ # Provides the hosts the default session can connect to. Must be an array
10
+ # of host:port pairs. (required)
11
+ hosts:
12
+ - localhost:27017
13
+ options:
14
+ # Change whether the session persists in safe mode by default.
15
+ # (default: false)
16
+ # safe: false
4
17
 
18
+ # Change the default consistency model to :eventual or :strong.
19
+ # :eventual will send reads to secondaries, :strong sends everything
20
+ # to master. (default: :eventual)
21
+ consistency: :strong
22
+ # Configure Mongoid specific options. (optional)
23
+ options:
24
+ # Configuration for whether or not to allow access to fields that do
25
+ # not have a field definition on the model. (default: true)
26
+ # allow_dynamic_fields: true
27
+
28
+ # Enable the identity map, needed for eager loading. (default: false)
29
+ # identity_map_enabled: false
30
+
31
+ # Includes the root model name in json serialization. (default: false)
32
+ # include_root_in_json: false
33
+
34
+ # Include the _type field in serializaion. (default: false)
35
+ # include_type_for_serialization: false
36
+
37
+ # Preload all models in development, needed when models use
38
+ # inheritance. (default: false)
39
+ # preload_models: false
40
+
41
+ # Protect id and type from mass assignment. (default: true)
42
+ # protect_sensitive_fields: true
43
+
44
+ # Raise an error when performing a #find and the document is not found.
45
+ # (default: true)
46
+ # raise_not_found_error: true
47
+
48
+ # Raise an error when defining a scope with the same name as an
49
+ # existing method. (default: false)
50
+ # scope_overwrite_exception: false
51
+
52
+ # Skip the database version check, used when connecting to a db without
53
+ # admin access. (default: false)
54
+ # skip_version_check: false
55
+
56
+ # User Active Support's time zone in conversions. (default: true)
57
+ # use_activesupport_time_zone: true
58
+
59
+ # Ensure all times are UTC in the app side. (default: false)
60
+ # use_utc: false
5
61
  test:
6
- host: localhost
7
- database: dummy_test
8
-
9
- # set these environment variables on your prod server
10
- production:
11
- host: <%= ENV['MONGOID_HOST'] %>
12
- port: <%= ENV['MONGOID_PORT'] %>
13
- username: <%= ENV['MONGOID_USERNAME'] %>
14
- password: <%= ENV['MONGOID_PASSWORD'] %>
15
- database: <%= ENV['MONGOID_DATABASE'] %>
16
- # slaves:
17
- # - host: slave1.local
18
- # port: 27018
19
- # - host: slave2.local
20
- # port: 27019
62
+ sessions:
63
+ default:
64
+ database: dummy_test
65
+ hosts:
66
+ - localhost:27017
67
+ options:
68
+ consistency: :strong
@@ -1,6 +1,9 @@
1
1
  Rails.application.routes.draw do
2
2
 
3
+ resources :posts
3
4
  resources :users
4
5
 
6
+ root :to => "posts#index"
7
+
5
8
  mount MediaMagick::Engine => "/media_magick"
6
9
  end
@@ -3,45 +3,108 @@ require 'spec_helper'
3
3
  describe MediaMagick do
4
4
  describe ApplicationHelper do
5
5
  describe 'attachmentUploader' do
6
- it 'should create a div.attachmentUploader.photos' do
7
- album = Album.new
6
+ let(:album) { Album.new }
7
+
8
+ before do
8
9
  album.stub(id: '12345678')
10
+ end
9
11
 
10
- helper.attachment_container(album, :photos) do
11
- end.should eq('<div class="attachmentUploader photos" data-id="12345678" data-model="Album" data-relation="photos" id="album-photos"></div>')
12
+ def conteiner_html(model, relation, data_attributes, &block)
13
+ content_tag(:div, nil, id: "#{model}-#{relation}", class: "attachmentUploader #{relation}", data: data_attributes) do
14
+ class_eval(&block) if block_given?
15
+ end
12
16
  end
13
17
 
14
- it 'should include partial option on data attributes' do
15
- album = Album.new
16
- album.stub(id: '12345678')
18
+ context 'without block' do
19
+ before do
20
+ album.stub(id: '12345678')
17
21
 
18
- helper.attachment_container(album, :photos, {}, {}, partial: 'albums/photo') do
19
- end.should eq('<div class="attachmentUploader photos" data-id="12345678" data-model="Album" data-partial="albums/photo" data-relation="photos" id="album-photos"></div>')
20
- end
22
+ helper.stub(:render)
23
+ end
21
24
 
22
- it 'should create a div.attachmentUploader.photos for embedded models' do
23
- album = Album.new
24
- album.stub(id: '12345678')
25
+ it 'should create a div with data attributes' do
26
+ helper.attachment_container(album, :photos).should eq(conteiner_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/image' }))
27
+ end
25
28
 
26
- track = album.tracks.new
27
- track.stub(id: '87654321')
29
+ it 'should render /upload partial' do
30
+ helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: {}, loadedAttachments: {}, partial: '/image')
28
31
 
29
- helper.attachment_container(track, :files, {}, {}, embedded_in: album) do
30
- end.should eq('<div class="attachmentUploader files" data-embedded-in-id="12345678" data-embedded-in-model="Album" data-id="87654321" data-model="Track" data-relation="files" id="track-files"></div>')
31
- end
32
+ helper.attachment_container(album, :photos)
33
+ end
32
34
 
33
- it 'should renders default partial if block is not given' do
34
- photo = AlbumPhotos.new
35
- photo.stub(filename: 'photo.jpg', url: 'url/photo.jpg')
35
+ context 'using partials' do
36
+ it 'should create a div with data-partial attributes' do
37
+ helper.attachment_container(album, :photos, partial: 'albums/photo').should eq(conteiner_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: 'albums/photo'}))
38
+ end
36
39
 
37
- file = AlbumFiles.new
38
- file.stub(filename: 'file.pdf', url: 'url/file.pdf')
40
+ it 'should include partial option on data attributes' do
41
+ helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: {}, loadedAttachments: {}, partial: 'albums/photo')
39
42
 
40
- album = Album.new(photos: [photo], files: [file])
41
- album.stub(id: '12345678')
43
+ helper.attachment_container(album, :photos, partial: 'albums/photo')
44
+ end
45
+ end
46
+
47
+ context 'partial for images' do
48
+ it 'should create a div with data-partial attributes' do
49
+ helper.attachment_container(album, :photos, as: 'file').should eq(conteiner_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/file'}))
50
+ end
51
+
52
+ it 'should include partial option on data attributes' do
53
+ helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: {}, loadedAttachments: {}, partial: '/file')
54
+
55
+ helper.attachment_container(album, :photos, as: 'file')
56
+ end
57
+ end
58
+
59
+ context 'embbeded models' do
60
+ let(:track) { album.tracks.new }
61
+
62
+ before do
63
+ track.stub(id: '87654321')
64
+ end
65
+
66
+ it 'should create a div with data-embedded-in-id and data-embedded-in-model attributes' do
67
+ helper.attachment_container(track, :files, embedded_in: album).should eq(conteiner_html('track', 'files', { id: 87654321, model: 'Track', embedded_in_id: 12345678, embedded_in_model: 'Album', relation: 'files', partial: '/image'}))
68
+ end
69
+
70
+ it 'should render /upload partial' do
71
+ helper.should_receive(:render).with('/upload', model: track, relations: :files, newAttachments: {}, loadedAttachments: {}, partial: '/image')
72
+
73
+ helper.attachment_container(track, :files, embedded_in: album)
74
+ end
75
+ end
76
+
77
+ context 'customizing newAttachments element' do
78
+ it 'should create a div with data attributes' do
79
+ helper.attachment_container(album, :photos, newAttachments: { class: 'thumbnails' }).should eq(conteiner_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/image' }))
80
+ end
81
+
82
+ it 'should render /upload partial with newAttachments attributes' do
83
+ helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: { class: 'thumbnails' }, loadedAttachments: {}, partial: '/image')
84
+
85
+ helper.attachment_container(album, :photos, newAttachments: { class: 'thumbnails' })
86
+ end
87
+ end
88
+
89
+ context 'customizing loadedAttachments element' do
90
+ it 'should create a div with data attributes' do
91
+ helper.attachment_container(album, :photos, loadedAttachments: { class: 'span3' }).should eq(conteiner_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/image' }))
92
+ end
93
+
94
+ it 'should render /upload partial with loadedAttachments attributes' do
95
+ helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: {}, loadedAttachments: { class: 'span3' }, partial: '/image')
96
+
97
+ helper.attachment_container(album, :photos, loadedAttachments: { class: 'span3' })
98
+ end
99
+ end
100
+ end
101
+
102
+ context 'with block' do
103
+ it 'should create a div with data attributes and content inside' do
104
+ expected = conteiner_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/image' }) { 'template here' }
42
105
 
43
- helper.attachment_container(album, :photos).should match(/url\/photo.jpg/)
44
- helper.attachment_container(album, :files).should match(/url\/file.pdf/)
106
+ helper.attachment_container(album, :photos) { 'template here' }.should eq(expected)
107
+ end
45
108
  end
46
109
  end
47
110
  end
@@ -48,7 +48,7 @@ describe 'Images' do
48
48
  end
49
49
 
50
50
  context "when uploading one file" do
51
- let(:user) { User.create }
51
+ let(:user) { User.create }
52
52
 
53
53
  it 'should save the image on mongoid document' do
54
54
  user.create_photo(photo: image_file)
@@ -0,0 +1,37 @@
1
+ require 'spec_helper'
2
+
3
+ describe MediaMagick::Controller::Helpers do
4
+ before do
5
+ @controller = Class.new
6
+ @controller.extend MediaMagick::Controller::Helpers
7
+ end
8
+
9
+ describe "getting doc by params" do
10
+ before do
11
+ @track = Track.new
12
+ @album = Album.create(tracks: [@track])
13
+ end
14
+
15
+ context "document is embedded" do
16
+ it "should get parent by params" do
17
+ params = {
18
+ :embedded_in_model => "album",
19
+ :embedded_in_id => "#{@album.id.to_s}",
20
+ :model => "track",
21
+ :model_id => "#{@track.id.to_s}"
22
+ }
23
+ @controller.find_doc_by_params(params).should eq(@track)
24
+ end
25
+ end
26
+
27
+ context "document is root" do
28
+ it "should get parent by params" do
29
+ params = {
30
+ :model => "album",
31
+ :model_id => "#{@album.id.to_s}"
32
+ }
33
+ @controller.find_doc_by_params(params).should eq(@album)
34
+ end
35
+ end
36
+ end
37
+ end
@@ -8,11 +8,11 @@ describe MediaMagick::Model do
8
8
  @klass = User
9
9
  end
10
10
 
11
- it "should includes .attaches_many" do
11
+ it "should includes .attaches_many" do
12
12
  @klass.should respond_to(:attaches_many)
13
13
  end
14
14
 
15
- it "should includes .attaches_one" do
15
+ it "should includes .attaches_one" do
16
16
  @klass.should respond_to(:attaches_one)
17
17
  end
18
18
  end
@@ -23,10 +23,6 @@ describe MediaMagick::Model do
23
23
  @instance = @klass.new
24
24
  end
25
25
 
26
- it 'should create a "has_many" relationship with photos' do
27
- @klass.relations['files'].relation.should eq(Mongoid::Relations::Referenced::Many)
28
- end
29
-
30
26
  it 'should create a "embeds_many" relationship with photos' do
31
27
  @klass.relations['photos'].relation.should eq(Mongoid::Relations::Embedded::Many)
32
28
  end
@@ -42,6 +38,42 @@ describe MediaMagick::Model do
42
38
  end
43
39
  end
44
40
 
41
+ describe 'allow_videos' do
42
+ let(:album) { Album.create }
43
+ subject { album.photos_and_videos.create }
44
+
45
+ it 'should include type field' do
46
+ subject.fields.should include('type')
47
+ end
48
+
49
+ it 'should include video field' do
50
+ subject.fields.should include('video')
51
+ end
52
+
53
+ it 'should accept youtube videos' do
54
+ video = album.photos_and_videos.create(video: 'youtube.com/watch?v=FfUHkPf9D9k')
55
+
56
+ video.type.should eq('video')
57
+
58
+ video.url.should eq("/uploads/album_photos_and_videos/photos_and_video/#{video.id}/FfUHkPf9D9k.jpg")
59
+ video.thumb.url.should eq("/uploads/album_photos_and_videos/photos_and_video/#{video.id}/thumb_FfUHkPf9D9k.jpg")
60
+ video.source.should eq('<iframe width="560" height="315" src="http://www.youtube.com/embed/FfUHkPf9D9k" frameborder="0" allowfullscreen></iframe>')
61
+
62
+ video.source(width: 156, height: 88).should eq('<iframe width="156" height="88" src="http://www.youtube.com/embed/FfUHkPf9D9k" frameborder="0" allowfullscreen></iframe>')
63
+ end
64
+
65
+ it 'should accept vimeo videos' do
66
+ video = album.photos_and_videos.create(video: 'vimeo.com/43401461')
67
+
68
+ video.type.should eq('video')
69
+
70
+ video.url.should eq("/uploads/album_photos_and_videos/photos_and_video/#{video.id}/43401461.jpg")
71
+ video.thumb.url.should eq("/uploads/album_photos_and_videos/photos_and_video/#{video.id}/thumb_43401461.jpg")
72
+ video.source.should eq('<iframe src="http://player.vimeo.com/video/43401461?title=0&byline=0&portrait=0" width="500" height="341" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>')
73
+ video.source(width: 156, height: 88).should eq('<iframe src="http://player.vimeo.com/video/43401461?title=0&byline=0&portrait=0" width="156" height="88" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>')
74
+ end
75
+ end
76
+
45
77
  describe '#photos' do
46
78
  subject { @instance.photos.new }
47
79
 
@@ -56,7 +88,7 @@ describe MediaMagick::Model do
56
88
  end
57
89
 
58
90
  it "should be ordered by ascending priority" do
59
- @instance = @klass.new
91
+ @instance = @klass.create
60
92
 
61
93
  photo2 = @instance.photos.create(priority: 1)
62
94
  photo1 = @instance.photos.create(priority: 0)
@@ -125,8 +157,8 @@ describe MediaMagick::Model do
125
157
  @klass.attaches_one(:image) do
126
158
  def test_method; end
127
159
  end
128
-
129
- @instance.build_image.should respond_to(:test_method)
160
+
161
+ @instance.build_image.should respond_to(:test_method)
130
162
  end
131
163
  end
132
164
 
@@ -0,0 +1,67 @@
1
+ require 'spec_helper'
2
+
3
+ describe MediaMagick::Video::Parser do
4
+ describe 'youtube' do
5
+ describe 'valid?' do
6
+ context 'with a valid url' do
7
+ subject { MediaMagick::Video::Parser.new('youtube.com/watch?v=FfUHkPf9D9k') }
8
+
9
+ it { should be_valid }
10
+ end
11
+
12
+ context 'with a invalid url' do
13
+ subject { MediaMagick::Video::Parser.new('youtube.com') }
14
+
15
+ it { should_not be_valid }
16
+ end
17
+ end
18
+
19
+ describe '#to_image' do
20
+ it 'should return a file with video image' do
21
+ video = MediaMagick::Video::Parser.new('youtube.com/watch?v=FfUHkPf9D9k')
22
+
23
+ video.to_image.should be_instance_of(File)
24
+ end
25
+ end
26
+
27
+ describe '#to_html' do
28
+ it 'should return video html' do
29
+ video = MediaMagick::Video::Parser.new('youtube.com/watch?v=FfUHkPf9D9k')
30
+
31
+ video.to_html.should eq('<iframe width="560" height="315" src="http://www.youtube.com/embed/FfUHkPf9D9k" frameborder="0" allowfullscreen></iframe>')
32
+ end
33
+ end
34
+ end
35
+
36
+ describe 'vimeo' do
37
+ describe 'valid?' do
38
+ context 'with a valid url' do
39
+ subject { MediaMagick::Video::Parser.new('vimeo.com/44539044') }
40
+
41
+ it { should be_valid }
42
+ end
43
+
44
+ context 'with a invalid url' do
45
+ subject { MediaMagick::Video::Parser.new('vimeo.com') }
46
+
47
+ it { should_not be_valid }
48
+ end
49
+ end
50
+
51
+ describe '#to_image' do
52
+ it 'should return a file with video image' do
53
+ video = MediaMagick::Video::Parser.new('vimeo.com/44539044')
54
+
55
+ video.to_image.should be_instance_of(File)
56
+ end
57
+ end
58
+
59
+ describe '#to_html' do
60
+ it 'should return video html' do
61
+ video = MediaMagick::Video::Parser.new('vimeo.com/44539044')
62
+
63
+ video.to_html.should eq('<iframe src="http://player.vimeo.com/video/44539044?title=0&byline=0&portrait=0" width="500" height="341" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>')
64
+ end
65
+ end
66
+ end
67
+ end