has_media 0.2.4 → 0.2.5

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -90,6 +90,27 @@ Configuration take place in config/initializers/has_media.rb
90
90
  require uploader
91
91
  end
92
92
 
93
+ == Routes
94
+
95
+ Add routes for media in your config/routes.rb
96
+
97
+ resources :media, :only => [:destroy]
98
+
99
+ == Helpers
100
+
101
+ Now you can use the has_media_field helper.
102
+
103
+ <%= form_for(@model, :html => {:multipart => true}) do |f| %>
104
+ <%= has_media_field @model, :context %>
105
+ <%= f.submit %>
106
+ <%- end %>
107
+
108
+ == Generating views
109
+
110
+ If you need to overload has_media views, just run : rails generate has_media:views
111
+
112
+ This will copy has_media views in your application
113
+
93
114
  == Encode with resque
94
115
 
95
116
  Just add a callback on media class you want to encode via resque.
data/Rakefile CHANGED
@@ -16,7 +16,7 @@ begin
16
16
  gem.homepage = "http://github.com/klacointe/has_media"
17
17
  gem.authors = ["klacointe", "spk"]
18
18
  gem.add_development_dependency "rspec", "~>2.0.0"
19
- gem.add_dependency('carrierwave-rails3', '~>0.4.5')
19
+ gem.add_dependency('carrierwave', '~>0.5')
20
20
  gem.add_dependency('activerecord', '~>3.0.0')
21
21
  gem.add_dependency('activesupport', '~>3.0.0')
22
22
  gem.add_dependency('mime-types', '~>1.16')
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.4
1
+ 0.2.5
@@ -0,0 +1,7 @@
1
+ class MediaController < ApplicationController
2
+ def destroy
3
+ @medium = Medium.find(params[:id])
4
+ @medium.destroy
5
+ render :template => 'has_media/destroy'
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ module HasMediaHelper
2
+
3
+ def generate_uid(*opts)
4
+ opts = opts.first
5
+ object = opts[:object]||nil
6
+ context = opts[:context]||nil
7
+ medium = opts[:medium]||nil
8
+ if medium.nil?
9
+ if object.nil? or object.new_record?
10
+ "#{object.class.to_s.underscore}-#{context.to_s.underscore}"
11
+ else
12
+ "#{object.class.to_s.underscore}-#{context.to_s.underscore}-#{object.id}"
13
+ end
14
+ else
15
+ "#{medium.class.to_s.underscore}-#{medium.id}"
16
+ end
17
+ end
18
+
19
+ def add_medium_link(opts)
20
+ link_to_function opts[:text] do |page|
21
+ page.insert_html :bottom, generate_uid(:object => opts[:object], :context => opts[:context]),
22
+ :partial => 'has_media/medium_field',
23
+ :locals => {
24
+ :object => opts[:object],
25
+ :context => opts[:context]
26
+ }
27
+ end
28
+ end
29
+
30
+ def remove_medium_link(opts)
31
+ link_to opts[:text], medium_url(opts[:medium]), :remote => true, :method => :delete
32
+ end
33
+
34
+ def has_media_field(model, context)
35
+ render :partial => "has_media/media_fields", :locals => {
36
+ :object => model,
37
+ :context => context
38
+ }
39
+ end
40
+
41
+ end
@@ -0,0 +1,18 @@
1
+ <%- media = object.send(context) %>
2
+ <%- media = [media] unless media.is_a?(Array) %>
3
+ <%- if defined?(readonly); readonly = readonly else readonly = true end %>
4
+
5
+ <%- media.each do |medium| %>
6
+ <%- unless medium.nil? %>
7
+ <div class="<%= medium.class.to_s.underscore %>" id="<%= generate_uid(:medium => medium) %>">
8
+ <%= link_to medium.original_file_uri, medium.original_file_uri %>
9
+
10
+ <%- if object.send(context).is_a?(Array) and !readonly %>
11
+ <%= remove_medium_link :text => "Remove",
12
+ :object => object,
13
+ :context => context,
14
+ :medium => medium %>
15
+ <%- end %>
16
+ </div>
17
+ <%- end %>
18
+ <%- end %>
@@ -0,0 +1,22 @@
1
+ <div id="<%= generate_uid(:object => object, :context => context) %>">
2
+ <%= render :partial => 'has_media/media', :locals => {
3
+ :object => object,
4
+ :context => context,
5
+ :readonly => false
6
+ } %>
7
+
8
+ <%= render :partial => 'has_media/medium_field',
9
+ :locals => {
10
+ :object => object,
11
+ :context => context
12
+ } %>
13
+
14
+ </div>
15
+ <%- if object.send(context).is_a?(Array) %>
16
+ <p>
17
+ <%= add_medium_link :text => "Add #{context}",
18
+ :object => object,
19
+ :context => context %>
20
+ </p>
21
+ <%- end %>
22
+
File without changes
@@ -0,0 +1,12 @@
1
+ <%- klass = object.class.to_s.underscore %>
2
+ <%- if object.send(context).is_a?(Array) %>
3
+ <p>
4
+ <%= label_tag "#{klass}[#{context}][]" %>
5
+ <%= file_field_tag "#{klass}[#{context}][]" %>
6
+ </p>
7
+ <%- else %>
8
+ <p>
9
+ <%= label_tag "#{klass}[#{context}]" %>
10
+ <%= file_field_tag "#{klass}[#{context}]" %>
11
+ </p>
12
+ <%- end %>
@@ -0,0 +1 @@
1
+ $("#<%= generate_uid(:medium => @medium ) %>").remove();
@@ -1,7 +1,12 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  class ImageUploader < MediumUploader
4
+ include CarrierWave::RMagick
4
5
 
