media_magick 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. data/.gitignore +2 -1
  2. data/CHANGELOG.md +53 -1
  3. data/Gemfile +0 -2
  4. data/Guardfile +5 -0
  5. data/LICENSE +1 -1
  6. data/README.md +32 -22
  7. data/Rakefile +1 -13
  8. data/app/assets/javascripts/media_magick/plupload_it.js +54 -45
  9. data/app/controllers/media_magick/attach_controller.rb +6 -6
  10. data/app/helpers/media_magick/application_helper.rb +58 -48
  11. data/app/views/_loader.html.erb +14 -0
  12. data/app/views/_uploader.html.erb +13 -0
  13. data/app/views/_video_uploader.html.erb +2 -0
  14. data/config/locales/en.yml +1 -0
  15. data/gemfiles/mongoid-3.0.gemfile +1 -2
  16. data/lib/media_magick.rb +1 -1
  17. data/lib/media_magick/controller/helpers.rb +25 -5
  18. data/lib/media_magick/engine.rb +1 -1
  19. data/lib/media_magick/image/dimensions.rb +23 -0
  20. data/lib/media_magick/model.rb +22 -5
  21. data/lib/media_magick/version.rb +2 -2
  22. data/media_magick.gemspec +10 -13
  23. data/spec/controllers/media_magick/attach_controller_spec.rb +35 -21
  24. data/spec/dummy/app/assets/javascripts/store/products.js +2 -0
  25. data/spec/dummy/app/assets/stylesheets/store/products.css +4 -0
  26. data/spec/dummy/app/controllers/store/products_controller.rb +83 -0
  27. data/spec/dummy/app/helpers/store/products_helper.rb +2 -0
  28. data/spec/dummy/app/models/post.rb +1 -0
  29. data/spec/dummy/app/models/store/product.rb +10 -0
  30. data/spec/dummy/app/models/user.rb +3 -2
  31. data/spec/dummy/app/uploaders/post_uploader.rb +11 -3
  32. data/spec/dummy/app/views/posts/_form.html.erb +16 -5
  33. data/spec/dummy/app/views/posts/index.html.erb +1 -1
  34. data/spec/dummy/app/views/store/products/_form.html.erb +34 -0
  35. data/spec/dummy/app/views/store/products/edit.html.erb +6 -0
  36. data/spec/dummy/app/views/store/products/index.html.erb +21 -0
  37. data/spec/dummy/app/views/store/products/new.html.erb +5 -0
  38. data/spec/dummy/app/views/store/products/show.html.erb +5 -0
  39. data/spec/dummy/config/boot.rb +1 -1
  40. data/spec/dummy/config/environments/development.rb +2 -1
  41. data/spec/dummy/config/routes.rb +5 -0
  42. data/spec/fixtures/example.jpg +0 -0
  43. data/spec/helpers/media_magick/application_helper_spec.rb +86 -33
  44. data/spec/lib/media_magick/controller/helper_spec.rb +40 -15
  45. data/spec/lib/media_magick/image/dimensions_spec.rb +62 -0
  46. data/spec/lib/media_magick/model_spec.rb +9 -3
  47. data/spec/views/_upload.html.erb_spec.rb +3 -3
  48. data/tmp/rspec_guard_result +1 -0
  49. metadata +52 -48
  50. data/app/views/_file.html.erb +0 -6
  51. data/app/views/_image.html.erb +0 -6
  52. data/app/views/_upload.html.erb +0 -12
  53. data/app/views/_video.html.erb +0 -8
@@ -0,0 +1,10 @@
1
+ module Store
2
+ class Product
3
+ include Mongoid::Document
4
+ include MediaMagick::Model
5
+
6
+ field :name, :type => String
7
+
8
+ attaches_many :images
9
+ end
10
+ end
@@ -4,7 +4,8 @@ class User
4
4
 
5
5
  field :name, type: String
6
6
 
7
- attaches_one :photo, uploader: PictureUploader
7
+ attaches_one :photo, uploader: PictureUploader
8
+ attaches_one :photo_and_video, uploader: PictureUploader, allow_videos: true
8
9
  attaches_one :compound_name_file
9
10
  attaches_one :file, type: :file
10
- end
11
+ end
@@ -1,7 +1,10 @@
1
1
  # encoding: utf-8
