Permalink
Browse files

add basic CRUD action

  • Loading branch information...
1 parent 9f85789 commit 5a5f9fd9b6b810ceab0655ef4bbde0eb9ccddc2f @netzfisch committed Feb 11, 2014
Showing with 126 additions and 0 deletions.
  1. +6 −0 Gemfile
  2. +29 −0 Gemfile.lock
  3. +11 −0 config/database.yml
  4. +21 −0 models/participant.rb
  5. +8 −0 spec/yasapp_spec.rb
  6. +9 −0 views/index.haml
  7. +19 −0 views/participant.slim
  8. +23 −0 yasapp.rb
View
6 Gemfile
@@ -2,11 +2,13 @@ source "https://rubygems.org"
ruby '1.9.3'
gem 'sinatra'
+gem 'sinatra-activerecord'
gem 'haml'
gem 'slim'
group :development, :test do
gem 'sinatra-contrib' # provides sinatra/reloader
+ gem 'sqlite3' # provides database connection
end
group :test do
@@ -15,3 +17,7 @@ group :test do
gem 'autotest-inotify' # monitors file alterations
gem 'autotest-growl' # shows visual notifications
end
+
+group :production do
+ gem 'pg' # provides database connection
+end
View
29 Gemfile.lock
@@ -2,6 +2,23 @@ GEM
remote: https://rubygems.org/
specs:
ZenTest (4.9.5)
+ activemodel (4.0.2)
+ activesupport (= 4.0.2)
+ builder (~> 3.1.0)
+ activerecord (4.0.2)
+ activemodel (= 4.0.2)
+ activerecord-deprecated_finders (~> 1.0.2)
+ activesupport (= 4.0.2)
+ arel (~> 4.0.0)
+ activerecord-deprecated_finders (1.0.3)
+ activesupport (4.0.2)
+ i18n (~> 0.6, >= 0.6.4)
+ minitest (~> 4.2)
+ multi_json (~> 1.3)
+ thread_safe (~> 0.1)
+ tzinfo (~> 0.3.37)
+ arel (4.0.2)
+ atomic (1.1.14)
autotest (4.4.6)
ZenTest (>= 4.4.1)
autotest-growl (0.2.16)
@@ -10,10 +27,13 @@ GEM
rb-inotify
autotest-standalone (4.5.11)
backports (3.5.0)
+ builder (3.1.4)
diff-lcs (1.2.5)
ffi (1.9.3)
haml (4.0.3)
tilt
+ i18n (0.6.9)
+ minitest (4.7.5)
multi_json (1.8.4)
rack (1.5.2)
rack-protection (1.5.2)
@@ -34,6 +54,9 @@ GEM
rack (~> 1.4)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
+ sinatra-activerecord (1.3.0)
+ activerecord (>= 3.0)
+ sinatra (~> 1.0)
sinatra-contrib (1.4.2)
backports (>= 2.0)
multi_json
@@ -44,8 +67,12 @@ GEM
slim (2.0.2)
temple (~> 0.6.6)
tilt (>= 1.3.3, < 2.1)
+ sqlite3 (1.3.8)
temple (0.6.7)
+ thread_safe (0.1.3)
+ atomic
tilt (1.4.1)
+ tzinfo (0.3.38)
PLATFORMS
ruby
@@ -57,5 +84,7 @@ DEPENDENCIES
haml
rspec
sinatra
+ sinatra-activerecord
sinatra-contrib
slim
+ sqlite3
View
11 config/database.yml
@@ -0,0 +1,11 @@
+development:
+ adapter: sqlite3
+ database: db/development.sqlite3
+
+test:
+ adapter: sqlite3
+ database: db/test.sqlite3
+
+production:
+ adapter: postgresql
+ database: production
View
21 models/participant.rb
@@ -0,0 +1,21 @@
+require 'sinatra/activerecord'
+
+configure do
+ # connect to the database
+ dbconfig = YAML.load(File.read('config/database.yml'))
+ ActiveRecord::Base.establish_connection dbconfig['development']
+ begin
+ ActiveRecord::Schema.define do
+ create_table :participants do |t|
+ t.string :name, :null => false, :limit => 100
+ t.string :email, :null => false, :limit => 50
+ end
+ end
+ rescue ActiveRecord::StatementInvalid
+ # do nothing - gobble up the error
+ end
+end
+
+# define a simple model
+class Participant < ActiveRecord::Base
+end
View
8 spec/yasapp_spec.rb
@@ -82,4 +82,12 @@
expect(last_response.body).to match 'USD'
end
end
+
+ describe "POST create '/participants'" do
+ pending
+ end
+
+ describe "GET show '/participants/:id'" do
+ pending
+ end
end
View
9 views/index.haml
@@ -26,3 +26,12 @@
%input#str{ name: "symbol", type: "text", value: "GOOG" }
%input{ type: "submit", value: "Fetch the quote!" }
%hr
+
+%h1 Add Participant to List
+%p
+ Enter the name and email of new participant.
+ %form{ action: "/participants", method: "post" }
+ %input#name{ name: "name", type: "text", value: "Name" }
+ %input#email{ name: "email", type: "text", value: "Email" }
+ %input{ type: "submit", value: "Add the record!" }
+%hr
View
19 views/participant.slim
@@ -0,0 +1,19 @@
+h1 Participants
+h3 New
+p
+ = @participant.name
+ | : #{@participant.email}
+h3 All
+table
+ thead
+ tr
+ th Name
+ th Email
+ tbody
+ - @participants.each do |participant|
+ tr
+ td.name = participant.name
+ td.email = participant.email
+p
+ a href="/" Add
+ | more participants!
View
23 yasapp.rb
@@ -2,6 +2,7 @@
require 'sinatra/reloader' if development?
require './helpers'
require './lib/stock'
+require './models/participant'
require 'haml'
require 'slim'
@@ -34,3 +35,25 @@
@stock = Stock.new params[:symbol]
slim :quote
end
+
+# save participant's details
+post '/participants' do
+ begin
+ @participant = Participant.new(name: params[:name], email: params[:email])
+ @participant.save
+ redirect "/participants/#{@participant.id}"
+ rescue
+ redirect '/'
+ end
+end
+
+# display participant's details
+get '/participants/:id' do
+ begin
+ @participant = Participant.find(params[:id])
+ @participants = Participant.find(:all)
+ slim :participant
+ rescue
+ redirect '/'
+ end
+end

0 comments on commit 5a5f9fd

Please sign in to comment.