Module: Attachs::Attachment::Attributes

Extended by:
ActiveSupport::Concern
Included in:
Attachs::Attachment
Defined in:
lib/attachs/attachment/attributes.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



87
88
89
90
91
92
93
# File 'lib/attachs/attachment/attributes.rb', line 87

def method_missing(name, *args, &block)
  if attributes.has_key?(name)
    attributes[name]
  else
    super
  end
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



6
7
8
# File 'lib/attachs/attachment/attributes.rb', line 6

def attributes
  @attributes
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/attachs/attachment/attributes.rb', line 6

def options
  @options
end

#original_attributesObject (readonly)

Returns the value of attribute original_attributes.



6
7
8
# File 'lib/attachs/attachment/attributes.rb', line 6

def original_attributes
  @original_attributes
end

#recordObject (readonly)

Returns the value of attribute record.



6
7
8
# File 'lib/attachs/attachment/attributes.rb', line 6

def record
  @record
end

#record_attributeObject (readonly)

Returns the value of attribute record_attribute.



6
7
8
# File 'lib/attachs/attachment/attributes.rb', line 6

def record_attribute
  @record_attribute
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/attachs/attachment/attributes.rb', line 6

def source
  @source
end

#valueObject (readonly)

Returns the value of attribute value.



6
7
8
# File 'lib/attachs/attachment/attributes.rb', line 6

def value
  @value
end

Instance Method Details

#basenameObject



21
22
23
24
25
# File 'lib/attachs/attachment/attributes.rb', line 21

def basename
  if filename
    File.basename filename, extension
  end
end

#blank?Boolean

Returns:

  • (Boolean)


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

def blank?
  !present?
end

#changed?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/attachs/attachment/attributes.rb', line 45

def changed?
  @original_attributes != @attributes
end

#extensionObject



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

def extension
  if filename
    File.extname filename
  end
end

#initialize(record, record_attribute, options = {}, attributes = {}) ⇒ Object



8
9
10
11
12
13
# File 'lib/attachs/attachment/attributes.rb', line 8

def initialize(record, record_attribute, options={}, attributes={})
  @record = record
  @record_attribute = record_attribute
  @options = options
  @original_attributes = @attributes = normalize_attributes(attributes)
end

#persisted?Boolean

Returns:

  • (Boolean)


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

def persisted?
  record.persisted? && paths.any? && uploaded_at
end

#positionObject



59
60
61
62
63
# File 'lib/attachs/attachment/attributes.rb', line 59

def position
  if options[:multiple]
    attributes[:position]
  end
end

#position=(value) ⇒ Object



65
66
67
68
69
70
# File 'lib/attachs/attachment/attributes.rb', line 65

def position=(value)
  if options[:multiple]
    attributes[:position] = value
    write_record
  end
end

#present?Boolean

Returns:

  • (Boolean)


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

def present?
  filename.present? && content_type.present? && size.present?
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


83
84
85
# File 'lib/attachs/attachment/attributes.rb', line 83

def respond_to_missing?(name, include_private=false)
  attributes.has_key?(name) || super
end

#stylesObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/attachs/attachment/attributes.rb', line 72

def styles
  case value = options[:styles]
  when Proc
    value.call(record) || {}
  when Hash
    value
  else
    {}
  end
end

#typeObject



49
50
51
52
53
54
55
56
57
# File 'lib/attachs/attachment/attributes.rb', line 49

def type
  if content_type
    if content_type.starts_with?('image/')
      'image'
    else
      'file'
    end
  end
end