file_upload_cache 1.0.0
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.
- data/Gemfile +11 -0
- data/Rakefile +21 -0
- data/app/controllers/file_upload_cache/cached_files_controller.rb +21 -0
- data/app/inputs/uploader_input.rb +53 -0
- data/app/models/file_upload_cache/cached_file.rb +29 -0
- data/config/routes.rb +6 -0
- data/lib/file_upload_cache/cached_attributes.rb +43 -0
- data/lib/file_upload_cache/engine.rb +4 -0
- data/lib/file_upload_cache.rb +20 -0
- metadata +75 -0
    
        data/Gemfile
    ADDED
    
    
    
        data/Rakefile
    ADDED
    
    | @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            # encoding: UTF-8
         | 
| 2 | 
            +
            require 'rubygems'
         | 
| 3 | 
            +
            begin
         | 
| 4 | 
            +
              require 'bundler/setup'
         | 
| 5 | 
            +
            rescue LoadError
         | 
| 6 | 
            +
              puts 'You must `gem install bundler` and `bundle install` to run rake tasks'
         | 
| 7 | 
            +
            end
         | 
| 8 | 
            +
             | 
| 9 | 
            +
            require 'rake'
         | 
| 10 | 
            +
             | 
| 11 | 
            +
            require 'rake/testtask'
         | 
| 12 | 
            +
             | 
| 13 | 
            +
            Rake::TestTask.new(:test) do |t|
         | 
| 14 | 
            +
              t.libs << 'lib'
         | 
| 15 | 
            +
              t.libs << 'test'
         | 
| 16 | 
            +
              t.pattern = 'test/**/*_test.rb'
         | 
| 17 | 
            +
              t.verbose = false
         | 
| 18 | 
            +
            end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
            task :default => :test
         | 
| 21 | 
            +
             | 
| @@ -0,0 +1,21 @@ | |
| 1 | 
            +
            module FileUploadCache
         | 
| 2 | 
            +
              class CachedFilesController < ActionController::Base
         | 
| 3 | 
            +
                before_filter :require_current_cached_file, :only => :show
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                def show
         | 
| 6 | 
            +
                  options = {:disposition => 'inline'}
         | 
| 7 | 
            +
                  options.merge!({:type => current_cached_file.content_type}) if current_cached_file.content_type
         | 
| 8 | 
            +
                  send_data current_cached_file.read, options
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                private
         | 
| 12 | 
            +
             | 
| 13 | 
            +
                def current_cached_file
         | 
| 14 | 
            +
                  @current_cached_file ||= FileUploadCache::CachedFile.find(params[:id])
         | 
| 15 | 
            +
                end
         | 
| 16 | 
            +
             | 
| 17 | 
            +
                def require_current_cached_file
         | 
| 18 | 
            +
                  render :text => 'not found', :status => :not_found unless current_cached_file
         | 
| 19 | 
            +
                end
         | 
| 20 | 
            +
              end
         | 
| 21 | 
            +
            end
         | 
| @@ -0,0 +1,53 @@ | |
| 1 | 
            +
            # A formtastic input which incorporates a cache_id that can be fetched from server
         | 
| 2 | 
            +
            #
         | 
| 3 | 
            +
            # Example: `form.input :file, as: "uploader"`
         | 
| 4 | 
            +
            if defined?(Formtastic)
         | 
| 5 | 
            +
              class UploaderInput < Formtastic::Inputs::FileInput
         | 
| 6 | 
            +
                def method_present?
         | 
| 7 | 
            +
                  object.send(method).present?
         | 
| 8 | 
            +
                end
         | 
| 9 | 
            +
             | 
| 10 | 
            +
                def wrapper_html_options
         | 
| 11 | 
            +
                  super.tap do |options|
         | 
| 12 | 
            +
                    options[:class] << " present" if method_present?
         | 
| 13 | 
            +
                  end
         | 
| 14 | 
            +
                end
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                def cache_html
         | 
| 17 | 
            +
                  builder.hidden_field("#{method}_cache_id", :value => object.send("cached_#{method}").try(:id), :id => "#{base_id}_cache")
         | 
| 18 | 
            +
                end
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                def file_html
         | 
