Skip to content

Commit

Permalink
added other controllers relevant to the twitter clone
Browse files Browse the repository at this point in the history
  • Loading branch information
Huvinesh Rajendran committed Oct 1, 2024
1 parent e1ca6ea commit 423a4e3
Show file tree
Hide file tree
Showing 24 changed files with 249 additions and 1 deletion.
39 changes: 39 additions & 0 deletions app/controllers/posts_controller.rb
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
25 changes: 25 additions & 0 deletions app/controllers/profiles_controller.rb
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
2 changes: 2 additions & 0 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class UsersController < ApplicationController
end
2 changes: 2 additions & 0 deletions app/helpers/posts_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module PostsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/registrations_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module RegistrationsHelper
end
2 changes: 2 additions & 0 deletions app/helpers/users_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module UsersHelper
end
5 changes: 5 additions & 0 deletions app/models/post.rb
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
2 changes: 2 additions & 0 deletions app/models/relationship.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
class Relationship < ApplicationRecord
end
11 changes: 10 additions & 1 deletion app/models/user.rb
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
14 changes: 14 additions & 0 deletions app/views/profiles/edit.html.erb
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 %>
9 changes: 9 additions & 0 deletions app/views/profiles/show.html.erb
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 %>
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,7 @@

# Defines the root path route ("/")
# root "posts#index"
get "/:username", to: "profiles#show", as: :profile
get "/profile/edit", to: "profiles#edit", as: :edit_profile
post "/profile", to: "profiles#update"
end
6 changes: 6 additions & 0 deletions db/migrate/20240928171644_create_users.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
class CreateUsers < ActiveRecord::Migration[8.0]
def change
create_table :users do |t|
t.string :username, null: false
t.string :first_name, null: false
t.string :last_name, null: false

t.string :email_address, null: false
t.string :password_digest, null: false

t.boolean :verified, null: false, default: false

t.timestamps
end
add_index :users, :email_address, unique: true
Expand Down
10 changes: 10 additions & 0 deletions db/migrate/20240929121715_create_posts.rb
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
10 changes: 10 additions & 0 deletions db/migrate/20240929125312_create_relationships.rb
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
7 changes: 7 additions & 0 deletions db/migrate/20240929184940_add_profile_fields_to_users.rb
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
48 changes: 48 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions test/controllers/posts_controller_test.rb
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
7 changes: 7 additions & 0 deletions test/controllers/registrations_controller_test.rb
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
7 changes: 7 additions & 0 deletions test/controllers/users_controller_test.rb
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
9 changes: 9 additions & 0 deletions test/fixtures/posts.yml
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
9 changes: 9 additions & 0 deletions test/fixtures/relationships.yml
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
7 changes: 7 additions & 0 deletions test/models/post_test.rb
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
7 changes: 7 additions & 0 deletions test/models/relationship_test.rb
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

0 comments on commit 423a4e3

Please sign in to comment.