2
2
 
3
+ require "media_magick/image/dimensions"
4
+
3
5
  class PostUploader < CarrierWave::Uploader::Base
4
6
  include CarrierWave::MiniMagick
7
+ include MediaMagick::Image::Dimensions
5
8
 
6
9
  storage :file
7
10
 
@@ -9,9 +12,14 @@ class PostUploader < CarrierWave::Uploader::Base
9
12
  "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
10
13
  end
11
14
 
12
- process :resize_to_fill => [50, 50]
15
+ # process :resize_to_fill => [960, 544]
16
+
13
17
  version :thumb do
14
- process :resize_to_fit => [300, 300]
18
+ process :resize_to_fit => [250, 136]
19
+ end
20
+
21
+ version :big do
22
+ process :resize_to_limit => [864, 489]
15
23
  end
16
24
 
17
- end
25
+ end
@@ -22,13 +22,13 @@
22
22
 
23
23
  <h3>uploads</h3>
24
24
  <div class="field" style="overflow: hidden;">
25
- <label>imagens and videos</label>
25
+ <label>images and videos</label>
26
26
  <%- if @post.new_record? -%>
27
27
  create first
28
28
  <%- else -%>
29
- <%= attachment_container_for_video(@post, :images) %>
30
- <br /><br />
31
- <%= attachment_container @post, :images %>
29
+ <%= attachment_uploader(@post, :images, :video) %>
30
+ <%= attachment_uploader(@post, :images, :image) %>
31
+ <%= attachment_loader(@post, :images) %>
32
32
  <%- end -%>
33
33
  </div>
34
34
 
@@ -37,7 +37,18 @@
37
37
  <%- if @post.new_record? -%>
38
38
  create first
39
39
  <%- else -%>
40
- <%= attachment_container @post, :files, :as => "file" %>
40
+ <%= attachment_uploader(@post, :files, :file) %>
41
+ <%= attachment_loader(@post, :files) %>
42
+ <%- end -%>
43
+ </div>
44
+
45
+ <div class="field" style="overflow: hidden;">
46
+ <label>just videos</label>
47
+ <%- if @post.new_record? -%>
48
+ create first
49
+ <%- else -%>
50
+ <%= attachment_uploader(@post, :just_videos, :video) %>
51
+ <%= attachment_loader(@post, :just_videos) %>
41
52
  <%- end -%>
42
53
  </div>
43
54
 
@@ -15,7 +15,7 @@
15
15
  <td><%= post.text %></td>
16
16
  <td><%= link_to 'Show', post %></td>
17
17
  <td><%= link_to 'Edit', edit_post_path(post) %></td>
18
- <td><%= link_to 'Destroy', post, confirm: 'Are you sure?', method: :delete %></td>
18
+ <td><%= link_to 'Destroy', post, method: :delete, data: { confirm: 'Are you sure?' } %></td>
19
19
  </tr>
20
20
  <% end %>
21
21
  </table>
