Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/sam/add ransack date and sort #4

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from

Conversation

bowdensl712
Copy link
Owner

Ransackの日付検索と並び替え機能を追加しました。
・Controller, Model, Viewsでのスタイルエラーも修正しました。

・From〜、To〜のsearch_fieldを_search.html.erbに追加
(date_fieldでエラーが出るため、解決するまでコメント化しております。)
・Articleモデルに:created_after、:created_beforeスコープを追加
・created_beforeの時間を23:59に変更(1日単位で指定できるように)
・application_controllerでsearch_optionsを追加する
・Rubocopスタイルエラーを修正
・Title, Dateで並び替えできます。
・検索結果がない場合、並び替えボタンが非表示されます。
・articleモデルでcreated_at欄を検索可能に変更しました。
・CSSを調整しました。
・HTMLのスタイルエラーを修正
@bowdensl712 bowdensl712 changed the base branch from main to develop February 17, 2023 01:38
Copy link

@takuyeaaaah takuyeaaaah left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bowdensl712 いったん途中まで見ましたー

Comment on lines 4 to 11
def set_search
search_options = {
created_after: params[:created_after],
created_before: params[:created_before]
}
@q = Article.ransack(params[:q])
@article_results = @q.result(distinct: true)
end

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ArticlesController ではなく、 ApplicationController に記述をしている理由を知りたいですー

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

articles#indexに@qと@article_resultsを設定しようとした時に、"No Ransack::Search object was provided to search_form_for!"というエラーが発生しました。エラーを調べた結果、こちらのIssue(https://github.com/activerecord-hackery/ransack/issues/3)で"ApplicationControllerに移せば治るよ"という返答がありましたので、それに従いました。

ただいま改めて試した結果、set_searchメソッドをArticlesControllerに移し、"before_action :set_search"をApplicationControllerに残せば治りました。

また別件、search_optionsの記述は不必要らしいので、削除いたしました。

@@ -1,22 +1,22 @@
class CommentsController < ApplicationController
http_basic_authenticate_with name: "shh", password: "secret", only: :destroy
http_basic_authenticate_with name: 'shh', password: 'secret', only: :destroy

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

この認証処理は全てのControllerに記述することになりそうなので、 ApplicationController に書いてしまって良さそう

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

修正いたしました!

params.require(:article).permit(:title, :body, :image_link, :language, :status)
end

def search

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Articleの一覧表示の時も、検索をする時も、どちらの場合も ArticleController#index を利用出来ないか確認してみてほしいですー

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

最初にRansackを導入した際に、結果が自動的にIndexで表示されておりましたが、IndexとResultページを分けた方が勉強になり、よりcustomizeできると思いました。今後はIndexに統一いたします。

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indexで検索結果を表示するように修正いたしました。

end
scope :created_after, ->(time) {
# time = year.to_s + "-" + month.to_s + "-" + day.to_s
time = time.to_time

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Timezoneの問題を発生させないようにしたいので

- time = time.to_time
+ time = Time.zone.parse(time)

参考
https://zenn.dev/k_kind/articles/1538aac0efcf17

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

かしこまりました!修正いたします。

scope :created_before, ->(time) {
# time = year.to_s + "-" + month.to_s + "-" + day.to_s
time = time.to_time
time = time + 23.hours + 59.minutes

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下では意図しない動きになってしまいますか?

- time = time + 23.hours + 59.minutes
+ time = time + 1.day
or
+ time = time + 24.hours

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

こちらの場合は、選択した日付の全日(00:00〜23:59)の投稿を選択するために追加いたしましたので、1.dayや24.hoursでは1分の誤差(翌日の00:00)が入るかと存ます。
例:
○ 2023-02-17 00:00:00 UTC〜2023-02-17 23:59:00 UTC
X 2023-02-17 00:00:00 UTC〜2023-02-18 00:00:00 UTC

こちらは間違っておりますでしょうか?

@@ -1,4 +1,3 @@
<footer>

<i><a href="<%= root_path %>" id="footer_link">Postblog Inc.</a> Reserved 2023</i>
<i><a href="<%= root_path %>" id="footer_link">Postblog Inc.</a> Reserved 2023</i>
</footer>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ファイルの最後の行には改行を入れてもらえると!
VScodeの設定で自動で改行を入れることが出来るよー
https://qiita.com/norikt/items/83674fadd79a88bf7824

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

承知しました!設定を有効にしました!

<%= form.label :language %><br>
<%= form.select :language, ['English', 'Japanese'] %>
<%= form.label :language %><br>
<%= form.select :language, ['English', 'Japanese'] %>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

['English', 'Japanese'] の部分もemumで管理すると良さそう
https://api.rubyonrails.org/v6.0.2.2/classes/ActiveRecord/Enum.html

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

enumに変更いたしました!

<%= form.label :status %><br>
<%= form.select :status, ['public', 'private', 'archived']%>
<%= form.label :status %><br>
<%= form.select :status, ['public', 'private', 'archived']%>

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

statusはモデル側でenumで管理してあげると幸せになれそう
https://api.rubyonrails.org/v6.0.2.2/classes/ActiveRecord/Enum.html

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Active Recordで既に:publicと:privateのclass methodを定義していたらしく、以下のエラーが発生しました。

You tried to define an enum named "status" on the model "Article", but this will generate a class method "public", which is already defined by Active Record.

しかし、enumの名称をpublic_article, private_article, archived_articleに変更したら、使えるようになりました。前の名称を指す部分(_form.html.erb等)も新しい名称に修正いたしました。

残念ながら、:statusと:language欄をstring → integerに変更する際に、既存のデータを数値化する方法が見つからず、欄内のデータを無くしました。今回は少数の記事で再入力が容易でしたが、データを無くさずにenum数値に変換する方法はございますか?

-HTTP basic authenticationをApplicationControllerに統一
-Article/Commentモデルのstatusとlanguage欄をenumに変更
-set_searchメソッドをArticlesControllerに移行
-ArticleモデルのCreated_afterとCreated_beforeスコープを修正
-Rubocopのスタイルエラーを修正
…dex.html.erbで表示 ・articles#set_searchメソッドを削除 ・results.html.erbを削除 ・_search.html.erbにcreated_after検索フィールドのデフォルト日付を2023-02-01に変更
@bowdensl712 bowdensl712 force-pushed the feature/sam/add_ransack_date_and_sort branch from 07f6450 to a0d8d38 Compare February 20, 2023 05:28
:created_afterと:created_beforeスコープを修正
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants