Sha256: e405cdde95d4bfbe06f2c3a5d5b0a24f0db3653d7ee01846bbfd40bcbac9692d

Contents?: true

Size: 1.5 KB

Versions: 70

Compression:

Stored size: 1.5 KB

Contents

module Bhf
  module ActiveRecord
    module Upload
      extend ActiveSupport::Concern

      included do
        before_save :bhf_upload
        cattr_accessor :bhf_upload_settings
      end

      def bhf_upload
        self.class.bhf_upload_settings.each do |settings|
          name_was = send("#{settings[:name]}_was")
          param_name = read_attribute(settings[:name]) || send(settings[:name])
          next if param_name.blank? or param_name.is_a?(String)
          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.gsub(/[^\w\.\-]/, '_')
              path = File.join(settings[:path], filename)
              File.open(path, 'wb') { |f| f.write(file.read) }
              filename
            else
              name_was
            end
          end
          write_attribute settings[:name], file_string
        end
      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

    end
  end
end

Version data entries

70 entries across 70 versions & 1 rubygems

Version Path
bhf-0.7.1 lib/bhf/active_record/upload.rb
bhf-0.7.0.rc3 lib/bhf/active_record/upload.rb
bhf-0.7.0.rc2 lib/bhf/active_record/upload.rb
bhf-0.7.0.rc1 lib/bhf/active_record/upload.rb
bhf-0.6.32 lib/bhf/active_record/upload.rb
bhf-0.6.31 lib/bhf/active_record/upload.rb
bhf-0.6.30 lib/bhf/active_record/upload.rb
bhf-0.6.29 lib/bhf/active_record/upload.rb
bhf-0.6.28 lib/bhf/active_record/upload.rb
bhf-0.6.27 lib/bhf/active_record/upload.rb
bhf-0.6.26 lib/bhf/active_record/upload.rb
bhf-0.6.25 lib/bhf/active_record/upload.rb
bhf-0.6.24 lib/bhf/active_record/upload.rb
bhf-0.6.23 lib/bhf/active_record/upload.rb
bhf-0.6.22 lib/bhf/active_record/upload.rb
bhf-0.6.21 lib/bhf/active_record/upload.rb
bhf-0.6.20 lib/bhf/active_record/upload.rb
bhf-0.6.15 lib/bhf/active_record/upload.rb
bhf-0.6.14 lib/bhf/active_record/upload.rb
bhf-0.6.13 lib/bhf/active_record/upload.rb