Class: AttachmentSan::Variant

Inherits:
Object
  • Object
show all
Defined in:
lib/attachment_san/variant.rb,
lib/attachment_san/variant.rb

Direct Known Subclasses

Original

Defined Under Namespace

Modules: ClassMethods Classes: Original

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, reflection) ⇒ Variant

Returns a new instance of Variant.



59
60
61
# File 'lib/attachment_san/variant.rb', line 59

def initialize(record, reflection)
  @record, @reflection = record, reflection
end

Instance Attribute Details

#recordObject (readonly)

Returns the value of attribute record.



57
58
59
# File 'lib/attachment_san/variant.rb', line 57

def record
  @record
end

#reflectionObject (readonly)

Returns the value of attribute reflection.



57
58
59
# File 'lib/attachment_san/variant.rb', line 57

def reflection
  @reflection
end

Instance Method Details

#base_optionsObject



63
64
65
# File 'lib/attachment_san/variant.rb', line 63

def base_options
  @record.class.attachment_san_options
end

#base_pathObject



67
68
69
# File 'lib/attachment_san/variant.rb', line 67

def base_path
  @reflection[:base_path] || base_options[:base_path]
end

#dir_pathObject



130
131
132
# File 'lib/attachment_san/variant.rb', line 130

def dir_path
  File.dirname(file_path)
end

#extensionObject



87
88
89
# File 'lib/attachment_san/variant.rb', line 87

def extension
  (ext = base_options[:extension]) == :keep_original ? @record.extension : ext
end

#file_pathObject



122
123
124
# File 'lib/attachment_san/variant.rb', line 122

def file_path
  File.join(base_path, filename)
end

#filenameObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/attachment_san/variant.rb', line 100

def filename
  unless @filename
    @filename = 
      case filename_scheme
      when :variant_name
        name.to_s
      when :keep_original
        filename_with_variant_name
      when :record_identifier
        # For now we take only the demodulized attachment class name.
        @record_class_name ||= @record.class.name.demodulize.underscore.pluralize
        "/#{@record_class_name}/#{@record.to_param}/#{name}"
      when :token
        File.join(token, filename_with_variant_name)
      else
        raise ArgumentError, "The :filename_scheme option should be one of `:token', `:filename_scheme', `:record_identifier', or `:variant_name', it currently is `#{filename_scheme.inspect}'."
      end
    @filename << ".#{extension}" unless extension.blank?
  end
  @filename
end

#filename_schemeObject



75
76
77
# File 'lib/attachment_san/variant.rb', line 75

def filename_scheme
  @reflection[:filename_scheme] || base_options[:filename_scheme]
end

#filename_with_variant_nameObject

Returns the original filename, without extension, and appends the variant name.



96
97
98
# File 'lib/attachment_san/variant.rb', line 96

def filename_with_variant_name
  "#{@record.filename_without_extension}.#{name}"
end

#mkdir!Object



134
135
136
# File 'lib/attachment_san/variant.rb', line 134

def mkdir!
  FileUtils.mkdir_p(dir_path)
end

#nameObject



83
84
85
# File 'lib/attachment_san/variant.rb', line 83

def name
  @reflection[:name]
end

#originalObject



79
80
81
# File 'lib/attachment_san/variant.rb', line 79

def original
  @record.original
end

#process!Object



138
139
140
141
# File 'lib/attachment_san/variant.rb', line 138

def process!
  mkdir!
  @reflection[:process].call(self)
end

#public_base_pathObject



71
72
73
# File 'lib/attachment_san/variant.rb', line 71

def public_base_path
  @reflection[:public_base_path] || base_options[:public_base_path]
end

#public_pathObject



126
127
128
# File 'lib/attachment_san/variant.rb', line 126

def public_path
  File.join(public_base_path, filename)
end

#tokenObject



91
92
93
# File 'lib/attachment_san/variant.rb', line 91

def token
  @record.token.scan(/.{2}/)
end