Skip to content

Commit

Permalink
Merge pull request misselliev#17 from misselliev/posts-V2
Browse files Browse the repository at this point in the history
Posts v2
  • Loading branch information
Santiago Torres authored Sep 25, 2019
2 parents c3cfce0 + 718f299 commit 83fde12
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 42 deletions.
13 changes: 11 additions & 2 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,19 @@ def new

def create
@post = current_user.posts.build(post_params)
@post.update(friend_id: params[:friend_id].to_i)
if @post.save
redirect_to posts_index_path
redirecting_data(params[:from], params[:friend_id].to_i) if params[:from] == 'user'
redirecting_data(params[:from]) if params[:from] == 'index'
else
flash[:alert] = 'Something went wrong'
render 'new'
end
end

def index
@posts = current_user.news_feed
@post = Post.new
end

def show
Expand Down Expand Up @@ -61,6 +65,11 @@ def authorized_viewer(user)
end

def post_params
params.require(:post).permit(:content, :author_id)
params.require(:post).permit(:content, :author_id, :friend_id)
end

def redirecting_data(from, user = nil)
redirect_to posts_index_path if from == 'index'
redirect_to user_path(user) if from == 'user'
end
end
2 changes: 2 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ def create

def index
@users = User.all
@post = Post.new
end

def show
@post = Post.new
@user = User.find_by_id(params[:id])
@is_pending = current_user.friendships.is_a_pending(@user).exists?
@is_incoming = @user.friendships.is_a_pending(current_user).exists?
Expand Down
6 changes: 6 additions & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,16 @@

class Post < ApplicationRecord
belongs_to :author, class_name: 'User'
validates :author, presence: true

belongs_to :friend, class_name: 'User'
validates :friend, presence: true

has_many :comments
has_many :likes

validates :content, presence: true, length: { minimum: 1, maximum: 500 }
scope :recent_posts, -> { where('created_at < ?', Time.current).order(created_at: :DESC) }
scope :mine_and_friends, ->(user) { where(friend_id: user).or(Post.where(author_id: user)).order(created_at: :DESC) }
scope :last_five_posts, -> { where('created_at < ?', Time.current).order(created_at: :DESC).limit(5) }
end
5 changes: 4 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ def downcase_email
end

def news_feed
Post.where(author: friends + [self]).recent_posts
a = Post.where(author: friends + [self]).mine_and_friends(self)
b = Post.where(author: friends + [self]).recent_posts
c = a + b
c.uniq!
end

def self.new_with_session(params, session)
Expand Down
22 changes: 11 additions & 11 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,17 @@
</div>
<br/>
<div class="ui container"><%= yield(:landing) %></div>
<div class="ui grid">
<div class="four wide column">

<%= yield(:left_side) %>
</div>
<div class="eight wide column">
<%= yield %>
</div>
<div class="four wide column">
<%= yield(:right_side) %>

<div class="ui centered container">
<div class="ui grid">
<div class="four wide column">
<%= yield(:left_side) %>
</div>
<div class="eight wide column">
<%= yield %>
</div>
<div class="four wide column">
<%= yield(:right_side) %>
</div>
</div>
</div>
</body>
Expand Down
2 changes: 2 additions & 0 deletions app/views/posts/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
</h2>
</div>
<br>
<%= render "shared/postform", locals: { object: current_user, from: 'index' } %>
<br>
<div class="centered aligned">
<% @posts.each do |post| %>
<%= render 'shared/display_post', locals: { object: post, from: 'index'} %>
Expand Down
11 changes: 0 additions & 11 deletions app/views/posts/new.html.erb

This file was deleted.

