Class: DynamicPaperclip::Attachment

Inherits:
Paperclip::Attachment
  • Object
show all
Defined in:
lib/dynamic_paperclip/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options = {})
  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_stylesObject (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

#stylesObject



29
30
31
# File 'lib/dynamic_paperclip/attachment.rb', line 29

def styles
  super.merge dynamic_styles
end