Skip to content

Commit

Permalink
Toggle penalty (#113)
Browse files Browse the repository at this point in the history
* rails generate migration AddPenaltyEnabledToNotes penalty_enabled:boolean

* note

* edit partial

* UI indicator
  • Loading branch information
adnjoo authored Oct 25, 2024
1 parent 10f6abc commit 3b1b651
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 42 deletions.
8 changes: 1 addition & 7 deletions app/controllers/notes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,6 @@ def update
end
end

def toggle_completion
@note = Note.find(params[:id])
@note.update(completed: !@note.completed)
redirect_to notes_path, notice: "Note status updated."
end

def archive
note = Note.find(params[:id])
note.update(archived: true)
Expand All @@ -63,6 +57,6 @@ def set_note
end

def note_params
params.require(:note).permit(:title, :content, :deadline)
params.require(:note).permit(:title, :content, :deadline, :completed, :penalty_enabled)
end
end
2 changes: 1 addition & 1 deletion app/models/note.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Note < ApplicationRecord
validates :title, presence: true

def schedule_penalty_check
if self.saved_change_to_attribute?(:deadline)
if penalty_enabled && saved_change_to_attribute?(:deadline)
puts("Scheduling penalty check for note #{self.id}")
# Schedule PenaltyJob to run 1 minute after the deadline
PenaltyJob.perform_at(self.deadline + 1.minute, self.id)
Expand Down
31 changes: 31 additions & 0 deletions app/views/notes/_edit_note.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<%= form_with model: note, local: true, class: "space-y-4" do |f| %>
<!-- Title Input -->
<div class="flex flex-col">
<%= f.label :title, class: "text-lg font-bold text-black uppercase mb-1" %>
<%= f.text_field :title, class: "border-4 border-black px-3 py-2 bg-white text-black focus:outline-none shadow-none" %>
</div>

<!-- Content Text Area -->
<div class="flex flex-col">
<%= f.label :content, class: "text-lg font-bold text-black uppercase mb-1" %>
<%= f.text_area :content, class: "border-4 border-black px-3 py-2 bg-white text-black focus:outline-none shadow-none h-40" %>
</div>

<!-- Deadline Input -->
<div class="flex flex-col max-w-xs">
<%= f.label :deadline, class: "text-lg font-bold text-black uppercase mb-1" %>
<%= f.text_field :deadline, id: "datepicker-#{note.id}", class: "border-4 border-black px-3 py-2 bg-white text-black focus:outline-none shadow-none" %>
</div>

<!-- Penalty Toggle Checkbox -->
<div class="flex flex-col">
<%= f.label :penalty_enabled, "Enable Penalty", class: "text-lg font-bold text-black uppercase mb-1" %>
<%= f.check_box :penalty_enabled, class: "mr-2" %>
</div>

<!-- Submit and Cancel Buttons -->
<div class="flex space-x-6">
<%= f.submit 'Update', class: "bg-black text-white px-5 py-2 uppercase font-bold tracking-wide cursor-pointer" %>
<button type="button" data-action="click->toggle#toggle" class="text-black underline hover:text-gray-700 uppercase">Cancel</button>
</div>
<% end %>
48 changes: 16 additions & 32 deletions app/views/notes/_note.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
<!-- Display note content -->
<div data-toggle-target="toggle">
<strong class="text-2xl font-bold uppercase text-black <%= 'line-through' if note.completed %>">
<% if note.penalty_enabled %>
<span class="text-red-500 text-lg">!</span>
<% end %>
<%= note.title %>
</strong>
<p class="text-lg text-black mt-2">
Expand All @@ -16,43 +19,24 @@
</p>
<% end %>

<div class="mt-4 flex space-x-6">
<%= form_with url: toggle_completion_note_path(note), method: :patch, local: true do |f| %>
<%= f.check_box :completed, checked: note.completed, class: "mr-2", onchange: 'this.form.submit();' %>
<label class="text-black">Completed</label>
<div class="mt-4 flex items-center space-x-4">
<%= form_with model: note, local: true do |f| %>
<div class="flex items-center space-x-2">
<!-- Submit the form when checkbox changes -->
<%= f.check_box :completed, checked: note.completed, class: "mr-2 w-5 h-5", onchange: 'this.form.submit();' %>
<label class="text-lg text-black">Completed</label>
</div>
<% end %>
</div>

<button type="button" data-action="click->toggle#toggle" class="text-black underline hover:text-gray-700 uppercase">Edit</button>
<%= link_to 'Delete', archive_note_path(note), class: 'text-red-700 underline hover:text-red-900 uppercase', data: { turbo_method: :patch, turbo_confirm: 'Are you sure?' } %>
<div class="mt-4 flex space-x-6">
<button type="button" data-action="click->toggle#toggle" class="text-blue-500 underline hover:text-blue-700 uppercase">Edit</button>
<%= link_to 'Delete', archive_note_path(note), class: 'text-red-500 underline hover:text-red-700 uppercase', data: { turbo_method: :patch, turbo_confirm: 'Are you sure?' } %>
</div>
</div>

<!-- Edit Form (hidden by default) -->
<div data-toggle-target="toggle" class="hidden">
<%= form_with model: note, local: true, class: "space-y-4" do |f| %>
<!-- Title Input -->
<div class="flex flex-col">
<%= f.label :title, class: "text-lg font-bold text-black uppercase mb-1" %>
<%= f.text_field :title, class: "border-4 border-black px-3 py-2 bg-white text-black focus:outline-none shadow-none" %>
</div>

<!-- Content Text Area -->
<div class="flex flex-col">
<%= f.label :content, class: "text-lg font-bold text-black uppercase mb-1" %>
<%= f.text_area :content, class: "border-4 border-black px-3 py-2 bg-white text-black focus:outline-none shadow-none h-40" %>
</div>

<!-- Deadline Input -->
<div class="flex flex-col max-w-xs">
<%= f.label :deadline, class: "text-lg font-bold text-black uppercase mb-1" %>
<%= f.text_field :deadline, id: "datepicker-#{note.id}", class: "border-4 border-black px-3 py-2 bg-white text-black focus:outline-none shadow-none" %>
</div>

<!-- Submit and Cancel Buttons -->
<div class="flex space-x-6">
<%= f.submit 'Update', class: "bg-black text-white px-5 py-2 uppercase font-bold tracking-wide cursor-pointer" %>
<button type="button" data-action="click->toggle#toggle" class="text-black underline hover:text-gray-700 uppercase">Cancel</button>
</div>
<% end %>
<div data-toggle-target="toggle" class="hidden mt-4">
<%= render 'edit_note', note: note %>
</div>
</li>
5 changes: 5 additions & 0 deletions db/migrate/20241025140406_add_penalty_enabled_to_notes.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPenaltyEnabledToNotes < ActiveRecord::Migration[7.2]
def change
add_column :notes, :penalty_enabled, :boolean
end
end
5 changes: 3 additions & 2 deletions db/schema.rb

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

0 comments on commit 3b1b651

Please sign in to comment.