Skip to content

Commit

Permalink
Add Project actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Sanchezdav committed Feb 23, 2021
1 parent 62cba95 commit ab7dbb0
Show file tree
Hide file tree
Showing 8 changed files with 173 additions and 9 deletions.
1 change: 1 addition & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception

before_action :configure_permitted_parameters, if: :devise_controller?
before_action :authenticate_user!

protected

Expand Down
51 changes: 51 additions & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
class ProjectsController < ApplicationController
before_action :set_project, only: [:show, :edit, :update, :destroy]

def index
@projects = Project.all.order(created_at: :desc)
end

def show
@task = Task.new
end

def new
@project = Project.new
end

def edit
end

def create
@project = current_user.projects.new(project_params)

if @project.save
redirect_to @project, notice: 'Project was successfully created.'
else
render :new
end
end

def update
if @project.update(project_params)
redirect_to @project, notice: 'Project was successfully updated.'
else
render :edit
end
end

def destroy
@project.destroy
redirect_to projects_url, notice: 'Project was successfully destroyed.'
end

private

def set_project
@project = current_user.projects.find(params[:id])
end

def project_params
params.require(:project).permit(:name, :description)
end
end
31 changes: 31 additions & 0 deletions app/views/projects/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= form_with(model: project, local: true) do |form| %>
<% if project.errors.any? %>
<ul>
<% project.errors.full_messages.each do |message| %>
<li><%= message %></li>
<% end %>
</ul>
<% end %>

<div class="flex flex-col mt-4">
<%= form.label :name %>
<%= form.text_field :name, class: 'flex-grow input' %>
</div>

<div class="flex flex-col mt-4">
<%= form.label :description %>
<%= form.text_area :description, class: 'flex-grow input h-36' %>
</div>

<div class="flex flex-col mt-4">
<% if project.persisted? %>
<div class="float-right">
<%= link_to 'Destroy', project, method: :delete, class: "text-red-500", data: { confirm: 'Are you sure?' } %>
</div>
<% end %>

<div class="flex flex-col">
<%= form.submit class: 'btn btn-primary' %>
</div>
</div>
<% end %>
12 changes: 12 additions & 0 deletions app/views/projects/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="lg:px-10 px-4 py-5">
<div class="flex flex-col items-center flex-1 h-full justify-center px-4 sm:px-0">
<h1 class="mb-5 text-xl">Edit project</h1>
<div class="flex rounded-lg shadow-lg w-full sm:w-3/4 lg:w-1/2 bg-white sm:mx-0">
<div class="flex flex-col w-full p-4">
<div class="flex flex-col flex-1 justify-center mb-8">
<%= render 'form', project: @project %>
</div>
</div>
</div>
</div>
</div>
30 changes: 30 additions & 0 deletions app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="container mx-auto my-8 px-4">
<div class="flex justify-between items-center mb-4">
<h1 class="text-3xl">Projects</h1>
<%= link_to 'New project', new_project_path, class: "btn btn-primary" %>
</div>

<% if @projects.any? %>
<div class="bg-white rounded shadow">
<table class="w-full">
<tbody>
<% @projects.each do |project| %>
<tr class="group border-t border-gray-400 hover:bg-gray-100">
<td class="p-3"><%= project.name %></td>
<td>
<%= link_to "View", project, class: "btn btn-primary" %>
<%= link_to "Edit", edit_project_path(project), class: "btn btn-success" %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
<% else %>
<div class="bg-white rounded shadow flex items-center justify-between p-8">
<div class="flex-1 text-center">
<p class="text-2xl font-semibold">Create your first project</p>
</div>
</div>
<% end %>
</div>
12 changes: 12 additions & 0 deletions app/views/projects/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<div class="lg:px-10 px-4 py-5">
<div class="flex flex-col items-center flex-1 h-full justify-center px-4 sm:px-0">
<h1 class="mb-5 text-xl">New project</h1>
<div class="flex rounded-lg shadow-lg w-full sm:w-3/4 lg:w-1/2 bg-white sm:mx-0">
<div class="flex flex-col w-full p-4">
<div class="flex flex-col flex-1 justify-center mb-8">
<%= render 'form', project: @project %>
</div>
</div>
</div>
</div>
</div>
25 changes: 25 additions & 0 deletions app/views/projects/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<div class="flex flex-col items-center justify-center">
<div class="flex items-center justify-between mb-5">
<h1 class="text-xl mr-5"><%= @project.name %></h1>
<div class="flex items-center">
<%= link_to 'All Projects', authenticated_root_path, class: 'text-blue-500 mr-5' %>
<%= link_to 'Edit', edit_project_path(@project), class: 'text-blue-500' %>
</div>
</div>
<div class="flex rounded-lg shadow-lg w-full sm:w-3/4 lg:w-1/2 bg-white sm:mx-0">
<div class="flex flex-col w-full p-4">
<div class="flex flex-col flex-1 justify-center">
<h2 class="text-lg mb-3">Tasks</h2>
<%= form_with(model: [@project, @task]) do |form| %>
<div class="flex mb-3">
<%= form.text_field :content, class: 'w-full rounded-lg mr-2', placeholder: 'Add a task' %>
<%= form.submit 'Create', class: 'btn btn-primary' %>
</div>
<div class="mb-3">
<div data-target="forms.errorsList"></div>
</div>
<% end %>
</div>
</div>
</div>
</div>
20 changes: 11 additions & 9 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
Rails.application.routes.draw do
unauthenticated :user do
devise_scope :user do
root to: 'unauthenticated#index', as: :unauthenticated_root
end
end

authenticated :user do
root to: 'home#index', as: :authenticated_root
unauthenticated :user do
devise_scope :user do
root to: 'unauthenticated#index', as: :unauthenticated_root
end
end

authenticated :user do
root to: 'projects#index', as: :authenticated_root
end

devise_for :users
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html

resources :projects do
resources :tasks, only: :create
end
end

0 comments on commit ab7dbb0

Please sign in to comment.