Class: Griddle::Attachment

Inherits:
Object
  • Object
show all
Includes:
Mongo
Defined in:
lib/griddle/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Attachment

Returns a new instance of Attachment.



33
34
35
36
37
38
39
# File 'lib/griddle/attachment.rb', line 33

def initialize(attributes = {})
  @grid = GridFileSystem.new(Griddle.database)
  @attributes = attributes.symbolize_keys
  initialize_processor
  initialize_styles
  create_attachments_for_styles
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

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



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/griddle/attachment.rb', line 65

def method_missing(method, *args, &block)
  key = method.to_s.gsub(/\=$/, '').to_sym
  if self.class.valid_attributes.include?(key)
    if key != method
      @attributes[key] = args[0]
    else
      @attributes[key]
    end
  else
    super
  end
end

Instance Attribute Details

#attributesObject

belongs_to :owner, :polymorphic => true



31
32
33
# File 'lib/griddle/attachment.rb', line 31

def attributes
  @attributes
end

Class Method Details

.attachment_for(options) ⇒ Object



6
7
8
9
10
11
12
# File 'lib/griddle/attachment.rb', line 6

def self.attachment_for(options)
  options = self.clean_options(options)
  options_for_search = {:name => options[:name], :owner_type => options[:owner_type], :owner_id => options[:owner_id]}
  record = collection.find_one(options_for_search)
  return new(record) unless record.nil?
  return new(options)
end

.collectionObject



14
15
16
# File 'lib/griddle/attachment.rb', line 14

def self.collection
  @collection ||= Griddle.database.collection('griddle.attachments')
end

.for(name, owner, options = {}) ⇒ Object



18
19
20
21
22
23
24
# File 'lib/griddle/attachment.rb', line 18

def self.for(name, owner, options = {})
  attachment_for(options.merge({
    :name => name,
    :owner_type => owner.class,
    :owner_id => owner.id
  }))
end

.valid_attributesObject



26
27
28
# File 'lib/griddle/attachment.rb', line 26

def self.valid_attributes
  [:name, :owner_id, :owner_type, :file_name, :file_size, :content_type, :styles, :options]
end

Instance Method Details

#assign(uploaded_file) ⇒ Object



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

def assign(uploaded_file)
  if valid_assignment?(uploaded_file)
    self.file = uploaded_file
    self.dirty!
  end
end

#collectionObject



56
57
58
# File 'lib/griddle/attachment.rb', line 56

def collection
  @collection ||= self.class.collection
end

#destroyObject



60
61
62
63
# File 'lib/griddle/attachment.rb', line 60

def destroy
  destroy_file
  collection.remove({:name => name, :owner_type => owner_type, :owner_id => owner_id})
end

#destroy_fileObject



78
79
80
81
# File 'lib/griddle/attachment.rb', line 78

def destroy_file
  @grid.delete(grid_key)
  destroy_styles
end

#dirty?Boolean

Returns:

  • (Boolean)


83
84
85
86
# File 'lib/griddle/attachment.rb', line 83

def dirty?
  @dirty ||= false
  @dirty
end

#exists?Boolean

Returns:

  • (Boolean)


88
89
90
# File 'lib/griddle/attachment.rb', line 88

def exists?
  Griddle.database['fs.files'].find({'filename' => self.grid_key}).count > 0
end

#fileObject



96
97
98
# File 'lib/griddle/attachment.rb', line 96

def file
  @grid.open(grid_key, 'r') if exists?
end

#file=(new_file) ⇒ Object



100
101
102
103
104
105
106
# File 'lib/griddle/attachment.rb', line 100

def file=(new_file)
  filename = clean_filename(new_file.respond_to?(:original_filename) ? new_file.original_filename : File.basename(new_file.path))
  self.file_name = filename
  self.file_size = File.size(new_file.path)
  self.content_type = new_file.content_type
  @tmp_file = new_file
end

#grid_keyObject



92
93
94
# File 'lib/griddle/attachment.rb', line 92

def grid_key
  "#{owner_type.tableize}/#{owner_id}/#{name}/#{self.file_name}".downcase
end

#name=(name) ⇒ Object



108
109
110
# File 'lib/griddle/attachment.rb', line 108

def name= name
  @attributes[:name] = name.to_sym
end

#owner_id=(id) ⇒ Object



112
113
114
# File 'lib/griddle/attachment.rb', line 112

def owner_id= id
  @attributes[:owner_id] = id.to_s
end

#owner_type=(str) ⇒ Object



116
117
118
# File 'lib/griddle/attachment.rb', line 116

def owner_type= str
  @attributes[:owner_type] = str.to_s
end

#processorObject



120
121
122
# File 'lib/griddle/attachment.rb', line 120

def processor
  @processor
end

#processor=(processor) ⇒ Object



124
125
126
127
# File 'lib/griddle/attachment.rb', line 124

def processor= processor
  @attributes[:processor] = processor
  initialize_processor
end

#saveObject



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

def save
  if valid?
    destroy
    save_file
    collection.insert(valid_attributes(@attributes).stringify_keys)
  end
end

#stylesObject



137
138
139
# File 'lib/griddle/attachment.rb', line 137

def styles
  @styles
end

#styles=(styles) ⇒ Object



141
142
143
144
# File 'lib/griddle/attachment.rb', line 141

def styles= styles
  @attributes[:styles] = styles
  initialize_styles
end

#valid?Boolean

Returns:

  • (Boolean)


146
147
148
# File 'lib/griddle/attachment.rb', line 146

def valid?
  dirty? && valid_assignment?(@tmp_file)
end

#valid_attributes(attributes) ⇒ Object



150
151
152
# File 'lib/griddle/attachment.rb', line 150

def valid_attributes(attributes)
  Hash[*attributes.select{|key, value| self.class.valid_attributes.include?(key) }.flatten]
end