media_magick 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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
data/.gitignore CHANGED
@@ -6,4 +6,5 @@ spec/dummy/tmp/
6
6
  spec/dummy/public/uploads
7
7
  spec/dummy/.sass-cache
8
8
  uploads
9
- .DS_Store
9
+ .DS_Store
10
+ .rvmrc
data/.travis.yml CHANGED
@@ -1,11 +1,15 @@
1
1
  language: ruby
2
2
  rvm:
3
- - 1.9.2
4
3
  - 1.9.3
5
- - ruby-head
4
+
5
+ services: mongodb
6
+
6
7
  notifications:
7
8
  email:
8
9
  - contato@lucasrenan.com
9
10
  - rbrancher@gmail.com
10
11
  - tiagogodinho3@gmail.com
11
- - stefano@heavenstudio.com.br
12
+
13
+ gemfile:
14
+ - Gemfile
15
+ - gemfiles/mongoid-3.0.gemfile
data/CHANGELOG.md CHANGED
@@ -1,3 +1,90 @@
1
+ ## Next Release (branch: master)
2
+
3
+ ### Improvements
4
+
5
+ * Adding support to videos.
6
+
7
+ Model:
8
+
9
+ ``` rb
10
+ class Album
11
+ include Mongoid::Document
12
+ include MediaMagick::Model
13
+
14
+ attaches_many :medias, allow_videos: true
15
+ end
16
+
17
+ album = Album.create
18
+ album.medias.create(video: 'youtube.com/watch?v=FfUHkPf9D9k')
19
+ album.reload.photos.first.url
20
+ album.reload.photos.first.filename
21
+
22
+ # Specific methods
23
+
24
+ album.reload.medias.first.type #=> 'video'
25
+ album.reload.medias.first.video #=> 'youtube.com/watch?v=FfUHkPf9D9k'
26
+ album.reload.medias.first.source #=> <iframe>youtube video</iframe>
27
+ ```
28
+
29
+ View:
30
+
31
+ ``` ruby
32
+ attachment_container_for_video @album, :files
33
+ ```
34
+
35
+ * Adding support to Mongoid 3.
36
+
37
+ ### Major Changes (Backwards Incompatible)
38
+
39
+ * Media Magick no longer supports Ruby 1.8.7.
40
+
41
+ * Adding the `as` option in `attachment_container` to define the partial to be rendered
42
+
43
+ After:
44
+
45
+ ``` rb
46
+ class Album
47
+ include Mongoid::Document
48
+ include MediaMagick::Model
49
+
50
+ attaches_many :files, type: 'file'
51
+ end
52
+ ```
53
+
54
+ Now:
55
+
56
+ ``` rb
57
+ attachment_container @album, :files, as: 'file'
58
+ ```
59
+
60
+ * Now **newAttachments** and **loadedAttachments** are options for `attachment_container`
61
+
62
+ After:
63
+
64
+ ``` rb
65
+ attachment_container @album, :photos, { class: 'thumbnails' }, { class: 'span3' }, partial: 'albums/photo'
66
+
67
+ # without newAttachments and loadedAttachments
68
+ attachment_container @album, :photos, {}, {}, partial: 'albums/photo'
69
+ ```
70
+
71
+ Now:
72
+
73
+ ``` rb
74
+ attachment_container @album, :photos, newAttachments: { class: 'thumbnails' }, loadedAttachments: { class: 'span3' }, partial: 'albums/photo'
75
+
76
+ # without newAttachments and loadedAttachments
77
+ attachment_container @album, :photos, partial: 'albums/photo'
78
+ ```
79
+
80
+ * `MediaMagick::Model#attachs_many` has been removed in favor of `attaches_many`.
81
+
82
+ * Removing related relations and the ability to create an image without a father.
83
+
84
+ ### Resolved Issues
85
+
86
+ * Javascript returning undefined instead of "" prevents attachments from working at embedded documents
87
+
1
88
  ## 0.1.0 - June 11, 2012
2
89
 
3
90
  ### Improvements
