-
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
Merged
+482
−17
Merged
Changes from 4 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4be5395
minor-feat: any column can be specified to read management
2bo 6d6ba0e
patch-chore: add admin_message model to test
2bo 7489186
patch-test: add spec for read_management_column_name
2bo 5c43143
patch-refactor: refactor read_management_column_name methods
2bo 6b1e611
patch-refactor: refactor method
2bo bf58746
patch-refactor: refactor code
2bo 729943e
patch-refactor: refactor acts_as_shiroyagi
2bo 24c3613
patch-refactor: refactor code
2bo 9ae560c
patch-doc: add readme about acts_as_shiroyagi
2bo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,10 +3,18 @@ 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 | ||||||
|
||||||
def read_management_column_name | ||||||
@read_management_column_name || :read_at | ||||||
end | ||||||
|
||||||
# NOTE: call this method to change column that use read management | ||||||
def read_management_column_name=(column_name) | ||||||
@read_management_column_name = column_name.to_sym | ||||||
end | ||||||
|
||||||
def reads_count | ||||||
reads.count | ||||||
end | ||||||
|
@@ -16,40 +24,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? | ||||||
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 | ||||||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
class AdminMessage < ApplicationRecord | ||
include Shiroyagi::ActsAsShiroyagi | ||
|
||
self.read_management_column_name = :user_read_at | ||
end |
8 changes: 8 additions & 0 deletions
8
spec/dummy/db/migrate/20220726044640_create_admin_messages.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
以下のprivate method追加した方が見やすいかと
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
bf58746
で修正しました