Class: Attachs::Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/attachs/attachment.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, attribute, options, source = false) ⇒ Attachment

Returns a new instance of Attachment.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/attachs/attachment.rb', line 8

def initialize(record, attribute, options, source=false)
  @record = record
  @attribute = attribute
  @options = options
  case source
  when nil
    %w(filename content_type size updated_at).each do |name|
      record.send "#{attribute}_#{name}=", nil
    end
  when false
    %w(filename content_type size updated_at).each do |name|
      instance_variable_set :"@#{name}", record.send("#{attribute}_#{name}")
    end
  else
    load source
    %w(filename content_type size updated_at).each do |name|
      record.send "#{attribute}_#{name}=", send(name)
    end
  end
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def attribute
  @attribute
end

#content_typeObject (readonly)

Returns the value of attribute content_type.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def content_type
  @content_type
end

#filenameObject (readonly)

Returns the value of attribute filename.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def filename
  @filename
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def options
  @options
end

#recordObject (readonly)

Returns the value of attribute record.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def record
  @record
end

#sizeObject (readonly)

Returns the value of attribute size.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def size
  @size
end

#updated_atObject (readonly)

Returns the value of attribute updated_at.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def updated_at
  @updated_at
end

#uploadObject (readonly)

Returns the value of attribute upload.



4
5
6
# File 'lib/attachs/attachment.rb', line 4

def upload
  @upload
end

Instance Method Details

#blank?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/attachs/attachment.rb', line 68

def blank?
  !exists?
end

#default?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/attachs/attachment.rb', line 72

def default?
  !options[:default_path].nil?
end

#exists?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/attachs/attachment.rb', line 64

def exists?
  filename and content_type and size and updated_at
end

#private?Boolean

Returns:

  • (Boolean)


37
38
39
# File 'lib/attachs/attachment.rb', line 37

def private?
  @private ||= options[:private] == true
end

#processed?Boolean

Returns:

  • (Boolean)


55
56
57
58
59
60
61
62
# File 'lib/attachs/attachment.rb', line 55

def processed?
  exists? and record.persisted? and !(
    record.send("#{attribute}_filename_changed?") or
    record.send("#{attribute}_content_type_changed?") or
    record.send("#{attribute}_size_changed?") or
    record.send("#{attribute}_updated_at_changed?")
  )
end

#processorsObject



41
42
43
44
45
# File 'lib/attachs/attachment.rb', line 41

def processors
  @processors ||= (options[:processors] || Attachs.config.default_processors).map do |processor|
    "Attachs::Processors::#{processor.to_s.classify}".constantize
  end
end

#public?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/attachs/attachment.rb', line 47

def public?
  !private?
end

#stylesObject



33
34
35
# File 'lib/attachs/attachment.rb', line 33

def styles
  @styles ||= ((options[:styles] || []) | Attachs.config.global_styles)
end

#upload?Boolean

Returns:

  • (Boolean)


51
52
53
# File 'lib/attachs/attachment.rb', line 51

def upload?
  !upload.nil?
end

#uploaded!Object



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

def uploaded!
  @upload = nil
end

#url?Boolean

Returns:

  • (Boolean)


76
77
78
# File 'lib/attachs/attachment.rb', line 76

def url?
  public? and (default? or processed?)
end