Module: Attach::ModelExtension::ClassMethods

Defined in:
lib/attach/model_extension.rb

Instance Method Summary collapse

Instance Method Details

#attachment(name, options = {}, &block) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/attach/model_extension.rb', line 79

def attachment(name, options = {}, &block)
  unless self.reflect_on_all_associations(:has_many).map(&:name).include?(:attachments)
    has_many :attachments, :as => :owner, :dependent => :destroy, :class_name => 'Attach::Attachment'
  end

  if block_given?
    Processor.register(self, name, &block)
  end

  define_method name do
    instance_variable_get("@#{name}") || begin
      attachment = self.attachments.where(:role => name, :parent_id => nil).first
      instance_variable_set("@#{name}", attachment)
    end
  end

  define_method "#{name}_file" do
    instance_variable_get("@#{name}_file")
  end

  define_method "#{name}_file=" do |file|
    instance_variable_set("@#{name}_file", file)
    if file.is_a?(ActionDispatch::Http::UploadedFile)
      @pending_attachments ||= []
      @pending_attachments << {:role => name, :file => file, :options => options}
    else
      nil
    end
  end

  define_method "#{name}_delete" do
    instance_variable_get("@#{name}_delete")
  end

  define_method "#{name}_delete=" do |delete|
    delete = delete.to_i
    instance_variable_set("@#{name}_delete", delete)
    if delete == 1
      @pending_attachment_deletions ||= []
      @pending_attachment_deletions << name
    end
  end
end

#includes_attachments(*options) ⇒ Object



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
75
76
77
# File 'lib/attach/model_extension.rb', line 31

def includes_attachments(*options)
  manipulate do |records|
    if records.empty?
      # Nothing to do
    else
      if options.first.is_a?(Hash)
        options = options.first
      else
        options = options.each_with_object({}) do |role, hash|
          hash[role.to_sym] = []
        end
      end

      options.keys.each do |key|
        if options[key].is_a?(Symbol)
          options[key] = [options[key]]
        end
      end

      root_attachments = {}
      Attachment.where(:owner_id => records.map(&:id), :owner_type => records.first.class.to_s, :role => options.keys).each do |attachment|
        root_attachments[[attachment.owner_id, attachment.role]] = attachment
      end

      child_attachments = {}
      child_roles = options.values.flatten
      Attachment.where(:parent_id => root_attachments.values.map(&:id), :role => child_roles).each do |attachment|
        child_attachments[[attachment.id, attachment.role]] = attachment
      end

      root_attachments.values.each do |attachment|
        options[attachment.role.to_sym].each do |role|
          attachment.instance_variable_set("@cached_children", {}) if attachment.instance_variable_get("@cached_children").nil?
          attachment.instance_variable_get("@cached_children")[role.to_sym] = attachment
        end
      end

      records.each do |record|
        options.keys.each do |role|
          if a = root_attachments[[record.id, role.to_s]]
            record.instance_variable_set("@#{role}", a)
          end
        end
      end
    end
  end
end