Class: Gluttonberg::Asset

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Library::AttachmentMixin
Defined in:
app/models/gluttonberg/asset.rb

Overview

Asset Model stores meta information for assets and get most of its file management functionality from AttachmentMixin

Constant Summary collapse

GIGA_SIZE =

constants for formatted file size

1073741824.0
MEGA_SIZE =
1048576.0
KILO_SIZE =
1024.0

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Library::AttachmentMixin

#asset_directory_public_url, #asset_folder_path, #asset_processing, #file, #file=, #file_extension, #generate_cropped_image, #location_on_disk, #original_file_on_disk, #tmp_location_on_disk, #tmp_original_file_on_disk, #url

Instance Attribute Details

#typeObject

Returns the value of attribute type.



42
43
44
# File 'app/models/gluttonberg/asset.rb', line 42

def type
  @type
end

Class Method Details

.create_assets_from_ftp(absolute_directory_path = nil) ⇒ Object

This is a helper method for rake task generate_asset_from_bulks_folder Make assets from files in bulks folder This is helpful to bootstrap server in case of large assets



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'app/models/gluttonberg/asset.rb', line 118

def self.create_assets_from_ftp(absolute_directory_path=nil)
  collection = AssetCollection.first_or_create(:name => "BULKS")
  absolute_directory_path = Rails.root+"/bulks" if absolute_directory_path.blank?
  files = Dir.entries(absolute_directory_path)
  assets = []
  files.each do |entry|
    unless AssetBulkImport.hidden_file?(entry)
      file = GbFile.init(File.join(absolute_directory_path, entry))
      asset_name_with_extention = entry.split(".").first
      asset_params = {:name => asset_name_with_extention  , :file => file  }
      assets << Asset.create(asset_params.merge({:asset_collection_ids => collection.id.to_s}))
    end
  end
  assets
end

.refresh_all_asset_typesObject

update asset type for all assets. Just for fixing purpose only



97
98
99
100
101
102
# File 'app/models/gluttonberg/asset.rb', line 97

def self.refresh_all_asset_types
  all.each do |asset|
    asset.auto_set_asset_type
    asset.save
  end
end

.search_assets(query) ⇒ Object



134
135
136
137
# File 'app/models/gluttonberg/asset.rb', line 134

def self.search_assets(query)
  command = Gluttonberg.like_or_ilike
  self.where(["name #{command} ? OR description #{command} ? ", "%#{query}%" , "%#{query}%" ] ).order("name ASC")
end

Instance Method Details

#alt_or_titleObject



49
50
51
# File 'app/models/gluttonberg/asset.rb', line 49

def alt_or_title
  alt.blank? ? title : alt
end

#auto_set_asset_typeObject



86
87
88
89
90
91
92
93
94
# File 'app/models/gluttonberg/asset.rb', line 86

def auto_set_asset_type
  self.asset_type = AssetType.for_file(mime_type, file_name)
  cat = self.category.to_s.downcase
  if cat == "image"
    self.type = "Photo"
  elsif cat == "video"
    self.type = "Video"
  end
end

#categoryObject

returns category of asset



54
55
56
57
58
59
60
# File 'app/models/gluttonberg/asset.rb', line 54

def category
  if asset_type.blank? then
    Library::UNCATEGORISED_CATEGORY
  else
    asset_type.asset_category.name
  end
end

#filename_without_extensionObject



111
112
113
# File 'app/models/gluttonberg/asset.rb', line 111

def filename_without_extension
  self.file_name.split(".").first unless self.file_name.blank?
end

#formatted_file_sizeObject



74
75
76
77
78
79
80
81
82
83
84
# File 'app/models/gluttonberg/asset.rb', line 74

def formatted_file_size
  unless size.blank?
    case
      when size == 1 then "1 Byte"
      when size < KILO_SIZE then "%d Bytes" % size
      when size < MEGA_SIZE then "%.2f KB" % (size / KILO_SIZE)
      when size < GIGA_SIZE then "%.2f MB" % (size / MEGA_SIZE)
      else "%.2f GB" % (size / GIGA_SIZE)
    end
  end
end

#set_category_and_typeObject

find out and set type and category of file



105
106
107
108
109
# File 'app/models/gluttonberg/asset.rb', line 105

def set_category_and_type
  unless file.nil?
    auto_set_asset_type
  end
end

#titleObject



62
63
64
# File 'app/models/gluttonberg/asset.rb', line 62

def title
  self.name
end

#to_json_for_ajax_newObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/models/gluttonberg/asset.rb', line 139

def to_json_for_ajax_new
  json = {
    "asset_id" => self.id,
    "title" => self.name,
    "category" => self.category,
    "url" => self.url
  }
  if self.category == "image"
    json["url"] = self.thumb_small_url
    json["jwysiwyg_image"] = self.url_for(:jwysiwyg_image)
  end
  json.to_json
end

#type_nameObject



66
67
68
69
70
71
72
# File 'app/models/gluttonberg/asset.rb', line 66

def type_name
  if asset_type.blank? then
    Library::UNCATEGORISED_CATEGORY
  else
    asset_type.name
  end
end