From 9259e504d42dc03afbf88c620398575dd6797f74 Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Mon, 24 Apr 2017 00:46:17 -0700 Subject: [PATCH 1/8] create models, migrations and db seed --- Gemfile.lock | 106 +++++++++++--------- Gemfile1.lock | 103 +++++++++++++++++++ app/models/follow.rb | 4 + app/models/user.rb | 10 +- db/migrate/20141020120711_create_users.rb | 12 ++- db/migrate/20170424000652_create_follows.rb | 9 ++ db/seeds.rb | 28 ++++++ 7 files changed, 218 insertions(+), 54 deletions(-) create mode 100644 Gemfile1.lock create mode 100644 app/models/follow.rb create mode 100644 db/migrate/20170424000652_create_follows.rb diff --git a/Gemfile.lock b/Gemfile.lock index b49bb8a..a066423 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,83 +1,86 @@ GEM remote: https://rubygems.org/ specs: - activemodel (4.2.1) - activesupport (= 4.2.1) - builder (~> 3.1) - activerecord (4.2.1) - activemodel (= 4.2.1) - activesupport (= 4.2.1) - arel (~> 6.0) - activesupport (4.2.1) + activemodel (5.0.2) + activesupport (= 5.0.2) + activerecord (5.0.2) + activemodel (= 5.0.2) + activesupport (= 5.0.2) + arel (~> 7.0) + activesupport (5.0.2) + concurrent-ruby (~> 1.0, >= 1.0.2) i18n (~> 0.7) - json (~> 1.7, >= 1.7.7) minitest (~> 5.1) - thread_safe (~> 0.3, >= 0.3.4) tzinfo (~> 1.1) - arel (6.0.0) - backports (3.6.4) - bcrypt (3.1.10) + addressable (2.5.1) + public_suffix (~> 2.0, >= 2.0.2) + arel (7.1.4) + backports (3.7.0) + bcrypt (3.1.11) bcrypt-ruby (3.1.5) bcrypt (>= 3.1.3) - builder (3.2.2) - capybara (2.4.4) + capybara (2.13.0) + addressable mime-types (>= 1.16) nokogiri (>= 1.3.3) rack (>= 1.0.0) rack-test (>= 0.5.4) xpath (~> 2.0) + concurrent-ruby (1.0.5) database_cleaner (1.4.1) - diff-lcs (1.2.5) - factory_girl (4.5.0) + diff-lcs (1.3) + factory_girl (4.8.0) activesupport (>= 3.0.0) - faker (1.4.3) + faker (1.7.3) i18n (~> 0.5) - i18n (0.7.0) - json (1.8.2) - mime-types (2.5) - mini_portile (0.6.2) - minitest (5.6.1) - multi_json (1.11.0) - nokogiri (1.6.6.2) - mini_portile (~> 0.6.0) - pg (0.18.2) - rack (1.6.1) + i18n (0.8.1) + mime-types (3.1) + mime-types-data (~> 3.2015) + mime-types-data (3.2016.0521) + mini_portile2 (2.1.0) + minitest (5.10.1) + multi_json (1.12.1) + nokogiri (1.7.1) + mini_portile2 (~> 2.1.0) + pg (0.20.0) + public_suffix (2.0.5) + rack (1.6.5) rack-protection (1.5.3) rack rack-test (0.6.3) rack (>= 1.0) - rake (10.4.2) - rspec (3.2.0) - rspec-core (~> 3.2.0) - rspec-expectations (~> 3.2.0) - rspec-mocks (~> 3.2.0) - rspec-core (3.2.3) - rspec-support (~> 3.2.0) - rspec-expectations (3.2.1) + rake (12.0.0) + rspec (3.5.0) + rspec-core (~> 3.5.0) + rspec-expectations (~> 3.5.0) + rspec-mocks (~> 3.5.0) + rspec-core (3.5.4) + rspec-support (~> 3.5.0) + rspec-expectations (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.2.0) - rspec-mocks (3.2.1) + rspec-support (~> 3.5.0) + rspec-mocks (3.5.0) diff-lcs (>= 1.2.0, < 2.0) - rspec-support (~> 3.2.0) - rspec-support (3.2.2) - shotgun (0.9.1) + rspec-support (~> 3.5.0) + rspec-support (3.5.0) + shotgun (0.9.2) rack (>= 1.0) - shoulda-matchers (2.8.0) - activesupport (>= 3.0.0) - sinatra (1.4.6) - rack (~> 1.4) + shoulda-matchers (3.1.1) + activesupport (>= 4.0.0) + sinatra (1.4.8) + rack (~> 1.5) rack-protection (~> 1.4) tilt (>= 1.3, < 3) - sinatra-contrib (1.4.2) + sinatra-contrib (1.4.7) backports (>= 2.0) multi_json rack-protection rack-test sinatra (~> 1.4.0) - tilt (~> 1.3) - thread_safe (0.3.5) - tilt (1.4.1) - tzinfo (1.2.2) + tilt (>= 1.3, < 3) + thread_safe (0.3.6) + tilt (2.0.7) + tzinfo (1.2.3) thread_safe (~> 0.1) xpath (2.0.0) nokogiri (~> 1.3) @@ -101,3 +104,6 @@ DEPENDENCIES shoulda-matchers sinatra sinatra-contrib + +BUNDLED WITH + 1.14.6 diff --git a/Gemfile1.lock b/Gemfile1.lock new file mode 100644 index 0000000..b49bb8a --- /dev/null +++ b/Gemfile1.lock @@ -0,0 +1,103 @@ +GEM + remote: https://rubygems.org/ + specs: + activemodel (4.2.1) + activesupport (= 4.2.1) + builder (~> 3.1) + activerecord (4.2.1) + activemodel (= 4.2.1) + activesupport (= 4.2.1) + arel (~> 6.0) + activesupport (4.2.1) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + arel (6.0.0) + backports (3.6.4) + bcrypt (3.1.10) + bcrypt-ruby (3.1.5) + bcrypt (>= 3.1.3) + builder (3.2.2) + capybara (2.4.4) + mime-types (>= 1.16) + nokogiri (>= 1.3.3) + rack (>= 1.0.0) + rack-test (>= 0.5.4) + xpath (~> 2.0) + database_cleaner (1.4.1) + diff-lcs (1.2.5) + factory_girl (4.5.0) + activesupport (>= 3.0.0) + faker (1.4.3) + i18n (~> 0.5) + i18n (0.7.0) + json (1.8.2) + mime-types (2.5) + mini_portile (0.6.2) + minitest (5.6.1) + multi_json (1.11.0) + nokogiri (1.6.6.2) + mini_portile (~> 0.6.0) + pg (0.18.2) + rack (1.6.1) + rack-protection (1.5.3) + rack + rack-test (0.6.3) + rack (>= 1.0) + rake (10.4.2) + rspec (3.2.0) + rspec-core (~> 3.2.0) + rspec-expectations (~> 3.2.0) + rspec-mocks (~> 3.2.0) + rspec-core (3.2.3) + rspec-support (~> 3.2.0) + rspec-expectations (3.2.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-mocks (3.2.1) + diff-lcs (>= 1.2.0, < 2.0) + rspec-support (~> 3.2.0) + rspec-support (3.2.2) + shotgun (0.9.1) + rack (>= 1.0) + shoulda-matchers (2.8.0) + activesupport (>= 3.0.0) + sinatra (1.4.6) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) + sinatra-contrib (1.4.2) + backports (>= 2.0) + multi_json + rack-protection + rack-test + sinatra (~> 1.4.0) + tilt (~> 1.3) + thread_safe (0.3.5) + tilt (1.4.1) + tzinfo (1.2.2) + thread_safe (~> 0.1) + xpath (2.0.0) + nokogiri (~> 1.3) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord + activesupport + bcrypt-ruby + capybara + database_cleaner (~> 1.4.1) + factory_girl + faker + pg + rack-test + rake + rspec + shotgun + shoulda-matchers + sinatra + sinatra-contrib diff --git a/app/models/follow.rb b/app/models/follow.rb new file mode 100644 index 0000000..528b96f --- /dev/null +++ b/app/models/follow.rb @@ -0,0 +1,4 @@ +class Follow < ActiveRecord::Base + belongs_to :follower, foreign_key: "follower_id", class_name: "User" + belongs_to :followee, foreign_key: "followee_id", class_name: "User" +end diff --git a/app/models/user.rb b/app/models/user.rb index 6b13ee3..ac40131 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,4 +1,10 @@ class User < ActiveRecord::Base - has_secure_password - # Remember to create a migration! + has_many :followers, through: :follower_follows, source: :follower + has_many :follower_follows, foreign_key: :followee_id, class_name: "Follow" + + has_many :followees, through: :followee_follows, source: :followee + has_many :followee_follows, foreign_key: :follower_id, class_name: "Follow" + + validates :first_name, :last_name, :email, :password_hash , presence:true + validates :email, uniqueness: true end diff --git a/db/migrate/20141020120711_create_users.rb b/db/migrate/20141020120711_create_users.rb index 7629266..5eb5ceb 100644 --- a/db/migrate/20141020120711_create_users.rb +++ b/db/migrate/20141020120711_create_users.rb @@ -1,8 +1,16 @@ class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| - t.string :name - t.string :password_digest + t.string :first_name, null: false + t.string :last_name, null: false + t.string :email, null: false + t.string :password_hash, null: false + t.string :race + t.string :educational_attainment + t.string :marital_status + t.string :sex + + t.timestamps end end end diff --git a/db/migrate/20170424000652_create_follows.rb b/db/migrate/20170424000652_create_follows.rb new file mode 100644 index 0000000..3dd420c --- /dev/null +++ b/db/migrate/20170424000652_create_follows.rb @@ -0,0 +1,9 @@ +class CreateFollows < ActiveRecord::Migration + def change + create_table :follows do |t| + t.integer :follower_id + t.integer :followee_id + t.timestamps + end + end +end diff --git a/db/seeds.rb b/db/seeds.rb index e69de29..7dae10a 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -0,0 +1,28 @@ +require 'faker' +# Faker::Demographic.race + +# Faker::Demographic.educational_attainment + +# Faker::Demographic.marital_status + +# Faker::Demographic.sex + +# Faker::Name.first_name + +# Faker::Name.last_name + +# Faker::Internet.safe_email + +# Faker::Internet.password + +5.times do + params = {first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: Faker::Internet.safe_email, + password_hash: Faker::Internet.password, race: Faker::Demographic.race, educational_attainment: Faker::Demographic.educational_attainment, + marital_status: Faker::Demographic.marital_status, sex: Faker::Demographic.sex} + + user = User.new(params) + user.save! +end + + + From 9016c695a44faa80d4788f898f2d0dc4e2bbae26 Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Thu, 4 May 2017 18:41:15 -0700 Subject: [PATCH 2/8] refactor --- Gemfile | 2 +- Gemfile.lock | 4 +- app/controllers/auth.rb | 11 ---- app/controllers/index.rb | 4 ++ app/controllers/session_controller.rb | 22 ++++++++ app/controllers/user_controller.rb | 69 +++++++++++++++++++++++ app/helpers/auth.rb | 20 +++++-- app/models/user.rb | 17 +++++- app/views/_errors.erb | 7 +++ app/views/index.erb | 4 ++ app/views/layout.erb | 4 +- app/views/sessions/new.erb | 12 ++++ app/views/users/new.erb | 20 +++++++ app/views/users/show.erb | 12 ++++ config/environment.rb | 1 + db/migrate/20141020120711_create_users.rb | 7 +-- db/seeds.rb | 21 +------ 17 files changed, 192 insertions(+), 45 deletions(-) delete mode 100644 app/controllers/auth.rb create mode 100644 app/controllers/index.rb create mode 100644 app/controllers/session_controller.rb create mode 100644 app/controllers/user_controller.rb create mode 100644 app/views/_errors.erb create mode 100644 app/views/index.erb create mode 100644 app/views/sessions/new.erb create mode 100644 app/views/users/new.erb create mode 100644 app/views/users/show.erb diff --git a/Gemfile b/Gemfile index b348e2c..c4fe494 100644 --- a/Gemfile +++ b/Gemfile @@ -9,7 +9,7 @@ gem 'sinatra-contrib' gem 'activesupport' gem 'activerecord' -gem 'bcrypt-ruby' +gem 'bcrypt' gem 'rake' diff --git a/Gemfile.lock b/Gemfile.lock index a066423..d2e49b1 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -17,8 +17,6 @@ GEM arel (7.1.4) backports (3.7.0) bcrypt (3.1.11) - bcrypt-ruby (3.1.5) - bcrypt (>= 3.1.3) capybara (2.13.0) addressable mime-types (>= 1.16) @@ -91,7 +89,7 @@ PLATFORMS DEPENDENCIES activerecord activesupport - bcrypt-ruby + bcrypt capybara database_cleaner (~> 1.4.1) factory_girl diff --git a/app/controllers/auth.rb b/app/controllers/auth.rb deleted file mode 100644 index 262f26f..0000000 --- a/app/controllers/auth.rb +++ /dev/null @@ -1,11 +0,0 @@ -get '/' do - erb :welcome -end - -get '/login' do - -end - -get '/signup' do - -end diff --git a/app/controllers/index.rb b/app/controllers/index.rb new file mode 100644 index 0000000..d5b8967 --- /dev/null +++ b/app/controllers/index.rb @@ -0,0 +1,4 @@ +get '/' do + # @restaurants = Restaurant.all + erb :'index' +end diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb new file mode 100644 index 0000000..4213dfb --- /dev/null +++ b/app/controllers/session_controller.rb @@ -0,0 +1,22 @@ +get '/sessions/new' do + erb :'sessions/new' +end + + +post '/sessions' do + @user = User.where(username: params[:user][:username]).first + if @user && @user.authenticate?(params[:user][:password]) + session[:id] = @user.id + redirect "/users/#{@user.id}" + else + @errors = [] + @errors << "Incorrect username or password" + erb :'sessions/new' + end +end + + +delete '/sessions/:id' do + session[:id] = nil + redirect 'sessions/new' +end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb new file mode 100644 index 0000000..e5ef6c7 --- /dev/null +++ b/app/controllers/user_controller.rb @@ -0,0 +1,69 @@ +# #index +# get '/users' do +# @users = User.all +# @users.map{|user| user.email.to_s} +# erb :'users/index' +# end + + +#new +get '/users/new' do + @user = User.new + erb :'users/new' +end + + +#create new +post '/users' do + @user = User.new(params[:user]) + if @user.save + session[:id] = @user.id + redirect "/users/#{@user.id}" + else + @errors = @user.errors.full_messages + erb :'users/new' + end +end + +#user show +get '/users/:id' do + @user = User.find(params[:id]) + erb :'users/show' +end + + + +# #edit +# get '/users/:id/edit' do +# @user = User.find(params[:id]) +# erb :'users/edit' +# end + +# #update +# def update_user +# @user = User.find(params[:id]) +# @user.update(params[:user]) +# if @user.valid? +# redirect "/users/#{@user.id}" +# else +# @errors = @user.errors.full_messages +# erb :'users/edit' +# end +# end + +# patch '/users/:id' do +# update_user +# end + +# put '/users/:id' do +# update_user +# end + + +# #delete +# delete '/users/:id' do +# User.find(params[:id]).destroy! +# redirect '/users' +# end + + diff --git a/app/helpers/auth.rb b/app/helpers/auth.rb index bbb442d..cceb690 100644 --- a/app/helpers/auth.rb +++ b/app/helpers/auth.rb @@ -1,7 +1,17 @@ -def current_user - if session[:user_id] - return User.find(session[:user_id]) - else - return nil +helpers do + def login(user) + session[:id] = user.id + end + + def current_user + @current_user ||= User.find(session[:id]) if session[:id] + end + + def logged_in? + !!current_user + end + + def logout + session[:id] = nil end end diff --git a/app/models/user.rb b/app/models/user.rb index ac40131..6c24732 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -5,6 +5,21 @@ class User < ActiveRecord::Base has_many :followees, through: :followee_follows, source: :followee has_many :followee_follows, foreign_key: :follower_id, class_name: "Follow" - validates :first_name, :last_name, :email, :password_hash , presence:true + validates :username, :email, :encrypted_password , presence:true validates :email, uniqueness: true + + + def password + @password ||= BCrypt::Password.new(encrypted_password) + end + + def password=(new_password) + @password = BCrypt::Password.create(new_password) + self.encrypted_password = @password + end + + def authenticate?(plain_text_password) + self.password == plain_text_password + end + end diff --git a/app/views/_errors.erb b/app/views/_errors.erb new file mode 100644 index 0000000..e8ee93d --- /dev/null +++ b/app/views/_errors.erb @@ -0,0 +1,7 @@ + diff --git a/app/views/index.erb b/app/views/index.erb new file mode 100644 index 0000000..7857e5c --- /dev/null +++ b/app/views/index.erb @@ -0,0 +1,4 @@ +

