-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathemail_cms.rb
More file actions
88 lines (73 loc) · 1.59 KB
/
email_cms.rb
File metadata and controls
88 lines (73 loc) · 1.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
require 'sinatra'
require 'haml'
require 'mongoid'
require 'sinatra/mongoid'
require 'mail'
get '/' do
@posts = Post.all
haml :posts
end
get '/:id' do
@post = Post.first(:conditions => {:id => params[:id]})
pass if @post.nil?
haml :post
end
get '/delete' do
Post.destroy_all
'Deleted all posts'
end
post '/incoming_message/' do
message = Mail.new(params[:message])
if message.multipart?
attachments = []
body = ""
message.parts.each do |part|
if part.content_type =~ /^text\/plain/i
body << part.body.decoded
elsif part.attachment?
@grid ||= Mongo::Grid::new(Mongoid.database)
file = @grid.put(part.body.decoded)
attachments << Attachment.new(:grid_id => file)
end
end
@post = Post.create!(:title => message.subject, :body => body, :attachments => attachments)
else
@post = Post.create!(:title => message.subject, :body => message.body.decoded)
end
haml :post
end
get '/images/:id' do
grid = Mongo::Grid::new(Mongoid.database)
grid.get(BSON::ObjectID.from_string(params[:id])).read
end
class Post
include Mongoid::Document
field :title
field :body
embeds_many :attachments
end
class Attachment
include Mongoid::Document
field :grid_id
end
__END__
@@layout
%html
%head
%body
=yield
@@posts
%h1 Posts
#posts
- for post in @posts
#post
%h2
%a{:href => "/#{post.id}"}= post.title
#content
= post.body
- for attachment in post.attachments do
%img{:src => "/images/#{attachment.grid_id}"}
@@post
%h1= @post.title
#content
= @post.body