paperclipdb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [Martin Caruso]
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README ADDED
@@ -0,0 +1,23 @@
1
+ Paperclipdb
2
+ ===========
3
+
4
+ Database storage support for paperclip file attachment plugin.
5
+
6
+ Install
7
+ =======
8
+
9
+ gem install paperclipdb
10
+ rake paperclipdb:setup
11
+ rake db:migrate
12
+
13
+ Usage
14
+ =======
15
+
16
+ class ModelWithAttach < ActiveRecord::Base
17
+ has_attached_file :attach,
18
+ :storage => "database" ,
19
+ :styles => { :medium => "300x300>", :thumb => "100x100>" },
20
+ :url => "/:class/:attachment/:id/:style/:basename.:extension"
21
+ end
22
+
23
+ Copyright (c) 2010 [Martin Caruso], released under the MIT license
@@ -0,0 +1,37 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the paperclipdb plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the paperclipdb plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'Paperclipdb'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
24
+
25
+ begin
26
+ require 'jeweler'
27
+ Jeweler::Tasks.new do |gemspec|
28
+ gemspec.name = "paperclipdb"
29
+ gemspec.summary = "Database storage support for paperclip file attachment plugin."
30
+ gemspec.description = "Database storage support for paperclip file attachment plugin"
31
+ gemspec.email = "caruso.martin@gmail.com"
32
+ gemspec.homepage = "http://github.com/mcaruso85/paperclipdb"
33
+ gemspec.authors = ["Martin Caruso"]
34
+ end
35
+ rescue LoadError
36
+ puts "Jeweler not available. Install it with: gem install jeweler"
37
+ end
@@ -0,0 +1,13 @@
1
+ class Paperclipdb::AttachmentsController < ApplicationController
2
+
3
+ def get_attachment
4
+ dir_name = '/' + params[:dir_name].join('/')
5
+ base_name = params[:file_name] + '.' + params[:format]
6
+ attachment = Paperclipdb::Attachment.find(:first, :conditions => [ "base_name = ? AND dir_name = ?", base_name, dir_name ])
7
+ render :text => proc { |response, output|
8
+ response.headers["Content-Type"] = attachment.content_type
9
+ output.write(attachment.file_data)
10
+ }
11
+ end
12
+
13
+ end
@@ -0,0 +1,4 @@
1
+ class Paperclipdb::Attachment < ActiveRecord::Base
2
+ attr_accessible :base_name, :dir_name, :file_data, :content_type, :file_size
3
+ end
4
+
@@ -0,0 +1,3 @@
1
+ ActionController::Routing::Routes.draw do |map|
2
+ map.connect 'paperclipdb/*dir_name/:file_name.:format', :controller => 'paperclipdb/attachments', :action => 'get_attachment'
3
+ end
@@ -0,0 +1,17 @@
1
+ class Attachments < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :attachments do |t|
4
+ t.string :dir_name, :null => false
5
+ t.string :base_name, :null => false
6
+ t.binary :file_data, :null => false
7
+ t.string :content_type
8
+ t.integer :file_size
9
+ t.timestamps
10
+ end
11
+ add_index :attachments, [:base_name, :dir_name], :unique => true
12
+ end
13
+
14
+ def self.down
15
+ drop_table :attachments
16
+ end
17
+ end
@@ -0,0 +1,8 @@
1
+ Description:
2
+ Explain the generator
3
+
4
+ Example:
5
+ ./script/generate paperclipdb Thing
6
+
7
+ This will create:
8
+ what/will/it/create
@@ -0,0 +1,8 @@
1
+ class PaperclipdbGenerator < Rails::Generator::NamedBase
2
+ def manifest
3
+ record do |m|
4
+ # m.directory "lib"
5
+ # m.template 'README', "README"
6
+ end
7
+ end
8
+ end
data/init.rb ADDED
@@ -0,0 +1 @@
1
+ require 'paperclipdb'
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,9 @@
1
+ require 'paperclipdb/storage.rb'
2
+
3
+ module Paperclipdb
4
+ class << self
5
+ def self.version
6
+ "0.1"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,63 @@
1
+ module Paperclip
2
+ module Storage
3
+ module Database
4
+
5
+ def self.extended(base)
6
+ base.instance_eval do
7
+ override_default_options base
8
+ end
9
+ end
10
+
11
+ def override_default_options(base)
12
+ @path = @url
13
+ @url = "/paperclipdb" + @url
14
+ end
15
+ private :override_default_options
16
+
17
+ def exists?(style = default_style)
18
+ return getAttachment(path(style)).nil?
19
+ end
20
+
21
+ def getAttachment(file_path)
22
+ return Paperclipdb::Attachment.find(:first, :conditions => [ "base_name = ? AND dir_name = ?", File.basename(file_path), File.dirname(file_path) ])
23
+ end
24
+
25
+ def to_file style = default_style
26
+ if @queued_for_write[style]
27
+ @queued_for_write[style]
28
+ elsif exists?(style)
29
+ attachment = getAttachment(path(style))
30
+ tempfile = Tempfile.new attachment.base_name
31
+ tempfile.write attachment.file_data
32
+ tempfile
33
+ else
34
+ nil
35
+ end
36
+ end
37
+
38
+ def flush_writes
39
+ @queued_for_write.each do |style, file|
40
+ attachment = Paperclipdb::Attachment.new
41
+ attachment.base_name = File.basename(path(style))
42
+ attachment.dir_name = File.dirname(path(style))
43
+ attachment.content_type = self.instance_variable_get("@_#{self.name.to_s}_content_type")
44
+ attachment.file_size = file.size
45
+ attachment.file_data = file.read
46
+ attachment.save
47
+ end
48
+ @queued_for_write = {}
49
+ end
50
+
51
+ def flush_deletes
52
+ @queued_for_delete.each do |path|
53
+ attachment = getAttachment(path)
54
+ if (!attachment.nil?)
55
+ attachment.destroy
56
+ end
57
+ end
58
+ @queued_for_delete = []
59
+ end
60
+
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,18 @@
1
+ # desc "Explaining what the task does"
2
+ # task :paperclipdb do
3
+ # # Task goes here
4
+ # end
5
+ namespace :paperclipdb do
6
+ desc "Copies migration files to db/migrate directory"
7
+ task :setup => :environment do
8
+ is_git_or_dir = proc {|path| path =~ /\.git/ || File.directory?(path) }
9
+ plugin_root = File.dirname(File.dirname(File.dirname(__FILE__)))
10
+ Dir.glob([File.join(plugin_root, "db", "migrate","*")]).reject(&is_git_or_dir).each do |file|
11
+ path = file.sub(plugin_root, '')
12
+ directory = File.dirname(path)
13
+ puts "Copying #{path}..."
14
+ FileUtils.mkdir_p(File.join(RAILS_ROOT, directory))
15
+ FileUtils.cp(file, File.join(RAILS_ROOT, path))
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ require 'test_helper'
2
+
3
+ class PaperclipdbTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+
9
+ end
@@ -0,0 +1,3 @@
1
+ require 'test/unit'
2
+ require 'active_support'
3
+ require 'active_support/test_case'
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,79 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: paperclipdb
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 1
8
+ - 0
9
+ version: 0.1.0
10
+ platform: ruby
11
+ authors:
12
+ - Martin Caruso
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-09-07 00:00:00 -03:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description: Database storage support for paperclip file attachment plugin
22
+ email: caruso.martin@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files:
28
+ - README
29
+ files:
30
+ - MIT-LICENSE
31
+ - README
32
+ - Rakefile
33
+ - app/controllers/paperclipdb/attachments_controller.rb
34
+ - app/models/paperclipdb/attachment.rb
35
+ - config/routes.rb
36
+ - db/migrate/20100906021538_attachments.rb
37
+ - generators/paperclipdb/USAGE
38
+ - generators/paperclipdb/paperclipdb_generator.rb
39
+ - init.rb
40
+ - install.rb
41
+ - lib/paperclipdb.rb
42
+ - lib/paperclipdb/storage.rb
43
+ - lib/tasks/paperclipdb.rake
44
+ - test/paperclipdb_test.rb
45
+ - test/test_helper.rb
46
+ - uninstall.rb
47
+ has_rdoc: true
48
+ homepage: http://github.com/mcaruso85/paperclipdb
49
+ licenses: []
50
+
51
+ post_install_message:
52
+ rdoc_options:
53
+ - --charset=UTF-8
54
+ require_paths:
55
+ - lib
56
+ required_ruby_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ requirements: []
71
+
72
+ rubyforge_project:
73
+ rubygems_version: 1.3.6
74
+ signing_key:
75
+ specification_version: 3
76
+ summary: Database storage support for paperclip file attachment plugin.
77
+ test_files:
78
+ - test/paperclipdb_test.rb
79
+ - test/test_helper.rb