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
- #assetable_type=(sType) ⇒ Object
- #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.
49 50 51 |
# File 'lib/papermill/papermill_asset.rb', line 49 def crop_h @crop_h end |
#crop_w ⇒ Object
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_x ⇒ Object
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_y ⇒ Object
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_orphans ⇒ Object
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.(assetable_type, assetable_key) if assetable_type assoc = assetable_type.constantize. assoc[assetable_key.try(:to_sym)] || assoc[:default] || Papermill:: else Papermill:: 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 |
#basename ⇒ Object
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::[:url_key_generator].call(style, self) end |
#content_type ⇒ Object
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_thumbnails ⇒ Object
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::[: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
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
169 170 171 |
# File 'lib/papermill/papermill_asset.rb', line 169 def has_valid_url_key?(key, style) !Papermill::[:use_url_key] || compute_url_key(style) == key end |
#height ⇒ Object
84 85 86 |
# File 'lib/papermill/papermill_asset.rb', line 84 def height @height ||= Paperclip::Geometry.from_file(file).height end |
#id_partition ⇒ Object
64 65 66 |
# File 'lib/papermill/papermill_asset.rb', line 64 def id_partition ("%09d" % self.id).scan(/\d{3}/).join("/") end |
#image? ⇒ Boolean
133 134 135 |
# File 'lib/papermill/papermill_asset.rb', line 133 def image? content_type.split("/")[0] == "image" end |
#name ⇒ Object
68 69 70 |
# File 'lib/papermill/papermill_asset.rb', line 68 def name file_file_name end |
#papermill_options ⇒ Object
129 130 131 |
# File 'lib/papermill/papermill_asset.rb', line 129 def self.class.(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 |
#size ⇒ Object
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 |
#width ⇒ Object
80 81 82 |
# File 'lib/papermill/papermill_asset.rb', line 80 def width @width ||= Paperclip::Geometry.from_file(file).width end |