Class: Lipsiadmin::DataBase::AttachmentTable

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/data_base/attachment_table.rb

Overview

This is the table that handle the attachments. You can define some global info like:

attachment_styles :thumb, "128x128"

In this way all models that use has_one_attachment or has_many_attachments handle a default style called thumb

Remember that any model that use has_one_attachment or has_many_attachments can override/add styles/validations etc…

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.inherited(subclass) ⇒ Object

:nodoc:



15
16
17
18
19
20
21
# File 'lib/data_base/attachment_table.rb', line 15

def self.inherited(subclass)#:nodoc:
  super
  subclass.write_inheritable_attribute(:attachment_definitions, {}) if subclass.attachment_definitions.nil?
  subclass.attachment_definitions[subclass.name] = {:validations => {}}.merge(Lipsiadmin::Attachment.options)
  subclass.send(:include, Lipsiadmin::DataBase::UtilityScopes)
  subclass.extend(ClassMethods)
end

Instance Method Details

#attachment_definitionsObject

This is the custom instance attachment_definition



58
59
60
61
# File 'lib/data_base/attachment_table.rb', line 58

def attachment_definitions#:nodoc:
  @instance_attachment_definitions ||= self.class.attachment_definitions[self.class.name].clone
  return @instance_attachment_definitions
end

#attachment_definitions=(options) ⇒ Object

:nodoc:



53
54
55
# File 'lib/data_base/attachment_table.rb', line 53

def attachment_definitions=(options)#:nodoc:
  attachment_definitions.merge!(options.symbolize_keys)
end

#destroy_attached_filesObject

Perform file.destroy



75
76
77
78
79
# File 'lib/data_base/attachment_table.rb', line 75

def destroy_attached_files
  logger.info("[Attachment] Deleting attachments.")
  file.queue_existing_for_delete
  file.flush_deletes
end

#fileObject

Returns a Lipsiadmin::Attachment::Attach instance



39
40
41
# File 'lib/data_base/attachment_table.rb', line 39

def file
  @_file ||= Lipsiadmin::Attachment::Attach.new(:attached, self, attachment_definitions)
end

#file=(tempfile) ⇒ Object

Assign a Lipsiadmin::Attachment::Attach instance to the current file



44
45
46
# File 'lib/data_base/attachment_table.rb', line 44

def file=(tempfile)
  file.assign(tempfile)
end

#file?Boolean

Returns true if the file exist

Returns:

  • (Boolean)


49
50
51
# File 'lib/data_base/attachment_table.rb', line 49

def file?
  file.exist?
end

#save_attached_filesObject

Perform file.save



69
70
71
72
# File 'lib/data_base/attachment_table.rb', line 69

def save_attached_files
  logger.info("[Attachment] Saving attachments.")
  file.save
end

#url(style = nil) ⇒ Object

Returns the url of the attachment, optionally you can pass the style like url(:thumb)



34
35
36
# File 'lib/data_base/attachment_table.rb', line 34

def url(style=nil)
  file.url(style)
end