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.

Parameters:

  • version_number (Integer)
  • attachable (Attaching)

    The object with attachments

Returns:



174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/cms/behaviors/attaching.rb', line 174

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).load
  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

#by_section(section) ⇒ ActiveRecord::Relation

Finds all instances of this Attaching content that exist in a given section.

Parameters:

Returns:

  • (ActiveRecord::Relation)

    A relation that will return Attaching instances.



126
127
128
129
130
# File 'lib/cms/behaviors/attaching.rb', line 126

def by_section(section)
  where(["#{SectionNode.table_name}.ancestry = ?", section.node.ancestry_path])
    .includes(:attachments => :section_node)
    .references(:section_nodes)
end

#definitions_for(name) ⇒ Object

Return all definitions for a given class.



190
191
192
# File 'lib/cms/behaviors/attaching.rb', line 190

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

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

Defines an single attachement with a given name.

Parameters:

  • name (Symbol)

    The name of the attachment

  • options (Hash) (defaults to: {})

    Accepts most Paperclip options for Paperclip::ClassMethods.has_attached_file

See Also:



137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/cms/behaviors/attaching.rb', line 137

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.

Parameters:

  • name (Symbol)

    The name of the attachments.

  • options (Hash) (defaults to: {})

    Accepts most Paperclip options for Paperclip::ClassMethods.has_attached_file

See Also:



155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/cms/behaviors/attaching.rb', line 155

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