Module: Cms::Behaviors::Attaching::ClassMethods

Defined in:
lib/cms/behaviors/attaching.rb

Instance Method Summary collapse

Instance Method Details

#attachments_as_of_version(version_number, attachable) ⇒ Array<Cms::Attachment>

Find all attachments as of the given version for the specified block. Excludes attachments that were deleted as of a version.


198
199
200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/cms/behaviors/attaching.rb', line 198

def attachments_as_of_version(version_number, attachable)
  found_versions = Cms::Attachment::Version.where(:attachable_id => attachable.id).
      where(:attachable_type => attachable.attachable_type).
      where(:attachable_version => version_number).
      order(:version).all
  found_attachments = []

  found_versions.each do |av|
    record = av.build_object_from_version
    found_attachments << record
  end
  found_attachments.delete_if { |value| value.deleted? }
  found_attachments
end

#definitions_for(name) ⇒ Object

Return all definitions for a given class.


214
215
216
# File 'lib/cms/behaviors/attaching.rb', line 214

def definitions_for(name)
  Cms::Attachment.definitions[self.name][name]
end

#has_attachment(name, options = {}) ⇒ Object

Defines an single attachement with a given name.


161
162
163
164
165
166
167
168
169
170
171
172
# File 'lib/cms/behaviors/attaching.rb', line 161

def has_attachment(name, options = {})
  options[:type] = :single
  options[:index] = Cms::Attachment.definitions[self.name].size
  Cms::Attachment.definitions[self.name][name] = options

  define_method name do
    attachment_named(name)
  end
  define_method "#{name}?" do
    (attachment_named(name) != nil)
  end
end

#has_many_attachments(name, options = {}) ⇒ Object

Allows multiple attachments under a specific name.


179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/cms/behaviors/attaching.rb', line 179

def has_many_attachments(name, options = {})
  options[:type] = :multiple
  Cms::Attachment.definitions[self.name][name] = options

  define_method name do
    attachments.named name
  end

  define_method "#{name}?" do
    !attachments.named(name).empty?
  end
end