Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Michelle #6

Open
wants to merge 11 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ gem 'shotgun'
gem 'bcrypt'
gem 'pry'

gem 'bootstrap-sass', '~> 3.2.0'
gem 'sass-rails', '>= 3.2'
gem 'autoprefixer-rails'


group :test do
gem 'shoulda-matchers'
Expand Down
40 changes: 40 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
GEM
remote: https://rubygems.org/
specs:
actionpack (4.1.1)
actionview (= 4.1.1)
activesupport (= 4.1.1)
rack (~> 1.5.2)
rack-test (~> 0.6.2)
actionview (4.1.1)
activesupport (= 4.1.1)
builder (~> 3.1)
erubis (~> 2.7.0)
activemodel (4.1.1)
activesupport (= 4.1.1)
builder (~> 3.1)
Expand All @@ -15,8 +24,12 @@ GEM
thread_safe (~> 0.1)
tzinfo (~> 1.1)
arel (5.0.1.20140414130214)
autoprefixer-rails (3.1.0.20140911)
execjs
backports (3.6.0)
bcrypt (3.1.7)
bootstrap-sass (3.2.0.2)
sass (~> 3.2)
builder (3.2.2)
capybara (2.2.1)
mime-types (>= 1.16)
Expand All @@ -26,10 +39,13 @@ GEM
xpath (~> 2.0)
coderay (1.1.0)
diff-lcs (1.2.5)
erubis (2.7.0)
execjs (2.2.1)
factory_girl (4.4.0)
activesupport (>= 3.0.0)
faker (1.3.0)
i18n (~> 0.5)
hike (1.2.3)
i18n (0.6.9)
json (1.8.1)
method_source (0.8.2)
Expand All @@ -49,6 +65,11 @@ GEM
rack
rack-test (0.6.2)
rack (>= 1.0)
railties (4.1.1)
actionpack (= 4.1.1)
activesupport (= 4.1.1)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.3.2)
rspec (2.14.1)
rspec-core (~> 2.14.0)
Expand All @@ -58,6 +79,12 @@ GEM
rspec-expectations (2.14.5)
diff-lcs (>= 1.1.3, < 2.0)
rspec-mocks (2.14.6)
sass (3.2.19)
sass-rails (4.0.3)
railties (>= 4.0.0, < 5.0)
sass (~> 3.2.0)
sprockets (~> 2.8, <= 2.11.0)
sprockets-rails (~> 2.0)
shotgun (0.9)
rack (>= 1.0)
shoulda-matchers (2.6.1)
Expand All @@ -74,6 +101,16 @@ GEM
sinatra (~> 1.4.0)
tilt (~> 1.3)
slop (3.4.7)
sprockets (2.11.0)
hike (~> 1.2)
multi_json (~> 1.0)
rack (~> 1.0)
tilt (~> 1.1, != 1.3.0)
sprockets-rails (2.1.4)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (~> 2.8)
thor (0.19.1)
thread_safe (0.3.4)
tilt (1.4.1)
tzinfo (1.2.0)
Expand All @@ -87,7 +124,9 @@ PLATFORMS
DEPENDENCIES
activerecord (~> 4.1)
activesupport (~> 4.1)
autoprefixer-rails
bcrypt
bootstrap-sass (~> 3.2.0)
capybara
factory_girl
faker
Expand All @@ -96,6 +135,7 @@ DEPENDENCIES
rack-test
rake
rspec
sass-rails (>= 3.2)
shotgun
shoulda-matchers
sinatra
Expand Down
2 changes: 2 additions & 0 deletions app/assets/stylesheets/application.css.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@import "bootstrap-sprockets";
@import "bootstrap";
50 changes: 50 additions & 0 deletions app/assets/stylesheets/bootstrap.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Core variables and mixins
@import "bootstrap/variables";
@import "bootstrap/mixins";

// Reset and dependencies
@import "bootstrap/normalize";
@import "bootstrap/print";
@import "bootstrap/glyphicons";

// Core CSS
@import "bootstrap/scaffolding";
@import "bootstrap/type";
@import "bootstrap/code";
@import "bootstrap/grid";
@import "bootstrap/tables";
@import "bootstrap/forms";
@import "bootstrap/buttons";