Index page of the site

+ +Login +Register diff --git a/app/views/layout.erb b/app/views/layout.erb index cc95ae8..97cd753 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -23,12 +23,12 @@
-
+
<%= yield %>
diff --git a/app/views/sessions/new.erb b/app/views/sessions/new.erb new file mode 100644 index 0000000..89d55bb --- /dev/null +++ b/app/views/sessions/new.erb @@ -0,0 +1,12 @@ +

sessions/new page

+
+ + +
+ + +
+ +
+ +<%= erb :'_errors' %> diff --git a/app/views/users/new.erb b/app/views/users/new.erb new file mode 100644 index 0000000..2dc4539 --- /dev/null +++ b/app/views/users/new.erb @@ -0,0 +1,20 @@ +

NEW USER PAGE

+
+
+ +
+ + +
+ + +
+ + +
+ +
+ + + +<%= erb :'_errors' %> diff --git a/app/views/users/show.erb b/app/views/users/show.erb new file mode 100644 index 0000000..dc361fb --- /dev/null +++ b/app/views/users/show.erb @@ -0,0 +1,12 @@ + +

Page showing User info with option to logout or do other things

+ +

<%= @user.username%>

+ +
+ + +
+ + +DO A THING diff --git a/config/environment.rb b/config/environment.rb index 7c23e40..4e8e40e 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -14,6 +14,7 @@ require 'pg' require 'active_record' require 'logger' +require 'bcrypt' require 'sinatra' require "sinatra/reloader" if development? diff --git a/db/migrate/20141020120711_create_users.rb b/db/migrate/20141020120711_create_users.rb index 5eb5ceb..69464ae 100644 --- a/db/migrate/20141020120711_create_users.rb +++ b/db/migrate/20141020120711_create_users.rb @@ -1,14 +1,13 @@ class CreateUsers < ActiveRecord::Migration def change create_table :users do |t| - t.string :first_name, null: false - t.string :last_name, null: false + t.string :username, null: false t.string :email, null: false - t.string :password_hash, null: false + t.string :encrypted_password, null: false t.string :race t.string :educational_attainment t.string :marital_status - t.string :sex + t.string :sex t.timestamps end diff --git a/db/seeds.rb b/db/seeds.rb index 7dae10a..ddae00e 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -1,27 +1,12 @@ require 'faker' -# Faker::Demographic.race - -# Faker::Demographic.educational_attainment - -# Faker::Demographic.marital_status - -# Faker::Demographic.sex - -# Faker::Name.first_name - -# Faker::Name.last_name - -# Faker::Internet.safe_email - -# Faker::Internet.password 5.times do - params = {first_name: Faker::Name.first_name, last_name: Faker::Name.last_name, email: Faker::Internet.safe_email, - password_hash: Faker::Internet.password, race: Faker::Demographic.race, educational_attainment: Faker::Demographic.educational_attainment, + params = {username: Faker::Internet.user_name, email: Faker::Internet.safe_email, + encrypted_password: Faker::Internet.password, race: Faker::Demographic.race, educational_attainment: Faker::Demographic.educational_attainment, marital_status: Faker::Demographic.marital_status, sex: Faker::Demographic.sex} user = User.new(params) - user.save! + user.save! end From 3a8e25ef8ce561afc239dc405f541acbea447c27 Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Fri, 5 May 2017 18:29:12 -0700 Subject: [PATCH 3/8] Add some forms --- app/controllers/session_controller.rb | 4 +- app/controllers/user_controller.rb | 61 +++++++++++++-------------- app/views/sessions/new.erb | 16 ++++--- app/views/users/edit.erb | 18 ++++++++ app/views/users/index.erb | 18 ++++++++ app/views/users/new.erb | 20 +++------ app/views/users/show.erb | 12 +++--- 7 files changed, 89 insertions(+), 60 deletions(-) create mode 100644 app/views/users/edit.erb create mode 100644 app/views/users/index.erb diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index 4213dfb..e309f65 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -4,8 +4,8 @@ post '/sessions' do - @user = User.where(username: params[:user][:username]).first - if @user && @user.authenticate?(params[:user][:password]) + @user = User.where(username: params[:username]).first + if @user && @user.authenticate?(params[:password]) session[:id] = @user.id redirect "/users/#{@user.id}" else diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index e5ef6c7..835484c 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -1,9 +1,9 @@ -# #index -# get '/users' do -# @users = User.all -# @users.map{|user| user.email.to_s} -# erb :'users/index' -# end +#index get all users info +get '/users' do + @users = User.all + @users.map{|user| user.email.to_s} + erb :'users/index' +end #new @@ -32,38 +32,37 @@ end +#edit +get '/users/:id/edit' do + @user = User.find(params[:id]) + erb :'users/edit' +end -# #edit -# get '/users/:id/edit' do -# @user = User.find(params[:id]) -# erb :'users/edit' -# end - -# #update -# def update_user -# @user = User.find(params[:id]) -# @user.update(params[:user]) -# if @user.valid? -# redirect "/users/#{@user.id}" -# else -# @errors = @user.errors.full_messages -# erb :'users/edit' -# end -# end +#update +def update_user + @user = User.find(params[:id]) + @user.update(params[:user]) + if @user.valid? + redirect "/users/#{@user.id}" + else + @errors = @user.errors.full_messages + erb :'users/edit' + end +end # patch '/users/:id' do # update_user # end -# put '/users/:id' do -# update_user -# end +put '/users/:id' do + update_user +end -# #delete -# delete '/users/:id' do -# User.find(params[:id]).destroy! -# redirect '/users' -# end +#delete +delete '/users/:id' do + User.find(params[:id]).destroy! + redirect '/users' +end diff --git a/app/views/sessions/new.erb b/app/views/sessions/new.erb index 89d55bb..8277833 100644 --- a/app/views/sessions/new.erb +++ b/app/views/sessions/new.erb @@ -1,12 +1,14 @@ -

