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,14 @@
1
+ <li class="attachment" id="<%= attachment.to_param %>" data-id="<%= attachment.to_param %>">
2
+ <% # smell %>
3
+ <% if attachment.file %>
4
+ <% if attachment.class::TYPE == "file" %>
5
+ <%= link_to attachment.filename, attachment.url, download: attachment.filename %>
6
+ <% else %>
7
+ <%= image_tag attachment.url, alt: attachment.filename %>
8
+ <% end %>
9
+ <% end %>
10
+
11
+ <%= link_to t('media_magick.remove'), "javascript://", method: "delete", class: "remove btn btn-mini btn-danger", data: { confirmation: t('media_magick.confirm_removal') } %>
12
+
13
+ <%= hidden_field_tag "#{model.parameterize}[#{relation.singularize}_ids][]", attachment.id if model && relation %>
14
+ </li>
@@ -0,0 +1,13 @@
1
+ <div class="newAttachments ">
2
+ <ul class="attachmentQueue">
3
+ </ul>
4
+
5
+ <div class="dropAttachments">
6
+ </div>
7
+ <%= link_to "javascript://", class: "pickAttachments btn" do %>
8
+ <%= t('media_magick.select') %>
9
+ <% end %>
10
+ <%= link_to "javascript://", class: "uploadAttachments btn" do %>
11
+ <%= t('media_magick.upload') %>
12
+ <% end %>
13
+ </div>
@@ -0,0 +1,2 @@
1
+ <%= text_field_tag "video", "", id: "", class: "attachmentVideoUploaderField" %>
2
+ <%= link_to "upload", "javascript://", class: "attachmentVideoUploaderButton" %>
@@ -3,3 +3,4 @@ en:
3
3
  confirm_removal: "are you sure you want to delete this file?"
4
4
  select: "select files"
5
5
  upload: "upload files"
6
+ remove: "remove"
@@ -1,7 +1,6 @@
1
1
  source 'https://rubygems.org'
2
2
 
3
3
  gem 'mongoid', '~> 3.0.0'
4
-
5
4
  gem 'jquery-rails'
6
5
 
7
- gemspec :path => '../'
6
+ gemspec :path => '../'
data/lib/media_magick.rb CHANGED
@@ -3,4 +3,4 @@ require 'plupload/rails'
3
3
 
4
4
  require 'media_magick/model'
5
5
  require 'media_magick/engine'
6
- require 'media_magick/version'
6
+ require 'media_magick/version'
@@ -3,18 +3,38 @@ module MediaMagick
3
3
  module Helpers
4
4
  extend ActiveSupport::Concern
5
5
 
6
- # {"embedded_in_model"=>"embedded_model",
7
- # "embedded_in_id"=>"embedded_id", "model"=>"model",
6
+ # {"embedded_in_model"=>"embedded_model",
7
+ # "embedded_in_id"=>"embedded_id", "model"=>"model",
8
8
  # "model_id"=>"id"
9
9
  # }
10
10
  def find_doc_by_params(params)
11
- if params[:embedded_in_model].blank?
11
+ if params[:embedded_in_model].blank?
12
12
  doc = params[:model].classify.constantize.find(params[:model_id])
13
13
  else
14
- doc = params[:embedded_in_model].classify.constantize.find(params[:embedded_in_id]).send(params[:model].pluralize.downcase).find(params[:model_id])
14
+ doc = params[:embedded_in_model].classify.constantize.find(params[:embedded_in_id]).send(params[:model].pluralize.downcase).find(params[:model_id])
15
15
  end
16
16
  doc
17
17
  end
18
+
19
+ # Creates a video based on a url
20
+ #
21
+ # @example Creates a video for an user
22
+ # user = User.create
23
+ # params = {relation: "photo", video: "youtube.com/watch?v=FfUHkPf9D9k"}
24
+ # create_video(user, params)
25
+ #
26
+ # @param [ Mongoid::Document ] Mongoid document object
27
+ # @param [ Hash ] Hash with relation name and video url
28
+ #
29
+ # @return [ Mongoid::Document ] The mongoid document object
30
+ def create_video(obj, params)
31
+ relation_metadata = obj.class.relations[params[:relation]]
32
+
33
+ unless relation_metadata.many? # one
34
+ return obj.send("create_#{params[:relation]}", {video: params[:video]})
35
+ end
36
+ obj.send(params[:relation]).create(video: params[:video])
37
+ end
18
38
  end
19
39
  end
