Class: Asset

Inherits:
KitIndexed show all
Defined in:
app/models/asset.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from KitIndexed

do_indexing, indexed_columns, paginate

Class Method Details

.asset_tagged_url(sid, tag, size) ⇒ Object



154
155
156
157
158
159
160
161
162
163
# File 'app/models/asset.rb', line 154

def self.asset_tagged_url(sid, tag, size)
  Rails.cache.fetch(Asset.cache_key(sid, tag, size), :expires_in=>120.seconds, :race_condition_ttl=>5.seconds) do
    a = Asset.sys(sid).where("tags like '%#{tag}%'").order(:id).last
    if a
      a.file.url(size)
    else
      nil
    end
  end 
end

.asset_url(sid, id, size) ⇒ Object



165
166
167
168
169
170
171
172
173
174
# File 'app/models/asset.rb', line 165

def self.asset_url(sid, id, size)
  Rails.cache.fetch(Asset.cache_key(sid, id, size), :expires_in=>120.seconds, :race_condition_ttl=>5.seconds) do
    a = Asset.find_sys_id(sid, id)
    if a
      a.file.url(size)
    else
      nil
    end
  end 
end

.cache_key(sid, id, size) ⇒ Object



150
151
152
# File 'app/models/asset.rb', line 150

def self.cache_key(sid, id, size)
  "asset_#{sid}_#{id}_#{size}"
end

.fix_oldObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/models/asset.rb', line 57

def Asset.fix_old
  Asset.order(:id).all.each do |asset|
    f = "#{Rails.root}/public/system/files/#{asset.id}/[0-9a-z]*"

    d = Dir.glob(f).first

    fn = d + "/original/#{asset.file_file_name}"

    if File.exist?(fn)
      image = File.new fn
      asset.file = image
      asset.save
    end

    image.close

  end
end

.image_typesObject



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

def self.image_types
  "'image/jpeg','image/gif','image/png'"
end

.wild_search(search, _sid) ⇒ Object



141
142
143
144
145
146
147
148
# File 'app/models/asset.rb', line 141

def self.wild_search(search, _sid)
  assets = Asset
  table = Asset.arel_table
  fields = table[:file_file_name].matches("%#{search}%")
  fields = fields.or(table[:tags].matches("%#{search}%"))
  fields = fields.or(table[:comment].matches("%#{search}%"))
  assets.where(fields).sys(_sid)
end

Instance Method Details

#best_url(width, height) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'app/models/asset.rb', line 25

def best_url(width, height) 
  biggest = width
  biggest = height if height>biggest
  if biggest>800
    self.url(:original)
  elsif biggest>500
    self.url(:large)
  elsif biggest>300
    self.url(:medium)
  elsif biggest>100
    self.url(:small)
  else
    self.url(:thumb)
  end
end

#calculate_sizeObject



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

def calculate_size  
  return unless image?   
   geo = Paperclip::Geometry.from_file(file.to_file(:original))
   self.height = geo.height
   self.width = geo.width
   self.is_image = 1
end

#display_name(length = :full) ⇒ Object



123
124
125
126
127
128
129
130
131
132
# File 'app/models/asset.rb', line 123

def display_name(length = :full)
  n = self.file_file_name
  n ||= "file"
 
  len = 255
  len = 30 if length==:medium
  len = 10 if length==:short
  len = 80 if length==:long 
  truncate(n, :length=>len)
end

#file_path(size = 'thumb') ⇒ Object



115
116
117
# File 'app/models/asset.rb', line 115

def file_path(size='thumb')
  url(size)
end

#image?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'app/models/asset.rb', line 103

def image?
  (self.file_content_type == "image/jpeg" || self.file_content_type == "image/png" || self.file_content_type == "image/gif")
end

#maybe_rename_filesObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'app/models/asset.rb', line 80

def maybe_rename_files
  return if self.new_record?
  
  if self.file_file_name_changed?
    (self.file.styles.keys+[:original]).each do |style|
      path = self.file.path(style)
      begin
        FileUtils.move(path.gsub(self.file_file_name, self.file_file_name_was), File.join(File.dirname(path), self.file_file_name)) 
      rescue Exception => e
        logger.info "Renaming file failed (nothing to worry about): #{e.message}"
      end
    end
  end
end

#nice_typeObject



76
77
78
# File 'app/models/asset.rb', line 76

def nice_type
  self.file_content_type.split('/')[1] rescue self.file_content_type
end

#serializable_hash(options = nil) ⇒ Object



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

def serializable_hash(options = nil)
  options ||= {}
  options[:methods] ||= []
  options[:methods] << :url
  super(options)
end

#simple_urlObject



21
22
23
# File 'app/models/asset.rb', line 21

def simple_url
  return "/file/#{self.id}/#{self.code}/#{self.file_file_name}"
end

#sys_file_path(size = "thumb") ⇒ Object



119
120
121
# File 'app/models/asset.rb', line 119

def sys_file_path(size="thumb")
  "public" + file_path(size)
end

#update_codeObject



48
49
50
51
52
53
54
55
# File 'app/models/asset.rb', line 48

def update_code
  bits = self.file.url(:original).split('/')
  if image?
    self.code = bits[9]
  else
    self.code = bits[7]
  end
end

#url(size = 'original') ⇒ Object



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

def url(size='original')
 file.url(size)
end