From 1cc596ec7367f1eb94977da80f4be5acaabf050c Mon Sep 17 00:00:00 2001 From: Tom Close Date: Tue, 6 Aug 2013 16:34:57 +0100 Subject: [PATCH] Created example app --- Gemfile | 1 + Gemfile.lock | 35 ++++++++++++++++++++++++++++++ app.rb | 2 +- db_examples.rb | 41 ++++++++++++++++++++++++++++++------ example_app/app.rb | 30 ++++++++++++++++++++++++++ example_app/mongoid.yml | 8 +++++++ example_app/views/form.erb | 0 example_app/views/index.erb | 16 ++++++++++++++ example_app/views/layout.erb | 17 +++++++++++++++ mongoid.yml | 4 +++- views/form.erb | 0 views/layout.erb | 0 12 files changed, 146 insertions(+), 8 deletions(-) create mode 100644 Gemfile.lock create mode 100644 example_app/app.rb create mode 100644 example_app/mongoid.yml create mode 100644 example_app/views/form.erb create mode 100644 example_app/views/index.erb create mode 100644 example_app/views/layout.erb create mode 100644 views/form.erb create mode 100644 views/layout.erb diff --git a/Gemfile b/Gemfile index ed07d3a..ea9fdff 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org' ruby '1.9.3' gem 'mongoid' +gem 'sinatra' diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..ba84b57 --- /dev/null +++ b/Gemfile.lock @@ -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 diff --git a/app.rb b/app.rb index ae0860a..394b10f 100644 --- a/app.rb +++ b/app.rb @@ -17,7 +17,7 @@ class User ## Sinatra app ## =========== -# display form +# display signup form get '/' do end diff --git a/db_examples.rb b/db_examples.rb index 0372fdf..cc0461e 100644 --- a/db_examples.rb +++ b/db_examples.rb @@ -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 => 'tom@example.com') + +# 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 => 'tom@example.com') +# 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} \ No newline at end of file +# 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} \ No newline at end of file diff --git a/example_app/app.rb b/example_app/app.rb new file mode 100644 index 0000000..c596ceb --- /dev/null +++ b/example_app/app.rb @@ -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 \ No newline at end of file diff --git a/example_app/mongoid.yml b/example_app/mongoid.yml new file mode 100644 index 0000000..2634a5e --- /dev/null +++ b/example_app/mongoid.yml @@ -0,0 +1,8 @@ +development: + sessions: + default: + hosts: + - localhost:27017 + database: mongo1_example + options: + raise_not_found_error: false \ No newline at end of file diff --git a/example_app/views/form.erb b/example_app/views/form.erb new file mode 100644 index 0000000..e69de29 diff --git a/example_app/views/index.erb b/example_app/views/index.erb new file mode 100644 index 0000000..9a3ace2 --- /dev/null +++ b/example_app/views/index.erb @@ -0,0 +1,16 @@ +

List of Fruits

+ + + + +

Add a new fruit

+ +
+ + + +
\ No newline at end of file diff --git a/example_app/views/layout.erb b/example_app/views/layout.erb new file mode 100644 index 0000000..5382859 --- /dev/null +++ b/example_app/views/layout.erb @@ -0,0 +1,17 @@ + + + + Fruit + + + + + + +
+ + <%= yield %> + +
+ + \ No newline at end of file diff --git a/mongoid.yml b/mongoid.yml index 5544c65..f847c4f 100644 --- a/mongoid.yml +++ b/mongoid.yml @@ -3,4 +3,6 @@ development: default: hosts: - localhost:27017 - database: mongo_examples \ No newline at end of file + database: mongo1 + options: + raise_not_found_error: false \ No newline at end of file diff --git a/views/form.erb b/views/form.erb new file mode 100644 index 0000000..e69de29 diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 0000000..e69de29