Skip to content

Commit ea14239

Browse files
committed
Rubocop Fixes
1 parent fd60df5 commit ea14239

File tree

5 files changed

+74
-39
lines changed

5 files changed

+74
-39
lines changed

app/controllers/better_together/checklist_items_controller.rb

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def destroy
6868
end
6969
end
7070

71-
def position # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
71+
def position # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
7272
# Reordering affects the checklist as a whole; require permission to update the parent
7373
authorize @checklist, :update?
7474

@@ -100,25 +100,27 @@ def position # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
100100
streams << turbo_stream.remove(helpers.dom_id(a))
101101

102102
# If direction is up, insert before sibling; if down, insert after sibling
103-
if direction == 'up'
104-
streams << turbo_stream.before(helpers.dom_id(b),
105-
partial: 'better_together/checklist_items/checklist_item', locals: { checklist_item: a, checklist: @checklist, moved: true })
106-
else
107-
streams << turbo_stream.after(helpers.dom_id(b),
108-
partial: 'better_together/checklist_items/checklist_item', locals: { checklist_item: a, checklist: @checklist, moved: true })
109-
end
103+
streams << if direction == 'up'
104+
turbo_stream.before(helpers.dom_id(b),
105+
partial: 'better_together/checklist_items/checklist_item',
106+
locals: { checklist_item: a, checklist: @checklist, moved: true })
107+
else
108+
turbo_stream.after(helpers.dom_id(b),
109+
partial: 'better_together/checklist_items/checklist_item',
110+
locals: { checklist_item: a, checklist: @checklist, moved: true })
111+
end
110112

111113
render turbo_stream: streams
112114
rescue StandardError
113115
# Fallback: update only the inner list contents
114-
render turbo_stream: turbo_stream.update("#{helpers.dom_id(@checklist, :checklist_items)}",
116+
render turbo_stream: turbo_stream.update(helpers.dom_id(@checklist, :checklist_items).to_s,
115117
partial: 'better_together/checklist_items/list_contents',
116118
locals: { checklist: @checklist })
117119
end
118120
end
119121
end
120122

121-
def reorder # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
123+
def reorder # rubocop:todo Metrics/AbcSize, Metrics/MethodLength, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
122124
# Reordering affects the checklist as a whole; require permission to update the parent
123125
authorize @checklist, :update?
124126

@@ -139,9 +141,9 @@ def reorder # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
139141
end
140142
end
141143

142-
respond_to do |format|
144+
respond_to do |format| # rubocop:todo Metrics/BlockLength
143145
format.json { head :no_content }
144-
format.turbo_stream do
146+
format.turbo_stream do # rubocop:todo Metrics/BlockLength
145147
# Try a minimal DOM update: if exactly one item moved, remove it and insert before/after the neighbor.
146148

147149
ordered = params[:ordered_ids].map(&:to_i)
@@ -174,23 +176,25 @@ def reorder # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
174176
neighbor = @checklist.checklist_items.find_by(id: neighbor_id)
175177
if neighbor
176178
streams << turbo_stream.after(helpers.dom_id(neighbor),
177-
partial: 'better_together/checklist_items/checklist_item', locals: { checklist_item: moved_item, checklist: @checklist, moved: true })
179+
partial: 'better_together/checklist_items/checklist_item',
180+
locals: { checklist_item: moved_item, checklist: @checklist, moved: true }) # rubocop:disable Layout/LineLength
178181
render turbo_stream: streams and return
179182
end
180183
end
181184

182185
# If neighbor not found (moved to end), append to the UL
183186
streams << turbo_stream.append("#{helpers.dom_id(@checklist, :checklist_items)} ul",
184-
partial: 'better_together/checklist_items/checklist_item', locals: { checklist_item: moved_item, checklist: @checklist, moved: true })
187+
partial: 'better_together/checklist_items/checklist_item',
188+
locals: { checklist_item: moved_item, checklist: @checklist, moved: true })
185189
render turbo_stream: streams and return
186190
end
187191

