Class: SpudMedia
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- SpudMedia
- Defined in:
- app/models/spud_media.rb
Instance Method Summary collapse
-
#attachment_url(style = nil) ⇒ Object
if you are using S3, attachment.url will automatically point to the S3 url protected files need to hit the rails middle-man first this method will provide the correct url for either case.
- #dynamic_styles ⇒ Object
- #has_custom_crop? ⇒ Boolean
- #image_from_type ⇒ Object
- #is_image? ⇒ Boolean
- #is_pdf? ⇒ Boolean
- #rename_file ⇒ Object
-
#validate_permissions ⇒ Object
If is_protected has changed, we need to make sure we are setting the appropriate permissions This means either moving the file in the filesystem or setting the appropriate ACL in S3.
Instance Method Details
#attachment_url(style = nil) ⇒ Object
if you are using S3, attachment.url will automatically point to the S3 url protected files need to hit the rails middle-man first this method will provide the correct url for either case
115 116 117 118 119 120 121 122 123 124 125 |
# File 'app/models/spud_media.rb', line 115 def (style=nil) # defaults to cropped style if that style exists, otherwise use original if !style style = (is_image? && has_custom_crop?) ? 'cropped' : 'original' end if Spud::Media.paperclip_storage == :s3 && is_protected return Paperclip::Interpolations.interpolate(Spud::Media.config.storage_url, , style) else return .url(style) end end |
#dynamic_styles ⇒ Object
101 102 103 104 105 106 107 108 109 110 |
# File 'app/models/spud_media.rb', line 101 def dynamic_styles styles = {} if is_image? styles[:small] = '50' if has_custom_crop? styles[:cropped] = {:geometry => '', :convert_options => "-resize #{crop_s}% -crop #{crop_w}x#{crop_h}+#{crop_x}+#{crop_y}"} end end return styles end |
#has_custom_crop? ⇒ Boolean
97 98 99 |
# File 'app/models/spud_media.rb', line 97 def has_custom_crop? return (crop_x && crop_y && crop_w && crop_h && crop_s) end |
#image_from_type ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/models/spud_media.rb', line 40 def image_from_type if self..blank? return "spud/admin/files_thumbs/dat_thumb.png" end if self.is_image? return self.(:small) elsif self..blank? return "spud/admin/files_thumbs/dat_thumb.png" elsif self..match(/jpeg|jpg/) return "spud/admin/files_thumbs/jpg_thumb.png" elsif self..match(/png/) return "spud/admin/files_thumbs/png_thumb.png" elsif self..match(/zip|tar|tar\.gz|gz/) return "spud/admin/files_thumbs/zip_thumb.png" elsif self..match(/xls|xlsx/) return "spud/admin/files_thumbs/xls_thumb.png" elsif self..match(/doc|docx/) return "spud/admin/files_thumbs/doc_thumb.png" elsif self..match(/ppt|pptx/) return "spud/admin/files_thumbs/ppt_thumb.png" elsif self..match(/txt|text/) return "spud/admin/files_thumbs/txt_thumb.png" elsif self..match(/pdf|ps/) return "spud/admin/files_thumbs/pdf_thumb.png" elsif self..match(/mp3|wav|aac/) return "spud/admin/files_thumbs/mp3_thumb.png" end return "spud/admin/files_thumbs/dat_thumb.png" end |
#is_image? ⇒ Boolean
81 82 83 84 85 86 87 |
# File 'app/models/spud_media.rb', line 81 def is_image? if self..blank? == false && self..match(/jpeg|jpg|png/) return true else return false end end |
#is_pdf? ⇒ Boolean
89 90 91 92 93 94 95 |
# File 'app/models/spud_media.rb', line 89 def is_pdf? if self..match(/pdf/) return true else return false end end |
#rename_file ⇒ Object
27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'app/models/spud_media.rb', line 27 def rename_file if self. && self..nil? == false .instance_write :file_name, self. return end # remove periods and other unsafe characters from file name to make routing easier if self..blank? == false extension = File.extname() filename = .chomp(extension).parameterize .instance_write :file_name, filename + extension end end |
#validate_permissions ⇒ Object
If is_protected has changed, we need to make sure we are setting the appropriate permissions This means either moving the file in the filesystem or setting the appropriate ACL in S3
129 130 131 132 133 134 135 |
# File 'app/models/spud_media.rb', line 129 def if Spud::Media.config.paperclip_storage == :filesystem elsif Spud::Media.config.paperclip_storage == :s3 end end |