Skip to content

Commit

Permalink
Merge pull request #17 from tatiananantes/Comments
Browse files Browse the repository at this point in the history
Comments
  • Loading branch information
monawoh authored Dec 16, 2021
2 parents 4c98b1d + 5905434 commit 0b0ff17
Show file tree
Hide file tree
Showing 31 changed files with 306 additions and 33 deletions.
3 changes: 3 additions & 0 deletions app/assets/stylesheets/album.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Album controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/albums.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Albums controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
11 changes: 9 additions & 2 deletions app/assets/stylesheets/application.scss
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
@import url("https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css");
@import url("https://fonts.googleapis.com/css2?family=Source+Sans+Pro:wght@300;400;600;700&display=swap");

$brand-primary: #4376d4;

:root {
--primary-color: #4376d4;
}

h1 {
font-size: calc(1.375rem + 1vw);
Expand All @@ -14,6 +17,10 @@ h1 {
padding-top: 20px;
}

.soft-bottom {
padding-bottom: 20px;
}

html,
body {
min-height: 100%;
Expand All @@ -31,7 +38,7 @@ body {

.site-main-wrap {
background: #f7f7f7;
padding: 80px 0 40px;
padding: 100px 0 40px;
display: block;
height: 100%;
}
Expand Down
3 changes: 3 additions & 0 deletions app/assets/stylesheets/photos.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the Photos controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: https://sass-lang.com/
26 changes: 26 additions & 0 deletions app/assets/stylesheets/posts.scss
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,30 @@
@media (min-width: 1200px) {
width: 45vw;
}
}

.reverse-column {
display: flex;
flex-direction: column-reverse;
}

textarea.form-control {
height: 100px;
}

.mg-l-auto {
display: block;
margin: 20px 0 20px auto;
}

.comment {
margin-bottom: 12px;
padding-bottom: 12px;
border-bottom: 1px solid rgba(0, 0, 0, 0.1);
p {
margin: 0;
}
&:last-of-type {
border-bottom: 0;
}
}
33 changes: 33 additions & 0 deletions app/controllers/albums_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class AlbumsController < ApplicationController
before_action :find_user

def new
@album = Album.new
end

def create
Album.create(album_params.merge(user_id: session[:user_id]))
redirect_to user_albums_path
end

def index
@album = @user.albums.all
end

def show
@album = @user.albums.find(params[:id])
end



private

def album_params
params.require(:album).permit(:name)
end

def find_user
@user = User.find_by_id(session[:user_id])
end

end
2 changes: 2 additions & 0 deletions app/controllers/photos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class PhotosController < ApplicationController
end
2 changes: 2 additions & 0 deletions app/helpers/album_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AlbumHelper
end
2 changes: 2 additions & 0 deletions app/helpers/albums_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module AlbumsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/photos_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PhotosHelper
end
4 changes: 4 additions & 0 deletions app/models/album.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class Album < ApplicationRecord
belongs_to :user
has_many :photos
end
7 changes: 7 additions & 0 deletions app/models/photo.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Photo < ApplicationRecord
belongs_to :album
belongs_to :user
has_many :comments, dependent: :destroy
has_one_attached :image
has_many :likes, dependent: :destroy
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ class User < ApplicationRecord
has_many :comments
acts_as_favoritor
has_many :likes
has_many :albums
has_many :photos

has_one_attached :avatar

Expand Down
3 changes: 3 additions & 0 deletions app/views/albums/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<% @user.albums.reverse.each do |album| %>
<p><%= album.name%></p>
<%end%>
19 changes: 19 additions & 0 deletions app/views/albums/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="center-block">
<div class="form-group-wrap">

<h1> What's the name of your album? </h1>
<%= form_with model: [ @user, @user.albums.build ], local: true do |form| %>

<div class="form-group">
<%= form.label :name, class: "control-label"%>
<%= form.text_field :name, class:"form-control" %>
</div>

<div class="form-group">
<%= form.submit "Create!", class: "btn btn-primary" %>
</div>

<% end %>

</div>
</div>
3 changes: 3 additions & 0 deletions app/views/albums/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<%=@album.name%>

This is an album
62 changes: 34 additions & 28 deletions app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,40 @@
<%= image_tag @post.image.variant(resize_to_limit: [1200, 630]) %>
<%end%>
</div>
<div class="post-footer soft-bottom">
<div>
<span><%= @post.comments.count %> <i class="bi bi-chat-text"></i> </span>
<span><%= @post.likes.count %> <i class="bi bi-hand-thumbs-up"></i> </span>
</div>
</div>
<div class="reverse-column">
<div>
<h2 class="h4 soft-bottom"><%= @post.comments.count %> <%= (@post.comments.count) == 1 ? 'Comment' : 'Comments'%></h2>
<% @post.comments.reverse.each do |comment| %>
<div class="comment">
<div class="profile-holder post-heading">
<% if comment.user.avatar.attached? %>
<%= image_tag comment.user.avatar.variant(resize_to_limit: [100, 100]) %>
<%end%>
<p class="profile-name"><a href="/users/<%=comment.user.id %>"><%=comment.user.username %></a> <br />
Commented <%= comment.created_at.strftime("%d %B %G at %R") %></p>
</div>
<p><%= comment.body %></p>
</div>
<% end %>
</div>
<div>
<%= form_with model: [ @post, @post.comments.build ], local: true do |form| %>
<div class="form-group">
<%= form.text_area :body, class: "form-control form-control-tall"%>
</div>
<div class="form-group">
<%= form.submit "Add comment", class: "btn btn-primary mg-l-auto" %>
</div>
<% end %>
<hr>
</div>
</div>
</div>
</div>



<h2>Comments</h2>
<% @post.comments.reverse.each do |comment| %>
<div class="profile-holder">
<% if comment.user.avatar.attached? %>
<%= image_tag comment.user.avatar.variant(resize_to_limit: [100, 100]) %>
<%end%>
<p><span><%= comment.user.username %></span> commented:</p>
</div>
<p><%= comment.body %></p>
<% end %>

<h2>Add a comment:</h2>
<%= form_with model: [ @post, @post.comments.build ], local: true do |form| %>

<p>
<%= form.label :body %><br>
<%= form.text_area :body %>
</p>
<p>
<%= form.submit %>
</p>
<% end %>


<p><%= @post.likes.count %> <%= (@post.likes.count) == 1 ? 'Like' : 'Likes'%></p>
<%= form_with model: [ @post, @post.likes.create ], local: true do |form| %> <%= form.submit "Like", class: "btn btn-primary" %> <% end %>
17 changes: 16 additions & 1 deletion app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,30 @@
<%end%>
<div>
<h1 class="h3"> <%= @user.username %> </h1>
<p>Member since: <%= @user.created_at.strftime("%d %G") %></p>
<p>Member since: <%= @user.created_at.strftime("%d %B %G") %></p>
<% if @user.id == session[:user_id]%>
<%= link_to "Edit details", edit_user_path(@user) %>
<%end%>

<% @user.albums.reverse.each do |album| %>
<p><a href="/users/<%=@user.id%>/albums/<%=album.id%>"><%= album.name%></p></a>
<%end%>

<% if @user.id == session[:user_id]%>
<%= link_to "Create album", new_user_album_path(@user) %>
<%end%>

</div>

</div>
</div>
<div class="col-sm-8 col-lg-7">
<% if @user.posts.count == 0 %>
<div class="text-center">
<h2 class="soft-bottom soft-top">Looks like you have yet to post anything.</h2>
<%= link_to "Join the conversation", posts_path, class: "btn btn-primary" %>
</div>
<% end %>
<% @user.posts.reverse.each do |post| %>
<div class="post bg-white">
<div class="profile-holder post-heading">
Expand Down
13 changes: 12 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,21 @@
Rails.application.routes.draw do
root "users#new"
resources :users
resources :users do
resources :albums do
resources :photos
end
end

resources :posts do
resources :comments, :likes
end



resources :photos do
resources :comments, :likes
end

get '/login', to: 'sessions#login'
post '/login', to: 'sessions#create'
post '/logout', to: 'sessions#delete'
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20211216152824_create_albums.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateAlbums < ActiveRecord::Migration[6.1]
def change
create_table :albums do |t|
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
10 changes: 10 additions & 0 deletions db/migrate/20211216153217_create_photos.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class CreatePhotos < ActiveRecord::Migration[6.1]
def change
create_table :photos do |t|
t.references :album, null: false, foreign_key: true
t.references :user, null: false, foreign_key: true

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20211216155141_add_name_to_albums.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNameToAlbums < ActiveRecord::Migration[6.1]
def change
add_column :albums, :name, :string
end
end
22 changes: 21 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2021_12_14_161456) do
ActiveRecord::Schema.define(version: 2021_12_16_155141) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -43,6 +43,14 @@
t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true
end

create_table "albums", force: :cascade do |t|
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.string "name"
t.index ["user_id"], name: "index_albums_on_user_id"
end

create_table "comments", force: :cascade do |t|
t.text "body"
t.bigint "post_id", null: false
Expand Down Expand Up @@ -80,6 +88,15 @@
t.index ["user_id"], name: "index_likes_on_user_id"
end

create_table "photos", force: :cascade do |t|
t.bigint "album_id", null: false
t.bigint "user_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["album_id"], name: "index_photos_on_album_id"
t.index ["user_id"], name: "index_photos_on_user_id"
end

create_table "posts", force: :cascade do |t|
t.string "message"
t.datetime "created_at", null: false
Expand All @@ -98,9 +115,12 @@

add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id"
add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id"
add_foreign_key "albums", "users"
add_foreign_key "comments", "posts"
add_foreign_key "comments", "users"
add_foreign_key "likes", "posts"
add_foreign_key "likes", "users"
add_foreign_key "photos", "albums"
add_foreign_key "photos", "users"
add_foreign_key "posts", "users"
end
15 changes: 15 additions & 0 deletions spec/helpers/album_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the AlbumHelper. For example:
#
# describe AlbumHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe AlbumHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
Loading

0 comments on commit 0b0ff17

Please sign in to comment.