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 @@