Skip to content

Commit

Permalink
Merge branch 'sprint2-issue9#CRUD-Implementation' of https://github.c…
Browse files Browse the repository at this point in the history
  • Loading branch information
angel23v committed Mar 8, 2024
2 parents 4f82c34 + 30a7584 commit 4884f9f
Show file tree
Hide file tree
Showing 14 changed files with 99 additions and 34 deletions.
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ gem "pg", "~> 1.1"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", ">= 5.0"

gem 'rubocop', '~> 1.62', require: false

# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"

Expand Down
25 changes: 25 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ GEM
tzinfo (~> 2.0)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
ast (2.4.2)
base64 (0.2.0)
bcrypt (3.1.20)
bigdecimal (3.1.6)
Expand Down Expand Up @@ -128,6 +129,8 @@ GEM
jbuilder (2.11.5)
actionview (>= 5.0.0)
activesupport (>= 5.0.0)
json (2.7.1)
language_server-protocol (3.17.0.3)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -165,6 +168,10 @@ GEM
racc (~> 1.4)
nokogiri (1.16.2-x86_64-linux)
racc (~> 1.4)
parallel (1.24.0)
parser (3.3.0.5)
ast (~> 2.4.1)
racc
orm_adapter (0.5.0)
pg (1.5.4)
psych (5.1.2)
Expand Down Expand Up @@ -210,6 +217,7 @@ GEM
rake (>= 12.2)
thor (~> 1.0, >= 1.2.2)
zeitwerk (~> 2.6)
rainbow (3.1.1)
rake (13.1.0)
rdoc (6.6.2)
psych (>= 4.0.0)
Expand All @@ -220,6 +228,21 @@ GEM
actionpack (>= 5.2)
railties (>= 5.2)
rexml (3.2.6)

rubocop (1.62.0)
json (~> 2.3)
language_server-protocol (>= 3.17.0)
parallel (~> 1.10)
parser (>= 3.3.0.2)
rainbow (>= 2.2.2, < 4.0)
regexp_parser (>= 1.8, < 3.0)
rexml (>= 3.2.5, < 4.0)
rubocop-ast (>= 1.31.1, < 2.0)
ruby-progressbar (~> 1.7)
unicode-display_width (>= 2.4.0, < 3.0)
rubocop-ast (1.31.1)
parser (>= 3.3.0.4)
ruby-progressbar (1.13.0)
ruby-vips (2.2.1)
ffi (~> 1.12)
rubyzip (2.3.2)
Expand All @@ -246,6 +269,7 @@ GEM
railties (>= 6.0.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
warden (1.2.9)
rack (>= 2.0.9)
web-console (4.2.1)
Expand Down Expand Up @@ -282,6 +306,7 @@ DEPENDENCIES
pg (~> 1.1)
puma (>= 5.0)
rails (~> 7.1.3)
rubocop (~> 1.62)
selenium-webdriver
sprockets-rails
stimulus-rails
Expand Down
1 change: 1 addition & 0 deletions app/controllers/main_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class MainController < ApplicationController
before_action :authenticate_user!
def home
@posts = Post.all
end
end
32 changes: 17 additions & 15 deletions app/controllers/posts_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class PostsController < ApplicationController
before_action :authenticate_user!, only: [:new, :create]
before_action :authenticate_user!
before_action :set_post, only: %i[show destroy edit update]
def new
@post = Post.new
end
Expand All @@ -9,7 +10,8 @@ def create

respond_to do |format|
if @post.save
redirect_to new_post_path
format.html { redirect_to root_path, notice: 'Post creado exitosamente.' }
format.json { render :show, status: :created, location: @post }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @post.errors, status: :unprocessable_entity }
Expand All @@ -21,13 +23,12 @@ def index
@posts = Post.user_post(current_user)
end

def edit
end
def edit; end

def update
respond_to do |format|
if @post.update(post_params)
format.html { redirect_to user_url(@post), notice: "User was successfully updated." }
format.html { redirect_to root_path, notice: "Post was successfully updated." }
format.json { render :show, status: :ok, location: @post }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -37,22 +38,23 @@ def update
end

