Skip to content

Commit

Permalink
Change more things
Browse files Browse the repository at this point in the history
  • Loading branch information
eltonsantos committed Sep 30, 2024
1 parent 59f41a3 commit 2866e3b
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 31 deletions.
5 changes: 4 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ class CategoriesController < ApplicationController

# GET /categories or /categories.json
def index
@categories = Category.all
@categories = Category.joins(tasks: :commitment).where(commitments: { user_id: current_user.id }).distinct
end

# GET /categories/new
def new
@category = Category.new
@categories = Category.joins(tasks: :commitment)
.where(commitments: { user_id: current_user.id })
.distinct
end

# GET /categories/1/edit
Expand Down
16 changes: 12 additions & 4 deletions app/controllers/commitments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class CommitmentsController < ApplicationController

# GET /commitments or /commitments.json
def index
@commitments = Commitment.all
@commitments = Commitment.where(user_id: current_user.id)
end

# GET /commitments/1 or /commitments/1.json
Expand All @@ -22,11 +22,19 @@ def edit

# POST /commitments or /commitments.json
def create

if current_user.commitments.where(active: true).exists?
flash[:alert] = "Você já tem um compromisso ativo. É permitido apenas um compromisso ativo por vez."
render :new and return
end

@commitment = Commitment.new(commitment_params)
@commitment.user = current_user
@commitment.active = true

respond_to do |format|
if @commitment.save
format.html { redirect_to @commitment, notice: "Commitment was successfully created." }
format.html { redirect_to root_path, notice: "Commitment was successfully created." }
format.json { render :show, status: :created, location: @commitment }
else
format.html { render :new, status: :unprocessable_entity }
Expand All @@ -39,7 +47,7 @@ def create
def update
respond_to do |format|
if @commitment.update(commitment_params)
format.html { redirect_to @commitment, notice: "Commitment was successfully updated." }
format.html { redirect_to root_path, notice: "Commitment was successfully updated." }
format.json { render :show, status: :ok, location: @commitment }
else
format.html { render :edit, status: :unprocessable_entity }
Expand All @@ -66,6 +74,6 @@ def set_commitment

# Only allow a list of trusted parameters through.
def commitment_params
params.require(:commitment).permit(:description, :user_id, :progress)
params.require(:commitment).permit(:description, :progress)
end
end
2 changes: 1 addition & 1 deletion app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class HomeController < ApplicationController

def index
@tasks = Task.all.order(:id)
@tasks = current_user.commitments.includes(:tasks).flat_map(&:tasks)
end
end
4 changes: 3 additions & 1 deletion app/controllers/tasks_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ def show
# GET /tasks/new
def new
@task = Task.new
@categories = current_user.commitments.includes(:tasks).flat_map(&:tasks).map(&:category).uniq
@categories = Category.joins(tasks: :commitment)
.where(commitments: { user_id: current_user.id })
.distinct
end

# GET /tasks/1/edit
Expand Down
2 changes: 1 addition & 1 deletion app/helpers/tasks_helper.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module TasksHelper
def category_select(form)
categories = Category.all
categories = Category.joins(tasks: :commitment).where(commitments: { user_id: current_user.id }).distinct
form.collection_select(:category_id, categories, :id, :name, { prompt: 'Selecione uma categoria' }, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full")
end
end
6 changes: 6 additions & 0 deletions app/models/commitment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,10 @@ class Commitment < ApplicationRecord

validates :description, presence: true
validates :progress, numericality: { greater_than_or_equal_to: 0, less_than_or_equal_to: 100 }

def check_active_status
if active && created_at < 7.days.ago
update(active: false)
end
end
end
14 changes: 2 additions & 12 deletions app/views/commitments/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,11 @@
<% end %>

<div class="my-5">
<%= form.label :description %>
<%= form.label :description, "Descrição" %>
<%= form.text_area :description, rows: 4, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :user_id %>
<%= form.text_field :user_id, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="my-5">
<%= form.label :progress %>
<%= form.number_field :progress, class: "block shadow rounded-md border border-gray-400 outline-none px-3 py-2 mt-2 w-full" %>
</div>

<div class="text-right">
<%= form.submit class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
<%= form.submit "Criar compromisso", class: "rounded-lg py-3 px-5 bg-blue-600 text-white inline-block font-medium cursor-pointer" %>
</div>
<% end %>
6 changes: 2 additions & 4 deletions app/views/commitments/new.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<div class="mx-auto md:w-2/3 w-full">
<h1 class="font-bold text-4xl">New commitment</h1>
<div class="mx-auto md:w-2/3 w-full bg-white p-8">
<h1 class="font-bold text-4xl text-stone-700">Novo compromisso</h1>

<%= render "form", commitment: @commitment %>

<%= link_to "Back to commitments", commitments_path, class: "ml-2 rounded-lg py-3 px-5 bg-gray-100 inline-block font-medium" %>
</div>
2 changes: 1 addition & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
</div>
<% else %>
<h1 class="font-bold text-center items-center text-4xl p-8">
Não há tarefas cadastradas para esse compromisso.
Não há dados cadastrados.
</h1>
<% end %>
</main>
Expand Down
12 changes: 6 additions & 6 deletions db/migrate/20240925035902_create_categories.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
class CreateCategories < ActiveRecord::Migration[7.2]
def change
create_table :categories do |t|
t.string :name
class CreateCategories < ActiveRecord::Migration[7.2]
def change
create_table :categories do |t|
t.string :name

t.timestamps
end
t.timestamps
end
end
end
1 change: 1 addition & 0 deletions db/seeds.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
User.create!(email: 'admin@admin.com', password: '123456', name: 'Admin', hours_per_week: 40)
User.create!(email: 'elton@elton.com', password: '123456', name: 'Elton', hours_per_week: 40)

Category.create!(name: "Desenvolvimento")

Expand Down

0 comments on commit 2866e3b

Please sign in to comment.