Skip to content

Commit

Permalink
Lists::RowComponent cache (#3)
Browse files Browse the repository at this point in the history
  • Loading branch information
mbajur authored Dec 14, 2024
1 parent 4ea1f57 commit b4b5c65
Show file tree
Hide file tree
Showing 21 changed files with 109 additions and 128 deletions.
74 changes: 38 additions & 36 deletions app/components/inner_plan/lists/row_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,50 +9,52 @@ def initialize(list, context: nil)
end

def template
lists = list.lists.ordered_by_position

div(
id: dom_id(@list),
data: {
update_url: helpers.update_position_list_path(list),
id: list.id
},
class: "mb-5"
) {
div(class: "d-flex w-100 mb-1") {
render(InnerPlan::Lists::Row::HandleComponent.new(list))

div(class: "ms-1 w-100") {
small(class: "text-body-tertiary") {
plain("#{list.tasks.completed.count}/#{list.tasks.count} completed")
}
helpers.cache(list, context) do
lists = list.lists.ordered_by_position

h3(class: "h5") {
link_to list.title, list, class: 'text-reset'
}
div(
id: dom_id(@list),
data: {
update_url: helpers.update_position_list_path(list),
id: list.id
},
class: "mb-5"
) {
div(class: "d-flex w-100 mb-1") {
render(InnerPlan::Lists::Row::HandleComponent.new(list))

if list.description.present?
div(class: "mb-2") {
list.description
div(class: "ms-1 w-100") {
small(class: "text-body-tertiary") {
plain("#{list.tasks.completed.count}/#{list.tasks.count} completed")
}
end

h3(class: "h5") {
link_to list.title, list, class: 'text-reset'
}

if list.description.present?
div(class: "mb-2") {
list.description
}
end
}
}
}

# Ongoing tasks
render(InnerPlan::Lists::OngoingTasksComponent.new(list, context: context))
# Ongoing tasks
render(InnerPlan::Lists::OngoingTasksComponent.new(list, context: context))

# Groups
render(InnerPlan::Groups::RowsComponent.new(lists, list: list, context: context))
# Groups
render(InnerPlan::Groups::RowsComponent.new(lists, list: list, context: context))

# Add new task form
last_group = lists.to_a.last || list
div(class: "mt-2 mb-4", id: dom_id(last_group, :add_task)) {
render InnerPlan::Tasks::InlineFormView.new(list: last_group)
}
# Add new task form
last_group = lists.to_a.last || list
div(class: "mt-2 mb-4", id: dom_id(last_group, :add_task)) {
render InnerPlan::Tasks::InlineFormView.new(list: last_group)
}

render(InnerPlan::Lists::CompletedTasksComponent.new(list, context: context))
}
render(InnerPlan::Lists::CompletedTasksComponent.new(list, context: context))
}
end
end

private
Expand Down
2 changes: 1 addition & 1 deletion app/components/inner_plan/nav_main_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def template
plain helpers.current_inner_plan_user.inner_plan_to_s
img(
class: "rounded-circle ms-2",
style: "margin-top:-.1 rem",
style: "margin-top:-.1rem",
src: helpers.current_inner_plan_user.inner_plan_avatar_url,
width: "20",
height: "20"
Expand Down
8 changes: 8 additions & 0 deletions app/models/inner_plan/list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class List < ApplicationRecord

validates :title, presence: true

after_commit :touch_parent_list, if: :sub?

def root?
parent_id.blank?
end
Expand All @@ -25,5 +27,11 @@ def sub?
def to_param
[id.to_s, title.to_url(limit: 50, truncate_words: false)].join('-')
end

private

def touch_parent_list
list.touch
end
end
end
9 changes: 9 additions & 0 deletions app/models/inner_plan/task.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ class Task < ApplicationRecord

validates :title, presence: true

after_commit :touch_lists

def ongoing?
!completed?
end
Expand All @@ -32,5 +34,12 @@ def reopen!
def to_param
[id.to_s, title.to_url(limit: 50, truncate_words: false)].join('-')
end

private

def touch_lists
list.touch
list.list.touch if list.sub?
end
end
end
18 changes: 18 additions & 0 deletions spec/models/inner_plan/list_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

require "rails_helper"

describe InnerPlan::List do
subject { build(:list) }

context "when sub" do
subject { create(:list, list: parent) }

let(:parent) { create(:list) }

it "touches parent list" do
parent.update!(updated_at: 1.year.ago)
expect { subject }.to change { parent.reload.updated_at }
end
end
end
35 changes: 35 additions & 0 deletions spec/models/inner_plan/task_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

require "rails_helper"

describe InnerPlan::Task do
subject { build(:task) }

context "when belongs to root list" do
subject { create(:task, list: list) }

let(:list) { create(:list) }

it "touches parent list" do
list.update!(updated_at: 1.year.ago)
expect { subject }.to change { list.reload.updated_at }
end
end

context "when belongs to sub list" do
subject { create(:task, list: list) }

let(:root_list) { create(:list) }
let(:list) { create(:list, list: root_list) }

it "touches sub list" do
list.update!(updated_at: 1.year.ago)
expect { subject }.to change { list.reload.updated_at }
end

it "touches root list" do
root_list.update!(updated_at: 1.year.ago)
expect { subject }.to change { root_list.reload.updated_at }
end
end
end
Empty file removed test/controllers/.keep
Empty file.
Empty file removed test/fixtures/files/.keep
Empty file.
13 changes: 0 additions & 13 deletions test/fixtures/inner_plan/groups.yml

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixtures/inner_plan/lists.yml

This file was deleted.

11 changes: 0 additions & 11 deletions test/fixtures/inner_plan/tasks.yml

This file was deleted.

Empty file removed test/helpers/.keep
Empty file.
7 changes: 0 additions & 7 deletions test/inner_plan_test.rb

This file was deleted.

Empty file removed test/integration/.keep
Empty file.
7 changes: 0 additions & 7 deletions test/integration/navigation_test.rb

This file was deleted.

Empty file removed test/mailers/.keep
Empty file.
Empty file removed test/models/.keep
Empty file.
9 changes: 0 additions & 9 deletions test/models/inner_plan/group_test.rb

This file was deleted.

9 changes: 0 additions & 9 deletions test/models/inner_plan/list_test.rb

This file was deleted.

9 changes: 0 additions & 9 deletions test/models/inner_plan/task_test.rb

This file was deleted.

15 changes: 0 additions & 15 deletions test/test_helper.rb

This file was deleted.

0 comments on commit b4b5c65

Please sign in to comment.