188192
# Fallback: update inner contents for complex reorders
189-
render turbo_stream: turbo_stream.update("#{helpers.dom_id(@checklist, :checklist_items)}",
193+
render turbo_stream: turbo_stream.update(helpers.dom_id(@checklist, :checklist_items).to_s,
190194
partial: 'better_together/checklist_items/list_contents',
191195
locals: { checklist: @checklist })
192196
rescue StandardError
193-
render turbo_stream: turbo_stream.update("#{helpers.dom_id(@checklist, :checklist_items)}",
197+
render turbo_stream: turbo_stream.update(helpers.dom_id(@checklist, :checklist_items).to_s,
194198
partial: 'better_together/checklist_items/list_contents',
195199
locals: { checklist: @checklist })
196200
end
@@ -199,7 +203,7 @@ def reorder # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
199203

200204
private
201205

202-
def set_checklist
206+
def set_checklist # rubocop:todo Metrics/CyclomaticComplexity, Metrics/AbcSize, Metrics/MethodLength, Metrics/PerceivedComplexity
203207
key = params[:checklist_id] || params[:id]
204208

205209
@checklist = nil

app/controllers/better_together/person_checklist_items_controller.rb

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# frozen_string_literal: true
22

33
module BetterTogether
4-
class PersonChecklistItemsController < ApplicationController
4+
class PersonChecklistItemsController < ApplicationController # rubocop:todo Style/Documentation
55
before_action :authenticate_user!
66
before_action :set_checklist
77
before_action :set_checklist_item
@@ -21,24 +21,28 @@ def show
2121
end
2222
end
2323

24-
def create
24+
def create # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
2525
# Diagnostic log to confirm authentication state for incoming requests
26-
Rails.logger.info("DBG PersonChecklistItemsController#create: current_user_id=#{current_user&.id}, warden_user_id=#{if request.env['warden']
27-
request.env['warden']&.user&.id
28-
end}")
26+
# rubocop:todo Layout/LineLength
27+
Rails.logger.info("DBG PersonChecklistItemsController#create: current_user_id=#{current_user&.id}, warden_user_id=#{request.env['warden']&.user&.id}")
28+
# rubocop:enable Layout/LineLength
2929
Rails.logger.info("DBG PersonChecklistItemsController#create: params=#{params.to_unsafe_h}")
3030
person = current_user.person
3131
pci = BetterTogether::PersonChecklistItem.find_or_initialize_by(person:, checklist: @checklist,
3232
checklist_item: @checklist_item)
3333
pci.completed_at = params[:completed] ? Time.zone.now : nil
3434

35-
respond_to do |format|
35+
respond_to do |format| # rubocop:todo Metrics/BlockLength
3636
if pci.save
37+
# rubocop:todo Layout/LineLength
3738
Rails.logger.info("DBG PersonChecklistItemsController#create: saved pci id=#{pci.id} completed_at=#{pci.completed_at}")
39+
# rubocop:enable Layout/LineLength
3840
# If checklist completed, trigger a hook (implement as ActiveSupport::Notifications for now)
3941
notify_if_checklist_complete(person)
4042
format.json do
43+
# rubocop:todo Layout/LineLength
4144
render json: { id: pci.id, completed_at: pci.completed_at, flash: { type: 'notice', message: t('flash.checklist_item.updated') } },
45+
# rubocop:enable Layout/LineLength
4246
status: :ok
4347
end
4448
format.html do
@@ -48,11 +52,15 @@ def create
4852
format.turbo_stream do
4953
flash.now[:notice] = t('flash.checklist_item.updated')
5054
render turbo_stream: turbo_stream.replace('flash_messages',
55+
# rubocop:todo Layout/LineLength
5156
partial: 'layouts/better_together/flash_messages', locals: { flash: })
57+
# rubocop:enable Layout/LineLength
5258
end
5359
else
5460
format.json do
61+
# rubocop:todo Layout/LineLength
5562
render json: { errors: pci.errors.full_messages, flash: { type: 'alert', message: t('flash.checklist_item.update_failed') } },
63+
# rubocop:enable Layout/LineLength
5664
status: :unprocessable_entity
5765
end
5866
format.html do
@@ -62,13 +70,18 @@ def create
6270
format.turbo_stream do
6371
flash.now[:alert] = t('flash.checklist_item.update_failed')
6472
render turbo_stream: turbo_stream.replace('flash_messages',
73+
# rubocop:todo Layout/LineLength
6574
partial: 'layouts/better_together/flash_messages', locals: { flash: })
75+
# rubocop:enable Layout/LineLength
6676
end
6777
end
6878
end
6979
rescue StandardError => e
80+
# rubocop:todo Layout/LineLength
7081
Rails.logger.error("PersonChecklistItemsController#create unexpected error: #{e.class} - #{e.message}\n#{e.backtrace.join("\n")}")
71-
render json: { errors: [e.message], flash: { type: 'alert', message: e.message } }, status: :internal_server_error
82+
# rubocop:enable Layout/LineLength
83+
render json: { errors: [e.message], flash: { type: 'alert', message: e.message } },
84+
status: :internal_server_error
7285
end
7386

7487
private
@@ -87,7 +100,7 @@ def notify_if_checklist_complete(person)
87100
completed = BetterTogether::PersonChecklistItem.where(person:,
88101
checklist: @checklist).where.not(completed_at: nil).count
89102

90-
return unless total > 0 && completed >= total
103+
return unless total.positive? && completed >= total
91104

92105
ActiveSupport::Notifications.instrument('better_together.checklist.completed', checklist_id: @checklist.id,
93106
person_id: person.id)

spec/features/checklist_person_completion_spec.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,23 @@
1+
# frozen_string_literal: true
2+
13
require 'rails_helper'
24

3-
RSpec.describe 'Person completes checklist', :js, type: :feature do
5+
RSpec.describe 'Person completes checklist', :js do
46
include Devise::Test::IntegrationHelpers
57

68
let(:user) { create(:user) }
7-
let!(:person) { create(:better_together_person, user: user) }
9+
let!(:person) { create(:better_together_person, user: user) } # rubocop:todo RSpec/LetSetup
810

911
before do
1012
find_or_create_test_user('[email protected]', 'password12345', :user)
1113
capybara_login_as_user
1214
end
1315

14-
xit 'allows a person to complete all items and shows completion message' do
16+
# rubocop:todo RSpec/PendingWithoutReason
17+
# rubocop:todo RSpec/MultipleExpectations
18+
xit 'allows a person to complete all items and shows completion message' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations, RSpec/PendingWithoutReason
19+
# rubocop:enable RSpec/MultipleExpectations
20+
# rubocop:enable RSpec/PendingWithoutReason
1521
checklist = create(:better_together_checklist, privacy: 'public')
1622
create_list(:better_together_checklist_item, 5, checklist: checklist)
1723

spec/requests/better_together/person_checklist_items_json_spec.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1+
# frozen_string_literal: true
2+
13
require 'rails_helper'
24

35
RSpec.describe 'PersonChecklistItems JSON', :as_user do
46
let(:checklist) { create(:better_together_checklist) }
57
let(:item) { create(:better_together_checklist_item, checklist: checklist) }
68

7-
it 'accepts JSON POST with headers and returns json' do
9+
# rubocop:todo RSpec/MultipleExpectations
10+
it 'accepts JSON POST with headers and returns json' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
11+
# rubocop:enable RSpec/MultipleExpectations
812
url = better_together.create_person_checklist_item_checklist_checklist_item_path(
913
locale: I18n.default_locale,
1014
checklist_id: checklist.id,
@@ -18,9 +22,9 @@
1822
expect(response).to have_http_status(:ok)
1923
body = JSON.parse(response.body)
2024
expect(body['completed_at']).not_to be_nil
21-
# New: server includes a flash payload in JSON for client-side display
22-
expect(body['flash']).to be_present
23-
expect(body['flash']['type']).to eq('notice')
24-
expect(body['flash']['message']).to eq(I18n.t('flash.checklist_item.updated'))
25+
# New: server includes a flash payload in JSON for client-side display
26+
expect(body['flash']).to be_present
27+
expect(body['flash']['type']).to eq('notice')
28+
expect(body['flash']['message']).to eq(I18n.t('flash.checklist_item.updated'))
2529
end
2630
end

spec/requests/better_together/person_checklist_items_spec.rb

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,35 @@
1+
# frozen_string_literal: true
2+
13
require 'rails_helper'
24

3-
RSpec.describe BetterTogether::PersonChecklistItemsController, type: :request, as_user: true do
5+
RSpec.describe BetterTogether::PersonChecklistItemsController, :as_user do # rubocop:todo RSpec/SpecFilePathFormat
46
include Devise::Test::IntegrationHelpers
57

68
let(:user) { create(:user) }
7-
let!(:person) { create(:better_together_person, user: user) }
9+
let!(:person) { create(:better_together_person, user: user) } # rubocop:todo RSpec/LetSetup
810
let(:checklist) { create(:better_together_checklist) }
911
let(:items) { create_list(:better_together_checklist_item, 3, checklist: checklist) }
1012

1113
before do
12-
configure_host_platform
14+
configure_host_platform
1315
# Use project's HTTP login helper to satisfy route constraints
1416
test_user = find_or_create_test_user(user.email, 'password12345', :user)
1517
login(test_user.email, 'password12345')
1618
end
1719

18-
it 'returns empty record when none exists and can create a completion' do
20+
# rubocop:todo RSpec/MultipleExpectations
21+
it 'returns empty record when none exists and can create a completion' do # rubocop:todo RSpec/ExampleLength, RSpec/MultipleExpectations
22+
# rubocop:enable RSpec/MultipleExpectations
23+
# rubocop:todo Layout/LineLength
1924
get "/#{I18n.default_locale}/#{BetterTogether.route_scope_path}/checklists/#{checklist.id}/checklist_items/#{items.first.id}/person_checklist_item"
25+
# rubocop:enable Layout/LineLength
2026
expect(response).to have_http_status(:ok)
2127
expect(JSON.parse(response.body)['completed_at']).to be_nil
2228

23-
post "/#{I18n.default_locale}/#{BetterTogether.route_scope_path}/checklists/#{checklist.id}/checklist_items/#{items.first.id}/person_checklist_item",
24-
params: { completed: true }, as: :json
29+
# rubocop:todo Layout/LineLength
30+
post "/#{I18n.default_locale}/#{BetterTogether.route_scope_path}/checklists/#{checklist.id}/checklist_items/#{items.first.id}/person_checklist_item",
31+
# rubocop:enable Layout/LineLength
32+
params: { completed: true }, as: :json
2533
expect(response).to have_http_status(:ok)
2634
data = JSON.parse(response.body)
2735
expect(data['completed_at']).not_to be_nil

0 commit comments

Comments
 (0)