-
Notifications
You must be signed in to change notification settings - Fork 6
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
minor-feat: any column can be specified #51
Changes from 3 commits
4be5395
6d6ba0e
7489186
5c43143
6b1e611
bf58746
729943e
24c3613
9ae560c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,10 +3,14 @@ module ActsAsShiroyagi | |||||
extend ActiveSupport::Concern | ||||||
|
||||||
class_methods do | ||||||
# TODO: The read_at column name should be customizable via an argument. | ||||||
def acts_as_shiroyagi(options = {}) | ||||||
end | ||||||
|
||||||
# NOTE: override this method change column to use read management | ||||||
def read_management_column_name | ||||||
:read_at | ||||||
end | ||||||
|
||||||
def reads_count | ||||||
reads.count | ||||||
end | ||||||
|
@@ -16,40 +20,40 @@ def unreads_count | |||||
end | ||||||
|
||||||
def mark_all_as_read | ||||||
unreads.update(read_at: Time.current) | ||||||
unreads.update(read_management_column_name => Time.current) | ||||||
end | ||||||
|
||||||
def mark_all_as_unread | ||||||
reads.update(read_at: nil) | ||||||
reads.update(read_management_column_name => nil) | ||||||
end | ||||||
end | ||||||
|
||||||
included do | ||||||
scope :reads, -> { where.not(read_at: nil) } | ||||||
scope :unreads, -> { where(read_at: nil) } | ||||||
scope :reads, -> { where.not(read_management_column_name => nil) } | ||||||
scope :unreads, -> { where(read_management_column_name => nil) } | ||||||
|
||||||
def mark_as_read | ||||||
update(read_at: Time.current) if unread? | ||||||
update(self.class.read_management_column_name => Time.current) if unread? | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 以下のprivate method追加した方が見やすいかと
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @satom9to5 |
||||||
end | ||||||
|
||||||
def mark_as_unread | ||||||
update(read_at: nil) if read? | ||||||
update(self.class.read_management_column_name => nil) if read? | ||||||
end | ||||||
|
||||||
def mark_as_read! | ||||||
update!(read_at: Time.current) if unread? | ||||||
update!(self.class.read_management_column_name => Time.current) if unread? | ||||||
end | ||||||
|
||||||
def mark_as_unread! | ||||||
update!(read_at: nil) if read? | ||||||
update!(self.class.read_management_column_name => nil) if read? | ||||||
end | ||||||
|
||||||
def read? | ||||||
read_at.present? | ||||||
send(self.class.read_management_column_name).send('present?') | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @satom9to5 |
||||||
end | ||||||
|
||||||
def unread? | ||||||
read_at.blank? | ||||||
send(self.class.read_management_column_name).send('blank?') | ||||||
end | ||||||
end | ||||||
end | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
class AdminMessage < ApplicationRecord | ||
include Shiroyagi::ActsAsShiroyagi | ||
|
||
def self.read_management_column_name | ||
:user_read_at | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
class CreateAdminMessages < ActiveRecord::Migration[6.1] | ||
def change | ||
create_table :admin_messages do |t| | ||
t.datetime :user_read_at | ||
t.timestamps | ||
end | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
FactoryBot.define do | ||
factory :admin_message, aliases: %i(unread_admin_message) do | ||
trait :with_read_at do | ||
user_read_at { Time.current } | ||
end | ||
|
||
factory :read_admin_message, traits: %i(with_read_at) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ARの
self.table_name = :hoge
みたいな感じで設定できた方が良さそうですシンプルに書けばこんな感じ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
もしくはdeviseの
devise :hoge
を参考に、acts_as_shiroyagi read_management_column_name: :hoge
みたいな?https://github.com/heartcombo/devise/blob/main/lib/devise/models.rb#L71-L111
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@satom9to5
5c43143
で修正しました
シンプル方針で