@@ -38,7 +125,11 @@ album = Album.new
38
125
  photo = album.photos.create(photo: params[:file])
39
126
  ```
40
127
 
41
- * Allow the `attachs_many` to be used in embedded documents
128
+ * Allow the `attaches_many` to be used in embedded documents
129
+
130
+ ``` erb
131
+ <%= attachment_container @album, :photos, {}, {}, embedded_in: @album.artist %>
132
+ ```
42
133
 
43
134
  ### Deprecations
44
135
 
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # MediaMagick [![Build Status](https://secure.travis-ci.org/nudesign/media_magick.png?branch=master)](http://travis-ci.org/nudesign/media_magick) [![Build Status](https://gemnasium.com/nudesign/media_magick.png?travis)](http://gemnasium.com/nudesign/media_magick)
1
+ # MediaMagick [![Build Status](https://secure.travis-ci.org/nudesign/media_magick.png?branch=master)](http://travis-ci.org/nudesign/media_magick) [![Build Status](https://gemnasium.com/nudesign/media_magick.png)](http://gemnasium.com/nudesign/media_magick) [![Code Climate](https://codeclimate.com/badge.png)](https://codeclimate.com/github/nudesign/media_magick)
2
2
 
3
3
  MediaMagick aims to make dealing with multimedia resources a very easy task – like magic. It wraps up robust solutions for upload, associate and display images, videos, audios and files to any model in your rails app.
4
4
 
@@ -54,12 +54,12 @@
54
54
  flash_swf_url: settings.flash_swf_url,
55
55
  max_file_size: settings.max_file_size,
56
56
  multipart_params: {
57
- id: $container.data('id'),
58
- relation: $container.data('relation'),
59
- model: $container.data('model'),
60
- partial: $container.data('partial'),
61
- embedded_in_model: $container.data('embedded-in-model'),
62
- embedded_in_id: $container.data('embedded-in-id')
57
+ id: $container.data('id'),
58
+ relation: $container.data('relation'),
59
+ model: $container.data('model'),
60
+ partial: $container.data('partial') === undefined ? '' : $container.data('partial'),
61
+ embedded_in_model: $container.data('embedded-in-model') === undefined ? '' : $container.data('embedded-in-model'),
62
+ embedded_in_id: $container.data('embedded-in-id') === undefined ? '' : $container.data('embedded-in-id')
63
63
  },
64
64
  resize: settings.resize,
65
65
  runtimes: settings.runtimes,
@@ -71,9 +71,10 @@
71
71
  uploader.bind('Init', function(up, params) {
72
72
  if ($('#' + settings.container + '-runtimeInfo').length > 0) $('#' + settings.container + '-runtimeInfo').text("Current runtime: " + params.runtime);
73
73
  // if ($container.find("dt").length > 0 && $container.find("dt").text() == "") $container.find("dt").text($container.attr('id'));
74
-
75
- $("a.remove").live('click', function() {
76
-
74
+
75
+ var modelAndRelation = $container.data('model') + "-" + $container.data('relation');
76
+
77
+ $("#" + $container.attr("id") + " a.remove").live('click', function() {
77
78
  var $attachment = $(this).parents('.attachment');
78
79
  var $attachmentUploader = $(this).parents('.attachmentUploader');
79
80
 
@@ -88,6 +89,26 @@
88
89
  $attachment.remove();
89
90
  });
90
91
  });
92
+
93
+ $("#attachmentVideoUploader" + modelAndRelation).live('click', function() {
94
+ var $attachment = $(this).parents('.attachment');
95
+ var $attachmentUploader = $(this).parents('.attachmentUploader');
96
+ var $videoField = $("#attachmentVideoUploaderField" + modelAndRelation);
97
+
98
+ $.get('/upload', {
99
+ model: $container.data('model'),
100
+ id: $container.data('id'),
101
+ relation: $container.data('relation'),
102
+ relation_id: $attachment.data('id'),
103
+ embedded_in_model: $attachmentUploader.data('embedded-in-model'),
104
+ embedded_in_id: $attachmentUploader.data('embedded-in-id'),
105
+ partial: $container.data('partial') === undefined ? '' : $container.data('partial'),
106
+ video: $videoField.val()
107
+ }, function(data) {
108
+ $("#" + $container.attr("id") + "-loadedAttachments").append(data);
109
+ $videoField.val("");
110
+ });
111
+ });
91
112
  });
92
113
 
93
114
  $('#' + settings.container + '-' + settings.upload_button).click(function(e) {
@@ -1,30 +1,26 @@
1
1
  require 'action_controller/railtie'
2
+ require 'media_magick/controller/helpers'
2
3
 
3
4
  module MediaMagick
4
5
  class AttachController < ActionController::Base
6
+ include MediaMagick::Controller::Helpers
7
+
5
8
  def create
6
9
  if !params[:embedded_in_model].blank?
7
10
  embedded_in = params[:embedded_in_model].constantize.find(params[:embedded_in_id])
8
11
  klass = embedded_in.send(params[:model].pluralize.downcase).find(params[:id])
9
- attachment = klass.send(params[:relation].pluralize).create(params[:relation].singularize => params[:file])
10
- klass.save
11
12
  else
12
- if params[:model].constantize.relations[params[:relation]][:relation] == Mongoid::Relations::Referenced::Many
13
- klass = params[:model].constantize.where(id: params[:id])
14
- klass = klass.empty? ? nil : klass.first
15
-
16
- if klass
17
- attachment = klass.send(params[:relation].pluralize).create(params[:relation].singularize => params[:file])
18
- else
19
- attachment = params[:model].constantize.relations[params[:relation]][:class_name].constantize.create!(params[:relation].singularize => params[:file])
20
- end
21
- else
22
- klass = params[:model].constantize.find(params[:id])
23
- attachment = klass.send(params[:relation].pluralize).create(params[:relation].singularize => params[:file])
24
- klass.save
25
- end
13
+ klass = params[:model].constantize.find(params[:id])
26
14
  end
27
15
 
16
+ if params[:video]
17
+ attachment = klass.send(params[:relation].pluralize).create(video: params[:video])
18
+ else
19
+ attachment = klass.send(params[:relation].pluralize).create(params[:relation].singularize => params[:file])
20
+ end
21
+
22
+ klass.save
23
+
28
24
  partial = params[:partial].blank? ? "/#{attachment.class::TYPE}" : params[:partial]
29
25
 
30
26
  render :partial => partial, :locals => {:model => params[:model], :relation => params[:relation], :attachment => attachment}
@@ -45,14 +41,10 @@ module MediaMagick
45
41
  attachments = params[:elements]
46
42
  attachments = attachments.split(',') unless attachments.kind_of?(Array)
47
43
 
48
- if !params[:embedded_in_model].blank?
49
- parent = params[:embedded_in_model].classify.constantize.find(params[:embedded_in_id]).send(params[:model].pluralize.downcase).find(params[:model_id])
50
- else
51
- parent = params[:model].constantize.find(params[:model_id])
52
- end
44
+ doc = find_doc_by_params(params)
53
45
 
54
46
  attachments.each_with_index do |id, i|
55
- attachment = parent.send(params[:relation]).find(id)
47
+ attachment = doc.send(params[:relation]).find(id)
56
48
  attachment.priority = i
57
49
  attachment.save
58
50
  end
@@ -61,14 +53,10 @@ module MediaMagick
61
53
  end
62
54
 
63
55
  def recreate_versions
64
- if !params[:embedded_in_model].blank?
65
- parent = params[:embedded_in_model].classify.constantize.find(params[:embedded_in_id]).send(params[:model].pluralize.downcase).find(params[:model_id])
66
- else
67
- parent = params[:model].classify.constantize.find(params[:model_id])
68
- end
56
+ doc = find_doc_by_params(params)
69
57
 
70
58
  errors = []
71
- parent.send(params[:relation].pluralize).each do |attachment|
59
+ doc.send(params[:relation].pluralize).each do |attachment|
72
60
  errors << attachment unless attachment.recreate_versions!
73
61
  end
74
62
 
@@ -1,13 +1,13 @@
1
1
  module MediaMagick
2
2
  module ApplicationHelper
3
- def attachment_container(model, relation, newAttachments = {}, loadedAttachments = {}, options = {})
3
+ def attachment_container(model, relation, options = {})
4
4
  data_attributes = {
5
5
  model: model.class.to_s,
6
6
  id: model.id.to_s,
7
7
  relation: relation.to_s
8
8
  }
9
9
 
10
- data_attributes.merge!(:partial => options[:partial]) if options[:partial]
10
+ data_attributes.merge!(:partial => get_partial_name(options))
11
11
  data_attributes.merge!(:embedded_in_id => options[:embedded_in].id.to_s, :embedded_in_model => options[:embedded_in].class.to_s) if options[:embedded_in]
12
12
 
13
13
  content_tag :div, id: model.class.to_s.downcase << '-' << relation.to_s, class: 'attachmentUploader ' << relation.to_s, data: data_attributes do
@@ -17,14 +17,55 @@ module MediaMagick
17
17
  partial_attributes = {
18
18
  model: model,
19
19
  relations: relation,
20
- newAttachments: newAttachments,
21
- loadedAttachments: loadedAttachments,
22
- partial: options[:partial]
20
+ newAttachments: options[:newAttachments] || {},
21
+ loadedAttachments: options[:loadedAttachments] || {},
22
+ partial: get_partial_name(options)
23
23
  }
24
24
 
25
25
  render '/upload', partial_attributes
26
26
  end
27
27
  end
28
28
  end
29
+
30
+ def attachment_container_for_video(model, relation, options = {})
31
+ data_attributes = {
32
+ model: model.class.to_s,
33
+ id: model.id.to_s,
34
+ relation: relation.to_s
35
+ }
36
+
37
+ data_attributes.merge!(:partial => get_partial_name(options))
38
+ data_attributes.merge!(:embedded_in_id => options[:embedded_in].id.to_s, :embedded_in_model => options[:embedded_in].class.to_s) if options[:embedded_in]
39
+
40
+ content_tag :div, id: model.class.to_s.downcase << '-' << relation.to_s, class: 'attachmentUploader ' << relation.to_s, data: data_attributes do
41
+ if block_given?
42
+ yield
43
+ else
44
+ partial_attributes = {
45
+ model: model,
46
+ relations: relation,
47
+ newAttachments: options[:newAttachments] || {},
48
+ loadedAttachments: options[:loadedAttachments] || {},
49
+ partial: get_partial_name(options)
50
+ }
51
+
52
+ render '/video', partial_attributes
53
+ end
54
+ end
55
+ end
56
+
57
+ private
58
+
59
+ def get_partial_name(options)
60
+ if options[:partial]
61
+ options[:partial]
62
+ else
63
+ if options[:as]
64
+ "/#{options[:as]}"
65
+ else
66
+ '/image'
67
+ end
68
+ end
69
+ end
29
70
  end
30
71
  end
@@ -1,6 +1,6 @@
1
1
  <li class="attachment" data-id="<%= attachment.to_param %>">
2
- <%= link_to attachment.filename, attachment.url unless attachment.file.nil? %>
3
- <%= link_to t('media_magick.remove'), "javascript://", method: "delete", confirm: t('media_magick.confirm_removal'), class: "remove btn btn-mini btn-danger" %>
2
+ <%= link_to attachment.filename, attachment.url, download: attachment.filename unless attachment.file.nil? %>
3
+ <%= link_to t('media_magick.remove'), "javascript://", method: "delete", class: "remove btn btn-mini btn-danger", data: { confirm: t('media_magick.confirm_removal') } %>
4
4
 
5
5
  <%= hidden_field_tag "#{model.parameterize}[#{relation.singularize}_ids][]", attachment.id if model && relation %>
6
6
  </li>
@@ -1,6 +1,6 @@
1
- <li class="attachment" id="<%= attachment.to_param %>" data-id="<%= attachment.to_param %>">
2
- <%= image_tag attachment.url, alt: attachment.filename %>
3
- <%= link_to t('media_magick.remove'), "javascript://", method: "delete", confirm: t('media_magick.confirm_removal'), class: "remove btn btn-mini btn-danger" %>
1
+ <li class="attachment" id="<%= attachment.to_param %>" data-id="<%= attachment.to_param %>">
2
+ <%= image_tag attachment.url, alt: attachment.filename if attachment.file %>
3
+ <%= link_to t('media_magick.remove'), "javascript://", method: "delete", class: "remove btn btn-mini btn-danger", data: { confirm: t('media_magick.confirm_removal') } %>
4
4
 
5
5
  <%= hidden_field_tag "#{model.parameterize}[#{relation.singularize}_ids][]", attachment.id if model && relation %>
6
6
  </li>
@@ -6,14 +6,7 @@
6
6
  <a class="pickAttachments btn" href="javascript://"><%= t('media_magick.select') %></a>
7
7
  <a class="uploadAttachments btn" href="javascript://"><%= t('media_magick.upload') %></a>
8
8
  </div>
9
+
9
10
  <ul class="loadedAttachments <%= loadedAttachments[:class] %>">
10
- <% model.send(relations).each do |attachment| %>
11
- <% if partial %>
12
- <%= render partial, attachment: attachment %>
13
- <% elsif attachment.class::TYPE.to_s == 'file' %>
14
- <%= render :partial => "/file", :locals => { :attachment => attachment } %>
15
- <% elsif attachment.class::TYPE.to_s == 'image' %>
16
- <%= render :partial => "/image", :locals => { :attachment => attachment, :model => nil, :relation => nil } %>
17
- <% end %>
18
- <% end %>
19
- </ul>
11
+ <%= render partial: partial, collection: model.send(relations), :as => :attachment, :locals => { model: nil, relation: nil } %>
12
+ </ul>
@@ -0,0 +1,8 @@
1
+ <% id = "#{model.class.name}-#{relations}" %>
2
+
3
+ <input type="text" id="attachmentVideoUploaderField<%= id %>">
4
+ <a id="attachmentVideoUploader<%= id %>" href="javascript://">upload</a>
5
+
6
+ <ul class="loadedAttachments <%= loadedAttachments[:class] %>">
7
+ <%= render partial: partial, collection: model.send(relations), :as => :attachment, :locals => { model: nil, relation: nil } %>
8
+ </ul>
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'mongoid', '~> 3.0.0'
4
+
5
+ gem 'jquery-rails'
6
+
7
+ gemspec :path => '../'
data/lib/media_magick.rb CHANGED
@@ -3,8 +3,4 @@ require 'plupload/rails'
3
3
 
4
4
  require 'media_magick/model'
5
5
  require 'media_magick/engine'
6
- require 'media_magick/version'
7
-
8
- module MediaMagick
9
- # Your code goes here...
10
- end
6
+ require 'media_magick/version'
@@ -0,0 +1,20 @@
1
+ module MediaMagick
2
+ module Controller
3
+ module Helpers
4
+ extend ActiveSupport::Concern
5
+
6
+ # {"embedded_in_model"=>"embedded_model",
7
+ # "embedded_in_id"=>"embedded_id", "model"=>"model",
8
+ # "model_id"=>"id"
9
+ # }
10
+ def find_doc_by_params(params)
11
+ if params[:embedded_in_model].blank?
12
+ doc = params[:model].classify.constantize.find(params[:model_id])
13
+ else
14
+ doc = params[:embedded_in_model].classify.constantize.find(params[:embedded_in_id]).send(params[:model].pluralize.downcase).find(params[:model_id])
15
+ end
16
+ doc
17
+ end
18
+ end
19
+ end
20
+ end
@@ -6,4 +6,4 @@ module MediaMagick
6
6
  ActionView::Base.send :include, MediaMagick::ApplicationHelper
7
7
  end
8
8
  end
9
- end
9
+ end