Sha256: 1f9212272deaf64908f163bd0f5ffb2e5d628d9cf64d7129bbc22be137f1e585

Contents?: true

Size: 1.21 KB

Versions: 6

Compression:

Stored size: 1.21 KB

Contents

require 'carrierwave'
require 'carrierwave/orm/activerecord'

module LadyJosephine
  class Image < ActiveRecord::Base

    mount_uploader :file, LadyJosephine::ImageUploader

    before_save :update_file_metadata

    def self.from_params(params)
      uploadable_type      = allowed_class_name params["model-name"]
      return nil if uploadable_type.nil?

      uploadable_uuid      = params["article-uuid"]
      uploadable_attribute = params["column-name"]
      uploadable_id        = uploadable_type.constantize.find_by(lady_josephine_server_uuid: uploadable_uuid).try :id
      block_uuid           = params["block-uuid"]
      file                 = params[:attachment][:file]

      new(
        block_uuid: block_uuid,
        uploadable_type: uploadable_type,
        uploadable_uuid: uploadable_uuid,
        uploadable_id: uploadable_id,
        uploadable_attribute: uploadable_attribute,
        file: file
      )
    end

    def self.allowed_class_name(uploadable_type)
      uploadable_type.classify if LadyJosephine::Whitelist.instance.is_whitelisted?(uploadable_type)
    end

    def update_file_metadata
      if file.present? && file?
        self.file_metadata = file.metadata.to_json
      end
    end

  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
lady_josephine-0.7.0 app/models/lady_josephine/image.rb
lady_josephine-0.6.3 app/models/lady_josephine/image.rb
lady_josephine-0.6.2 app/models/lady_josephine/image.rb
lady_josephine-0.6.1 app/models/lady_josephine/image.rb
lady_josephine-0.6.0 app/models/lady_josephine/image.rb
lady_josephine-0.5.0 app/models/lady_josephine/image.rb