20
- end
40
+ 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
@@ -0,0 +1,23 @@
1
+ module MediaMagick
2
+ module Image
3
+ module Dimensions
4
+ extend ActiveSupport::Concern
5
+
6
+ def size
7
+ version_key = version_name.present? ? version_name : "_original"
8
+
9
+ begin
10
+ if model.dimensions[version_key.to_s].nil?
11
+ image = MiniMagick::Image.open(file.path)
12
+ model.dimensions[version_key.to_s] = {"width" => image[:width], "height" => image[:height]}
13
+ model.save
14
+ end
15
+
16
+ return model.dimensions[version_key.to_s]
17
+ rescue
18
+ return {"width" => 0, "height" => 0}
19
+ end
20
+ end
21
+ end
22
+ end
23
+ end
@@ -8,11 +8,15 @@ module MediaMagick
8
8
  extend ActiveSupport::Concern
9
9
 
10
10
  module ClassMethods
11
+ # smell
11
12
  def attaches_many(name, options = {})
12
13
  attaches_block = block_given? ? Proc.new : nil
13
14
 
14
15
  name_camelcase = create_attaches_class(name, options, attaches_block) do
15
- create_video_methods(name) if options[:allow_videos]
16
+ if options[:allow_videos]
17
+ raise "name 'videos' not allowed" if name.to_s == "videos"
18
+ create_video_methods(name)
19
+ end
16
20
 
17
21
  field :priority, type: Integer, default: 0
18
22
 
@@ -24,11 +28,15 @@ module MediaMagick
24
28
  embeds_many(name, :as => :attachmentable, class_name: "#{self}#{name_camelcase}")
25
29
  end
26
30
 
31
+ # smell
27
32
  def attaches_one(name, options = {})
28
33
  attaches_block = block_given? ? Proc.new : nil
29
34
 
30
35
  name_camelcase = create_attaches_class(name, options, attaches_block) do
31
- create_video_methods(name) if options[:allow_videos]
36
+ if options[:allow_videos]
37
+ raise "name 'video' not allowed" if name.to_s == "video"
38
+ create_video_methods(name)
39
+ end
32
40
 
33
41
  embedded_in(name)
34
42
  end
@@ -44,6 +52,7 @@ module MediaMagick
44
52
  extend CarrierWave::Mount
45
53
 
46
54
  field :type, type: String, default: options[:as] || 'image'
55
+ field :dimensions, type: Hash, default: {}
47
56
 
48
57
  def self.create_video_methods(name)
49
58
  field :video, type: String
@@ -53,13 +62,11 @@ module MediaMagick
53
62
  super
54
63
 
55
64
  video = MediaMagick::Video::Parser.new(url)
56
-
57
65
  send(self.class::ATTACHMENT).store!(video.to_image) if video.valid?
58
66
  end
59
67
 
60
68
  def source(options = {})
61
69
  video = MediaMagick::Video::Parser.new(self.video)
62
-
63
70
  video.to_html(options) if video.valid?
64
71
  end
65
72
  end
@@ -80,10 +87,20 @@ module MediaMagick
80
87
  end
81
88
 
82
89
  name_camelcase = name.to_s.camelcase
83
- Object.const_set "#{self}#{name_camelcase}", klass
90
+
91
+ # sets klass to a constant
92
+ # ProductImages = klass
93
+ constantize_embedded_klass(self, name_camelcase, klass)
84
94
 
85
95
  return name_camelcase
86
96
  end
97
+
98
+ def constantize_embedded_klass(klass_self, relation_name, embedded_klass)
99
+ parent = klass_self.parents.first # module or Object
100
+ embedded_klass_name = "#{klass_self.to_s.demodulize}#{relation_name}"
101
+ parent.const_set embedded_klass_name, embedded_klass
102
+ end
103
+
87
104
  end
88
105
  end
89
106
  end
@@ -1,3 +1,3 @@
1
1
  module MediaMagick
2
- VERSION = '0.2.0'
3
- end
2
+ VERSION = '0.3.0'
3
+ end
data/media_magick.gemspec CHANGED
@@ -14,19 +14,16 @@ Gem::Specification.new do |gem|
14
14
  gem.name = 'media_magick'
15
15
  gem.require_paths = ['lib']
16
16
  gem.version = MediaMagick::VERSION
17
-
18
- gem.add_dependency 'carrierwave', '~> 0.7.0'
19
- gem.add_dependency 'mongoid', '>= 2.4'
17
+
18
+ gem.add_dependency 'carrierwave', '~> 0.8.0'
19
+ gem.add_dependency 'mongoid', '>= 2.7.0'
20
20
  gem.add_dependency 'plupload-rails', '~> 1.0.6'
