기도 통계 + QT→기도제목 가져오기 - stats/import_from_qt 액션 + 뷰 + 테스트
ID: e8a43807-77a5-45e6-b4da-f4b57a33d7ab
## 목표
기도 통계 페이지와 QT에서 기도제목 가져오기 기능 구현
## 구현 항목
### 1. PrayersController에 stats 액션 추가
- 파일: `app/controllers/prayers_controller.rb`
- `stats` 액션 추가:
```ruby
def stats
prayers = current_user.prayer_requests
active_prayers = prayers.where(is_active: true)
@prayer_stats = {
total: prayers.count,
active: active_prayers.count,
by_response: {
keep_praying: prayers.where(response_type: :keep_praying).count,
waiting: prayers.where(response_type: :waiting).count,
yes: prayers.where(response_type: :yes).count,
no: prayers.where(response_type: :no).count
},
answer_rate: calculate_answer_rate(prayers),
by_category: {
daily: prayers.where(category: :daily).count,
weekly: prayers.where(category: :weekly).count
},
recent_30days_check_rate: calculate_check_rate(current_user),
total_checks: current_user.prayer_check_logs.count
}
end
private
def calculate_answer_rate(prayers)
total = prayers.count
return 0 if total.zero?
answered = prayers.where(response_type: [:yes, :no, :waiting]).count
(answered * 100.0 / total).round(1)
end
def calculate_check_rate(user)
total_days = 30
checked_days = user.prayer_check_logs
.where(check_date: 30.days.ago.to_date..Date.current)
.select(:check_date).distinct.count
(checked_days * 100.0 / total_days).round(1)
end
```
### 2. PrayersController에 import_from_qt 액션 추가
- `import_from_qt` 액션 (POST):
```ruby
def import_from_qt
meditation = current_user.user_meditations.find(params[:meditation_id])
if meditation.prayer_topic.blank?
redirect_to prayers_path, alert: "가져올 기도제목이 없습니다."
return
end
@prayer = current_user.prayer_requests.build(
content: meditation.prayer_topic,
category: :daily,
response_type: :keep_praying,
visibility: :private,
is_active: true,
sort_order: (current_user.prayer_requests.maximum(:sort_order) || 0) + 1
)
if @prayer.save
redirect_to prayers_path, notice: "QT 묵상에서 기도제목을 가져왔습니다."
else
redirect_to prayers_path, alert: "기도제목 저장에 실패했습니다."
end
end
```
### 3. 라우트 수정
- 파일: `config/routes.rb`
- 기존 prayers 리소스에 collection 라우트 추가:
```ruby
resources :prayers do
collection do
get :stats
post :import_from_qt
end
# 기존 member 라우트 유지
member do
post :check
end
end
```
- **주의**: 기존 prayers 라우트 구조를 잘 확인하고, 이미 member do ... end가 있으면 그 안에 추가하지 말고 collection do ... end를 별도로 추가
### 4. 기도 통계 뷰
- 파일: `app/views/prayers/stats.html.erb` (신규)
- 디자인:
- 상단: 뒤로가기 + "기도 통계" 제목
- 카드 1: 전체 통계 (총 기도수, 활성, 응답률)
- 카드 2: 응답 현황 (keep_praying/waiting/yes/no 각각 개수 + 비율)
- 카드 3: 카테고리별 (매일/주간)
- 카드 4: 기도 실천 (최근 30일 이행률 + 총 체크 수)
- shared 파셜 활용: _card, _badge, _progress, _separator
### 5. prayers/index.html.erb 수정
- 기존 index 상단에 "통계 보기" 링크 버튼 추가
```erb
<%= link_to "통계", stats_prayers_path, class: "..." %>
```
### 6. QT→기도제목 가져오기 UI
- UserMeditation의 prayer_topic이 있는 묵상에서 "기도 목록에 추가" 버튼 표시
- 파일: `app/views/qt/_meditation_form.html.erb` 또는 묵상 상세 페이지
- **주의**: 묵상 뷰 파일을 먼저 확인하고, prayer_topic 필드가 어디서 표시되는지 파악한 후 적절한 위치에 버튼 추가
- 버튼은 `button_to import_from_qt_prayers_path(meditation_id: meditation.id), method: :post` 사용
### 7. 테스트
- 파일: `test/controllers/prayers_controller_test.rb`에 추가
- stats 테스트:
```ruby
test "stats shows prayer statistics" do
sign_in users(:daniel)
get stats_prayers_path
assert_response :success
end
test "stats requires authentication" do
get stats_prayers_path
assert_redirected_to new_user_session_path
end
```
- import_from_qt 테스트:
```ruby
test "import_from_qt creates prayer from meditation" do
sign_in users(:daniel)
meditation = user_meditations(:completed_meditation) # prayer_topic이 있는 fixture
assert_difference "PrayerRequest.count", 1 do
post import_from_qt_prayers_path, params: { meditation_id: meditation.id }
end
assert_redirected_to prayers_path
end
test "import_from_qt rejects blank prayer_topic" do
sign_in users(:daniel)
meditation = user_meditations(:minimal_meditation) # prayer_topic이 없는 fixture
assert_no_difference "PrayerRequest.count" do
post import_from_qt_prayers_path, params: { meditation_id: meditation.id }
end
assert_redirected_to prayers_path
end
```
## 기존 코드 참고
- PrayerRequest 모델: category(daily/weekly), response_type(keep_praying/waiting/yes/no), visibility(private/partners/qt_plan/partners_qt_plan)
- PrayerCheckLog: [user_id, prayer_request_id, check_date] 유니크
- UserMeditation: prayer_topic 컬럼 존재
- prayers/index.html.erb: 기존 목록 뷰 확인 후 통계 링크 추가
## 주의사항
- 기존 prayers 테스트가 깨지지 않게 주의
- fixture 날짜는 과거 고정 날짜 사용 (Date.current 충돌 방지)
- shared 파셜의 strict locals 준수
- ERB 멀티라인 주석 사용 금지 (각 줄 단일 라인 주석)
- 전체 테스트 통과 확인: `bin/rails test`
- `parallelize(workers: 1)` 테스트 설정 확인
- UUID PK 사용
첨부 이미지
이미지 추가 (Ctrl+V로 붙여넣기 또는 클릭)
JPEG, PNG, GIF, WebP / 최대 10MB
활동 로그
-
팀팀리드 상태 변경: 리뷰 → 완료
2026년 03월 02일 04:55:47
-
Pprayer-stats-dev 기도 통계 + QT→기도제목 가져오기 구현 완료
2026년 03월 02일 04:54:37
상세 보기
## 변경 사항
### Phase 1: 기도 통계
- `app/controllers/prayers_controller.rb` - stats 액션 추가 (전체/활성/응답별/카테고리별 통계, 30일 이행률)
- `config/routes.rb` - prayers collection에 `get :stats`, `post :import_from_qt` 추가
- `app/views/prayers/stats.html.erb` (신규) - 4개 카드 레이아웃 (전체 통계, 응답 현황, 카테고리별, 기도 실천)
- `app/views/prayers/index.html.erb` - 헤더에 통계 링크 버튼 추가
- `test/controllers/prayers_controller_test.rb` - stats 테스트 3개 추가### Phase 2: QT→기도제목 가져오기
- `app/controllers/prayers_controller.rb` - import_from_qt 액션 추가
- `app/views/qt/meditations/_form.html.erb` - prayer_topic 존재 시 "기도 목록에 추가" 버튼 추가
- `test/fixtures/user_meditations.yml` - prayer_topic 없는 fixture(daniel_no_prayer) 추가
- `test/controllers/prayers_controller_test.rb` - import_from_qt 테스트 3개 추가### 테스트 결과
- prayers_controller_test: 21 runs, 50 assertions, 0 failures, 0 errors
- 전체 테스트: 350 runs, 804 assertions, 2 failures (실패 2개는 session-stats 관련으로 제 변경과 무관)### 사용된 shared 파셜
- _card (variant: :default, padding: :md)
- _badge (variant: :info/:warning/:success/:error)
- _progress (value:, label:, color:)
- _separator (기본 horizontal) -
Pprayer-stats-dev 상태 변경: 진행 중 → 리뷰
2026년 03월 02일 04:54:22
-
Pprayer-stats-dev 기도 통계 + QT→기도제목 가져오기 구현 시작
2026년 03월 02일 04:48:34
-
Pprayer-stats-dev 티켓 클레임 완료
2026년 03월 02일 04:48:29