From 9279e324fbc62b014dad7489bb99ad874030d8ce Mon Sep 17 00:00:00 2001 From: calvin Date: Thu, 5 Dec 2024 22:15:41 -0700 Subject: [PATCH] filter by date --- app/controllers/memos_controller.rb | 5 ++++- app/models/memo.rb | 1 + app/views/layouts/_memo.html.erb | 8 ++------ app/views/layouts/application.html.erb | 2 ++ app/views/layouts/tags/_listing.html.erb | 2 +- app/views/memos/index.html.erb | 11 +++++++++++ config/application.rb | 3 ++- 7 files changed, 23 insertions(+), 9 deletions(-) diff --git a/app/controllers/memos_controller.rb b/app/controllers/memos_controller.rb index 3196f31..b5863a0 100644 --- a/app/controllers/memos_controller.rb +++ b/app/controllers/memos_controller.rb @@ -2,10 +2,13 @@ class MemosController < ApplicationController def index - @tid = params[:query] + @tid = params[:tid] + @date = params[:date] if @tid # @memos = current_user.memos.find_by_sql("select memos.id,memos.body,memos.created_at,memos.attachment from memos inner join memos_tags on memos.id = memos_tags.memo_id where tag_id = #{@tid} order by memos.created_at desc") @memos = Memo.filter_by_tag @tid + elsif @date + @memos = Memo.filter_by_date(@date).order("created_at") else @memos = current_user.memos end diff --git a/app/models/memo.rb b/app/models/memo.rb index 9ebdecb..b4fd7e8 100644 --- a/app/models/memo.rb +++ b/app/models/memo.rb @@ -1,5 +1,6 @@ class Memo < ApplicationRecord scope :filter_by_tag, ->(tagid) { joins(:tags).where( tags: { id: tagid } ) } + scope :filter_by_date, lambda { |date| where("strftime('%m/%d/%Y',datetime(created_at,'localtime')) = ?", date) } belongs_to :user has_and_belongs_to_many :tags def get_tags diff --git a/app/views/layouts/_memo.html.erb b/app/views/layouts/_memo.html.erb index f520b2f..5d548de 100644 --- a/app/views/layouts/_memo.html.erb +++ b/app/views/layouts/_memo.html.erb @@ -25,7 +25,7 @@ <% end %> <% memo.tag_ids.each do |tagid| tag = Tag.find(tagid) %> - <%= link_to "#" + tag.name, memos_path(query: tagid) %> + <%= link_to "#" + tag.name, memos_path(tid: tagid) %> <% end %> @@ -44,11 +44,7 @@