Class: DynamicPaperclip::Attachment
- Inherits:
-
Paperclip::Attachment
- Object
- Paperclip::Attachment
- DynamicPaperclip::Attachment
- Defined in:
- lib/dynamic_paperclip/attachment.rb
Instance Attribute Summary collapse
-
#dynamic_styles ⇒ Object
readonly
Returns the value of attribute dynamic_styles.
Instance Method Summary collapse
-
#delete_styles(*styles) ⇒ Object
Immediately deletes given styles without impacting existing delete queue.
- #dynamic_url(definition) ⇒ Object
-
#initialize(name, instance, options = {}) ⇒ Attachment
constructor
A new instance of Attachment.
- #process_dynamic_style(style_name) ⇒ Object
- #styles ⇒ Object
Constructor Details
#initialize(name, instance, options = {}) ⇒ Attachment
Returns a new instance of Attachment.
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
# File 'lib/dynamic_paperclip/attachment.rb', line 5 def initialize(name, instance, = {}) super @dynamic_styles = {} # Add existing dynamic styles if instance.persisted? path_with_wildcard = path('dynamic_*') # Could be nil if the attachment doesn't exist if path_with_wildcard style_position = path_with_wildcard.index('dynamic_*') Dir.glob(path_with_wildcard) do |file| style_name = file[style_position..-1].split('/').first # In the event that the style name is used as the filename, # we want to remove the extension for our style name add_dynamic_style! File.basename(style_name, File.extname(style_name)) end end end end |
Instance Attribute Details
#dynamic_styles ⇒ Object (readonly)
Returns the value of attribute dynamic_styles.
3 4 5 |
# File 'lib/dynamic_paperclip/attachment.rb', line 3 def dynamic_styles @dynamic_styles end |
Instance Method Details
#delete_styles(*styles) ⇒ Object
Immediately deletes given styles without impacting existing delete queue
51 52 53 54 55 56 57 58 59 |
# File 'lib/dynamic_paperclip/attachment.rb', line 51 def delete_styles(*styles) old_delete_queue = @queued_for_delete @queued_for_delete = [] queue_some_for_delete *styles flush_deletes @queued_for_delete = old_delete_queue end |
#dynamic_url(definition) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/dynamic_paperclip/attachment.rb', line 38 def dynamic_url(definition) raise DynamicPaperclip::Errors::SecretNotSet, "No secret has been configured. Please run the dynamic_paperclip:install generator." unless DynamicPaperclip.config.secret.present? style_name = StyleNaming.dynamic_style_name_from_definition(definition) url = url(style_name) delimiter_char = url.match(/\?/) ? '&' : '?' "#{url}#{delimiter_char}s=#{UrlSecurity.generate_hash(style_name)}" end |
#process_dynamic_style(style_name) ⇒ Object
33 34 35 36 |
# File 'lib/dynamic_paperclip/attachment.rb', line 33 def process_dynamic_style(style_name) add_dynamic_style! style_name reprocess! style_name end |
#styles ⇒ Object
29 30 31 |
# File 'lib/dynamic_paperclip/attachment.rb', line 29 def styles super.merge dynamic_styles end |