// Components
@import "bootstrap/component-animations";
@import "bootstrap/dropdowns";
@import "bootstrap/button-groups";
@import "bootstrap/input-groups";
@import "bootstrap/navs";
@import "bootstrap/navbar";
@import "bootstrap/breadcrumbs";
@import "bootstrap/pagination";
@import "bootstrap/pager";
@import "bootstrap/labels";
@import "bootstrap/badges";
@import "bootstrap/jumbotron";
@import "bootstrap/thumbnails";
@import "bootstrap/alerts";
@import "bootstrap/progress-bars";
@import "bootstrap/media";
@import "bootstrap/list-group";
@import "bootstrap/panels";
@import "bootstrap/responsive-embed";
@import "bootstrap/wells";
@import "bootstrap/close";

// Components w/ JavaScript
@import "bootstrap/modals";
@import "bootstrap/tooltip";
@import "bootstrap/popovers";
@import "bootstrap/carousel";

// Utility classes
@import "bootstrap/utilities";
@import "bootstrap/responsive-utilities";
3 changes: 3 additions & 0 deletions app/controllers/_user_info_partial.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="user_info">
<img src="<%=user.photo_url%>"> <a href="/user/<%=user.id%>"><%user.name%></a>
</div>
25 changes: 25 additions & 0 deletions app/controllers/bite.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
before "/bite/:id/edit" do
@bite = Bite.find(params[:id])
@authorized = true if current_user == User.find(@bite.author)
end

get "/bite/:id/edit" do
if @authorized != true
redirect to "user/#{session[:user_id]}/bitefeed"
end
erb :'/bites/edit'
end

put "/bite/:id/edit" do
@bite.update(content: params[:bite_content])
#CHECK FOR NEW HASHTAGS AND DESTROY FROM OLD
#check_for_hashtags(@bite)

redirect to "user/#{session[:user_id]}/bitefeed"
end

delete "/bite/:id" do
Bite.find(params[:id]).destroy

redirect to "user/#{session[:user_id]}/bitefeed"
end
6 changes: 6 additions & 0 deletions app/controllers/hashtag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
get '/hashtag/:id' do
@tag = Hashtag.find(params[:id])
@bites_for_tag = @tag.bites

erb :'hashtags/show'
end
29 changes: 24 additions & 5 deletions app/controllers/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,41 @@

post '/' do
@user = User.find_by(name: params[:user][:name])
if @user.authenticate(params[:user][:password])
if User.exists?(name: params[:user][:name]) == false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try

if ! User.exists?

or

unless User.exists?

session[:login_error] = "Invalid ID or password."
redirect to ('/')
elsif @user.authenticate(params[:user][:password])
session[:user_id] = @user.id
redirect to("/user/#{@user.id}/newsfeed")
redirect to("/user/#{@user.id}/bitefeed")
else
session[:login_error] = "Invalid ID or password."
redirect to('/')
end
end

get '/signup' do
if session[:signup_error]
@error = session[:signup_error]
session[:signup_error] = nil
end

erb :'signup'
end

post '/signup' do
@user = User.create(params[:user])

redirect to("/user/#{@user.id}/newsfeed")
if params[:user][:password] != params[:user][:confirm_password]
session[:signup_error] = "Password did not match. Please try again."
redirect to ('/signup')
else
@user = User.create(name: params[:user][:name], password: params[:user][:password])
#hacky way to order bites by created_at?
@user.followed_users << @user
session[:user_id] = @user.id
redirect to("/user/#{@user.id}/bitefeed")
end
end

get '/logout' do
session.clear
redirect to ('/')
end
60 changes: 56 additions & 4 deletions app/controllers/user.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,64 @@
require 'json'

before '/user/:id/bitefeed' do
@authorized = true if current_user == User.find(params[:id])
end

get "/user/:id" do
@user = User.find(params[:id])
@bites = @user.bites
@followers = @user.followers
@followed_users =
@bites = @user.bites.order("created_at DESC")
following?(@user) ? @btn_display = "UNBite Me" : @btn_display = "Bite Me"

erb :'users/show'
end

get "/user/:id/newsfeed" do
post "/user/:id" do
@user = User.find(session[:user_id])
@user_on_page = User.find(params[:id])
if following?(@user_on_page)