| 21 | 
            +
                  builder.file_field(method, input_html_options.merge(:class => 'cached_file', :id => base_id))
         | 
| 22 | 
            +
                end
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                def existing_html
         | 
| 25 | 
            +
                  if method_present?
         | 
| 26 | 
            +
                    existing = template.content_tag(:span, object.send("cached_#{method}").try(:original_filename))
         | 
| 27 | 
            +
                    template.content_tag(:div, :id => "#{base_id}_existing") do
         | 
| 28 | 
            +
                      template.link_to(existing, Rails.application.routes.url_helpers.file_upload_cache_cached_file_path(object.send("cached_#{method}").try(:id))) <<
         | 
| 29 | 
            +
                      template.content_tag(:span, " replace", :id => "#{base_id}_replace")
         | 
| 30 | 
            +
                    end
         | 
| 31 | 
            +
                  end or "".html_safe
         | 
| 32 | 
            +
                end
         | 
| 33 | 
            +
             | 
| 34 | 
            +
                def replace_html
         | 
| 35 | 
            +
                  file_html
         | 
| 36 | 
            +
                end
         | 
| 37 | 
            +
             | 
| 38 | 
            +
                def to_html
         | 
| 39 | 
            +
                  input_wrapping do
         | 
| 40 | 
            +
                    label_html <<
         | 
| 41 | 
            +
                    cache_html <<
         | 
| 42 | 
            +
                    existing_html <<
         | 
| 43 | 
            +
                    file_html
         | 
| 44 | 
            +
                  end
         | 
| 45 | 
            +
                end
         | 
| 46 | 
            +
             | 
| 47 | 
            +
                def base_id
         | 
| 48 | 
            +
                  input_html_options[:id] || method
         | 
| 49 | 
            +
                end
         | 
| 50 | 
            +
             | 
| 51 | 
            +
              end
         | 
| 52 | 
            +
             | 
| 53 | 
            +
            end
         | 
| @@ -0,0 +1,29 @@ | |
| 1 | 
            +
            module FileUploadCache
         | 
| 2 | 
            +
              class CachedFile
         | 
| 3 | 
            +
                attr_accessor :read, :id, :original_filename, :content_type
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                def initialize(attributes)
         | 
| 6 | 
            +
                  attributes.each do |k, v|
         | 
| 7 | 
            +
                    self.send("#{k}=", v)
         | 
| 8 | 
            +
                  end
         | 
| 9 | 
            +
                end
         | 
| 10 | 
            +
             | 
| 11 | 
            +
                def self.store(image)
         | 
| 12 | 
            +
                  id = UUID.generate(:compact)
         | 
| 13 | 
            +
                  cached_file = self.new(:read              => image.read, 
         | 
| 14 | 
            +
                                         :original_filename => image.respond_to?(:original_filename) ? image.original_filename : nil, 
         | 
| 15 | 
            +
                                         :id                => id, 
         | 
| 16 | 
            +
                                         :content_type      => image.respond_to?(:content_type) ? image.content_type : nil)
         | 
| 17 | 
            +
             | 
| 18 | 
            +
                  FileUploadCache.file_cache.write("FileUploadCache::#{id}", cached_file) 
         | 
| 19 | 
            +
             | 
| 20 | 
            +
                  cached_file
         | 
| 21 | 
            +
                end
         | 
| 22 | 
            +
             | 
| 23 | 
            +
                def self.find(id)
         | 
| 24 | 
            +
                  FileUploadCache.file_cache.read("FileUploadCache::#{id}")
         | 
| 25 | 
            +
                end
         | 
| 26 | 
            +
             | 
| 27 | 
            +
              end
         | 
| 28 | 
            +
             | 
| 29 | 
            +
            end
         | 
    
        data/config/routes.rb
    ADDED
    
    
| @@ -0,0 +1,43 @@ | |
| 1 | 
            +
            module FileUploadCache
         | 
| 2 | 
            +
              module CachedAttributes
         | 
| 3 | 
            +
                extend ActiveSupport::Concern
         | 
| 4 | 
            +
             | 
| 5 | 
            +
                module ClassMethods
         | 
| 6 | 
            +
             | 
