Skip to content

Commit

Permalink
Working index and student profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
gglin committed Jun 25, 2013
1 parent 09696d8 commit 417663c
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 70 deletions.
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
source 'https://rubygems.org'

gem 'sinatra', require: 'sinatra/base'
gem 'sinatra', require: 'sinatra/base'
gem 'sqlite3'
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ GEM
rack (~> 1.5, >= 1.5.2)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
sqlite3 (1.3.7)
tilt (1.4.1)

PLATFORMS
ruby

DEPENDENCIES
sinatra
sqlite3
17 changes: 16 additions & 1 deletion app.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
require 'sinatra/base'
require_relative 'lib/models/student.rb'
require 'sqlite3'

require_relative 'lib/concerns/persistable'
require_relative 'lib/concerns/findable'

require_relative 'lib/models/student'

# Why is it a good idea to wrap our App class in a module?
module StudentSite
Expand Down Expand Up @@ -28,5 +33,15 @@ def set_random_numbers(arg)
erb :artists
end

get '/students' do
@students = Student.all
erb :'students/students'
end

get '/students/:url' do
# @students = Student.all
@student = Student.find_by_url(params[:url])
erb :'students/student_profile'
end
end
end
50 changes: 44 additions & 6 deletions lib/concerns/persistable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,68 @@ def table_name
end

def drop
database.execute "DROP TABLE IF EXISTS #{table_name};"
db.execute "DROP TABLE IF EXISTS #{table_name};"
end

def table_exists?(table_name)
database.execute "SELECT * FROM sqlite_master WHERE type = 'table' AND name = ?", table_name
db.execute "SELECT * FROM sqlite_master WHERE type = 'table' AND name = ?", table_name
end

def create_table
database.execute "CREATE TABLE IF NOT EXISTS #{table_name} (
db.execute "CREATE TABLE IF NOT EXISTS #{table_name} (
#{attributes_for_create}
)"
end

def attributes_for_create
self.attributes.collect{|k,v| [k,v].join(" ")}.join(",")
self.attributes.collect{|k,v| [k,v].join(" ")}.join(", ")
end

def attributes_for_update
self.attributes.keys.reject{|k| k == :id}.collect{|k| "#{k} = ?"}.join(",")
self.attributes.keys.reject{|k| k == :id}.collect{|k| "#{k} = ?"}.join(", ")
end

def column_names_for_insert
self.attributes.keys[1..-1].join(",")
self.attributes.keys[1..-1].join(", ")
end

end


module InstanceMethods

def persisted?
self.id
end

def save
persisted? ? update : insert
end

def attributes
self.class.attributes.keys.collect do |attribute|
self.send(attribute)
end
end

def attributes_for_sql
self.attributes[1..-1]
end

def question_marks_for_sql
("?," * attributes_for_sql.size)[0..-2]
end

private

def insert
self.class.db.execute "INSERT INTO #{self.class.table_name} (#{self.class.column_names_for_insert}) VALUES (#{self.question_marks_for_sql})", self.attributes_for_sql
self.id = self.class.db.last_insert_row_id
end

def update
self.class.db.execute "UPDATE #{self.class.table_name} SET #{self.class.attributes_for_update} WHERE id = ?", [attributes_for_sql, self.id].flatten
end

end
end
71 changes: 17 additions & 54 deletions lib/models/student.rb
Original file line number Diff line number Diff line change
@@ -1,80 +1,43 @@
require_relative '../concerns/persistable'
require_relative '../concerns/findable'

class Student
extend Persistable::ClassMethods

extend Persistable::ClassMethods
include Persistable::InstanceMethods

extend Findable::ClassMethods

ATTRIBUTES = {
:id => "INTEGER PRIMARY KEY",
:name => "TEXT",
:url => "TEXT",
:img => "TEXT",
:tagline => "TEXT",
:bio => "TEXT",
:treehouse_profile => "TEXT"
:prof_pic => "TEXT",
:twitter => "TEXT",
:linkedin => "TEXT",
:github => "TEXT",
:blog => "TEXT",
:quote => "TEXT"
}

