-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added other controllers relevant to the twitter clone
- Loading branch information
Huvinesh Rajendran
committed
Oct 1, 2024
1 parent
e1ca6ea
commit 423a4e3
Showing
24 changed files
with
249 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
class PostsController < ApplicationController | ||
before_action :require_authentication | ||
|
||
def index | ||
@post = Post.new | ||
@posts = Post.all.order(created_at: :desc) | ||
end | ||
|
||
def create | ||
@post = current_user.posts.new(post_params) | ||
|
||
respond_to do |f| | ||
if @post.save | ||
format.turbo_stream | ||
else | ||
format.html do | ||
flash[:post_errors] = @post.errors.full_messages | ||
redirect_to root_path | ||
end | ||
end | ||
end | ||
end | ||
|
||
def show | ||
@post = Post.find(params[:id]) | ||
end | ||
|
||
def destroy | ||
@post = current_user.posts.find(params[:id]) | ||
@post.destroy | ||
redirect_to root_path, notice: "Post has been successfully deleted." | ||
end | ||
|
||
private | ||
|
||
def post_params | ||
params.require(:post).permit(:body) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
class ProfilesController < ApplicationController | ||
def show | ||
@user = User.find_by(username: params[:username]) | ||
@tweets = @user.tweets.order(created_at: :desc) | ||
end | ||
|
||
def edit | ||
@user = current_user | ||
end | ||
|
||
def update | ||
@user = current_user | ||
if @user.update(profile_params) | ||
redirect_to profile_path(@user.username), notice: "Profile updated successfully" | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
private | ||
|
||
def profile_params | ||
params.require(:user).permit(:name, :username, :bio) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class UsersController < ApplicationController | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module PostsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module RegistrationsHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
module UsersHelper | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class Post < ApplicationRecord | ||
belongs_to :user | ||
|
||
validates :content, presence: true, length: { maximum: 280 } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
class Relationship < ApplicationRecord | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,15 @@ | ||
class User < ApplicationRecord | ||
has_secure_password | ||
has_many :sessions, dependent: :destroy | ||
has_many :posts, dependent: :destroy | ||
has_many :active_relationships, class_name: "Relationship", foreign_key: "follower_id", dependent: :destroy | ||
has_many :passive_relationships, class_name: "Relationship", foreign_key: "followed_id", dependent: :destroy | ||
has_many :following, through: :active_relationships, source: :followed | ||
has_many :followers, through: :passive_relationships, source: :follower | ||
|
||
normalizes :email_address, with: -> e { e.strip.downcase } | ||
validates :username, presence: true, uniqueness: true | ||
validates :first_name, presence: true | ||
validates :last_name, presence: true | ||
validates :email_address, presence: true, uniqueness: true | ||
normalizes :email_address, with: ->(e) { e.strip.downcase } | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<h1> Edit Profile </h1> | ||
' | ||
<%= form_with(model: @user, url: profile_path, local: true) do |f| %> | ||
<%= f.label :name %> | ||
<%= f.text_field :name %> | ||
<%= f.label :username %> | ||
<%= f.text_field :username %> | ||
<%= f.label :bio %> | ||
<%= f.text_field :bio %> | ||
<%= f.submit 'Update Profile' %> | ||
<% end %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<h1><%= @user.first_name + @user.last_name %></h1> | ||
<h2><%= @user.username %></h2> | ||
<p><%= @user.bio %><p> | ||
<% if current_user == @user %> | ||
<%= link_to 'Edit Profile', edit_profile_path %> | ||
<% end %> | ||
|
||
<h2> Posts </h2> | ||
<%= render @posts %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreatePosts < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :posts do |t| | ||
t.text :content | ||
t.references :user, null: false, foreign_key: true | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
class CreateRelationships < ActiveRecord::Migration[8.0] | ||
def change | ||
create_table :relationships do |t| | ||
t.integer :follower_id | ||
t.integer :followed_id | ||
|
||
t.timestamps | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AddProfileFieldsToUsers < ActiveRecord::Migration[8.0] | ||
def change | ||
add_column :users, :name, :string | ||
add_column :users, :username, :string | ||
add_column :users, :bio, :text | ||
end | ||
end |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class PostsControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class RegistrationsControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class UsersControllerTest < ActionDispatch::IntegrationTest | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
content: MyText | ||
user: one | ||
|
||
two: | ||
content: MyText | ||
user: two |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html | ||
|
||
one: | ||
follower_id: 1 | ||
followed_id: 1 | ||
|
||
two: | ||
follower_id: 1 | ||
followed_id: 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class PostTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
require "test_helper" | ||
|
||
class RelationshipTest < ActiveSupport::TestCase | ||
# test "the truth" do | ||
# assert true | ||
# end | ||
end |