5
6
  # Choose what kind of storage to use for this uploader
6
7
  storage :file
8
+
9
+ version :thumb do
10
+ process :resize_to_fit => [100, 100]
11
+ end
7
12
  end
data/has_media.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{has_media}
8
- s.version = "0.2.4"
8
+ s.version = "0.2.5"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["klacointe", "spk"]
12
- s.date = %q{2010-11-09}
12
+ s.date = %q{2010-11-23}
13
13
  s.description = %q{Media Managment Library for ActiveRecord and Carrierwave}
14
14
  s.email = %q{kevinlacointe@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -24,6 +24,13 @@ Gem::Specification.new do |s|
24
24
  "README.rdoc",
25
25
  "Rakefile",
26
26
  "VERSION",
27
+ "app/controllers/media_controller.rb",
28
+ "app/helpers/has_media_helper.rb",
29
+ "app/views/has_media/_media.html.erb",
30
+ "app/views/has_media/_media_fields.html.erb",
31
+ "app/views/has_media/_medium.html.erb",
32
+ "app/views/has_media/_medium_field.html.erb",
33
+ "app/views/has_media/destroy.js.erb",
27
34
  "config/locales/en.yml",
28
35
  "examples/models/audio.rb",
29
36
  "examples/models/document.rb",
@@ -36,9 +43,11 @@ Gem::Specification.new do |s|
36
43
  "examples/uploaders/pdf_uploader.rb",
37
44
  "examples/uploaders/video_uploader.rb",
38
45
  "has_media.gemspec",
46
+ "lib/carrierwave/uploader/url.rb",
39
47
  "lib/generators/active_record/has_media_generator.rb",
40
48
  "lib/generators/active_record/templates/migration.rb",
41
49
  "lib/generators/has_media/install_generator.rb",
50
+ "lib/generators/has_media/views_generator.rb",
42
51
  "lib/generators/templates/README",
43
52
  "lib/generators/templates/has_media.rb",
44
53
  "lib/has_media.rb",
@@ -84,20 +93,20 @@ Gem::Specification.new do |s|
84
93
 
85
94
  if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
86
95
  s.add_development_dependency(%q<rspec>, ["~> 2.0.0"])
87
- s.add_runtime_dependency(%q<carrierwave-rails3>, ["~> 0.4.5"])
96
+ s.add_runtime_dependency(%q<carrierwave>, ["~> 0.5"])
88
97
  s.add_runtime_dependency(%q<activerecord>, ["~> 3.0.0"])
89
98
  s.add_runtime_dependency(%q<activesupport>, ["~> 3.0.0"])
90
99
  s.add_runtime_dependency(%q<mime-types>, ["~> 1.16"])
91
100
  else
92
101
  s.add_dependency(%q<rspec>, ["~> 2.0.0"])
93
- s.add_dependency(%q<carrierwave-rails3>, ["~> 0.4.5"])
102
+ s.add_dependency(%q<carrierwave>, ["~> 0.5"])
94
103
  s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
95
104
  s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
96
105
  s.add_dependency(%q<mime-types>, ["~> 1.16"])
97
106
  end
98
107
  else
99
108
  s.add_dependency(%q<rspec>, ["~> 2.0.0"])
100
- s.add_dependency(%q<carrierwave-rails3>, ["~> 0.4.5"])
109
+ s.add_dependency(%q<carrierwave>, ["~> 0.5"])
101
110
  s.add_dependency(%q<activerecord>, ["~> 3.0.0"])
102
111
  s.add_dependency(%q<activesupport>, ["~> 3.0.0"])
103
112
  s.add_dependency(%q<mime-types>, ["~> 1.16"])
@@ -0,0 +1,20 @@
1
+ # encoding: utf-8
2
+
3
+ module CarrierWave
4
+ module Uploader
5
+ module Url
6
+ ##
7
+ # === Returns
8
+ #
9
+ # [String] the location where this file is accessible via a url
10
+ #
11
+ def url
12
+ if file.respond_to?(:url) and not file.url.blank?
13
+ file.url
14
+ elsif current_path
15
+ File.expand_path(current_path).gsub(File.expand_path(root), HasMedia.directory_uri)
16
+ end
17
+ end
18
+ end # Url
19
+ end # Uploader
20
+ end # CarrierWave
@@ -0,0 +1,60 @@
1
+ module HasMedia
2
+ module Generators
3
+ class ViewsGenerator < Rails::Generators::Base
4
+ source_root File.expand_path("../../../../app/views", __FILE__)
5
+ desc "Copies all HasMedia views to your application."
6
+
7
+ class_option :template_engine, :type => :string, :aliases => "-t",
8
+ :desc => "Template engine for the views. Available options are 'erb' and 'haml'."
9
+
10
+ def copy_views
11
+ case options[:template_engine].to_s
12
+ when "haml"
13
+ verify_haml_existence
14
+ verify_haml_version
15
+ create_and_copy_haml_views
16
+ else
17
+ directory "has_media", "app/views/has_media"
18
+ end
19
+ end
20
+
21
+ protected
22
+
23
+ def verify_haml_existence
24
+ begin
25
+ require 'haml'
26
+ rescue LoadError
27
+ say "HAML is not installed, or it is not specified in your Gemfile."
28
+ exit
29
+ end
30
+ end
31
+
32
+ def verify_haml_version
33
+ unless Haml.version[:major] == 2 and Haml.version[:minor] >= 3 or Haml.version[:major] >= 3
34
+ say "To generate HAML templates, you need to install HAML 2.3 or above."
35
+ exit
36
+ end
37
+ end
38
+
39
+ def create_and_copy_haml_views
40
+ require 'tmpdir'
41
+ html_root = "#{self.class.source_root}/has_media"
42
+
43
+ Dir.mktmpdir("has_media-haml.") do |haml_root|
44
+ Dir["#{html_root}/**/*"].each do |path|
45
+ relative_path = path.sub(html_root, "")
46
+ source_path = (haml_root + relative_path).sub(/erb$/, "haml")
47
+
48
+ if File.directory?(path)
49
+ FileUtils.mkdir_p(source_path)
50
+ else
51
+ `html2haml -r #{path} #{source_path}`
52
+ end
53
+ end
54
+
55
+ directory haml_root, "app/views/has_media"
56
+ end
57
+ end
58
+ end
59
+ end
60
+ end
data/lib/has_media.rb CHANGED
@@ -1,11 +1,16 @@
1
1
  require 'rubygems'
2
+ require 'rails'
2
3
  require 'active_record'
3
4
  require 'active_support'
4
5
  require 'carrierwave'
5
6
  require 'mime/types'
7
+ require File.expand_path(File.dirname(__FILE__)) + '/../app/helpers/has_media_helper'
6
8
 
7
9
  module HasMedia
8
10
 
11
+ class Engine < Rails::Engine
12
+ end
13
+
9
14
  VERSION = "0.0.1"
10
15
 
11
16
  @@medium_types = {}
@@ -168,6 +173,9 @@ end
168
173
  class ActiveRecord::Base
169
174
  include HasMedia
170
175
  end
176
+ class ActionController::Base
177
+ helper HasMediaHelper
178
+ end
171
179
 
172
180
  require File.dirname(__FILE__) + '/has_media/uploaders/medium_uploader'
173
181
  Dir.glob(File.dirname(__FILE__) + '/has_media/uploaders/*.rb').each do |uploader|
@@ -168,6 +168,25 @@ describe "HasMedia" do
168
168
  @medium.image.filename)