@@ -0,0 +1,34 @@
1
+ <%= form_for(@store_product) do |f| %>
2
+ <% if @store_product.errors.any? %>
3
+ <div id="error_explanation">
4
+ <h2><%= pluralize(@store_product.errors.count, "error") %> prohibited this store_product from being saved:</h2>
5
+
6
+ <ul>
7
+ <% @store_product.errors.full_messages.each do |msg| %>
8
+ <li><%= msg %></li>
9
+ <% end %>
10
+ </ul>
11
+ </div>
12
+ <% end %>
13
+
14
+ <div class="field">
15
+ <%= f.label :name %><br />
16
+ <%= f.text_field :name %>
17
+ </div>
18
+
19
+ <h3>uploads</h3>
20
+
21
+ <div class="field">
22
+ <label>files</label>
23
+ <%- if @store_product.new_record? -%>
24
+ create first
25
+ <%- else -%>
26
+ <%= attachment_uploader(@store_product, :images, :image) %>
27
+ <%= attachment_loader(@store_product, :images) %>
28
+ <%- end -%>
29
+ </div>
30
+
31
+ <div class="actions">
32
+ <%= f.submit %>
33
+ </div>
34
+ <% end %>
@@ -0,0 +1,6 @@
1
+ <h1>Editing store_product</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Show', @store_product %> |
6
+ <%= link_to 'Back', store_products_path %>
@@ -0,0 +1,21 @@
1
+ <h1>Listing store_products</h1>
2
+
3
+ <table>
4
+ <tr>
5
+ <th></th>
6
+ <th></th>
7
+ <th></th>
8
+ </tr>
9
+
10
+ <% @store_products.each do |store_product| %>
11
+ <tr>
12
+ <td><%= link_to 'Show', store_product %></td>
13
+ <td><%= link_to 'Edit', edit_store_product_path(store_product) %></td>
14
+ <td><%= link_to 'Destroy', store_product, method: :delete, data: { confirm: 'Are you sure?' } %></td>
15
+ </tr>
16
+ <% end %>
17
+ </table>
18
+
19
+ <br />
20
+
21
+ <%= link_to 'New Product', new_store_product_path %>
@@ -0,0 +1,5 @@
1
+ <h1>New store_product</h1>
2
+
3
+ <%= render 'form' %>
4
+
5
+ <%= link_to 'Back', store_products_path %>
@@ -0,0 +1,5 @@
1
+ <p id="notice"><%= notice %></p>
2
+
3
+
4
+ <%= link_to 'Edit', edit_store_product_path(@store_product) %> |
5
+ <%= link_to 'Back', store_products_path %>
@@ -7,4 +7,4 @@ if File.exist?(gemfile)
7
7
  Bundler.setup
8
8
  end
9
9
 
10
- $:.unshift File.expand_path('../../../../lib', __FILE__)
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -26,5 +26,6 @@ Dummy::Application.configure do
26
26
  config.assets.compress = false
27
27
 
28
28
  # Expands the lines which load the assets
29
- config.assets.debug = true
29
+ config.assets.debug = false #true
30
+
30
31
  end
@@ -1,5 +1,10 @@
1
1
  Rails.application.routes.draw do
2
2
 
3
+ namespace :store do
4
+ resources :products
5
+ end
6
+
7
+
3
8
  resources :posts
4
9
  resources :users
5
10
 
Binary file
@@ -9,53 +9,78 @@ describe MediaMagick do
9
9
  album.stub(id: '12345678')
10
10
  end
11
11
 
12
- def conteiner_html(model, relation, data_attributes, &block)
13
- content_tag(:div, nil, id: "#{model}-#{relation}", class: "attachmentUploader #{relation}", data: data_attributes) do
12
+ def uploader_container_html(model, relation, type, data_attributes, &block)
13
+ id = "#{model}-#{relation.to_s}-#{type.to_s}"
14
+ classes = "attachmentUploader"
15
+
16
+ content_tag(:div, nil, id: id, class: classes, data: data_attributes) do
17
+ class_eval(&block) if block_given?
18
+ end
19
+ end
20
+
21
+ def loader_container_html(model, relation, data_attributes, &block)
22
+ id = "#{model}-#{relation.to_s}-loadedAttachments"
23
+ classes = "#{relation.to_s} loadedAttachments"
24
+
25
+ content_tag(:div, nil, id: id, class: classes, data: data_attributes) do
14
26
  class_eval(&block) if block_given?
15
27
  end
16
28
  end
17
29
 
18
30
  context 'without block' do
19
31
  before do
20
- album.stub(id: '12345678')
32
+ # album.stub(id: '12345678')
21
33
 
22
34
  helper.stub(:render)
23
35
  end
24
36
 
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' }))
37
+ it 'should render /uploader partial' do
38
+ helper.should_receive(:render).with('/uploader')
39
+
40
+ helper.attachment_uploader(album, :photos, :image)
27
41
  end
28
42
 
29
- it 'should render /upload partial' do
30
- helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: {}, loadedAttachments: {}, partial: '/image')
43
+ it 'should create a div with data attributes' do
44
+ data_attributes = { id: 12345678, model: 'Album', relation: 'photos', partial: '/uploader' }
45
+ html = uploader_container_html('Album', 'photos', 'image', data_attributes)
31
46
 
32
- helper.attachment_container(album, :photos)
47
+ helper.attachment_uploader(album, :photos, :image).should eq(html)
33
48
  end
34
49
 
35
50
  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