sessions/new page

-
- - +

Login


- -
- + +
+ +
+
+ +
+
<%= erb :'_errors' %> diff --git a/app/views/users/edit.erb b/app/views/users/edit.erb new file mode 100644 index 0000000..49e1510 --- /dev/null +++ b/app/views/users/edit.erb @@ -0,0 +1,18 @@ +

Edit user details

+ + +
+ + +
+
+ +
+
+ +
+ + + +
+ diff --git a/app/views/users/index.erb b/app/views/users/index.erb new file mode 100644 index 0000000..83b076c --- /dev/null +++ b/app/views/users/index.erb @@ -0,0 +1,18 @@ +

All Users

+
+
    + <% @users.each do |user| %> +
  • <%= "#{user.username} - #{user.email}"%> - Show details + Edit + +
    + + +
    +
  • + <% end %> +
+
+ + + diff --git a/app/views/users/new.erb b/app/views/users/new.erb index 2dc4539..4efbaa3 100644 --- a/app/views/users/new.erb +++ b/app/views/users/new.erb @@ -1,20 +1,10 @@ -

NEW USER PAGE

-
-
+

New User Page

- - -
- - -
- - -
- +
+
+
+
- - <%= erb :'_errors' %> diff --git a/app/views/users/show.erb b/app/views/users/show.erb index dc361fb..2465b00 100644 --- a/app/views/users/show.erb +++ b/app/views/users/show.erb @@ -1,12 +1,14 @@ -