2 changes: 1 addition & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
</div>
<div class="ui segment">
<div class="content">
<%= @post.content %>
<%= simple_format(@post.content) %>
<% if post_owner?(@post) %>
<%= link_to edit_post_path(@post) do %>
<div class="ui bottom right attached mini label">EDIT</div>
Expand Down
11 changes: 9 additions & 2 deletions app/views/shared/_display_post.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
<% end %>
<div class="content">
<%= link_to user_path(locals[:object].author) do %>
<div class="ui sub header"><%= "#{locals[:object].author.name} #{locals[:object].author.lastname}" %></div>
<% if locals[:object].author != locals[:object].friend %>
<div class="ui sub header"><%= "#{locals[:object].author.name} #{locals[:object].author.lastname}" %>
<i class="angle right icon" style="margin: 0 7px 0 7px"></i>
<%= "#{locals[:object].friend.name} #{locals[:object].friend.lastname}" %>
</div>
<% else %>
<div class="ui sub header"><%= "#{locals[:object].author.name} #{locals[:object].author.lastname}" %></div>
<% end %>
<% end %>
<small style="color: grey"><%= time_ago_in_words(locals[:object].created_at, include_seconds: true).gsub('about', '') %></small>
</div>
Expand All @@ -18,7 +25,7 @@
</div>
<div class="ui segment">
<div class="content">
<%= locals[:object].content %>
<%= simple_format(locals[:object].content) %>
</div>
</div>
<div class="ui segment card">
Expand Down
3 changes: 0 additions & 3 deletions app/views/shared/_links.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,5 @@
<a class="item">
<%= link_to 'Users index', users_index_path %>
</a>
<a class="item">
<%= link_to 'Create a post', new_post_path %>
</a>
</div>
</div>
20 changes: 13 additions & 7 deletions app/views/shared/_postform.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
<%= form_for(@post, :html => {:class => "ui form ten wide column"}) do |f| %>
<br>
<%= form_for(@post, from: locals[:from], object: locals[:object], html: { class: "ui form" }) do |f| %>
<%= render "devise/shared/error_messages", resource: @post %>
<div class="field">
<%= f.text_area :content, placeholder: "Write your post here..." %>
</div>
<%= hidden_field_tag 'friend_id', locals[:object].id %>
<%= hidden_field_tag 'from', locals[:from] %>
<% if locals[:from] == 'index' %>
<%= f.text_area :content, placeholder: "Write your post here...", style: "height: 30px" %>
<% elsif locals[:from] == 'user' && locals[:object] != current_user %>
<%= f.text_area :content, placeholder: "Write a post to #{locals[:object].name}", style: "height: 30px" %>
<% else %>
<%= f.text_area :content, placeholder: "Write your post here...", style: "height: 30px" %>
<% end %>

<br>
<div class="ui centered grid actions">
<%= f.submit "Post", class: 'medium ui teal basic button' %>
</div>
<%= f.submit "Post", class: 'medium ui teal basic fluid button'%>
<br>
<% end %>
5 changes: 3 additions & 2 deletions app/views/users/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
<i class="ui user outline icon"></i>
</h2>
</div>
<br>
<div class="centered aligned">
<% if current_user.friends.include?(@user) || current_user == @user %>
<% @user.posts.recent_posts.each do |post| %>

<%= render "shared/postform", locals: { object: @user, from: 'user' } %>
<% Post.mine_and_friends(@user).each do |post| %>
<%= render 'shared/display_post', locals: {object: post, from: 'user', user: @user} %>
<% end %>
<% end %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20190925163224_add_friend_id_to_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFriendIdToPost < ActiveRecord::Migration[5.2]
def change
add_column :posts, :friend_id, :integer
end
end
5 changes: 5 additions & 0 deletions db/migrate/20190925165947_add_fk_to_friend_id_in_post.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddFkToFriendIdInPost < ActiveRecord::Migration[5.2]
def change
add_foreign_key :posts, :users, column: :friend_id
end
end
4 changes: 3 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: 2019_09_20_163305) do
ActiveRecord::Schema.define(version: 2019_09_25_165947) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -49,6 +49,7 @@
t.integer "author_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "friend_id"
end

create_table "users", force: :cascade do |t|
Expand All @@ -75,4 +76,5 @@
add_foreign_key "likes", "posts"
add_foreign_key "likes", "users"
add_foreign_key "posts", "users", column: "author_id"
add_foreign_key "posts", "users", column: "friend_id"
end
14 changes: 13 additions & 1 deletion db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,26 @@
updated_at: date_time)

# Creating posts
5.times do
3.times do
date_time = Faker::Time.between_dates(from: 3.months.ago - 1, to: Date.today, period: :all)
user.posts.build(
content: Faker::Hacker.say_something_smart,
friend_id: user,
created_at: date_time,
updated_at: date_time
).save
end

2.times do
friend = Array(0..4).sample
date_time = Faker::Time.between_dates(from: 3.months.ago - 1, to: Date.today, period: :all)
user.posts.build(
content: Faker::Hacker.say_something_smart,
friend_id: friend,
created_at: date_time,
updated_at: date_time
).save
end
end

# Creating friendships
Expand Down

0 comments on commit 83fde12

Please sign in to comment.