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.



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

def crop_h
  @crop_h
end

#crop_wObject

Returns the value of attribute crop_w.



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

def crop_w
  @crop_w
end

#crop_xObject

Returns the value of attribute crop_x.



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

def crop_x
  @crop_x
end

#crop_yObject

Returns the value of attribute crop_y.



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

def crop_y
  @crop_y
end

Class Method Details

.destroy_orphansObject



159
160
161
162
163
# File 'lib/papermill/papermill_asset.rb', line 159

def self.destroy_orphans
  self.name != self.base_class.name ? 
  PapermillAsset.delete_all(["created_at < ? AND assetable_id IS NULL AND type = ?", 1.hour.ago, self.name]) :
  PapermillAsset.delete_all(["created_at < ? AND assetable_id IS NULL AND type IS NULL", 1.hour.ago])
end

.papermill_options(assetable_type, assetable_key) ⇒ Object



120
121
122
123
124
125
126
127
# File 'lib/papermill/papermill_asset.rb', line 120

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

Instance Method Details

#assetable_type=(sType) ⇒ Object



30
31
32
# File 'lib/papermill/papermill_asset.rb', line 30

def assetable_type=(sType)
   super(sType.to_s.classify.constantize.base_class.to_s)
end

#basenameObject



72
73
74
# File 'lib/papermill/papermill_asset.rb', line 72

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

#compute_url_key(style) ⇒ Object



165
166
167
# File 'lib/papermill/papermill_asset.rb', line 165

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

#content_typeObject



112
113
114
# File 'lib/papermill/papermill_asset.rb', line 112

def content_type
  file_content_type
end

#create_thumb_file(style_name, style = nil) ⇒ Object



137
138
139
140
141
142
143
144
145
146
# File 'lib/papermill/papermill_asset.rb', line 137

def create_thumb_file(style_name, style = nil)
  return false unless self.image?
  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)
  return true
end

#destroy_thumbnailsObject



148
149
150
151
152
153
154
155
156
157
# File 'lib/papermill/papermill_asset.rb', line 148

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



76
77
78
# File 'lib/papermill/papermill_asset.rb', line 76

def extension
  File.extname(name)
end

#Filedata=(data) ⇒ Object



51
52
53
54
55
56
57
58
# File 'lib/papermill/papermill_asset.rb', line 51

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 = MIME::Types.type_for(data.original_filename))
    data.content_type = mime.first.simplified
  end
  self.file = data
end

#Filename=(name) ⇒ Object



60
61
62
# File 'lib/papermill/papermill_asset.rb', line 60

def Filename=(name)
  @real_file_name = name
end

#has_valid_url_key?(key, style) ⇒ Boolean

Returns:

  • (Boolean)


169
170
171
# File 'lib/papermill/papermill_asset.rb', line 169

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

#heightObject



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

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

#id_partitionObject



64
65
66
# File 'lib/papermill/papermill_asset.rb', line 64

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

#image?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/papermill/papermill_asset.rb', line 133

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

#nameObject



68
69
70
# File 'lib/papermill/papermill_asset.rb', line 68

def name
  file_file_name
end

#papermill_optionsObject



129
130
131
# File 'lib/papermill/papermill_asset.rb', line 129

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

#path(style = nil) ⇒ Object



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

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

#path!(style = nil) ⇒ Object



107
108
109
110
# File 'lib/papermill/papermill_asset.rb', line 107

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



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

def size
  file_file_size
end

#style_name(style) ⇒ Object



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

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

#url(style = nil) ⇒ Object



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

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

#url!(style = nil) ⇒ Object



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

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



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

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