Module: ErpTechSvcs::Extensions::ActiveRecord::HasFileAssets::InstanceMethods

Defined in:
lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb

Instance Method Summary collapse

Instance Method Details

#add_file(data, path = nil, capabilities = nil) ⇒ Object

Capabilites can be passed via a hash

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



45
46
47
48
49
50
51
52
53
54
55
56
57
# 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_filesObject

destroy all files related to this model regardless of other relationships



80
81
82
83
84
# 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

#imagesObject



59
60
61
# File 'lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb', line 59

def images
  self.files.where('type = ?', 'Image')
end

#pdfsObject



71
72
73
# File 'lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb', line 71

def pdfs
  self.files.where('type = ?', 'Pdf')
end

#stylesheetsObject



67
68
69
# File 'lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb', line 67

def stylesheets
  self.files.where('type = ?', 'Stylesheet')
end

#templatesObject



63
64
65
# File 'lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb', line 63

def templates
  self.files.where('type = ?', 'Template')
end

#xmlsObject



75
76
77
# File 'lib/erp_tech_svcs/extensions/active_record/has_file_assets.rb', line 75

def xmls
  self.files.where('type = ?', 'XmlFile')
end