21
21
  gem.add_dependency 'rails', '~> 3.2.0'
22
-
23
- gem.add_development_dependency 'bson_ext', '~> 1.7.0'
24
- gem.add_development_dependency 'mini_magick', '~> 3.4'
25
- gem.add_development_dependency 'rake', '~> 0.9'
26
- gem.add_development_dependency 'rspec-rails', '~> 2.11.0'
27
- gem.add_development_dependency 'simplecov', '~> 0.7.0'
22
+ gem.add_dependency 'mini_magick', '~> 3.6.0'
28
23
 
29
- #gems for code analyses
30
- gem.add_development_dependency 'flog', '~> 3.0.0.b2'
31
- gem.add_development_dependency 'flay', '~> 2.0.0.b1'
32
- end
24
+ gem.add_development_dependency 'rake', '~> 10.1.0'
25
+ gem.add_development_dependency 'rspec-rails', '~> 2.13.2'
26
+ gem.add_development_dependency 'simplecov', '~> 0.7.1'
27
+ gem.add_development_dependency 'guard-rspec', '~> 3.0.2'
28
+ gem.add_development_dependency 'rb-fsevent', '~> 0.9.3'
29
+ end
@@ -12,23 +12,10 @@ describe MediaMagick::AttachController do
12
12
  post :create, { model: 'Album', id: album.id, relation: 'photos', file: fixture_file_upload("#{File.expand_path('../../..', __FILE__)}/support/fixtures/nu.jpg") }
13
13
  }.to change { album.reload.photos.count }.by(1)
14
14
 
15
- response.should render_template('_image')
16
-
15
+ response.should render_template('_loader')
17
16
  response.body.should =~ /nu.jpg/m
18
17
  end
19
18
 
20
- it "creates a new video" do
21
- album = Album.create
22
-
23
- expect {
24
- post :create, { model: 'Album', id: album.id, relation: 'photos_and_videos', video: 'youtube.com/watch?v=FfUHkPf9D9k' }
25
- }.to change { album.reload.photos_and_videos.count }.by(1)
26
-
27
- response.should render_template('_image')
28
-
29
- response.body.should =~ /FfUHkPf9D9k/m
30
- end
31
-
32
19
  it "creates a new photo for embedded models" do
33
20
  album = Album.create
34
21
  track = album.tracks.create
@@ -37,16 +24,43 @@ describe MediaMagick::AttachController do
37
24
  post :create, { embedded_in_id: album.id, embedded_in_model: 'Album', model: 'Track', id: track.id, relation: 'files', file: fixture_file_upload("#{File.expand_path('../../..', __FILE__)}/support/fixtures/nu.jpg") }
38
25
  }.to change { track.reload.files.count }.by(1)
39
26
 
40
- response.should render_template('_image')
41
-
27
+ response.should render_template('_loader')
42
28
  response.body.should =~ /nu.jpg/m
43
29
  end
44
30
 
45
- it "render a personalized partial" do
31
+ it "renders a custom partial" do
46
32
  album = Album.create
47
- post :create, { model: 'Album', id: album.id, relation: 'photos', partial: 'albums/photo', file: fixture_file_upload("#{File.expand_path('../../..', __FILE__)}/support/fixtures/nu.jpg") }
33
+ post :create, { model: 'Album', id: album.id, relation: 'photos', loader_partial: 'albums/photo', file: fixture_file_upload("#{File.expand_path('../../..', __FILE__)}/support/fixtures/nu.jpg") }
48
34
  response.should render_template('albums/_photo')
49
35
  end
36
+
37
+ describe "creating videos" do
38
+ context "relation is attaches many" do
39
+ it "creates a new video" do
40
+ album = Album.create
41
+
42
+ expect {
43
+ post :create, { model: 'Album', id: album.id, relation: 'photos_and_videos', video: 'youtube.com/watch?v=FfUHkPf9D9k' }
44
+ }.to change { album.reload.photos_and_videos.count }.by(1)
45
+
46
+ response.should render_template('_loader')
47
+ response.body.should =~ /FfUHkPf9D9k/m
48
+ end
49
+ end
50
+
51
+ context "relation is attaches one" do
52
+ it "creates a new video" do
53
+ user = User.create
54
+ video_url = 'youtube.com/watch?v=FfUHkPf9D9k'
55
+
56
+ post :create, { model: 'User', id: user.id, relation: 'photo_and_video', video: video_url }
57
+
58
+ user.reload.photo_and_video.video.should eq(video_url)
59
+ response.should render_template('_loader')
60
+ response.body.should =~ /FfUHkPf9D9k/m
61
+ end
62
+ end
63
+ end
50
64
  end
