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
# 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_*')
    style_position = path_with_wildcard.index('dynamic_*')

    Dir.glob(path_with_wildcard) do |file|
      add_dynamic_style! file[style_position..-1].split('/').first
    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

#dynamic_url(definition) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/dynamic_paperclip/attachment.rb', line 32

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)}".html_safe
end

#process_dynamic_style(definition) ⇒ Object



25
26
27
28
29
30
# File 'lib/dynamic_paperclip/attachment.rb', line 25

def process_dynamic_style(definition)
  style_name = StyleNaming.dynamic_style_name_from_definition(definition)

  add_dynamic_style! style_name
  reprocess! style_name
end

#stylesObject



21
22
23
# File 'lib/dynamic_paperclip/attachment.rb', line 21

def styles
  super.merge dynamic_styles
end