Module: LocusFocus::Acts::PolymorphicPaperclip::ClassMethods
- Defined in:
- lib/sga/acts_as_polymorphic_paperclip.rb
Instance Method Summary collapse
-
#acts_as_polymorphic_paperclip(options = {}) ⇒ Object
Extends the model to afford the ability to associate other records with the receiving record.
Instance Method Details
#acts_as_polymorphic_paperclip(options = {}) ⇒ Object
Extends the model to afford the ability to associate other records with the receiving record.
This module needs the paperclip plugin to work www.thoughtbot.com/projects/paperclip
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/sga/acts_as_polymorphic_paperclip.rb', line 16 def acts_as_polymorphic_paperclip( = {}) write_inheritable_attribute(:acts_as_polymorphic_paperclip_options, { :counter_cache => [:counter_cache], :styles => [:styles] }) class_inheritable_reader :acts_as_polymorphic_paperclip_options has_many :attachings, :as => :attachable, :dependent => :destroy has_many :assets, :through => :attachings do def attach(asset_id) asset_id = extract_id(asset_id) asset = Asset.find(asset_id) @owner.assets << asset @owner.assets(true) end def detach(asset_id, delete_if_no_attachings = false) asset_id = extract_id(asset_id) attaching = @owner.attachings.find(:first, :conditions => ['asset_id = ?', asset_id]) attachable = attaching.attachable raise ActiveRecord::RecordNotFound unless attaching result = attaching.destroy asset = Asset.find(asset_id) if asset.attachings.empty? && delete_if_no_attachings# delete if no longer attached to anything override_default_styles, normalised_styles = attachable.override_default_styles?(asset.name) asset.data.instance_variable_set("@styles", normalised_styles) if override_default_styles asset.data.send(:queue_existing_for_delete) asset.data.send(:flush_deletes) asset.save # needed to permanently remove file name and urls end result end protected def extract_id(obj) return obj.id unless obj.class == Fixnum || obj.class == String obj.to_i if obj.to_i > 0 end end # Virtual attribute for the ActionController::UploadedStringIO # which consists of these attributes "content_type", "original_filename" & "original_path" # content_type: image/png # original_filename: 64x16.png # original_path: 64x16.png attr_accessor :data include LocusFocus::Acts::PolymorphicPaperclip::InstanceMethods include LocusFocus::Acts::PolymorphicPaperclip::ExtentionMethods if [:with_extention] write_inheritable_attribute(:wuid_host, [:wuid_host]) if [:wuid_host] class_inheritable_reader :wuid_host end |