Sha256: 2b42d811c3d84528845a9d2593e5e566ee08f8e069cd3ceca7cc4bc5c6ed8a38

Contents?: true

Size: 1.26 KB

Versions: 3

Compression:

Stored size: 1.26 KB

Contents

module S3Relay
  module Model

    def s3_relay(attribute, has_many=false)
      upload_type = attribute.to_s.classify

      if has_many
        has_many attribute, as: :parent, class_name: "S3Relay::Upload"

        define_method attribute do
          S3Relay::Upload
            .where(
              parent_type: self.class.to_s,
              parent_id:   self.id,
              upload_type: upload_type
            )
        end
      else
        has_one attribute, as: :parent, class_name: "S3Relay::Upload"

        define_method attribute do
          S3Relay::Upload
            .where(
              parent_type: self.class.to_s,
              parent_id:   self.id,
              upload_type: upload_type
            )
            .order(:pending_at => 'desc').last
        end
      end

      virtual_attribute = "new_#{attribute}_uuids"
      attr_accessor virtual_attribute

      association_method = "associate_#{attribute}"

      after_save association_method.to_sym

      define_method association_method do
        new_uuids = send(virtual_attribute)
        return if new_uuids.blank?

        S3Relay::Upload.where(uuid: new_uuids, upload_type: upload_type)
          .update_all(parent_type: self.class.to_s, parent_id: self.id)
      end

    end

  end

end

Version data entries

3 entries across 3 versions & 2 rubygems

Version Path
mongoid-direct-s3-upload-0.1.1 lib/s3_relay/model.rb
mongoid-direct-s3-upload-0.1.0 lib/s3_relay/model.rb
mongoid_direct_s3_upload-0.1.0 lib/s3_relay/model.rb