Sha256: 5084267f5f01308aa0164a031d9f3a2c34dc2981bf03e4d8b96d4833d1ec1f15

Contents?: true

Size: 1.42 KB

Versions: 16

Compression:

Stored size: 1.42 KB

Contents

module Bhf
  module ActiveRecord
    module Upload
      extend ActiveSupport::Concern

      included do
        before_save :bhf_upload
        cattr_accessor :bhf_upload_settings
      end

      module ClassMethods
        def setup_upload(settings)
          self.bhf_upload_settings = settings.each_with_object([]) do |s, obj|
            obj << {:path => '', :name => :file}.merge(s)
          end
        end
      end

      def bhf_upload
        self.class.bhf_upload_settings.each do |settings|
          name_was = send("#{settings[:name]}_was")
          param_name = read_attribute(settings[:name])
          file_string = if param_name && param_name[:delete].to_i != 0
            # File.delete(settings[:path] + name_was.to_s) if File.exist?(settings[:path] + name_was.to_s)
            nil
          else
            file = param_name && param_name[:file]
            if file.is_a? ActionDispatch::Http::UploadedFile
              # File.delete(settings[:path] + name_was.to_s) if File.exist?(settings[:path] + name_was.to_s)

              filename = Time.now.to_i.to_s+'_'+file.original_filename.downcase.sub(/[^\w\.\-]/,'_')
              path = File.join(settings[:path], filename)
              File.open(path, 'w') { |f| f.write(file.read) }
              filename
            else
              name_was
            end
          end
          write_attribute settings[:name], file_string
        end
      end

    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
bhf-0.3.10 lib/bhf/active_record/upload.rb
bhf-0.3.9 lib/bhf/active_record/upload.rb
bhf-0.3.8 lib/bhf/active_record/upload.rb
bhf-0.3.7 lib/bhf/active_record/upload.rb
bhf-0.3.6.4 lib/bhf/active_record/upload.rb
bhf-0.3.6.3 lib/bhf/active_record/upload.rb
bhf-0.3.6.2 lib/bhf/active_record/upload.rb
bhf-0.3.6.1 lib/bhf/active_record/upload.rb
bhf-0.3.5 lib/bhf/active_record/upload.rb
bhf-0.3.4 lib/bhf/active_record/upload.rb
bhf-0.3.3 lib/bhf/active_record/upload.rb
bhf-0.3.2 lib/bhf/active_record/upload.rb
bhf-0.3.1 lib/bhf/active_record/upload.rb
bhf-0.3.0 lib/bhf/active_record/upload.rb
bhf-0.2.9 lib/bhf/active_record/upload.rb
bhf-0.2.8 lib/bhf/active_record/upload.rb