attachable 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,4 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in attachable.gemspec
4
+ gemspec
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 [name of plugin creator]
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,39 @@
1
+ Attachable
2
+ ==========
3
+
4
+ A really simple file attachment plugin for Rails 3. Designed to work with any generic files, i.e not specific to images.
5
+ It will not do any magical processing for you, it just stores the data in the database. Write your own magic.
6
+
7
+
8
+ Usage
9
+ =======
10
+
11
+ Add the following fields to your table:
12
+
13
+ add_column :medias, :file_name, :string
14
+ add_column :medias, :file_type, :string
15
+ add_column :medias, :file_size, :integer
16
+ add_column :medias, :file_data, :binary, :limit => 2.megabytes
17
+
18
+ In your model:
19
+ class Media < ActiveRecord::Base
20
+ attachable
21
+ end
22
+
23
+ In a controller that should be able to view the result:
24
+ class MediasController < ApplicationController
25
+ def show
26
+ @media = Media(params[:id])
27
+ send_data @media.file_contents, :filename => @media.file_name, :type => @media.file_type, :disposition => 'inline'
28
+ end
29
+ end
30
+
31
+ To upload, just set the form field to :file AND remember the multipart => true bit like shown:
32
+ <% form_for(@media, :html => { :multipart => true }) do |f| %>
33
+ <%= f.file_field :file %>
34
+ ....more stuff....
35
+
36
+
37
+ In theory you can change the prefix from "file" to anything by setting the file_prefix option in the model, but I haven't tested it.
38
+
39
+ Copyright (c) 2010 Brian Michalski, released under the MIT license
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,20 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "attachable/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "attachable"
7
+ s.version = Attachable::VERSION
8
+ s.authors = ["Brian Michalski"]
9
+ s.email = ["bmichalski@gmail.com"]
10
+ s.homepage = "https://github.com/bamnet/attachable"
11
+ s.summary = %q{A simple set of extensions to support files in a model}
12
+ s.description = %q{Add methods to automatically manage a file as part of a Rails model}
13
+
14
+ s.rubyforge_project = "attachable"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+ end
data/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ ActiveRecord::Base.class_eval do
2
+ extend Attachable::ClassMethods
3
+ end
@@ -0,0 +1 @@
1
+ # Install hook code here
@@ -0,0 +1,65 @@
1
+ # Attachable
2
+ require 'attachable/version'
3
+ require 'active_record'
4
+ require 'action_controller'
5
+
6
+ # Attachable provides simple file upload handlers for Rails 3 models.
7
+ module Attachable
8
+
9
+ module ClassMethods
10
+
11
+ # Loads the attachable methods, scope, and config into the model.
12
+ def attachable(options = {})
13
+ # Store the default prefix for file data
14
+ # Defaults to "file"
15
+ cattr_accessor :attachment_file_prefix
16
+ self.attachment_file_prefix = (options[:file_prefix] || :file).to_s
17
+
18
+ # Setup the default scope so the file data isn't included by default
19
+ send :default_scope, attachable_scope
20
+
21
+ # Include all the important stuff
22
+ send :include, InstanceMethods
23
+ end
24
+
25
+ # Generate the default scope, which includes every column except for the data column.
26
+ # We use this so queries, by default, don't include the file data which could be quite large.
27
+ def attachable_scope
28
+ select(column_names.reject { |n| n == "#{attachment_file_prefix}_data" }.collect {|n| "#{table_name}.#{n}" }.join(','))
29
+ end
30
+ end
31
+
32
+ module InstanceMethods
33
+
34
+ # Read the file data back from where ever it is stored. Uses a cached copy if it exists.
35
+ # If the cached attribute doens't exist, it reloads the model to get it
36
+ def file_contents
37
+ #Try to hit the cache, reload if it fails
38
+ if !attribute_present?("#{attachment_file_prefix}_data")
39
+ reload :select => "#{attachment_file_prefix}_data"
40
+ end
41
+ send("#{attachment_file_prefix}_data")
42
+ end
43
+
44
+ # Take in a UploadFile and copy it's properties to four fields,
45
+ # data, size, original filename. An UploadFile wraps a Tempfile,
46
+ # but includes some extra data (like the original filename and
47
+ # content type). We can't garuntee this file to be at the beginning,
48
+ # so we always rewind it.
49
+ def file=(tempfile)
50
+ tempfile.rewind #This may not be super efficient, but it's the necessary fix for Rails 3.1
51
+ self["#{attachment_file_prefix}_data"] = tempfile.read
52
+ self["#{attachment_file_prefix}_size"] = tempfile.size
53
+ self["#{attachment_file_prefix}_name"] = tempfile.original_filename
54
+ self["#{attachment_file_prefix}_type"] = tempfile.content_type
55
+ end
56
+ end
57
+
58
+ end
59
+
60
+ # Rails 3 compatability?
61
+ if defined? Rails
62
+ ActiveRecord::Base.class_eval do
63
+ extend Attachable::ClassMethods
64
+ end
65
+ end
@@ -0,0 +1,3 @@
1
+ module Attachable
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,8 @@
1
+ require 'test_helper'
2
+
3
+ class AttachableTest < ActiveSupport::TestCase
4
+ # Replace this with your real tests.
5
+ test "the truth" do
6
+ assert true
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'active_support'
@@ -0,0 +1 @@
1
+ # Uninstall hook code here
metadata ADDED
@@ -0,0 +1,62 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: attachable
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Brian Michalski
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2012-01-22 00:00:00.000000000 -08:00
13
+ default_executable:
14
+ dependencies: []
15
+ description: Add methods to automatically manage a file as part of a Rails model
16
+ email:
17
+ - bmichalski@gmail.com
18
+ executables: []
19
+ extensions: []
20
+ extra_rdoc_files: []
21
+ files:
22
+ - .gitignore
23
+ - Gemfile
24
+ - MIT-LICENSE
25
+ - README
26
+ - Rakefile
27
+ - attachable.gemspec
28
+ - init.rb
29
+ - install.rb
30
+ - lib/attachable.rb
31
+ - lib/attachable/version.rb
32
+ - test/attachable_test.rb
33
+ - test/test_helper.rb
34
+ - uninstall.rb
35
+ has_rdoc: true
36
+ homepage: https://github.com/bamnet/attachable
37
+ licenses: []
38
+ post_install_message:
39
+ rdoc_options: []
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ! '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ required_rubygems_version: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ requirements: []
55
+ rubyforge_project: attachable
56
+ rubygems_version: 1.6.2
57
+ signing_key:
58
+ specification_version: 3
59
+ summary: A simple set of extensions to support files in a model
60
+ test_files:
61
+ - test/attachable_test.rb
62
+ - test/test_helper.rb