Class: Resizor::AttachedResizorAsset

Inherits:
ResizorAsset show all
Defined in:
lib/resizor/asset.rb

Instance Attribute Summary collapse

Attributes inherited from ResizorAsset

#height, #id, #mime_type, #name, #path, #size, #width

Instance Method Summary collapse

Methods inherited from ResizorAsset

#resize_token_for, #save_to_resizor, #url

Constructor Details

#initialize(attachment_name, instance, options = {}) ⇒ AttachedResizorAsset

Returns a new instance of AttachedResizorAsset.



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/resizor/asset.rb', line 75

def initialize(attachment_name, instance, options = {})
  @attachment_name = attachment_name
  @instance = instance
  @options = options
  @id = instance_read("resizor_id")
  @name = instance_read("name")
  @mime_type = instance_read("mime_type")
  @size = instance_read("size")
  @width = instance_read("width")
  @height = instance_read("height")
end

Instance Attribute Details

#attachment_nameObject

Returns the value of attribute attachment_name.



73
74
75
# File 'lib/resizor/asset.rb', line 73

def attachment_name
  @attachment_name
end

#fileObject

Returns the value of attribute file.



73
74
75
# File 'lib/resizor/asset.rb', line 73

def file
  @file
end

#instanceObject

Returns the value of attribute instance.



73
74
75
# File 'lib/resizor/asset.rb', line 73

def instance
  @instance
end

Instance Method Details

#assign(in_file) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/resizor/asset.rb', line 87

def assign in_file
  if in_file.is_a?(Resizor::ResizorAsset)
    @id = in_file.id
    @name = in_file.name
    @mime_type = in_file.mime_type
    @size = in_file.size
    @width = in_file.width
    @height = in_file.height
  else
    @file = in_file
  end
end

#clearObject



137
138
139
140
141
142
143
144
# File 'lib/resizor/asset.rb', line 137

def clear
  @id = nil
  @name = nil
  @mime_type = nil
  @size = nil
  @width = nil
  @height = nil
end

#deleteObject



125
126
127
# File 'lib/resizor/asset.rb', line 125

def delete
  destroy
end

#destroyObject



129
130
131
132
133
134
135
# File 'lib/resizor/asset.rb', line 129

def destroy
  if ret = super
    clear
    instance.send(:save)
  end
  ret
end

#saveObject



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

def save
  if file
    @path = File.join(Dir.tmpdir, file.original_filename)
    if file.is_a?(Tempfile) || file.respond_to?(:path)
      FileUtils.move(file.path, @path)
    else
      File.open(@path, 'wb') { |f| f.write(file.read) }
    end
    if save_to_resizor
      File.unlink(@path)
      @file = nil
    else
      instance.errors[attachment_name.to_sym] << "can't be saved"
      return false
    end
  end
  instance_write(:resizor_id, @id)
  instance_write(:name, @name)
  instance_write(:mime_type, @mime_type)
  instance_write(:size, @size)
  instance_write(:width, @width)
  instance_write(:height, @height)
  return true
end