51
+ it 'creates a div with data-partial attribute' do
52
+ html = uploader_container_html('Album', 'photos', 'image', { id: 12345678, model: 'Album', relation: 'photos', partial: 'albums/photo'})
39
53
 
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')
42
-
43
- helper.attachment_container(album, :photos, partial: 'albums/photo')
54
+ helper.attachment_uploader(album, :photos, :image, partial: 'albums/photo').should eq(html)
44
55
  end
45
- end
46
56
 
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'}))
57
+ it 'creates a div with data-loader_partial attribute' do
58
+ attrs = { id: 12345678, model: 'Album', relation: 'photos', partial: '/uploader', loader_partial: 'custom_loader'}
59
+ html = uploader_container_html('Album', 'photos', 'image', attrs)
60
+
61
+ helper.attachment_uploader(album, :photos, :image, loader_partial: 'custom_loader').should eq(html)
50
62
  end
51
63
 
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')
64
+ it 'includes partial option on data attributes' do
65
+ helper.should_receive(:render).with('albums/photo')
54
66
 
55
- helper.attachment_container(album, :photos, as: 'file')
67
+ helper.attachment_uploader(album, :photos, :image, partial: 'albums/photo')
56
68
  end
69
+
57
70
  end
58
71
 
72
+ # context 'partial for images' do
73
+ # xit 'should create a div with data-partial attributes' do
74
+ # helper.attachment_container(album, :photos, as: 'file').should eq(uploader_container_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/file'}))
75
+ # end
76
+
77
+ # xit 'should include partial option on data attributes' do
78
+ # helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: {}, loadedAttachments: {}, partial: '/file')
79
+
80
+ # helper.attachment_container(album, :photos, as: 'file')
81
+ # end
82
+ # end
83
+
59
84
  context 'embbeded models' do
60
85
  let(:track) { album.tracks.new }
61
86
 
@@ -64,22 +89,24 @@ describe MediaMagick do
64
89
  end
65
90
 
66
91
  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'}))
92
+ html = uploader_container_html('Track', 'files', 'file', { id: 87654321, model: 'Track', embedded_in_id: 12345678, embedded_in_model: 'Album', relation: 'files', partial: '/uploader'})
93
+
94
+ helper.attachment_uploader(track, :files, :file, embedded_in: album).should eq(html)
68
95
  end
69
96
 
70
- it 'should render /upload partial' do
71
- helper.should_receive(:render).with('/upload', model: track, relations: :files, newAttachments: {}, loadedAttachments: {}, partial: '/image')
97
+ xit 'should render /upload partial' do
98
+ helper.should_receive(:render).with('/upload', model: track, relations: :files, newAttachments: {}, loadedAttachments: {}, partial: '/uploader')
72
99
 
73
100
  helper.attachment_container(track, :files, embedded_in: album)
74
101
  end
75
102
  end
76
103
 
77
104
  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' }))
105
+ xit 'should create a div with data attributes' do
106
+ helper.attachment_container(album, :photos, newAttachments: { class: 'thumbnails' }).should eq(uploader_container_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/image' }))
80
107
  end
81
108
 
82
- it 'should render /upload partial with newAttachments attributes' do
109
+ xit 'should render /upload partial with newAttachments attributes' do
83
110
  helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: { class: 'thumbnails' }, loadedAttachments: {}, partial: '/image')
84
111
 
85
112
  helper.attachment_container(album, :photos, newAttachments: { class: 'thumbnails' })
@@ -87,11 +114,11 @@ describe MediaMagick do
87
114
  end
88
115
 
89
116
  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' }))
