Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

add placholder for Mongo models page; change formatting of Databases>…

…Mongo page; add jQuery for content refreshing (instead of reloading the page every time)
  • Loading branch information...
commit f1139a08811352a311f6d1d00317a0615186f3e9 1 parent dd7cc70
@jacaetevha jacaetevha authored
Showing with 70 additions and 20 deletions.
  1. +2 −0  .gitignore
  2. +27 −4 app.rb
  3. +40 −16 databases/mongo.md
  4. +1 −0  models/mongo.md
View
2  .gitignore
@@ -3,3 +3,5 @@ Gemfile.lock
.rvmrc
config/database.yml
+bin
+.bundle
View
31 app.rb
@@ -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
@@ -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">
@@ -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 %>
View
56 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'
@@ -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
@@ -68,9 +87,11 @@ 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
@@ -78,6 +99,9 @@ your database (see more documentation at [MongoDB's tutorial][rubydrivertutorial
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
+[mongo_models]: /p/models/mongo.md
View
1  models/mongo.md
@@ -0,0 +1 @@
+This page is under construction.
Please sign in to comment.
Something went wrong with that request. Please try again.