Page showing User info with option to logout or do other things

+

users show

<%= @user.username%>

- +

<%= @user.email%>

- - + +
+show all users + + -DO A THING From 1c08cd4bf58822abce09215f74d1ca47ad772bd2 Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Mon, 22 May 2017 21:11:56 -0700 Subject: [PATCH 4/8] Update models and migrations --- app/controllers/index.rb | 2 +- app/controllers/session_controller.rb | 2 +- app/controllers/user_controller.rb | 6 +-- app/models/follow.rb | 4 -- app/models/forecast.rb | 3 ++ app/models/user.rb | 6 +-- app/views/_errors.erb | 2 +- app/views/index.erb | 3 +- app/views/layout.erb | 22 +++++--- db/migrate/20141020120711_create_users.rb | 4 -- db/migrate/20170424000652_create_follows.rb | 9 ---- db/migrate/20170522202650_create_forecasts.rb | 13 +++++ public/css/application.css | 54 ++++++++++++++++++- 13 files changed, 90 insertions(+), 40 deletions(-) delete mode 100644 app/models/follow.rb create mode 100644 app/models/forecast.rb delete mode 100644 db/migrate/20170424000652_create_follows.rb create mode 100644 db/migrate/20170522202650_create_forecasts.rb diff --git a/app/controllers/index.rb b/app/controllers/index.rb index d5b8967..0280c76 100644 --- a/app/controllers/index.rb +++ b/app/controllers/index.rb @@ -1,4 +1,4 @@ get '/' do - # @restaurants = Restaurant.all + @forecasts = Forecast.all erb :'index' end diff --git a/app/controllers/session_controller.rb b/app/controllers/session_controller.rb index e309f65..339e16a 100644 --- a/app/controllers/session_controller.rb +++ b/app/controllers/session_controller.rb @@ -18,5 +18,5 @@ delete '/sessions/:id' do session[:id] = nil - redirect 'sessions/new' + redirect '/' end diff --git a/app/controllers/user_controller.rb b/app/controllers/user_controller.rb index 835484c..a6491de 100644 --- a/app/controllers/user_controller.rb +++ b/app/controllers/user_controller.rb @@ -39,7 +39,7 @@ end #update -def update_user +put '/users/:id' do @user = User.find(params[:id]) @user.update(params[:user]) if @user.valid? @@ -54,10 +54,6 @@ def update_user # update_user # end -put '/users/:id' do - update_user -end - #delete delete '/users/:id' do diff --git a/app/models/follow.rb b/app/models/follow.rb deleted file mode 100644 index 528b96f..0000000 --- a/app/models/follow.rb +++ /dev/null @@ -1,4 +0,0 @@ -class Follow < ActiveRecord::Base - belongs_to :follower, foreign_key: "follower_id", class_name: "User" - belongs_to :followee, foreign_key: "followee_id", class_name: "User" -end diff --git a/app/models/forecast.rb b/app/models/forecast.rb new file mode 100644 index 0000000..103e952 --- /dev/null +++ b/app/models/forecast.rb @@ -0,0 +1,3 @@ +class Forecast < ActiveRecord::Base + belongs_to :user +end diff --git a/app/models/user.rb b/app/models/user.rb index 6c24732..dccc0e8 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -1,9 +1,5 @@ class User < ActiveRecord::Base - has_many :followers, through: :follower_follows, source: :follower - has_many :follower_follows, foreign_key: :followee_id, class_name: "Follow" - - has_many :followees, through: :followee_follows, source: :followee - has_many :followee_follows, foreign_key: :follower_id, class_name: "Follow" + has_many :forecasts validates :username, :email, :encrypted_password , presence:true validates :email, uniqueness: true diff --git a/app/views/_errors.erb b/app/views/_errors.erb index e8ee93d..3d0cef0 100644 --- a/app/views/_errors.erb +++ b/app/views/_errors.erb @@ -1,4 +1,4 @@ -
    +
      <% if @errors %> <% @errors.each do |error| %>
    • <%= error %>
    • diff --git a/app/views/index.erb b/app/views/index.erb index 7857e5c..a99e78f 100644 --- a/app/views/index.erb +++ b/app/views/index.erb @@ -1,4 +1,3 @@

      Index page of the site

      -Login -Register + diff --git a/app/views/layout.erb b/app/views/layout.erb index 97cd753..49df949 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -23,12 +23,22 @@
      - + +
      + <%if !session[:id]%> + login | + register + <%else%> + <%=current_user.username%> | +
      + + +
      + + <%end%> +
      + +
      <%= yield %>
      diff --git a/db/migrate/20141020120711_create_users.rb b/db/migrate/20141020120711_create_users.rb index 69464ae..7cbb73f 100644 --- a/db/migrate/20141020120711_create_users.rb +++ b/db/migrate/20141020120711_create_users.rb @@ -4,10 +4,6 @@ def change t.string :username, null: false t.string :email, null: false t.string :encrypted_password, null: false - t.string :race - t.string :educational_attainment - t.string :marital_status - t.string :sex t.timestamps end diff --git a/db/migrate/20170424000652_create_follows.rb b/db/migrate/20170424000652_create_follows.rb deleted file mode 100644 index 3dd420c..0000000 --- a/db/migrate/20170424000652_create_follows.rb +++ /dev/null @@ -1,9 +0,0 @@ -class CreateFollows < ActiveRecord::Migration - def change - create_table :follows do |t| - t.integer :follower_id - t.integer :followee_id - t.timestamps - end - end -end diff --git a/db/migrate/20170522202650_create_forecasts.rb b/db/migrate/20170522202650_create_forecasts.rb new file mode 100644 index 0000000..0c2e37c --- /dev/null +++ b/db/migrate/20170522202650_create_forecasts.rb @@ -0,0 +1,13 @@ +class CreateForecasts < ActiveRecord::Migration + def change + create_table :forecasts do |t| + t.string :location + t.string :temperature + t.string :wind + t.string :body + + t.timestamps + + end + end +end diff --git a/public/css/application.css b/public/css/application.css index 69cdf5f..fc0bd80 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -1,5 +1,50 @@ +.right{ + float: right; + margin-right: 5%; + text-decoration: none; +} + +a:link { + text-decoration: none; +} + +a:visited { + text-decoration: none; +} + +a:hover { + text-decoration: underline; +} + +a:active { + text-decoration: underline; +} + +.errors{ + color: red; +} + +.container{ + display: inline-block; + border: 1px solid black; + margin:4px; + padding:4px; + vertical-align: top; + height: 20vh; +} + + + + + + + + + + + /* CSS declarations go here */ -body {font-family: Vollkorn; font-size: 160%; line-height: 160%; } +/*body {font-family: Vollkorn; font-size: 160%; line-height: 160%; } h1, h2, h3, h4, h5, h6 {font-family: Georgia; line-height:100%; margin-top: 0.5em; margin-bottom: 0.5em; color: #36AECC; font-style: italic; font-weight: 400;} h1 {font-size: 1.8em;} h2 {font-size: 1.6em;} @@ -64,4 +109,9 @@ code { line-height: 1.4em; margin-left: -1em; overflow: auto; -} +}*/ + + + + + From b0f58efa430e3e31d9bdb2ede83581f756b2b75b Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Mon, 22 May 2017 22:35:48 -0700 Subject: [PATCH 5/8] update routes --- app/controllers/forecasts_controller.rb | 53 +++++++++++++++++++ ...n_controller.rb => sessions_controller.rb} | 0 ...user_controller.rb => users_controller.rb} | 0 db/migrate/20170522202650_create_forecasts.rb | 1 + 4 files changed, 54 insertions(+) create mode 100644 app/controllers/forecasts_controller.rb rename app/controllers/{session_controller.rb => sessions_controller.rb} (100%) rename app/controllers/{user_controller.rb => users_controller.rb} (100%) diff --git a/app/controllers/forecasts_controller.rb b/app/controllers/forecasts_controller.rb new file mode 100644 index 0000000..54e967f --- /dev/null +++ b/app/controllers/forecasts_controller.rb @@ -0,0 +1,53 @@ +get '/forecasts' do + @forecasts = Forecasts.all +end + + +get '/forecasts/new' do + erb '/forecasts/new' +end + + +post '/forecasts' do + @forecast = Forecasts.new(params[:forecast]) + if @forecast.save + redirect "forecasts" + else + @errors = @forecast.full_messages + erb '/forecasts/new' + end +end + + + +get '/forecasts/:id' do + @forecast = Forecast.find(params[:id]) + # @text_messages = @forecast.text_messages + erb :'forecasts/show' +end + + +#Edit +get '/forecasts/:id/edit' do + @forecast = Forecast.find(params[:id]) + erb :'restaurants/edit' +end + +put '/forecasts/:id' do +@forecast = Forecast.find(params[:id]) +@forecast.update_attributes(params[:forecast]) + if @forecast.save + redirect "/" + else + @errors = @restaurant.errors.full_messages + erb :'forecasts/edit' + end +end + +#Delete +delete '/forecast/:id' do + @forecast = Forecast.find(params[:id]) + @forecast.destroy + redirect '/' +end + diff --git a/app/controllers/session_controller.rb b/app/controllers/sessions_controller.rb similarity index 100% rename from app/controllers/session_controller.rb rename to app/controllers/sessions_controller.rb diff --git a/app/controllers/user_controller.rb b/app/controllers/users_controller.rb similarity index 100% rename from app/controllers/user_controller.rb rename to app/controllers/users_controller.rb diff --git a/db/migrate/20170522202650_create_forecasts.rb b/db/migrate/20170522202650_create_forecasts.rb index 0c2e37c..0c88c37 100644 --- a/db/migrate/20170522202650_create_forecasts.rb +++ b/db/migrate/20170522202650_create_forecasts.rb @@ -2,6 +2,7 @@ class CreateForecasts < ActiveRecord::Migration def change create_table :forecasts do |t| t.string :location + t.datetime :date t.string :temperature t.string :wind t.string :body From 958389f570d0cf4ca9dd4fb174a8c52b2c3f4a8e Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Thu, 25 May 2017 19:03:52 -0700 Subject: [PATCH 6/8] some form css --- app/controllers/users_controller.rb | 1 + app/views/layout.erb | 2 +- public/css/application.css | 12 ++++++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index a6491de..28489f5 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -58,6 +58,7 @@ #delete delete '/users/:id' do User.find(params[:id]).destroy! + session[:id] = nil redirect '/users' end diff --git a/app/views/layout.erb b/app/views/layout.erb index 49df949..17badad 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -34,7 +34,7 @@ - + <%end%> diff --git a/public/css/application.css b/public/css/application.css index fc0bd80..12b318d 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -33,7 +33,19 @@ a:active { height: 20vh; } +input{ + width: 20%; + padding: 12px 20px; + margin: 10px 0; + box-sizing: border-box; + border: 1px solid #ccc; + + outline: none; +} +input:focus { + border: 1px solid #555; +} From 4f3f283640d08b1ee991b712de02443ca74dbccf Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Thu, 25 May 2017 23:37:03 -0700 Subject: [PATCH 7/8] cstyle the forms --- public/css/application.css | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/public/css/application.css b/public/css/application.css index 12b318d..a480ecf 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -24,14 +24,15 @@ a:active { color: red; } -.container{ +/*.container{ display: inline-block; border: 1px solid black; margin:4px; padding:4px; vertical-align: top; height: 20vh; -} +}*/ + input{ width: 20%; @@ -39,6 +40,10 @@ input{ margin: 10px 0; box-sizing: border-box; border: 1px solid #ccc; + + +border-radius: 3em 1em 5em / 0.5em 2em; +margin-left: 1em; outline: none; } From f029e19a18de385e46060b526ab02660b5e166cd Mon Sep 17 00:00:00 2001 From: Iliyan Ganev Date: Fri, 26 May 2017 17:49:26 -0700 Subject: [PATCH 8/8] put key --- Gemfile | 1 + Gemfile.lock | 4 ++++ app/controllers/index.rb | 2 +- app/models/forecast.rb | 16 ++++++++++++++++ app/views/layout.erb | 3 ++- app/views/sessions/new.erb | 6 ++---- app/views/users/new.erb | 2 ++ app/views/users/show.erb | 9 ++++++--- config/environment.rb | 1 + db/migrate/20141020120711_create_users.rb | 2 ++ public/css/application.css | 6 +++--- 11 files changed, 40 insertions(+), 12 deletions(-) diff --git a/Gemfile b/Gemfile index c4fe494..3f53e43 100644 --- a/Gemfile +++ b/Gemfile @@ -10,6 +10,7 @@ gem 'sinatra-contrib' gem 'activesupport' gem 'activerecord' gem 'bcrypt' +gem 'httparty' gem 'rake' diff --git a/Gemfile.lock b/Gemfile.lock index d2e49b1..9742795 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -31,6 +31,8 @@ GEM activesupport (>= 3.0.0) faker (1.7.3) i18n (~> 0.5) + httparty (0.15.5) + multi_xml (>= 0.5.2) i18n (0.8.1) mime-types (3.1) mime-types-data (~> 3.2015) @@ -38,6 +40,7 @@ GEM mini_portile2 (2.1.0) minitest (5.10.1) multi_json (1.12.1) + multi_xml (0.6.0) nokogiri (1.7.1) mini_portile2 (~> 2.1.0) pg (0.20.0) @@ -94,6 +97,7 @@ DEPENDENCIES database_cleaner (~> 1.4.1) factory_girl faker + httparty pg rack-test rake diff --git a/app/controllers/index.rb b/app/controllers/index.rb index 0280c76..8786a49 100644 --- a/app/controllers/index.rb +++ b/app/controllers/index.rb @@ -1,4 +1,4 @@ get '/' do - @forecasts = Forecast.all + # @forecasts = Forecast.all erb :'index' end diff --git a/app/models/forecast.rb b/app/models/forecast.rb index 103e952..7114b84 100644 --- a/app/models/forecast.rb +++ b/app/models/forecast.rb @@ -1,3 +1,19 @@ class Forecast < ActiveRecord::Base belongs_to :user + + + def response(city) + + response = HTTParty.get("http://api.wunderground.com/api/putapikey/conditions/q/CA/#{city}.json") + + + weather = response['current_observation']['weather'] + temp = response['current_observation']['temp_c'] + + p weather + p "temp is #{temp} C" + + end + + end diff --git a/app/views/layout.erb b/app/views/layout.erb index 17badad..6155cc0 100644 --- a/app/views/layout.erb +++ b/app/views/layout.erb @@ -29,7 +29,8 @@ login | register <%else%> - <%=current_user.username%> | + +<%=current_user.username%> |
      diff --git a/app/views/sessions/new.erb b/app/views/sessions/new.erb index 8277833..b019115 100644 --- a/app/views/sessions/new.erb +++ b/app/views/sessions/new.erb @@ -2,11 +2,9 @@

      -
      - +
      -
      - +
      diff --git a/app/views/users/new.erb b/app/views/users/new.erb index 4efbaa3..0cf9f4a 100644 --- a/app/views/users/new.erb +++ b/app/views/users/new.erb @@ -3,6 +3,8 @@


      +
      +

      diff --git a/app/views/users/show.erb b/app/views/users/show.erb index 2465b00..d2ae90d 100644 --- a/app/views/users/show.erb +++ b/app/views/users/show.erb @@ -3,9 +3,12 @@

      <%= @user.username%>

      <%= @user.email%>

      -
      - - + +Edit + + + +
      show all users diff --git a/config/environment.rb b/config/environment.rb index 4e8e40e..6cf3a82 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -15,6 +15,7 @@ require 'active_record' require 'logger' require 'bcrypt' +require 'httparty' require 'sinatra' require "sinatra/reloader" if development? diff --git a/db/migrate/20141020120711_create_users.rb b/db/migrate/20141020120711_create_users.rb index 7cbb73f..40b2ab7 100644 --- a/db/migrate/20141020120711_create_users.rb +++ b/db/migrate/20141020120711_create_users.rb @@ -3,6 +3,8 @@ def change create_table :users do |t| t.string :username, null: false t.string :email, null: false + t.integer :phone_number + t.string :city t.string :encrypted_password, null: false t.timestamps diff --git a/public/css/application.css b/public/css/application.css index a480ecf..63a9c80 100644 --- a/public/css/application.css +++ b/public/css/application.css @@ -40,8 +40,8 @@ input{ margin: 10px 0; box-sizing: border-box; border: 1px solid #ccc; - - + + border-radius: 3em 1em 5em / 0.5em 2em; margin-left: 1em; @@ -53,7 +53,7 @@ input:focus { } - +1db7c15a6b05e4c2