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

minor-feat: any column can be specified #51

Merged
merged 9 commits into from
Jul 29, 2022
30 changes: 19 additions & 11 deletions lib/shiroyagi/acts_as_shiroyagi.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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?
Copy link
Member

Choose a reason for hiding this comment

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

以下のprivate method追加した方が見やすいかと

def read_management_column_name
  self.class.read_management_column_name
end

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@satom9to5
bf58746
で修正しました

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?')
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
send(self.class.read_management_column_name).send('present?')
send(self.class.read_management_column_name).present?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@satom9to5
6b1e611
で修正しました

end

def unread?
read_at.blank?
send(self.class.read_management_column_name).send('blank?')
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/dummy/app/models/admin_message.rb
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 spec/dummy/db/migrate/20220726044640_create_admin_messages.rb
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
18 changes: 12 additions & 6 deletions spec/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,21 @@
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
# This file is the source Rails uses to define your schema when running `bin/rails
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
# be faster and is potentially less error prone than running all of your
# migrations from scratch. Old migrations may fail to apply correctly if those
# migrations use external dependencies or application code.
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20171127060854) do
ActiveRecord::Schema.define(version: 2022_07_26_044640) do

create_table "admin_messages", force: :cascade do |t|
t.datetime "user_read_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
end

create_table "messages", force: :cascade do |t|
t.integer "from_user_id", null: false
Expand Down
9 changes: 9 additions & 0 deletions spec/factories/admin_messages.rb
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
Loading