Module: RactorRailsShim::ActiveStorageAttachedPatch
- Defined in:
- lib/ractor_rails_shim/patches/active_storage.rb
Defined Under Namespace
Modules: ActiveStorageServicePatch, BlobBuildPatch, BlobChecksumPatch, BlobMetadataTypePatch
Constant Summary collapse
- SHAREABLE_SCOPE_HOST =
Frozen, shareable host used as the defining
selfof attachment scope lambdas. A frozen Module is Ractor-shareable, so a lambda created viaSHAREABLE_SCOPE_HOST.instance_eval { -> { where(name: name) } }has a shareableselfand makesRactor.make_shareable(reflections)succeed. Module.new.freeze
Instance Method Summary collapse
- #has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false) {|reflection| ... } ⇒ Object
- #has_one_attached(name, dependent: :purge_later, service: nil, strict_loading: false) {|reflection| ... } ⇒ Object
Instance Method Details
#has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false) {|reflection| ... } ⇒ Object
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/ractor_rails_shim/patches/active_storage.rb', line 193 def has_many_attached(name, dependent: :purge_later, service: nil, strict_loading: false) ActiveStorage::Attached::Model.validate_service_configuration(service, self, name) unless service.is_a?(Proc) generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1 # frozen_string_literal: true def #{name} @active_storage_attached ||= {} @active_storage_attached[:#{name}] ||= ActiveStorage::Attached::Many.new("#{name}", self) end def #{name}=(attachables) attachables = Array(attachables).compact_blank pending_uploads = attachment_changes["#{name}"].try(:pending_uploads) attachment_changes["#{name}"] = if attachables.none? ActiveStorage::Attached::Changes::DeleteMany.new("#{name}", self) else ActiveStorage::Attached::Changes::CreateMany.new("#{name}", self, attachables, pending_uploads: pending_uploads) end end CODE scope_lambda = SHAREABLE_SCOPE_HOST.instance_eval { ->(record = nil) { where(name: name) } } has_many :"#{name}_attachments", scope_lambda, as: :record, class_name: "ActiveStorage::Attachment", inverse_of: :record, dependent: :destroy, strict_loading: strict_loading has_many :"#{name}_blobs", through: :"#{name}_attachments", class_name: "ActiveStorage::Blob", source: :blob, strict_loading: strict_loading scope :"with_attached_#{name}", -> { if ActiveStorage.track_variants includes("#{name}_attachments": { blob: { variant_records: { image_attachment: :blob }, preview_image_attachment: { blob: { variant_records: { image_attachment: :blob } } } } }) else includes("#{name}_attachments": :blob) end } after_save { [name.to_s]&.save } after_commit(on: %i[ create update ]) { .delete(name.to_s).try(:upload) } reflection = ActiveRecord::Reflection.create( :has_many_attached, name, nil, { dependent: dependent, service_name: service }, self ) yield reflection if block_given? ActiveRecord::Reflection.(self, name, reflection) end |
#has_one_attached(name, dependent: :purge_later, service: nil, strict_loading: false) {|reflection| ... } ⇒ Object
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/ractor_rails_shim/patches/active_storage.rb', line 141 def has_one_attached(name, dependent: :purge_later, service: nil, strict_loading: false) ActiveStorage::Attached::Model.validate_service_configuration(service, self, name) unless service.is_a?(Proc) generated_association_methods.class_eval <<-CODE, __FILE__, __LINE__ + 1 # frozen_string_literal: true def #{name} @active_storage_attached ||= {} @active_storage_attached[:#{name}] ||= ActiveStorage::Attached::One.new("#{name}", self) end def #{name}=(attachable) attachment_changes["#{name}"] = if attachable.nil? || attachable == "" ActiveStorage::Attached::Changes::DeleteOne.new("#{name}", self) else ActiveStorage::Attached::Changes::CreateOne.new("#{name}", self, attachable) end end CODE # Ractor fix: scope lambda built with a shareable `self` so the # reflection (and therefore `User.reflections`) is Ractor-shareable. scope_lambda = SHAREABLE_SCOPE_HOST.instance_eval { ->(record = nil) { where(name: name) } } has_one :"#{name}_attachment", scope_lambda, class_name: "ActiveStorage::Attachment", as: :record, inverse_of: :record, dependent: :destroy, strict_loading: strict_loading has_one :"#{name}_blob", through: :"#{name}_attachment", class_name: "ActiveStorage::Blob", source: :blob, strict_loading: strict_loading scope :"with_attached_#{name}", -> { if ActiveStorage.track_variants includes("#{name}_attachment": { blob: { variant_records: { image_attachment: :blob }, preview_image_attachment: { blob: { variant_records: { image_attachment: :blob } } } } }) else includes("#{name}_attachment": :blob) end } after_save { [name.to_s]&.save } after_commit(on: %i[ create update ]) { .delete(name.to_s).try(:upload) } reflection = ActiveRecord::Reflection.create( :has_one_attached, name, nil, { dependent: dependent, service_name: service }, self ) yield reflection if block_given? ActiveRecord::Reflection.(self, name, reflection) end |