Sha256: 84f342ea5a79604106fb7cdb62d2b2166326c791b3d2c8ff6a3cd3a782becff9
Contents?: true
Size: 1.31 KB
Versions: 1
Compression:
Stored size: 1.31 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 virtual_attribute = "new_#{attribute}_uuids" 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 virtual_attribute = "new_#{attribute}_uuid" end 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
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
s3_relay-0.3.2 | lib/s3_relay/model.rb |