169
169
  end
170
170
 
171
+ it "should define file_uri for custom versions" do
172
+ @medium.image = @image
173
+ @medium.save!
174
+ @medium.image.file_uri(:thumb).should == "/media/image/#{@medium.image.id}/image_thumb.png"
175
+ @medium.image.file_path(:thumb).should == "/tmp/image/#{@medium.image.id}/image_thumb.png"
176
+ end
177
+
178
+ it "should define file url for carrierwave versions" do
179
+ @medium.image = @image
180
+ @medium.save!
181
+ @medium.image.file.thumb.url.should == "/media/image/#{@medium.image.id}/thumb_image.jpg"
182
+ end
183
+
184
+ it "should define file path for carrierwave versions" do
185
+ @medium.image = @image
186
+ @medium.save!
187
+ @medium.image.file.thumb.path.should == "/tmp/image/#{@medium.image.id}/thumb_image.jpg"
188
+ end
189
+
171
190
  it "pdf should exist" do
172
191
  @medium.pdf = @pdf
173
192
  @medium.save!
data/spec/spec_helper.rb CHANGED
@@ -33,6 +33,7 @@ class TestMigration < ActiveRecord::Migration
33
33
  t.string :filename
34
34
  t.integer :encode_status
35
35
  t.string :type
36
+ t.string :file
36
37
  t.timestamps