@user.followed_relationships.find_by(followed_user_id: params[:id]).destroy
erb :'/partials/_follow_info', layout: false
else
@user.followed_users << @user_on_page
erb :'/partials/_follow_info', layout: false
end
end

$PLACEHOLDER_SAYINGS = ["Everyone is so interested in what you're eating...", "Whatcha biting on?", "Yum that looks good, you should tell everyone!", "Nom Nom Nom", "Don't forget to #foodporn", "Everyone's going to be so jealous when they see this.", "Oh, I'm so interested, please tell me more."]

get "/user/:id/bitefeed" do
if @authorized == false

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unless @authorized

redirect to ("/user/#{session[:user_id]}/bitefeed")
end

@user = User.find(session[:user_id])
@bite_feed = followers_bites
@trending_hashtags = trending_hashtags

erb :'users/bitefeed'
end

post "/user/:id/new" do
@user = User.find(session[:user_id])
@bite = @user.bites.create(content: params[:bite_content])

erb :'/partials/_bite_display', locals: {bite: @bite}, layout: false
end

get "/user/:id/followers" do
@user = User.find(params[:id])
@users_followers = User.find(params[:id]).followers
@followers = @users_followers - [@user]
@users_following = User.find(params[:id]).followed_users
@following = @users_following - [@user]

erb :'users/followers'
end

# get "/user/:id/following" do
# @user = User.find(params[:id])
# @followers = []

# erb :'users/followers'
# end
28 changes: 28 additions & 0 deletions app/helpers/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,32 @@ def current_user
def logged_in?
!current_user.nil?
end

def following?(bite_user)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this belongs in a model?

user = User.find(session[:user_id])
following = user.followed_users.select {|followed_user| followed_user == bite_user}
following.length == 0 ? false : true
end

def followers_bites

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps this goes in a model too?

bite_feed = []
logged_in_user = User.find(session[:user_id])
logged_in_user.followed_users.each {|user| bite_feed << user.bites.order("created_at DESC")}
# bite_feed.sort_by {|bite| bite.created_at}
bite_feed.flatten
end

def check_for_hashtags(bite_object)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also model?

bite_object.content.split(" ").each do |word|
if Hashtag.exists?(current_user.bites.find(bite_object.id).hashtags.find_by(hashtag: word.match(/#\w{0,}/).to_s)) == false
bite_object.hashtags << Hashtag.find_or_create_by(hashtag: word.match(/#\w{0,}/).to_s) if word[0] == "#"
end
end
end

def trending_hashtags

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also model?

Hashtag.all.order("bites_count DESC").limit(5)
end
end

# User.order("users.solutions_count DESC").limit(10)
6 changes: 4 additions & 2 deletions app/models/bite.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Bite < ActiveRecord::Base
belongs_to :user
# Remember to create a migration!
belongs_to :author, class_name: "User", foreign_key: :user_id
has_many :bite_hashtags
has_many :hashtags, through: :bite_hashtags

end
4 changes: 4 additions & 0 deletions app/models/bite_hashtag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class BiteHashtag < ActiveRecord::Base
belongs_to :bite, counter_cache: :hashtags_count
belongs_to :hashtag, counter_cache: :bites_count
end
4 changes: 4 additions & 0 deletions app/models/hashtag.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Hashtag < ActiveRecord::Base
has_many :bite_hashtags
has_many :bites, through: :bite_hashtags
end
9 changes: 6 additions & 3 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ class User < ActiveRecord::Base
has_secure_password

has_many :bites
has_many :follower_relationships, foreign_key: :follower_id, class_name: "Relationship" # TODO: Get queries working properly. Matt switched the foreign keys on lines 5 and 6 with one another, and the associations worked properly. But this may not be the symantically correct way to do this. Read Michael Hartl site!!!!
has_many :followed_relationships, foreign_key: :followed_user_id, class_name: "Relationship"
has_many :follower_relationships, class_name: "Relationship", foreign_key: :followed_user_id
#Determine your followers by finding where your id is the followed_user_id
has_many :followed_relationships, class_name: "Relationship", foreign_key: :follower_id
has_many :followers, through: :follower_relationships, source: :follower
has_many :followed_users, through: :followed_relationships, source: :followed_user
# Remember to create a migration!

# scope :following, ->(following) { where(:follower_id => following AND :followed_user_id => self.id) }

end
Loading