Sha256: 935c03f52c2f1e8f040f83f6c1abffc26c5b69a20f17f5f4c67057f48775eade

Contents?: true

Size: 740 Bytes

Versions: 7

Compression:

Stored size: 740 Bytes

Contents

require 'securerandom'

module Notee
  class Image < ActiveRecord::Base

    # accessors
    attr_accessor :file

    # callbacks
    before_save :manage_image
    before_destroy :protect_default

    private
    def manage_image
      return unless self.file

      image_dir = Rails.root.to_s + "/public/notee"
      FileUtils.mkdir_p(image_dir) unless FileTest.exist?(image_dir)
      image_name = Time.now.strftime('%Y%m%d%H%M%S') + '--' + SecureRandom.uuid + '.jpg'
      self.transaction do
        open(image_dir + "/" + image_name, 'wb') do |output|
          output.write(self.file.read)
        end
        self.content = image_name
      end
    end

    def protect_default
      return false if self.id == 1
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
notee-0.3.7 app/models/notee/image.rb
notee-0.3.6 app/models/notee/image.rb
notee-0.3.5 app/models/notee/image.rb
notee-0.3.4.1 app/models/notee/image.rb
notee-0.3.4 app/models/notee/image.rb
notee-0.3.3 app/models/notee/image.rb
notee-0.3.2 app/models/notee/image.rb