Module: Bulldog::HasAttachment::ClassMethods

Defined in:
lib/bulldog/has_attachment.rb

Instance Method Summary collapse

Instance Method Details

#attachment_reflectionsObject



182
183
184
185
186
187
188
189
190
191
192
# File 'lib/bulldog/has_attachment.rb', line 182

def attachment_reflections
  @attachment_reflections ||=
    begin
      hash = {}
      superhash = superclass.attachment_reflections
      superhash.map do |name, reflection|
        hash[name] = reflection.clone
      end
      hash
    end
end

#define_attachment_accessors(name) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'lib/bulldog/has_attachment.rb', line 194

def define_attachment_accessors(name)
  module_eval <<-EOS, __FILE__, __LINE__
    def #{name}
      _attachment_for(:#{name})
    end

    def #{name}=(value)
      assign_attachment(:#{name}, value)
    end

    def #{name}?
      _attachment_for(:#{name}).present?
    end
  EOS
end

#define_attachment_attribute_methods(name) ⇒ Object



210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
# File 'lib/bulldog/has_attachment.rb', line 210

def define_attachment_attribute_methods(name)
  # HACK: Without this, methods defined via
  # #attribute_method_suffix (e.g., #ATTACHMENT_changed?) won't
  # be defined unless the attachment is assigned first.
  # ActiveRecord appears to give us no other way without
  # defining an after_initialize, which is slow.
  attribute_method_suffixes.each do |suffix|
    next unless suffix[0] == ?_  # skip =, ?.
    class_eval <<-EOS
      def #{name}#{suffix}(*args, &block)
        attribute#{suffix}('#{name}', *args, &block)
      end
    EOS
  end
end