Module: MongoStash

Defined in:
lib/mongo_stash.rb

Defined Under Namespace

Modules: ClassMethods

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.classesObject

Returns the value of attribute classes.



114
115
116
# File 'lib/mongo_stash.rb', line 114

def classes
  @classes
end

Class Method Details

.all_after_stashObject



117
118
119
120
121
# File 'lib/mongo_stash.rb', line 117

def all_after_stash
  MongoStash.classes.each do |m|
	  m.all_after_stash
	end
end

.all_fix_dots_in_keys(for_real = false) ⇒ Object



147
148
149
150
151
# File 'lib/mongo_stash.rb', line 147

def all_fix_dots_in_keys(for_real=false)
  MongoStash.classes.each do |c|
    MongoStash.fix_dots_in_keys(c, for_real)
  end
end

.fix_dots_in_keys(c, for_real = false) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/mongo_stash.rb', line 123

def fix_dots_in_keys(c, for_real=false)
  puts "\n#{c}" unless for_real
  selection = c.schema.select{|k,v| v[:type]==:attachment }
  img_keys = selection.respond_to?(:keys) ? selection.keys : selection.map{|a|a[0]}
  c.find({}, {:fields=>img_keys}).each do |e|
    old_hash = e.doc.select{|k,v| img_keys.include?(k) }
    fixed_hash = Marshal.load(Marshal.dump(old_hash))
    img_keys.each do |k|
      (fixed_hash[k]||{}).keys.each do |style|
        fixed_hash[k][style.tr('.','_')] = fixed_hash[k].delete(style)
      end
    end
    next if old_hash==fixed_hash
    
    if for_real
      c.collection.update({'_id'=>e.id}, {'$set'=>fixed_hash})
    else
      puts old_hash.inspect
      puts fixed_hash.inspect
    end
    
  end
end

.included(base) ⇒ Object



7
8
9
10
11
# File 'lib/mongo_stash.rb', line 7

def self.included(base)
  MongoStash.classes << base
  base.extend(ClassMethods)
	base.gridfs = GRID
end

Instance Method Details

#after_deleteObject



59
60
61
62
63
64
# File 'lib/mongo_stash.rb', line 59

def after_delete
  super
   model.schema.each do |k,v|
	  delete_files_for(k) if v[:type]==:attachment
	end
end

#after_saveObject



66
67
68
69
70
71
72
73
# File 'lib/mongo_stash.rb', line 66

def after_save
  super
	unless @temp_attachments.nil?
		@temp_attachments.each do |k,v|
			after_stash(k)
		end
	end
end

#after_stash(col) ⇒ Object



75
# File 'lib/mongo_stash.rb', line 75

def after_stash(col); end

#build_image_tag(col = 'image', style = 'original', html_attributes = {}) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
# File 'lib/mongo_stash.rb', line 25

def build_image_tag(col='image', style='original', html_attributes={})
  return '' if @doc[col].nil?||@doc[col][style].nil?
  title_field, alt_field = col+'_tooltip', col+'_alternative_text'
	title = @doc[title_field] if model.schema.keys.include?(title_field)
	alt = @doc[alt_field] if model.schema.keys.include?(alt_field)
	html_attributes = {:src => "/gridfs/#{@doc[col][style]}", :title => title, :alt => alt}.update(html_attributes)
	html_attributes = html_attributes.map do |k,v|
	  %{#{k}="#{model.html_escape(v.to_s)}"}
	end.join(' ')
  "<img #{html_attributes} />"
end

#convert(col, convert_steps, style) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# File 'lib/mongo_stash.rb', line 77

def convert(col, convert_steps, style)
  return if @doc[col].nil?
  if @temp_attachments.nil? || @temp_attachments[col].nil?
    f = model.gridfs.get(@doc[col]['original'])
    return unless f.content_type[/^image\//]
    src = Tempfile.new('MongoStash_src')
    src.binmode
    src.write(f.read(4096)) until f.eof?
    src.close
    @temp_attachments ||= {}
    @temp_attachments[col] ||= {}
    @temp_attachments[col][:tempfile] = src
    @temp_attachments[col][:type] = f.content_type
	else
	  return unless @temp_attachments[col][:type][/^image\//]
	  src = @temp_attachments[col][:tempfile]
	end
  model.gridfs.delete(@doc[col][style]) unless @doc[col][style].nil?
	ext = style[/[a-zA-Z]+$/].insert(0,'.')
   content_type = Rack::Mime.mime_type(ext) 
   unless content_type[/^image\//]
     ext = '.jpg'
     content_type = 'image/jpeg'
   end
	dest = Tempfile.new(['MongoStash_dest', ext])
	dest.binmode
	dest.close
  system "convert \"#{src.path}\" #{convert_steps} \"#{dest.path}\""
	filename = "#{model.name}/#{self.id}/#{style}"
	attachment_id = model.gridfs.put(dest.open, {:filename=>filename, :content_type=>content_type})
   @doc[col] = @doc[col].update({style=>attachment_id})
	model.collection.update({'_id'=>@doc['_id']}, @doc)
	#src.close!
	dest.close!
end

#delete_files_for(col) ⇒ Object



50
51
52
53
54
55
56
57
# File 'lib/mongo_stash.rb', line 50

def delete_files_for(col)
  obj = (@old_doc||@doc)[col]
	if obj.respond_to?(:each)
	  obj.each do |k,v|
		  model.gridfs.delete(v)
		end
	end
end

#fix_type_attachment(k, v) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/mongo_stash.rb', line 37

def fix_type_attachment(k,v)
  if v=='nil'
     delete_files_for(k) unless new?
		@doc[k] = nil
  elsif v.is_a?(Hash)&&v.key?(:tempfile)
	  delete_files_for(k) unless new?
		@temp_attachments ||= {}
		@temp_attachments[k] = v
     attachment_id = model.gridfs.put(v[:tempfile], {:filename=>v[:filename], :content_type=>v[:type]})
		@doc[k] = {'original'=>attachment_id}
	end
end