Module: Attach::ModelExtension::ClassMethods

Defined in:
lib/attach/model_extension.rb

Instance Method Summary collapse

Instance Method Details

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



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
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
# File 'lib/attach/model_extension.rb', line 108

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

  dsl = AttachmentDSL.new(&block)

  dsl.processors.each do |processor|
    Processor.register(self, name, &processor)
  end

  if dsl.validators.size > 0
    validate do
      attachment = @pending_attachments && @pending_attachments[name] ? @pending_attachments[name] : send(name)
      file_errors = []
      dsl.validators.each do |validator|
        validator.call(attachment, file_errors)
      end
      file_errors.each { |e| errors.add("#{name}_file", e) }
    end
  end

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

  define_method "#{name}=" do |file|
    if file.is_a?(Attach::Attachment)
      @replaced_attachment = self.try(name)
      attachment = file
      attachment.owner = self
      attachment.role = name
      attachment.processed = false
    elsif file
      attachment = Attachment.new({:owner => self, :role => name}.merge(options))
      case file
      when ActionDispatch::Http::UploadedFile
        attachment.binary = file.tempfile
        attachment.file_name = file.original_filename
        attachment.file_type = file.content_type
      when Attach::File
        attachment.binary = file.data
        attachment.file_name = file.name
        attachment.file_type = file.type
      else
        attachment.binary = file
        attachment.file_name = "untitled"
        attachment.file_type = "application/octet-stream"
      end
    end

    if attachment
      @pending_attachments ||= {}
      @pending_attachments[name] = attachment
    end
    instance_variable_set("@#{name}", attachment)
  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



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

def includes_attachments(*options)
  manipulate do |records|
    if records.empty?
      # Nothing to do
    else

      if options.first.is_a?(Hash)
        options = options.first
        binaries_to_include = options.delete(:_include_binaries) || {}
      else
        binaries_to_include = {}
        options = options.each_with_object({}) do |opt, hash|
          if opt.is_a?(Symbol) || opt.is_a?(String)
            hash[opt.to_sym] = []
          elsif opt.is_a?(Hash)
            opt.each do |key, value|
              if key == :_include_binaries
                binaries_to_include = value
              else
                hash[key.to_sym] = value
              end
            end
          end
        end

      end

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

      attachments_for_binary_preload = []
      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
        if binaries_to_include[attachment.role.to_sym] && binaries_to_include[attachment.role.to_sym].include?(:_self)
          attachments_for_binary_preload << attachment
        end
      end

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

        root_attachments.values.each do |attachment|
          options[attachment.role.to_sym].each do |role|
            child_attachment = child_attachments[[attachment.id, role.to_s]]

            if child_attachment && binaries_to_include[attachment.role.to_sym] && binaries_to_include[attachment.role.to_sym].include?(role)
              attachments_for_binary_preload << child_attachment
            end

            attachment.instance_variable_set("@cached_children", {}) if attachment.instance_variable_get("@cached_children").nil?
            attachment.instance_variable_get("@cached_children")[role.to_sym] = child_attachments[[attachment.id, role.to_s]] || :nil
          end
        end
      end

      if binaries = Attach.backend.read_multi(attachments_for_binary_preload)
        attachments_for_binary_preload.each do |attachment|
          attachment.instance_variable_set("@binary", binaries[attachment.id] || :nil)
        end
      else
        # Preloading binaries isn't supported by the backend
      end

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