Skip to content

Commit

Permalink
Created example app
Browse files Browse the repository at this point in the history
  • Loading branch information
tomclose committed Aug 6, 2013
1 parent 45a2e0d commit 1cc596e
Show file tree
Hide file tree
Showing 12 changed files with 146 additions and 8 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source 'https://rubygems.org'
ruby '1.9.3'
gem 'mongoid'
gem 'sinatra'
35 changes: 35 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
GEM
remote: https://rubygems.org/
specs:
activemodel (3.2.14)
activesupport (= 3.2.14)
builder (~> 3.0.0)
activesupport (3.2.14)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
builder (3.0.4)
i18n (0.6.4)
mongoid (3.1.4)
activemodel (~> 3.2)
moped (~> 1.4)
origin (~> 1.0)
tzinfo (~> 0.3.22)
moped (1.5.0)
multi_json (1.7.8)
origin (1.1.0)
rack (1.5.2)
rack-protection (1.5.0)
rack
sinatra (1.4.3)
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (1.4.1)
tzinfo (0.3.37)

PLATFORMS
ruby

DEPENDENCIES
mongoid
sinatra
2 changes: 1 addition & 1 deletion app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ class User
## Sinatra app
## ===========

# display form
# display signup form
get '/' do

end
Expand Down
41 changes: 35 additions & 6 deletions db_examples.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,45 @@ class User
end


# How many users are there currently in the database
User.count
### Create a new user

u1 = User.new(:name => "Tom", :email => '[email protected]')

# get the properties
u1.name
u1.email

# change the name
u1.name = "Tom Close"

# check the name has changed
u1.name

# save the user to the database
u1.save

u = User.new(:name => "Tom", :email => '[email protected]')
# TODO: create another two users

u.save
### Using the database
# ====================

u1 = User.first
# To find out about what's in the database, we use methods of User

# How many users are there currently in the database
User.count

User.all {|u| puts u.name}
# find the first user
u2 = User.first

# check the properties
u2.name
u2.email

# find a specific user
u3 = User.find_by(:name => "Tom Close")

# pull all users out of the database
users = User.all

# print out their names
users.each {|u| puts u.name}
30 changes: 30 additions & 0 deletions example_app/app.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
require 'sinatra'
require 'mongoid'
require 'json'

## Mongoid setup
## =============

Mongoid.load!("mongoid.yml", :development)

class Fruit
include Mongoid::Document

field :name
end

## Sinatra app
## ===========

get '/' do
@fruits = Fruit.all
erb :index
end

post '/' do
name = params[:fruit_name]
f = Fruit.new(:name => name)
f.save
@fruits = Fruit.all
erb :index
end
8 changes: 8 additions & 0 deletions example_app/mongoid.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
development:
sessions:
default:
hosts:
- localhost:27017
database: mongo1_example
options:
raise_not_found_error: false
Empty file added example_app/views/form.erb
Empty file.
16 changes: 16 additions & 0 deletions example_app/views/index.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<h1>List of Fruits</h1>


<ul>
<% @fruits.each do |f| %>
<li><%= f.name %></li>
<% end %>
</ul>

<h3>Add a new fruit</h3>

<form method='post'>
<label>Name of fruit</label>
<input type='text' name='fruit_name'>
<input type='submit'>
</form>
17 changes: 17 additions & 0 deletions example_app/views/layout.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html>
<head>
<title>Fruit</title>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.0.0-rc1/css/bootstrap.min.css">

<link rel="stylesheet" href="/dist/css/bootstrap.min.css">
</head>
<body>
<div class='container'>

<%= yield %>

</div>
</body>
</html>
4 changes: 3 additions & 1 deletion mongoid.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ development:
default:
hosts:
- localhost:27017
database: mongo_examples
database: mongo1
options:
raise_not_found_error: false
Empty file added views/form.erb
Empty file.
Empty file added views/layout.erb
Empty file.

0 comments on commit 1cc596e

Please sign in to comment.