37
38
  end
38
39
  create_table :media_links, :force => true do |t|
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: has_media
3
3
  version: !ruby/object:Gem::Version
4
- hash: 31
4
+ hash: 29
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 4
10
- version: 0.2.4
9
+ - 5
10
+ version: 0.2.5
11
11
  platform: ruby
12
12
  authors:
13
13
  - klacointe
@@ -16,7 +16,7 @@ autorequire:
16
16
  bindir: bin
17
17
  cert_chain: []
18
18
 
19
- date: 2010-11-09 00:00:00 +01:00
19
+ date: 2010-11-23 00:00:00 +01:00
20
20
  default_executable:
21
21
  dependencies:
22
22
  - !ruby/object:Gem::Dependency
@@ -36,19 +36,18 @@ dependencies:
36
36
  type: :development
37
37
  version_requirements: *id001
38
38
  - !ruby/object:Gem::Dependency
39
- name: carrierwave-rails3
39
+ name: carrierwave
40
40
  prerelease: false
41
41
  requirement: &id002 !ruby/object:Gem::Requirement
42
42
  none: false
43
43
  requirements:
44
44
  - - ~>
45
45
  - !ruby/object:Gem::Version
46
- hash: 5
46
+ hash: 1
47
47
  segments:
48
48
  - 0
49
- - 4
50
49
  - 5
51
- version: 0.4.5
50
+ version: "0.5"
52
51
  type: :runtime
53
52
  version_requirements: *id002
54
53
  - !ruby/object:Gem::Dependency
@@ -115,6 +114,13 @@ files:
115
114
  - README.rdoc
116
115
  - Rakefile
117
116
  - VERSION
117
+ - app/controllers/media_controller.rb
118
+ - app/helpers/has_media_helper.rb
119
+ - app/views/has_media/_media.html.erb
120
+ - app/views/has_media/_media_fields.html.erb
121
+ - app/views/has_media/_medium.html.erb
122
+ - app/views/has_media/_medium_field.html.erb
123
+ - app/views/has_media/destroy.js.erb
118
124
  - config/locales/en.yml
119
125
  - examples/models/audio.rb
120
126
  - examples/models/document.rb
@@ -127,9 +133,11 @@ files:
127
133
  - examples/uploaders/pdf_uploader.rb
128
134
  - examples/uploaders/video_uploader.rb
129
135
  - has_media.gemspec
136
+ - lib/carrierwave/uploader/url.rb
130
137
  - lib/generators/active_record/has_media_generator.rb
131
138
  - lib/generators/active_record/templates/migration.rb
132
139
  - lib/generators/has_media/install_generator.rb
140
+ - lib/generators/has_media/views_generator.rb
133
141
  - lib/generators/templates/README
134
142
  - lib/generators/templates/has_media.rb
135
143
  - lib/has_media.rb