diff --git a/app/controllers/posts_controller.rb b/app/controllers/posts_controller.rb index 9ec7d4d..f861f37 100644 --- a/app/controllers/posts_controller.rb +++ b/app/controllers/posts_controller.rb @@ -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 @@ -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 diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 4b6488c..af17798 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -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? diff --git a/app/models/post.rb b/app/models/post.rb index 0126518..ab41e6b 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -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 diff --git a/app/models/user.rb b/app/models/user.rb index b85b014..8002a7e 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -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) diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 6d4c533..f74cc71 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -29,17 +29,17 @@
<%= yield(:landing) %>
-
-
- - <%= yield(:left_side) %> -
-
- <%= yield %> -
-
- <%= yield(:right_side) %> - +
+
+
+ <%= yield(:left_side) %> +
+
+ <%= yield %> +
+
+ <%= yield(:right_side) %> +
diff --git a/app/views/posts/index.html.erb b/app/views/posts/index.html.erb index c65bad5..f71195a 100644 --- a/app/views/posts/index.html.erb +++ b/app/views/posts/index.html.erb @@ -5,6 +5,8 @@

+<%= render "shared/postform", locals: { object: current_user, from: 'index' } %> +
<% @posts.each do |post| %> <%= render 'shared/display_post', locals: { object: post, from: 'index'} %> diff --git a/app/views/posts/new.html.erb b/app/views/posts/new.html.erb deleted file mode 100644 index d884b6c..0000000 --- a/app/views/posts/new.html.erb +++ /dev/null @@ -1,11 +0,0 @@ -
-

- Create a Post - -

-
-
-
- <%= render "shared/postform" %> - <%= render 'shared/columns'%> -
diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 79e98b3..506bd00 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -27,7 +27,7 @@
- <%= @post.content %> + <%= simple_format(@post.content) %> <% if post_owner?(@post) %> <%= link_to edit_post_path(@post) do %>
EDIT
diff --git a/app/views/shared/_display_post.html.erb b/app/views/shared/_display_post.html.erb index 3255cdf..a135b96 100644 --- a/app/views/shared/_display_post.html.erb +++ b/app/views/shared/_display_post.html.erb @@ -9,7 +9,14 @@ <% end %>
<%= link_to user_path(locals[:object].author) do %> -
<%= "#{locals[:object].author.name} #{locals[:object].author.lastname}" %>
+ <% if locals[:object].author != locals[:object].friend %> +
<%= "#{locals[:object].author.name} #{locals[:object].author.lastname}" %> + + <%= "#{locals[:object].friend.name} #{locals[:object].friend.lastname}" %> +
+ <% else %> +
<%= "#{locals[:object].author.name} #{locals[:object].author.lastname}" %>
+ <% end %> <% end %> <%= time_ago_in_words(locals[:object].created_at, include_seconds: true).gsub('about', '') %>
@@ -18,7 +25,7 @@
- <%= locals[:object].content %> + <%= simple_format(locals[:object].content) %>
diff --git a/app/views/shared/_links.html.erb b/app/views/shared/_links.html.erb index 8d631d5..3bae0ff 100644 --- a/app/views/shared/_links.html.erb +++ b/app/views/shared/_links.html.erb @@ -6,8 +6,5 @@ <%= link_to 'Users index', users_index_path %> - - <%= link_to 'Create a post', new_post_path %> -
\ No newline at end of file diff --git a/app/views/shared/_postform.html.erb b/app/views/shared/_postform.html.erb index c7ca8b3..34c517a 100644 --- a/app/views/shared/_postform.html.erb +++ b/app/views/shared/_postform.html.erb @@ -1,11 +1,17 @@ -<%= form_for(@post, :html => {:class => "ui form ten wide column"}) do |f| %> +
+<%= form_for(@post, from: locals[:from], object: locals[:object], html: { class: "ui form" }) do |f| %> <%= render "devise/shared/error_messages", resource: @post %> -
- <%= f.text_area :content, placeholder: "Write your post here..." %> -
+ <%= 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 %> +
-
- <%= f.submit "Post", class: 'medium ui teal basic button' %> -
+ <%= f.submit "Post", class: 'medium ui teal basic fluid button'%>
<% end %> \ No newline at end of file diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 9ef7b4d..7220577 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -4,10 +4,11 @@
-
<% 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 %> diff --git a/db/migrate/20190925163224_add_friend_id_to_post.rb b/db/migrate/20190925163224_add_friend_id_to_post.rb new file mode 100644 index 0000000..0c2bbce --- /dev/null +++ b/db/migrate/20190925163224_add_friend_id_to_post.rb @@ -0,0 +1,5 @@ +class AddFriendIdToPost < ActiveRecord::Migration[5.2] + def change + add_column :posts, :friend_id, :integer + end +end diff --git a/db/migrate/20190925165947_add_fk_to_friend_id_in_post.rb b/db/migrate/20190925165947_add_fk_to_friend_id_in_post.rb new file mode 100644 index 0000000..f9c8c97 --- /dev/null +++ b/db/migrate/20190925165947_add_fk_to_friend_id_in_post.rb @@ -0,0 +1,5 @@ +class AddFkToFriendIdInPost < ActiveRecord::Migration[5.2] + def change + add_foreign_key :posts, :users, column: :friend_id + end +end diff --git a/db/schema.rb b/db/schema.rb index 6ddcbe2..5577c36 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -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" @@ -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| @@ -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 diff --git a/db/seeds.rb b/db/seeds.rb index e6ea71b..9c90539 100644 --- a/db/seeds.rb +++ b/db/seeds.rb @@ -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