Class: PapermillAsset

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/papermill/papermill_asset.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#crop_hObject

Returns the value of attribute crop_h.



35
36
37
# File 'lib/papermill/papermill_asset.rb', line 35

def crop_h
  @crop_h
end

#crop_wObject

Returns the value of attribute crop_w.



35
36
37
# File 'lib/papermill/papermill_asset.rb', line 35

def crop_w
  @crop_w
end

#crop_xObject

Returns the value of attribute crop_x.



35
36
37
# File 'lib/papermill/papermill_asset.rb', line 35

def crop_x
  @crop_x
end

#crop_yObject

Returns the value of attribute crop_y.



35
36
37
# File 'lib/papermill/papermill_asset.rb', line 35

def crop_y
  @crop_y
end

Class Method Details

.destroy_orphansObject



143
144
145
# File 'lib/papermill/papermill_asset.rb', line 143

def self.destroy_orphans
  self.all(:conditions => ["id < 0 AND created_at < ?", DateTime.now.yesterday]).each &:destroy
end

.papermill_options(assetable_class, assetable_key) ⇒ Object



106
107
108
109
110
111
112
113
# File 'lib/papermill/papermill_asset.rb', line 106

def self.papermill_options(assetable_class, assetable_key)
  if assetable_class
    assoc = assetable_class.constantize.papermill_associations
    assoc[assetable_key.try(:to_sym)] || assoc[Papermill::options[:base_association_name]]
  else
    Papermill::options
  end
end

Instance Method Details

#basenameObject



58
59
60
# File 'lib/papermill/papermill_asset.rb', line 58

def basename
  name.gsub(/#{extension}$/, "").strip
end

#compute_url_key(style) ⇒ Object



147
148
149
# File 'lib/papermill/papermill_asset.rb', line 147

def compute_url_key(style)
  Papermill::options[:url_key_generator].call(style, self)
end

#content_typeObject



98
99
100
# File 'lib/papermill/papermill_asset.rb', line 98

def content_type
  file_content_type
end

#create_thumb_file(style_name, style = nil) ⇒ Object



123
124
125
126
127
128
129
130
# File 'lib/papermill/papermill_asset.rb', line 123

def create_thumb_file(style_name, style = nil)
  destroy_thumbnails if style_name.to_s == "original"
  style = self.class.compute_style(style_name) unless style.is_a?(Hash)
  FileUtils.mkdir_p File.dirname(new_path = file.path(style_name))
  FileUtils.cp((tmp_path = Paperclip::PapermillPaperclipProcessor.make(file, style).path), new_path)
  FileUtils.chmod(0644, new_path) unless Papermill::MSWIN
  File.delete(tmp_path)
end

#destroy_thumbnailsObject



132
133
134
135
136
137
138
139
140
141
# File 'lib/papermill/papermill_asset.rb', line 132

def destroy_thumbnails
  thumbnail_folder_mask = Papermill::options[:use_url_key] ? "*/*/" : "*/"
  original_folder = "#{File.dirname(file.path)}/"
  Dir.glob("#{root_directory}/#{thumbnail_folder_mask}").each do |f| 
    FileUtils.rm_r(f) unless f == original_folder
  end
  Dir.glob("#{root_directory}/*/").each do |f|
    FileUtils.rm_r(f) if Dir.entries(f) == [".", ".."]
  end
end

#extensionObject



62
63
64
# File 'lib/papermill/papermill_asset.rb', line 62

def extension
  File.extname(name)
end

#Filedata=(data) ⇒ Object



37
38
39
40
41
42
43
44
# File 'lib/papermill/papermill_asset.rb', line 37

def Filedata=(data)
  if !Papermill::MSWIN && !(mime = `file --mime -br #{data.path}`).blank? && !mime.starts_with?("cannot open")
    data.content_type = mime.strip.split(";").first
  elsif MIME_TYPE_LOADED && (mime = MIME::Types.type_for(data.original_filename))
    data.content_type = mime.first.simplified
  end
  self.file = data
end

#Filename=(name) ⇒ Object



46
47
48
# File 'lib/papermill/papermill_asset.rb', line 46

def Filename=(name)
  @real_file_name = name
end

#has_valid_url_key?(key, style) ⇒ Boolean

Returns:

  • (Boolean)


151
152
153
# File 'lib/papermill/papermill_asset.rb', line 151

def has_valid_url_key?(key, style)
  !Papermill::options[:use_url_key] || compute_url_key(style) == key
end

#heightObject



70
71
72
# File 'lib/papermill/papermill_asset.rb', line 70

def height
  @height ||= Paperclip::Geometry.from_file(file).height
end

#id_partitionObject



50
51
52
# File 'lib/papermill/papermill_asset.rb', line 50

def id_partition
  ("%09d" % self.id).scan(/\d{3}/).join("/")
end

#image?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'lib/papermill/papermill_asset.rb', line 119

def image?
  content_type.split("/")[0] == "image"
end

#nameObject



54
55
56
# File 'lib/papermill/papermill_asset.rb', line 54

def name
  file_file_name
end

#papermill_optionsObject



115
116
117
# File 'lib/papermill/papermill_asset.rb', line 115

def papermill_options
  self.class.papermill_options(assetable_type, assetable_key)
end

#path(style = nil) ⇒ Object



83
84
85
86
# File 'lib/papermill/papermill_asset.rb', line 83

def path(style = nil)
  return path!(style) if style.is_a?(Hash)
  file.path(style)
end

#path!(style = nil) ⇒ Object



93
94
95
96
# File 'lib/papermill/papermill_asset.rb', line 93

def path!(style = nil)
  create_thumb_file(style_name = style_name(style), style) unless File.exists?(self.path(style_name))
  file.path(style_name)
end

#sizeObject



74
75
76
# File 'lib/papermill/papermill_asset.rb', line 74

def size
  file_file_size
end

#style_name(style) ⇒ Object



102
103
104
# File 'lib/papermill/papermill_asset.rb', line 102

def style_name(style)
  style.is_a?(Hash) ? (style[:name] || style.hash).to_s : (style || "original").to_s
end

#url(style = nil) ⇒ Object



78
79
80
81
# File 'lib/papermill/papermill_asset.rb', line 78

def url(style = nil)
  return url!(style) if style.is_a?(Hash)
  file.url(style)
end

#url!(style = nil) ⇒ Object



88
89
90
91
# File 'lib/papermill/papermill_asset.rb', line 88

def url!(style = nil)
  create_thumb_file(style_name = style_name(style), style) unless File.exists?(self.path(style_name))
  file.url(style_name)
end

#widthObject



66
67
68
# File 'lib/papermill/papermill_asset.rb', line 66

def width
  @width ||= Paperclip::Geometry.from_file(file).width
end