51
65
  end
52
66
 
@@ -102,8 +116,8 @@ describe MediaMagick::AttachController do
102
116
  end
103
117
  end
104
118
 
105
- describe "recriate versions" do
106
- it "recriate images versions" do
119
+ describe "recreate versions" do
120
+ it "recreate images versions" do
107
121
  album = Album.create
108
122
 
109
123
  request.env["HTTP_REFERER"] = "/"
@@ -112,7 +126,7 @@ describe MediaMagick::AttachController do
112
126
  response.status.should be(302)
113
127
  end
114
128
 
115
- it "recriate images versions for embedded models" do
129
+ it "recreate images versions for embedded models" do
116
130
  album = Album.create
117
131
  track = album.tracks.create
118
132
 
@@ -0,0 +1,2 @@
1
+ // Place all the behaviors and hooks related to the matching controller here.
2
+ // All this logic will automatically be available in application.js.
@@ -0,0 +1,4 @@
1
+ /*
2
+ Place all the styles related to the matching controller here.
3
+ They will automatically be included in application.css.
4
+ */
@@ -0,0 +1,83 @@
1
+ class Store::ProductsController < ApplicationController
2
+ # GET /store/products
3
+ # GET /store/products.json
4
+ def index
5
+ @store_products = Store::Product.all
6
+
7
+ respond_to do |format|
8
+ format.html # index.html.erb
9
+ format.json { render json: @store_products }
10
+ end
11
+ end
12
+
13
+ # GET /store/products/1
14
+ # GET /store/products/1.json
15
+ def show
16
+ @store_product = Store::Product.find(params[:id])
17
+
18
+ respond_to do |format|
19
+ format.html # show.html.erb
20
+ format.json { render json: @store_product }
21
+ end
22
+ end
23
+
24
+ # GET /store/products/new
25
+ # GET /store/products/new.json
26
+ def new
27
+ @store_product = Store::Product.new
28
+
29
+ respond_to do |format|
30
+ format.html # new.html.erb
31
+ format.json { render json: @store_product }
32
+ end
33
+ end
34
+
35
+ # GET /store/products/1/edit
36
+ def edit
37
+ @store_product = Store::Product.find(params[:id])
38
+ end
39
+
40
+ # POST /store/products
41
+ # POST /store/products.json
42
+ def create
43
+ @store_product = Store::Product.new(params[:store_product])
44
+
45
+ respond_to do |format|
46
+ if @store_product.save
47
+ format.html { redirect_to @store_product, notice: 'Product was successfully created.' }
48
+ format.json { render json: @store_product, status: :created, location: @store_product }
49
+ else
50
+ format.html { render action: "new" }
51
+ format.json { render json: @store_product.errors, status: :unprocessable_entity }
52
+ end
53
+ end
54
+ end
55
+
56
+ # PUT /store/products/1
57
+ # PUT /store/products/1.json
58
+ def update
59
+ @store_product = Store::Product.find(params[:id])
60
+
61
+ respond_to do |format|
62
+ if @store_product.update_attributes(params[:store_product])
63
+ format.html { redirect_to @store_product, notice: 'Product was successfully updated.' }
64
+ format.json { head :no_content }
65
+ else
66
+ format.html { render action: "edit" }
67
+ format.json { render json: @store_product.errors, status: :unprocessable_entity }
68
+ end
69
+ end
70
+ end
71
+
72
+ # DELETE /store/products/1
73
+ # DELETE /store/products/1.json
74
+ def destroy
75
+ @store_product = Store::Product.find(params[:id])
76
+ @store_product.destroy
77
+
78
+ respond_to do |format|
79
+ format.html { redirect_to store_products_url }
80
+ format.json { head :no_content }
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,2 @@
1
+ module Store::ProductsHelper
2
+ end
@@ -7,4 +7,5 @@ class Post
7
7
 
8
8
  attaches_many :files, :type => 'file'
9
9
  attaches_many :images, :type => 'image', :uploader => PostUploader, :allow_videos => true
10
+ attaches_many :just_videos, :type => 'image', :uploader => PostUploader, :allow_videos => true
10
11
  end