media_magick 0.0.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 (75) hide show
  1. data/.gitignore +9 -0
  2. data/.rspec +1 -0
  3. data/.travis.yml +11 -0
  4. data/Gemfile +7 -0
  5. data/LICENSE +22 -0
  6. data/README.md +91 -0
  7. data/Rakefile +18 -0
  8. data/app/assets/images/media_magick/.gitkeep +0 -0
  9. data/app/assets/javascripts/media_magick/application.js +15 -0
  10. data/app/assets/javascripts/media_magick/plupload_it.js +135 -0
  11. data/app/assets/javascripts/media_magick/toggleSortable.js +71 -0
  12. data/app/assets/stylesheets/media_magick/application.css +13 -0
  13. data/app/controllers/media_magick/application_controller.rb +4 -0
  14. data/app/controllers/media_magick/attach_controller.rb +50 -0
  15. data/app/helpers/media_magick/application_helper.rb +13 -0
  16. data/app/views/_file.html.erb +4 -0
  17. data/app/views/_image.html.erb +4 -0
  18. data/app/views/_upload.html.erb +17 -0
  19. data/app/views/layouts/media_magick/application.html.erb +14 -0
  20. data/config/locales/en.yml +5 -0
  21. data/config/locales/pt-BR.yml +8 -0
  22. data/config/routes.rb +6 -0
  23. data/lib/carrierwave/mongoid.rb +65 -0
  24. data/lib/media_magick/attachment_uploader.rb +46 -0
  25. data/lib/media_magick/engine.rb +9 -0
  26. data/lib/media_magick/model.rb +39 -0
  27. data/lib/media_magick/version.rb +3 -0
  28. data/lib/media_magick.rb +10 -0
  29. data/lib/tasks/media_magick_tasks.rake +4 -0
  30. data/media_magick.gemspec +28 -0
  31. data/script/rails +8 -0
  32. data/spec/controllers/media_magick/attach_controller_spec.rb +59 -0
  33. data/spec/dummy/README.rdoc +261 -0
  34. data/spec/dummy/Rakefile +7 -0
  35. data/spec/dummy/app/assets/javascripts/application.js +15 -0
  36. data/spec/dummy/app/assets/stylesheets/application.css +13 -0
  37. data/spec/dummy/app/controllers/application_controller.rb +3 -0
  38. data/spec/dummy/app/helpers/application_helper.rb +2 -0
  39. data/spec/dummy/app/mailers/.gitkeep +0 -0
  40. data/spec/dummy/app/models/.gitkeep +0 -0
  41. data/spec/dummy/app/models/album.rb +14 -0
  42. data/spec/dummy/app/models/product.rb +9 -0
  43. data/spec/dummy/app/uploaders/picture_uploader.rb +55 -0
  44. data/spec/dummy/app/views/layouts/application.html.erb +14 -0
  45. data/spec/dummy/config/application.rb +62 -0
  46. data/spec/dummy/config/boot.rb +10 -0
  47. data/spec/dummy/config/environment.rb +5 -0
  48. data/spec/dummy/config/environments/development.rb +30 -0
  49. data/spec/dummy/config/environments/production.rb +67 -0
  50. data/spec/dummy/config/environments/test.rb +34 -0
  51. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -0
  52. data/spec/dummy/config/initializers/inflections.rb +15 -0
  53. data/spec/dummy/config/initializers/mime_types.rb +5 -0
  54. data/spec/dummy/config/initializers/secret_token.rb +7 -0
  55. data/spec/dummy/config/initializers/session_store.rb +8 -0
  56. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -0
  57. data/spec/dummy/config/locales/en.yml +5 -0
  58. data/spec/dummy/config/mongoid.yml +20 -0
  59. data/spec/dummy/config/routes.rb +4 -0
  60. data/spec/dummy/config.ru +4 -0
  61. data/spec/dummy/lib/assets/.gitkeep +0 -0
  62. data/spec/dummy/log/.gitkeep +0 -0
  63. data/spec/dummy/public/404.html +26 -0
  64. data/spec/dummy/public/422.html +26 -0
  65. data/spec/dummy/public/500.html +25 -0
  66. data/spec/dummy/public/favicon.ico +0 -0
  67. data/spec/dummy/script/rails +6 -0
  68. data/spec/helpers/media_magick/application_helper_spec.rb +29 -0
  69. data/spec/integration/images_spec.rb +47 -0
  70. data/spec/lib/media_magick/model_spec.rb +80 -0
  71. data/spec/routing/attach_spec.rb +24 -0
  72. data/spec/spec_helper.rb +40 -0
  73. data/spec/support/fixtures/nu.jpg +0 -0
  74. data/spec/support/fixtures/nu.txt +1 -0
  75. metadata +319 -0
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ Gemfile.lock
2
+ coverage
3
+ pkg
4
+ spec/dummy/log/*.log
5
+ spec/dummy/tmp/
6
+ spec/dummy/public/uploads
7
+ spec/dummy/.sass-cache
8
+ uploads
9
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,11 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.2
4
+ - 1.9.3
5
+ - ruby-head
6
+ notifications:
7
+ email:
8
+ - contato@lucasrenan.com
9
+ - rbrancher@gmail.com
10
+ - tiagogodinho3@gmail.com
11
+ - stefano@heavenstudio.com.br
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in media_magick.gemspec
4
+ gemspec
5
+
6
+ # jquery-rails is used by the dummy application
7
+ gem 'jquery-rails'
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 Lucas Renan, Rodrigo Brancher e Tiago Rafael Godinho
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,91 @@
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)
2
+
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
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'media_magick'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install media_magick
18
+
19
+ ## Usage
20
+
21
+ ### Model
22
+
23
+ ``` ruby
24
+ class Album
25
+ include Mongoid::Document
26
+ include MediaMagick::Model
27
+
28
+ attachs_many :photos, type: 'image'
29
+ end
30
+
31
+ album = Album.create
32
+ album.photos.create(photo: params[:file])
33
+ album.reload.photos.first.url
34
+ album.reload.photos.first.filename
35
+ ```
36
+
37
+ #### Custom classes
38
+
39
+ ``` ruby
40
+ class Album
41
+ include Mongoid::Document
42
+ include MediaMagick::Model
43
+
44
+ attachs_many :photos, type: 'image' do
45
+ field :tags, type: Array
46
+ end
47
+ end
48
+
49
+ album = Album.create
50
+ album.photos.create(photo: params[:file], tags: ['ruby', 'guru'])
51
+ album.reload.photos.first.tags #=> ['ruby', 'guru']
52
+ ```
53
+
54
+ #### Custom uploader
55
+
56
+ ``` ruby
57
+ class PhotoUploader < CarrierWave::Uploader::Base
58
+ include CarrierWave::MiniMagick
59
+
60
+ storage :file
61
+
62
+ def store_dir
63
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
64
+ end
65
+
66
+ version :thumb do
67
+ process :resize_to_fit => [156, 156]
68
+ end
69
+ end
70
+ ```
71
+
72
+ ``` ruby
73
+ class Album
74
+ include Mongoid::Document
75
+ include MediaMagick::Model
76
+
77
+ attachs_many :photos, type: 'image', uploader: PhotoUploader
78
+ end
79
+
80
+ album = Album.create
81
+ album.photos.create(photo: params[:file])
82
+ album.reload.photos.first.thumb.url
83
+ ```
84
+
85
+ ## Contributing
86
+
87
+ 1. Fork it
88
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
89
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
90
+ 4. Push to the branch (`git push origin my-new-feature`)
91
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env rake
2
+ begin
3
+ require 'bundler/setup'
4
+ rescue LoadError
5
+ puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
6
+ end
7
+
8
+ require 'bundler/gem_tasks'
9
+ require 'rspec/core/rake_task'
10
+
11
+ APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
12
+ load 'rails/tasks/engine.rake'
13
+
14
+ Bundler::GemHelper.install_tasks
15
+
16
+ RSpec::Core::RakeTask.new(:spec)
17
+
18
+ task :default => 'spec'
File without changes
@@ -0,0 +1,15 @@
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or vendor/assets/javascripts of plugins, if any, can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // the compiled file.
9
+ //
10
+ // WARNING: THE FIRST BLANK LINE MARKS THE END OF WHAT'S TO BE PROCESSED, ANY BLANK LINE SHOULD
11
+ // GO AFTER THE REQUIRES BELOW.
12
+ //
13
+ //= require jquery
14
+ //= require jquery_ujs
15
+ //= require_tree .
@@ -0,0 +1,135 @@
1
+ //= require plupload
2
+
3
+ // optional, only needed if you'd like to use plupload localized
4
+ //= require plupload/i18n/pt-br
5
+
6
+ // optional, but recommended. it sets generic settings like flash url, etc.
7
+ //= require plupload.settings
8
+
9
+ // optional, only if you want to use the jquery integration
10
+ //= require jquery.plupload.queue
11
+
12
+ // optional, choose the ones you'd like to use
13
+ //= require plupload.flash
14
+ //= require plupload.html4
15
+ //= require plupload.html5
16
+ //= require plupload.gears
17
+
18
+ (function($) {
19
+
20
+ $.fn.pluploadIt = function (options) {
21
+
22
+ var settings = $.extend({
23
+ browse_button: 'pickAttachments', // triggers modal to select files
24
+ container: 'attachmentUploader',
25
+ drop_element: 'dropAttachments',
26
+ flash_swf_url: '/assets/plupload.flash.swf',
27
+ max_file_size: '10mb',
28
+ queue_element: 'attachmentQueue',
29
+ resize: false,
30
+ runtimes: 'gears,html5,flash,browserplus,html4',
31
+ silverlight_xap_url: '/assets/plupload.silverlight.xap',
32
+ target_list: 'loadedAttachments',
33
+ unique_names: false,
34
+ upload_button: 'uploadAttachments',
35
+ url: '/upload'
36
+ }, options);
37
+
38
+ return this.each(function() {
39
+
40
+ var $container = $(this);
41
+ settings.container = $container.attr('id');
42
+
43
+ // setup unique ids from classes
44
+ $container.find('.' + settings.browse_button).attr('id', settings.container + '-' + settings.browse_button);
45
+ $container.find('.' + settings.drop_element).attr('id', settings.container + '-' + settings.drop_element);
46
+ $container.find('.' + settings.queue_element).attr('id', settings.container + '-' + settings.queue_element);
47
+ $container.find('.' + settings.target_list).attr('id', settings.container + '-' + settings.target_list);
48
+ $container.find('.' + settings.upload_button).attr('id', settings.container + '-' + settings.upload_button);
49
+
50
+ var uploader = new plupload.Uploader({
51
+ browse_button: settings.container + '-' + settings.browse_button,
52
+ container: settings.container,
53
+ drop_element: settings.container + '-' + settings.drop_element,
54
+ flash_swf_url: settings.flash_swf_url,
55
+ max_file_size: settings.max_file_size,
56
+ multipart_params: {
57
+ id: $container.data('id'),
58
+ relation: $container.data('relation'),
59
+ model: $container.data('model')
60
+ },
61
+ resize: settings.resize,
62
+ runtimes: settings.runtimes,
63
+ silverlight_xap_url: settings.silverlight_xap_url,
64
+ unique_names: settings.unique_names,
65
+ url: settings.url
66
+ });
67
+
68
+ uploader.bind('Init', function(up, params) {
69
+ if ($('#' + settings.container + '-runtimeInfo').length > 0) $('#' + settings.container + '-runtimeInfo').text("Current runtime: " + params.runtime);
70
+ // if ($container.find("dt").length > 0 && $container.find("dt").text() == "") $container.find("dt").text($container.attr('id'));
71
+
72
+ $("a.remove").live('click', function() {
73
+
74
+ var $attachment = $(this).parents('.attachment');
75
+
76
+ $.get('/remove', {
77
+ model: $container.data('model'),
78
+ id: $container.data('id'),
79
+ relation: $container.data('relation'),
80
+ relation_id: $attachment.data('id')
81
+ }, function(data) {
82
+ $attachment.remove();
83
+ });
84
+ });
85
+ });
86
+
87
+ $('#' + settings.container + '-' + settings.upload_button).click(function(e) {
88
+ uploader.start();
89
+ e.preventDefault();
90
+ });
91
+
92
+ uploader.init();
93
+
94
+ (function (container, queue_element, target_list) {
95
+ uploader.bind('FilesAdded', function(up, files) {
96
+ $.each(files, function(i, file) {
97
+ $('#' + container + '-' + queue_element).append(
98
+ '<li id="' + file.id + '" class="attachment">' +
99
+ file.name + ' (' + plupload.formatSize(file.size) + ') <span class="status"></span>' +
100
+ '</li>'
101
+ ).find("li:last").append(
102
+ $('<a href="javascript://" class="remove btn btn-mini">x</a>').bind('click', function () {
103
+ uploader.removeFile(file);
104
+ $(this).parent().remove();
105
+ })
106
+ );
107
+ });
108
+
109
+ up.refresh(); // Reposition Flash/Silverlight
110
+ });
111
+
112
+ uploader.bind('UploadProgress', function(up, file) {
113
+ $('#' + file.id + " span.status").html(file.percent + "%");
114
+ });
115
+
116
+ uploader.bind('Error', function(up, err) {
117
+ $('#' + container + '-' + queue_element).append("<li class='attachment error'>Error: " + err.code +
118
+ ", Message: " + err.message +
119
+ (err.file ? ", File: " + err.file.name : "") +
120
+ "</li>");
121
+
122
+ up.refresh(); // Reposition Flash/Silverlight
123
+ });
124
+
125
+ uploader.bind('FileUploaded', function(up, file, response) {
126
+ $('#' + file.id).addClass('completed');
127
+ $('#' + file.id + " span.status").html("100%");
128
+ $("#" + container + '-' + target_list).append(response.response);
129
+ });
130
+ })(settings.container, settings.queue_element, settings.target_list);
131
+
132
+ });
133
+
134
+ };
135
+ })(jQuery);
@@ -0,0 +1,71 @@
1
+ mediaMagick = {
2
+ toggleSortable: function (parentNode, updateUrl, options) {
3
+ if (parentNode === undefined) { return 'error - no node specified'; }
4
+
5
+ var settings = $.extend({
6
+ 'linkSelector': 'a.toggleSortable',
7
+ 'loadData': function () {
8
+ return {elements: function () { return parentNode.sortable('toArray'); }}
9
+ },
10
+ 'messagingEngine': 'sticky'
11
+ }, options);
12
+
13
+ if (parentNode.hasClass('sortableActive')) {
14
+
15
+ parentNode.sortable('disable');
16
+ parentNode.removeClass('sortableActive');
17
+
18
+ if (animation !== undefined) {
19
+ clearInterval(animation);
20
+ }
21
+
22
+ var currMessage = $(settings.linkSelector).html();
23
+ $(settings.linkSelector).html($(settings.linkSelector).data('message'));
24
+ $(settings.linkSelector).data('message', currMessage);
25
+
26
+ $.ajax({
27
+ type: "PUT",
28
+ url: updateUrl,
29
+ data: settings.loadData(),
30
+ beforeSend: function () {
31
+ if (settings.messagingEngine === 'sticky') $.sticky('salvando ordenação…');
32
+ },
33
+ success: function () {
34
+ if (settings.messagingEngine === 'sticky') $.sticky('ordenação salva!');
35
+ parentNode.sortable('destroy');
36
+ },
37
+ error: function () {
38
+ if (settings.messagingEngine === 'sticky') $.sticky('erro ao salvar ordenação…');
39
+ parentNode.sortable('destroy');
40
+ }
41
+ });
42
+ } else {
43
+ parentNode.addClass('sortableActive');
44
+ parentNode.children().removeClass('clearleft');
45
+ parentNode.sortable();
46
+
47
+ var currMessage = $(settings.linkSelector).html();
48
+ $(settings.linkSelector).html($(settings.linkSelector).data('message'));
49
+ $(settings.linkSelector).data('message', currMessage);
50
+
51
+ animation = setInterval(function () {
52
+ mediaMagick.shake(parentNode.children());
53
+ }, 100);
54
+ }
55
+ },
56
+ shake: function (nodes) {
57
+ var arr = new Array();
58
+ nodes.each(function (i, el) {
59
+ var el = $(el);
60
+
61
+ var rT = Math.floor(Math.random() * 3);
62
+ var rL = Math.floor(Math.random() * 2);
63
+
64
+ el.css('margin-top', -rT);
65
+ el.css('margin-bottom', 24 + rT);
66
+
67
+ el.css('margin-left', 6 - rL);
68
+ el.css('margin-right', 6 + rL);
69
+ });
70
+ }
71
+ }
@@ -0,0 +1,13 @@
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or vendor/assets/stylesheets of plugins, if any, can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the top of the
9
+ * compiled file, but it's generally better to create a new file per style scope.
10
+ *
11
+ *= require_self
12
+ *= require_tree .
13
+ */
@@ -0,0 +1,4 @@
1
+ module MediaMagick
2
+ class ApplicationController < ActionController::Base
3
+ end
4
+ end
@@ -0,0 +1,50 @@
1
+ require 'action_controller/railtie'
2
+
3
+ module MediaMagick
4
+ class AttachController < ActionController::Base
5
+ def create
6
+ klass = params[:model].constantize.find(params[:id])
7
+ attachment = klass.send(params[:relation].pluralize).create(params[:relation].singularize => params[:file])
8
+ klass.save
9
+
10
+ render :partial => "/#{attachment.class::TYPE}", :locals => {:attachment => attachment}
11
+ end
12
+
13
+ def destroy
14
+ attachment = params[:model].classify.constantize.find(params[:id]).send(params[:relation].pluralize).find(params[:relation_id])
15
+ attachment.destroy
16
+ render nothing: true
17
+ end
18
+
19
+ def update_priority
20
+ attachments = params[:elements]
21
+ attachments = attachments.split(',') unless attachments.kind_of?(Array)
22
+
23
+ parent = params[:model].constantize.find(params[:model_id])
24
+
25
+ attachments.each_with_index do |id, i|
26
+ attachment = parent.send(params[:relation]).find(id)
27
+ attachment.priority = i
28
+ attachment.save
29
+ end
30
+
31
+ render :text => params[:attachments]
32
+ end
33
+
34
+ def recreate_versions
35
+ parent = params[:model].classify.constantize.find(params[:model_id])
36
+
37
+ errors = []
38
+ parent.send(params[:relation].pluralize).each do |attachment|
39
+ errors << attachment unless attachment.recreate_versions!
40
+ end
41
+
42
+ if errors.empty?
43
+ redirect_to :back, notice: t('media_magick.recreate_versions.ok')
44
+ else
45
+ redirect_to :back, notice: t('media_magick.recreate_versions.error')
46
+ end
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,13 @@
1
+ module MediaMagick
2
+ module ApplicationHelper
3
+ def attachment_container(model, relation, newAttachments = {}, loadedAttachments= {})
4
+ content_tag :div, id: model.class.to_s.downcase << '-' << relation.to_s, class: 'attachmentUploader ' << relation.to_s, data: { model: model.class.to_s, id: model.id.to_s, relation: relation.to_s } do
5
+ if block_given?
6
+ yield
7
+ else
8
+ render :partial => "/upload", :locals => { model: model, relations: relation, newAttachments: newAttachments, loadedAttachments: loadedAttachments }
9
+ end
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,4 @@
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" %>
4
+ </li>
@@ -0,0 +1,4 @@
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" %>
4
+ </li>
@@ -0,0 +1,17 @@
1
+ <div class="newAttachments <%= newAttachments[:class] %>">
2
+ <ul class="attachmentQueue">
3
+ </ul>
4
+
5
+ <div class="dropAttachments"></div>
6
+ <a class="pickAttachments btn" href="javascript://"><%= t('media_magick.select') %></a>
7
+ <a class="uploadAttachments btn" href="javascript://"><%= t('media_magick.upload') %></a>
8
+ </div>
9
+ <ul class="loadedAttachments <%= loadedAttachments[:class] %>">
10
+ <% for attachment in model.send(relations).each do %>
11
+ <% if attachment.class::TYPE.to_s == 'file' %>
12
+ <%= render :partial => "/file", :locals => { :attachment => attachment } %>
13
+ <% elsif attachment.class::TYPE.to_s == 'image' %>
14
+ <%= render :partial => "/image", :locals => { :attachment => attachment } %>
15
+ <% end %>
16
+ <% end %>
17
+ </ul>
@@ -0,0 +1,14 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <title>MediaMagick</title>
5
+ <%= stylesheet_link_tag "media_magick/application", :media => "all" %>
6
+ <%= javascript_include_tag "media_magick/application" %>
7
+ <%= csrf_meta_tags %>
8
+ </head>
9
+ <body>
10
+
11
+ <%= yield %>
12
+
13
+ </body>
14
+ </html>
@@ -0,0 +1,5 @@
1
+ en:
2
+ media_magick:
3
+ confirm_removal: "are you sure you want to delete this file?"
4
+ select: "select files"
5
+ upload: "upload files"
@@ -0,0 +1,8 @@
1
+ pt-BR:
2
+ media_magick:
3
+ confirm_removal: "têm certeza que deseja apagar este arquivo?"
4
+ select: "selecionar arquivos"
5
+ upload: "enviar arquivos"
6
+ recreate_versions:
7
+ ok: "imagens recriadas com sucesso!"
8
+ error: "ocorreu um erro ao recriar as imagens…"
data/config/routes.rb ADDED
@@ -0,0 +1,6 @@
1
+ Rails.application.routes.draw do
2
+ match 'upload' => 'media_magick/attach#create'
3
+ match 'remove' => 'media_magick/attach#destroy'
4
+ match 'update_priority' => 'media_magick/attach#update_priority'
5
+ match 'recreate_versions' => 'media_magick/attach#recreate_versions', as: 'recreate_versions'
6
+ end
@@ -0,0 +1,65 @@
1
+ require 'carrierwave'
2
+ require 'carrierwave/validations/active_model'
3
+ require 'mongoid'
4
+
5
+ module CarrierWave
6
+ module Mongoid
7
+ include CarrierWave::Mount
8
+ ##
9
+ # See +CarrierWave::Mount#mount_uploader+ for documentation
10
+ #
11
+ def mount_uploader(column, uploader=nil, options={}, &block)
12
+ field options[:mount_on] || column
13
+
14
+ super
15
+
16
+ alias_method :read_uploader, :read_attribute
17
+ alias_method :write_uploader, :write_attribute
18
+ public :read_uploader
19
+ public :write_uploader
20
+
21
+ include CarrierWave::Validations::ActiveModel
22
+
23
+ validates_integrity_of column if uploader_option(column.to_sym, :validate_integrity)
24
+ validates_processing_of column if uploader_option(column.to_sym, :validate_processing)
25
+
26
+ after_save :"store_#{column}!"
27
+ before_save :"write_#{column}_identifier"
28
+ after_destroy :"remove_#{column}!"
29
+ before_update :"store_previous_model_for_#{column}"
30
+ after_save :"remove_previously_stored_#{column}"
31
+
32
+ class_eval <<-RUBY, __FILE__, __LINE__+1
33
+ def #{column}=(new_file)
34
+ column = _mounter(:#{column}).serialization_column
35
+ send(:"\#{column}_will_change!")
36
+ super
37
+ end
38
+
39
+ # Overrides Mongoid's default dirty behavior to instead work more like
40
+ # ActiveRecord's. Mongoid doesn't deem an attribute as changed unless
41
+ # the new value is different than the original. Given that CarrierWave
42
+ # caches files before save, it's necessary to know that there's a
43
+ # pending change even though the attribute value itself might not
44
+ # reflect that yet.
45
+ def #{column}_changed?
46
+ changed_attributes.has_key?("#{column}")
47
+ end
48
+
49
+ def find_previous_model_for_#{column}
50
+ if self.embedded?
51
+ ancestors = [[ self.metadata.key, self._parent ]].tap { |x| x.unshift([ x.first.last.metadata.key, x.first.last._parent ]) while x.first.last.embedded? }
52
+ first_parent = ancestors.first.last
53
+ reloaded_parent = first_parent.class.find(first_parent.to_key.first)
54
+ association = ancestors.inject(reloaded_parent) { |parent,(key,ancestor)| (parent.is_a?(Array) ? parent.find(ancestor.to_key.first) : parent).send(key) }
55
+ association.is_a?(Array) ? association.find(to_key.first) : association
56
+ else
57
+ self.class.find(to_key.first)
58
+ end
59
+ end
60
+ RUBY
61
+ end
62
+ end # Mongoid
63
+ end # CarrierWave
64
+
65
+ Mongoid::Document::ClassMethods.send(:include, CarrierWave::Mongoid)
@@ -0,0 +1,46 @@
1
+ class AttachmentUploader < CarrierWave::Uploader::Base
2
+
3
+ # Include RMagick or MiniMagick support:
4
+ # include CarrierWave::RMagick
5
+ # include CarrierWave::MiniMagick
6
+
7
+ # Choose what kind of storage to use for this uploader:
8
+ storage :file
9
+ # storage :fog
10
+
11
+ # Override the directory where uploaded files will be stored.
12
+ # This is a sensible default for uploaders that are meant to be mounted:
13
+ def store_dir
14
+ "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
15
+ end
16
+
17
+ # Provide a default URL as a default if there hasn't been a file uploaded:
18
+ # def default_url
19
+ # "/images/fallback/" + [version_name, "default.png"].compact.join('_')
20
+ # end
21
+
22
+ # Process files as they are uploaded:
23
+ # process :scale => [200, 300]
24
+ #
25
+ # def scale(width, height)
26
+ # # do something
27
+ # end
28
+
29
+ # Create different versions of your uploaded files:
30
+ # version :thumb do
31
+ # process :resize_to_fit => [156, 156]
32
+ # end
33
+
34
+ # Add a white list of extensions which are allowed to be uploaded.
35
+ # For images you might use something like this:
36
+ # def extension_white_list
37
+ # %w(jpg jpeg gif png)
38
+ # end
39
+
40
+ # Override the filename of the uploaded files:
41
+ # Avoid using model.id or version_name here, see uploader/store.rb for details.
42
+ # def filename
43
+ # "something.jpg" if original_filename
44
+ # end
45
+
46
+ end
@@ -0,0 +1,9 @@
1
+ module MediaMagick
2
+ class Engine < ::Rails::Engine
3
+ isolate_namespace MediaMagick
4
+
5
+ initializer 'media_magick.include_helper' do
6
+ ActionView::Base.send :include, MediaMagick::ApplicationHelper
7
+ end
8
+ end
9
+ end