Class: PapermillAsset
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- PapermillAsset
- Defined in:
- lib/papermill/papermill_asset.rb
Instance Attribute Summary collapse
-
#crop_h ⇒ Object
Returns the value of attribute crop_h.
-
#crop_w ⇒ Object
Returns the value of attribute crop_w.
-
#crop_x ⇒ Object
Returns the value of attribute crop_x.
-
#crop_y ⇒ Object
Returns the value of attribute crop_y.
Class Method Summary collapse
Instance Method Summary collapse
- #basename ⇒ Object
- #compute_url_key(style) ⇒ Object
- #content_type ⇒ Object
- #create_thumb_file(style_name, style = nil) ⇒ Object
- #destroy_thumbnails ⇒ Object
- #extension ⇒ Object
- #Filedata=(data) ⇒ Object
- #Filename=(name) ⇒ Object
- #has_valid_url_key?(key, style) ⇒ Boolean
- #height ⇒ Object
- #id_partition ⇒ Object
- #image? ⇒ Boolean
- #name ⇒ Object
- #papermill_options ⇒ Object
- #path(style = nil) ⇒ Object
- #path!(style = nil) ⇒ Object
- #size ⇒ Object
- #style_name(style) ⇒ Object
- #url(style = nil) ⇒ Object
- #url!(style = nil) ⇒ Object
- #width ⇒ Object
Instance Attribute Details
#crop_h ⇒ Object
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_w ⇒ Object
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_x ⇒ Object
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_y ⇒ Object
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_orphans ⇒ Object
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.(assetable_class, assetable_key) if assetable_class assoc = assetable_class.constantize.papermill_associations assoc[assetable_key.try(:to_sym)] || assoc[Papermill::[:base_association_name]] else Papermill:: end end |
Instance Method Details
#basename ⇒ Object
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::[:url_key_generator].call(style, self) end |
#content_type ⇒ Object
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_thumbnails ⇒ Object
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::[: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 |
#extension ⇒ Object
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
151 152 153 |
# File 'lib/papermill/papermill_asset.rb', line 151 def has_valid_url_key?(key, style) !Papermill::[:use_url_key] || compute_url_key(style) == key end |
#height ⇒ Object
70 71 72 |
# File 'lib/papermill/papermill_asset.rb', line 70 def height @height ||= Paperclip::Geometry.from_file(file).height end |
#id_partition ⇒ Object
50 51 52 |
# File 'lib/papermill/papermill_asset.rb', line 50 def id_partition ("%09d" % self.id).scan(/\d{3}/).join("/") end |
#image? ⇒ Boolean
119 120 121 |
# File 'lib/papermill/papermill_asset.rb', line 119 def image? content_type.split("/")[0] == "image" end |
#name ⇒ Object
54 55 56 |
# File 'lib/papermill/papermill_asset.rb', line 54 def name file_file_name end |
#papermill_options ⇒ Object
115 116 117 |
# File 'lib/papermill/papermill_asset.rb', line 115 def self.class.(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 |
#size ⇒ Object
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 |
#width ⇒ Object
66 67 68 |
# File 'lib/papermill/papermill_asset.rb', line 66 def width @width ||= Paperclip::Geometry.from_file(file).width end |