| 7 | 
            +
                  def cached_file_for(field)
         | 
| 8 | 
            +
                    attr_accessor :"#{field}_cache_id", :"cached_#{field}"
         | 
| 9 | 
            +
                    define_method "#{field}_with_cache=" do |value|
         | 
| 10 | 
            +
                      instance_variable_set("@#{field}_original", value)
         | 
| 11 | 
            +
                      self.send("#{field}_without_cache=", value)
         | 
| 12 | 
            +
                    end
         | 
| 13 | 
            +
             | 
| 14 | 
            +
                    alias_method_chain :"#{field}=", :cache
         | 
| 15 | 
            +
             | 
| 16 | 
            +
                    before_validation lambda { 
         | 
| 17 | 
            +
                      original = self.instance_variable_get("@#{field}_original")
         | 
| 18 | 
            +
                      self.send("cached_#{field}=", CachedFile.store(original)) unless original.blank?
         | 
| 19 | 
            +
                      if( ! self.send("#{field}_cache_id").blank? && original.blank? )
         | 
| 20 | 
            +
                        cached_file = CachedFile.find(self.send("#{field}_cache_id"))
         | 
| 21 | 
            +
             | 
| 22 | 
            +
                        tf = Tempfile.new("temp_file")
         | 
| 23 | 
            +
             | 
| 24 | 
            +
                        begin
         | 
| 25 | 
            +
                          tf.binmode
         | 
| 26 | 
            +
                          tf.write(cached_file.read)
         | 
| 27 | 
            +
             | 
| 28 | 
            +
                          tf.rewind
         | 
| 29 | 
            +
                          self.send("#{field}=", tf)
         | 
| 30 | 
            +
                          self.send("cached_#{field}=", cached_file)
         | 
| 31 | 
            +
                        ensure
         | 
| 32 | 
            +
                          tf.close
         | 
| 33 | 
            +
                        end
         | 
| 34 | 
            +
             | 
| 35 | 
            +
                      end
         | 
| 36 | 
            +
                    }
         | 
| 37 | 
            +
                  end
         | 
| 38 | 
            +
                end
         | 
| 39 | 
            +
              end
         | 
| 40 | 
            +
             | 
| 41 | 
            +
            end
         | 
| 42 | 
            +
             | 
| 43 | 
            +
            ActiveRecord::Base.send(:include, FileUploadCache::CachedAttributes)
         | 
| @@ -0,0 +1,20 @@ | |
| 1 | 
            +
            require 'file_upload_cache/engine.rb'
         | 
| 2 | 
            +
            require 'active_support/core_ext/module/attribute_accessors.rb'
         | 
| 3 | 
            +
            require 'file_upload_cache/cached_attributes.rb'
         | 
| 4 | 
            +
            require 'uuid'
         | 
| 5 | 
            +
             | 
| 6 | 
            +
            module FileUploadCache
         | 
| 7 | 
            +
              mattr_accessor :cache
         | 
| 8 | 
            +
             | 
| 9 | 
            +
              def self.file_cache
         | 
| 10 | 
            +
                if cache
         | 
| 11 | 
            +
                  cache
         | 
| 12 | 
            +
                elsif defined?(Rails)
         | 
| 13 | 
            +
                  Rails.cache
         | 
| 14 | 
            +
                else
         | 
| 15 | 
            +
                  raise "Unspecified Cache Store for File Upload Cache"
         | 
| 16 | 
            +
                end
         | 
| 17 | 
            +
              end
         | 
| 18 | 
            +
             | 
| 19 | 
            +
            end
         | 
| 20 | 
            +
             | 
    
        metadata
    ADDED
    
    | @@ -0,0 +1,75 @@ | |
| 1 | 
            +
            --- !ruby/object:Gem::Specification
         | 
| 2 | 
            +
            name: file_upload_cache
         | 
| 3 | 
            +
            version: !ruby/object:Gem::Version
         | 
| 4 | 
            +
              version: 1.0.0
         | 
| 5 | 
            +
              prerelease: 
         | 
| 6 | 
            +
            platform: ruby
         | 
| 7 | 
            +
            authors:
         | 
| 8 | 
            +
            - Ken Mazaika
         | 
