Sha256: adf290436cca9106573a5456eccf21019d279b948ec067fab19ee40fe9454678

Contents?: true

Size: 965 Bytes

Versions: 16

Compression:

Stored size: 965 Bytes

Contents

# == Schema Information
#
# Table name: notee_images
#
#  id         :integer          not null, primary key
#  content    :string           not null
#  created_at :datetime         not null
#  updated_at :datetime         not null
#

require 'securerandom'

module Notee
  class Image < ApplicationRecord
    # accessors
    attr_accessor :file

    # callbacks
    before_save :manage_image
    before_save :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'
      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

16 entries across 16 versions & 1 rubygems

Version Path
notee-1.1.2.4 app/models/notee/image.rb
notee-1.1.2.3 app/models/notee/image.rb
notee-1.1.2.2 app/models/notee/image.rb
notee-1.1.2.1 app/models/notee/image.rb
notee-1.1.2 app/models/notee/image.rb
notee-1.1.1 app/models/notee/image.rb
notee-1.1.0 app/models/notee/image.rb
notee-1.0.8 app/models/notee/image.rb
notee-1.0.7 app/models/notee/image.rb
notee-1.0.6 app/models/notee/image.rb
notee-1.0.5 app/models/notee/image.rb
notee-1.0.4 app/models/notee/image.rb
notee-1.0.3 app/models/notee/image.rb
notee-1.0.2 app/models/notee/image.rb
notee-1.0.1 app/models/notee/image.rb
notee-1.0.0 app/models/notee/image.rb