module ErpTechSvcs::Extensions::ActiveRecord::HasFileAssets::InstanceMethods

Public Instance Methods

add_file(data, path=nil, capabilities=nil) click to toggle source

Capabilites can be passed via a hash {

:download => ['admin', 'employee'],
:edit     => ['admin']

}

# File lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb, line 45
def add_file(data, path=nil, capabilities=nil)
  file_asset = FileAsset.create!(:base_path => path, :data => data)

  # set capabilites if they are passed
  capabilities.each do |capability_type, roles|
    file_asset.add_capability(capability_type, nil, roles)
  end if capabilities

  self.files << file_asset
  self.save

  file_asset
end
destroy_all_files() click to toggle source

destroy all files related to this model regardless of other relationships

# File lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb, line 80
def destroy_all_files
  statement = "delete from file_assets where id in (select file_asset_id from file_asset_holders where (file_asset_holder_type = '#{self.class.to_s}' or file_asset_holder_type = '#{self.class.superclass.to_s}' ) and file_asset_holder_id = #{self.id} )"

  ::ActiveRecord::Base.connection.execute(statement)
end
images() click to toggle source
# File lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb, line 59
def images
  self.files.where('type = ?', 'Image')
end
pdfs() click to toggle source
# File lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb, line 71
def pdfs
  self.files.where('type = ?', 'Pdf')
end
stylesheets() click to toggle source
# File lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb, line 67
def stylesheets
  self.files.where('type = ?', 'Stylesheet')
end
templates() click to toggle source
# File lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb, line 63
def templates
  self.files.where('type = ?', 'Template')
end
xmls() click to toggle source
# File lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb, line 75
def xmls
  self.files.where('type = ?', 'XmlFile')
end