| 9 | 
            +
            autorequire: 
         | 
| 10 | 
            +
            bindir: bin
         | 
| 11 | 
            +
            cert_chain: []
         | 
| 12 | 
            +
            date: 2012-04-10 00:00:00.000000000 Z
         | 
| 13 | 
            +
            dependencies:
         | 
| 14 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 15 | 
            +
              name: uuid
         | 
| 16 | 
            +
              requirement: &70190206724540 !ruby/object:Gem::Requirement
         | 
| 17 | 
            +
                none: false
         | 
| 18 | 
            +
                requirements:
         | 
| 19 | 
            +
                - - ! '>='
         | 
| 20 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 21 | 
            +
                    version: '0'
         | 
| 22 | 
            +
              type: :runtime
         | 
| 23 | 
            +
              prerelease: false
         | 
| 24 | 
            +
              version_requirements: *70190206724540
         | 
| 25 | 
            +
            - !ruby/object:Gem::Dependency
         | 
| 26 | 
            +
              name: rails
         | 
| 27 | 
            +
              requirement: &70190206724100 !ruby/object:Gem::Requirement
         | 
| 28 | 
            +
                none: false
         | 
| 29 | 
            +
                requirements:
         | 
| 30 | 
            +
                - - ! '>='
         | 
| 31 | 
            +
                  - !ruby/object:Gem::Version
         | 
| 32 | 
            +
                    version: '0'
         | 
| 33 | 
            +
              type: :runtime
         | 
| 34 | 
            +
              prerelease: false
         | 
| 35 | 
            +
              version_requirements: *70190206724100
         | 
| 36 | 
            +
            description: Insert FileUploadCache description.
         | 
| 37 | 
            +
            email: 
         | 
| 38 | 
            +
            executables: []
         | 
| 39 | 
            +
            extensions: []
         | 
| 40 | 
            +
            extra_rdoc_files: []
         | 
| 41 | 
            +
            files:
         | 
| 42 | 
            +
            - app/controllers/file_upload_cache/cached_files_controller.rb
         | 
| 43 | 
            +
            - app/inputs/uploader_input.rb
         | 
| 44 | 
            +
            - app/models/file_upload_cache/cached_file.rb
         | 
| 45 | 
            +
            - lib/file_upload_cache/cached_attributes.rb
         | 
| 46 | 
            +
            - lib/file_upload_cache/engine.rb
         | 
| 47 | 
            +
            - lib/file_upload_cache.rb
         | 
| 48 | 
            +
            - config/routes.rb
         | 
| 49 | 
            +
            - Rakefile
         | 
| 50 | 
            +
            - Gemfile
         | 
| 51 | 
            +
            homepage: 
         | 
| 52 | 
            +
            licenses: []
         | 
| 53 | 
            +
            post_install_message: 
         | 
| 54 | 
            +
            rdoc_options: []
         | 
| 55 | 
            +
            require_paths:
         | 
| 56 | 
            +
            - lib
         | 
| 57 | 
            +
            required_ruby_version: !ruby/object:Gem::Requirement
         | 
| 58 | 
            +
              none: false
         | 
| 59 | 
            +
              requirements:
         | 
| 60 | 
            +
              - - ! '>='
         | 
| 61 | 
            +
                - !ruby/object:Gem::Version
         | 
| 62 | 
            +
                  version: '0'
         | 
| 63 | 
            +
            required_rubygems_version: !ruby/object:Gem::Requirement
         | 
| 64 | 
            +
              none: false
         | 
| 65 | 
            +
              requirements:
         | 
| 66 | 
            +
              - - ! '>='
         | 
| 67 | 
            +
                - !ruby/object:Gem::Version
         | 
| 68 | 
            +
                  version: '0'
         | 
| 69 | 
            +
            requirements: []
         | 
| 70 | 
            +
            rubyforge_project: 
         | 
| 71 | 
            +
            rubygems_version: 1.8.10
         | 
| 72 | 
            +
            signing_key: 
         | 
| 73 | 
            +
            specification_version: 3
         | 
| 74 | 
            +
            summary: Insert FileUploadCache summary.
         | 
| 75 | 
            +
            test_files: []
         |