def destroy
@post.destroy!
@post.destroy

respond_to do |format|
format.html { redirect_to users_url, notice: "Post was successfully destroyed." }
format.html { redirect_to root_path, notice: 'Post was succesfully deleted.' }
format.json { head :no_content }
end
end

private
# Use callbacks to share common setup or constraints between actions.
def set_post
@user = Post.user_post(current_user).find(params[:id])
end

# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:body)
end
# Use callbacks to share common setup or constraints between actions.
def set_post
@post = Post.user_post(current_user).find(params[:id])
end

# Only allow a list of trusted parameters through.
def post_params
params.require(:post).permit(:body)
end
end
5 changes: 3 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class UsersController < ApplicationController
before_action :set_user, only: %i[ show edit update destroy ]
before_action :authenticate_user!

# GET /users or /users.json
def index
Expand Down Expand Up @@ -48,6 +48,7 @@ def update
end

# DELETE /users/1 or /users/1.json
=begin
def destroy
@user.destroy!
Expand All @@ -56,7 +57,7 @@ def destroy
format.json { head :no_content }
end
end

=end
private
# Use callbacks to share common setup or constraints between actions.
def set_user
Expand Down
1 change: 1 addition & 0 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ class Post < ApplicationRecord

scope :user_post, ->(user) { where(user_id: user.id) }

has_one_attached :post_image
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class User < ApplicationRecord
has_one_attached :image_profile
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
has_many :posts
Expand Down
2 changes: 1 addition & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<head>
<title>S10a1ProyectoFinalTeam03S10a1ProyectoFinal</title>
<title>GYMRAT APP</title>
<meta name="viewport" content="width=device-width,initial-scale=1">
<%= csrf_meta_tags %>
<%= csp_meta_tag %>
Expand Down
31 changes: 29 additions & 2 deletions app/views/main/home.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,29 @@
<h1>Main#home</h1>
<p>Find me in app/views/main/home.html.erb</p>
<h1>Feed del usuario <%= current_user.id %></h1>
<%= Date.today %>
<p>Esto es un template para probar funcionalidades, el diseño se modificará después.</p>

<%= button_to 'Sign Out', destroy_user_session_path, method: :delete %>


<%= link_to new_post_path do%>
<div style="margin-top: 1rem;">
<span>Create a new post</span>
</div>
<%end%>
<% @posts.each do |post| %>
<div class='post' style="background-color: antiquewhite;">
<h2> <%=post.body%> </h2>
<h3> Este es el post <%=post.id%> y pertenezo al usuario <%=post.user_id%> </h3>
<%= link_to post_path(post.id) do%>
<span>Details</span>
<%end%>

<%= link_to edit_post_path(post.id) do%>
<div>Edit</div>
<%end%>

<%= button_to post_path(post.id), method: :delete do%>
<span>Delete</span>
<%end%>
</div>
<% end %>
13 changes: 13 additions & 0 deletions app/views/posts/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<h2 class="mt-0"><%= title %></h2>
<%= form_with(model: @post) do |form| %>

<div>
<%= form.label :body, style: "display: block" %>
<%= form.text_field :body %>
</div>

<div>
<%= form.submit %>
</div>

<% end %>
1 change: 1 addition & 0 deletions app/views/posts/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render 'form', event: @event, title: 'Edit post' %>
13 changes: 1 addition & 12 deletions app/views/posts/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1 @@
<%= form_with(model: @post) do |form| %>

<div>
<%= form.label :body, style: "display: block" %>
<%= form.text_field :body %>
</div>

<div>
<%= form.submit %>
</div>

<% end %>
<%= render 'form', event: @event, title: 'New post' %>
3 changes: 2 additions & 1 deletion app/views/posts/show.html.erb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
<h1> Hola </h1>
<h1>Publicación de <%= current_user.email %></h1>
<h2><%=@post.body%></h2>
3 changes: 2 additions & 1 deletion db/schema.rb

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

0 comments on commit 4884f9f

Please sign in to comment.