Skip to content

Commit

Permalink
add placholder for Mongo models page; change formatting of Databases>…
Browse files Browse the repository at this point in the history
…Mongo page; add jQuery for content refreshing (instead of reloading the page every time)
  • Loading branch information
jacaetevha committed Feb 26, 2011
1 parent dd7cc70 commit f1139a0
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 20 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Expand Up @@ -3,3 +3,5 @@ Gemfile.lock
.rvmrc
config/database.yml

bin
.bundle
31 changes: 27 additions & 4 deletions app.rb
Expand Up @@ -19,13 +19,13 @@
get '/p/:topic' do
readme = File.new "#{params[:topic]}/README.md"
output = RDiscount.new(readme.read).to_html
erb output
erb output, :layout => false
end

get '/p/:topic/:article' do
post = File.new "#{params[:topic]}/#{params[:article]}"
output = RDiscount.new(post.read).to_html
erb output
erb output, :layout => false
end

get '/style.css' do
Expand All @@ -42,6 +42,20 @@
<meta http-equiv="X-UA-Compatible" content="chrome=1">
<title>Sinatra Book Contrib</title>
<link rel="stylesheet" type="text/css" href="/style.css" />
<script
type='text/javascript'
src='https://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js'>
</script>
<script type='text/javascript'>
function load_article(url) {
jQuery.ajax({
url: url,
success: function(response) {
$('#content').html(response);
}
});
}
</script>
</head>
<body>
<div id="header">
Expand All @@ -53,10 +67,19 @@
<div id="menu">
<ul>
<% @menu.each_key do |me| %>
<li><a href="/p/<%= "#{me}" %>"><%= me %></a>
<li>
<a href='#<%= "#{me}" %>' onclick="load_article('/p/<%= "#{me}" %>');">
<%= me %>
</a>
<ul>
<% @menu[:"#{me}"].each do |mi| %>
<li><a href="/p/<%= "#{me}/#{mi}" %>"><%= mi.gsub('_', ' ').gsub('.md', '') %></a></li>
<li>
<a
href='#<%= "#{me}_#{mi}" %>'
onclick="load_article('/p/<%= "#{me}/#{mi}" %>');">
<%= mi.gsub('_', ' ').gsub('.md', '') %>
</a>
</li>
<% end %>
</ul></li>
<% end %>
Expand Down
56 changes: 40 additions & 16 deletions databases/mongo.md
@@ -1,10 +1,15 @@
Mongo
-----

[Mongo][mongo] is a document-oriented database. It is easy to connect your
applications to a Mongo database without the use of an Object Relational
Mapper (ORM). The first step is to create a connection to your Mongo instance.
You can do this in your _configure_ block:
[Mongo][mongo] is a document-oriented database. Though Object Relational
Mappers (ORMs) are often used to connect to databases, you will see here
that it is very easy to connect your applications to a Mongo database
without the use of an ORM, though several exist. See the
[Mongo models][mongo_models] page for a discussion of some of the ORMs
available.

The first step is in connecting your application to an instance of Mongo is
to create a connection. You can do this in your _configure_ block:

require 'rubygems'
require 'sinatra'
Expand All @@ -23,40 +28,54 @@ your Mongo instance.
settings.mongo_db.collection_names
end

Using the Ruby driver you can find, insert, update, or delete from
your database (see more documentation at [MongoDB's tutorial][rubydrivertutorial]):

helpers do
# a helper method to turn a string ID
# representation into a BSON::ObjectId
def object_id val
BSON::ObjectId.from_string(val)
end
def document_by_id id
id = object_id(id) if String === id
settings.mongo_db['test'].
find_one(:_id => id).to_json
end
end


###Finding Records###

# list all documents in the test collection
get '/list_documents/?' do
get '/documents/?' do
content_type :json
settings.mongo_db['test'].find.to_a.to_json
end

# find a document by its ID
get '/document/:id/?' do
content_type :json
document_by_id(params[:id]).to_json
end

###Inserting Records###

# insert a new document from the request parameters,
# then return the full document
post '/new_document/?' do
content_type :json
new_id = settings.mongo_db['test'].insert params
settings.mongo_db['test'].
find_one(:_id => new_id).
to_json
document_by_id(new_id).to_json
end


###Updating Records###

# update the document specified by :id, setting its
# contents to params, then return the full document
put '/update/:id/?' do
content_type :json
id = object_id(params[:id])
settings.mongo_db['test'].update(:_id => id, params)
settings.mongo_db['test'].find_one(:_id => id).to_json
document_by_id(id).to_json
end

# update the document specified by :id, setting just its
Expand All @@ -68,16 +87,21 @@ your database (see more documentation at [MongoDB's tutorial][rubydrivertutorial
name = params[:name]
settings.mongo_db['test'].
update(:_id => id, {"$set" => {:name => name}})
settings.mongo_db['test'].find_one(:_id => id).to_json
document_by_id(id).to_json
end

###Deleting Records###

# delete the specified document and return success
delete '/remove/:id' do
content_type :json
settings.mongo_db['test'].
remove(:_id => object_id(params[:id]))
{:success => true}.to_json
end


For more information on using the Ruby driver without an ORM take a look at [MongoDB's tutorial][rubydrivertutorial].

[mongo]: http://www.mongodb.org/
[rubydrivertutorial]: http://api.mongodb.org/ruby/current/file.TUTORIAL.html
[rubydrivertutorial]: http://api.mongodb.org/ruby/current/file.TUTORIAL.html
[mongo_models]: /p/models/mongo.md
1 change: 1 addition & 0 deletions models/mongo.md
@@ -0,0 +1 @@
This page is under construction.

0 comments on commit f1139a0

Please sign in to comment.