diff --git a/Gemfile b/Gemfile index a4aeb27..bae01e6 100755 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ source 'https://rubygems.org' -gem 'sinatra', require: 'sinatra/base' \ No newline at end of file +gem 'sinatra', require: 'sinatra/base' +gem 'sqlite3' \ No newline at end of file diff --git a/Gemfile.lock b/Gemfile.lock index 5c68765..f2555dd 100755 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -8,6 +8,7 @@ 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 @@ -15,3 +16,4 @@ PLATFORMS DEPENDENCIES sinatra + sqlite3 diff --git a/app.rb b/app.rb index 29535cd..8fefaca 100755 --- a/app.rb +++ b/app.rb @@ -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 @@ -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 \ No newline at end of file diff --git a/lib/concerns/persistable.rb b/lib/concerns/persistable.rb index 1af08af..ae445b0 100644 --- a/lib/concerns/persistable.rb +++ b/lib/concerns/persistable.rb @@ -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 \ No newline at end of file diff --git a/lib/models/student.rb b/lib/models/student.rb index 4d736fd..c5de947 100644 --- a/lib/models/student.rb +++ b/lib/models/student.rb @@ -1,40 +1,32 @@ 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 @@ -42,39 +34,10 @@ def self.attr_accessors 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 \ No newline at end of file diff --git a/student.db b/student.db index ead6e6f..b53cd8e 100644 Binary files a/student.db and b/student.db differ diff --git a/test.rb b/test.rb index 6cf67ed..fc671a2 100644 --- a/test.rb +++ b/test.rb @@ -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 @@ -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 @@ -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 \ No newline at end of file