117
+ xit 'should create a div with data attributes' do
118
+ helper.attachment_container(album, :photos, loadedAttachments: { class: 'span3' }).should eq(uploader_container_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/image' }))
92
119
  end
93
120
 
94
- it 'should render /upload partial with loadedAttachments attributes' do
121
+ xit 'should render /upload partial with loadedAttachments attributes' do
95
122
  helper.should_receive(:render).with('/upload', model: album, relations: :photos, newAttachments: {}, loadedAttachments: { class: 'span3' }, partial: '/image')
96
123
 
97
124
  helper.attachment_container(album, :photos, loadedAttachments: { class: 'span3' })
@@ -100,12 +127,38 @@ describe MediaMagick do
100
127
  end
101
128
 
102
129
  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' }
130
+ xit 'should create a div with data attributes and content inside' do
131
+ expected = uploader_container_html('album', 'photos', { id: 12345678, model: 'Album', relation: 'photos', partial: '/image' }) { 'template here' }
105
132
 
106
133
  helper.attachment_container(album, :photos) { 'template here' }.should eq(expected)
107
134
  end
108
135
  end
136
+
137
+ context 'when model is inside a module' do
138
+ let(:model) { Store::Product.new }
139
+
140
+ before do
141
+ helper.stub(:render)
142
+ end
143
+
144
+ describe 'uploader' do
145
+ it 'converts string :: to - from model name to create id attribute' do
146
+ data_attributes = { id: model.id.to_s, model: 'Store::Product', relation: 'images', partial: '/uploader' }
147
+ html = uploader_container_html('Store-Product', 'images', 'image', data_attributes)
148
+
149
+ helper.attachment_uploader(model, :images, :image).should eq(html)
150
+ end
151
+ end
152
+
153
+ describe 'loader' do
154
+ it 'converts string :: to - from model name to create id attribute' do
155
+ data_attributes = { id: model.id.to_s, model: 'Store::Product', relation: 'images', partial: '/loader' }
156
+ html = loader_container_html('Store-Product', :images, data_attributes)
157
+
158
+ helper.attachment_loader(model, :images).should eq(html)
159
+ end
160
+ end
161
+ end
109
162
  end
110
163
  end
111
164
  end
@@ -1,37 +1,62 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe MediaMagick::Controller::Helpers do
4
+ let(:controller) { Class.new }
5
+
4
6
  before do
5
- @controller = Class.new
6
- @controller.extend MediaMagick::Controller::Helpers
7
+ controller.extend MediaMagick::Controller::Helpers
7
8
  end
8
9
 
9
10
  describe "getting doc by params" do
10
- before do
11
- @track = Track.new
12
- @album = Album.create(tracks: [@track])
13
- end
11
+ let(:track) { Track.new }
12
+ let!(:album) { Album.create(tracks: [track]) }
14
13
 
15
14
  context "document is embedded" do
16
15
  it "should get parent by params" do
17
16
  params = {
18
- :embedded_in_model => "album",
19
- :embedded_in_id => "#{@album.id.to_s}",
20
- :model => "track",
21
- :model_id => "#{@track.id.to_s}"
17
+ :embedded_in_model => "album",
18
+ :embedded_in_id => "#{album.id.to_s}",
19
+ :model => "track",
20
+ :model_id => "#{track.id.to_s}"
22
21
  }
23
- @controller.find_doc_by_params(params).should eq(@track)
22
+ controller.find_doc_by_params(params).should eq(track)
24
23
  end
25
24
  end
26
25
 
27
26
  context "document is root" do
28
27
  it "should get parent by params" do
29
28
  params = {
30
- :model => "album",
31
- :model_id => "#{@album.id.to_s}"
29
+ :model => "album",
30
+ :model_id => "#{album.id.to_s}"
32
31
  }
33
- @controller.find_doc_by_params(params).should eq(@album)
32
+ controller.find_doc_by_params(params).should eq(album)
33
+ end
34
+ end
35
+ end
36
+
37
+ describe "creating video" do
38
+ context "relation is attaches many" do
39
+ let(:video_url) { "youtube.com/watch?v=FfUHkPf9D9k" }
40
+ let!(:album) { Album.create }
41
+
42
+ it "should create video" do
43
+ params = {relation: "photos_and_videos", video: video_url}
44
+
45
+ controller.create_video(album, params).should be_true
46
+ album.reload.photos_and_videos.first.video.should eq(video_url)
47
+ end
48
+ end
49
+
50
+ context "relation is attaches one" do
51
+ let(:video_url) { "youtube.com/watch?v=FfUHkPf9D9k" }
52
+ let!(:user) { User.create }
53
+
54
+ it "should create video" do
55
+ params = {relation: "photo_and_video", video: video_url}
56
+
57
+ controller.create_video(user, params).should be_true
58
+ user.reload.photo_and_video.video.should eq(video_url)
34
59
  end
35
60
  end
36
61
  end
37
- end
62
+ end