def self.attributes
ATTRIBUTES
end

def self.find_by(*args)
args.flatten.each do |arg|
define_singleton_method("find_by_#{arg}") do |value|
result = self.database.execute "SELECT * FROM #{self.table_name} WHERE #{arg} = ?", value
new_from_db(result.first)
end
end
end

find_by(self.attributes.keys)

def self.find(id)
self.find_by_id(id)
end

def self.all
results = database.execute "SELECT * FROM students;"
results.collect{|row| new_from_db(row)}
end

def self.attr_accessors
self.attributes.keys.each do |k|
attr_accessor k
end
end
attr_accessors

def self.database
@@db ||= SQLite3::Database.new('student.db')
end

def persisted?
self.id
end

def save
persisted? ? update : insert
end

def attributes
self.class.attributes.keys.collect do |attribute|
self.send(attribute)
end
end

def attributes_for_sql
self.attributes[1..-1]
end
find_by(self.attributes.keys)

def question_marks_for_sql
("?," * attributes_for_sql.size)[0..-2]
def self.db
@@db ||= SQLite3::Database.new('student.db')
end

private
def insert
self.class.database.execute "INSERT INTO #{self.class.table_name} (#{self.class.column_names_for_insert}) VALUES (#{self.question_marks_for_sql})", self.attributes_for_sql
self.id = self.class.database.last_insert_row_id
end

def update
self.class.database.execute "UPDATE #{self.class.table_name} SET #{self.class.attributes_for_update} WHERE id = ?", [attributes_for_sql, self.id].flatten
end
end
Binary file modified student.db
Binary file not shown.
26 changes: 18 additions & 8 deletions test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
require 'pry-debugger'

require_relative 'lib/concerns/persistable'

require_relative 'lib/concerns/findable'
require_relative 'lib/models/student'

include Fis::Test
Expand Down Expand Up @@ -57,13 +57,12 @@
assert_equal Student.find(s.id).name, "Bob Whitney"
end

test 'should be able to retrive a student with a where statment' do
test 'should be able to retrieve a student with a where statement' do
s = Student.new
s.name = "Jeff Baird"
s.save

assert_equal Student.where(:name => "Jeff Baird").map { |s| s.name }, [s.name]

end

test 'should be able to retrieve multiple students with the same name' do
Expand All @@ -75,18 +74,29 @@
s2.name = "Alice Adams"
s2.save


assert_equal Student.where(:name => "Alice Adams").map {|s| s.name}, [s.name,s2.name]
end

test 'should be able to find_by any attribue' do
test 'should be able to find_by any attribute' do
s = Student.new
s.name = "Avi Flombaum"
s.tagline = "Hello World"
s.bio = "Dean at Flatiron School"
s.save

assert_equal Student.find_by_tagline("Hello World").tagline, "Hello World"
assert_equal Student.find_by_bio("Dean at Flatiron School").bio, "Dean at Flatiron School"
assert_equal Student.find_by_name("Avi Flombaum").name, "Avi Flombaum"
assert_equal Student.find_by_tagline("Hello World").tagline, "Hello World"
assert_equal Student.find_by_bio("Dean at Flatiron School").bio, "Dean at Flatiron School"
assert_equal Student.find_by_name("Avi Flombaum").name, "Avi Flombaum"
end

test 'should return nil when find_by method fails to match' do
assert_equal Student.find(3409), nil
assert_equal Student.find_by_tagline("Hello Worl"), nil
assert_equal Student.find_by_bio("Dean at Flatiron Scho"), nil
assert_equal Student.find_by_name("Avi Flomb"), nil
end

test 'should return nil when where method fails to match & array otherwise' do
assert_equal Student.where(:name => "Avi Flombaum").class, Array
assert_equal Student.where(:name => "Avi Flombau"), nil
end

0 comments on commit 417663c

Please sign in to comment.