Add basic models for tracking engagement

This commit is contained in:
Jorge Manrubia
2025-04-03 11:15:24 +02:00
parent 832134385a
commit 4fcb5a9c85
8 changed files with 454 additions and 341 deletions
+1 -1
View File
@@ -1,5 +1,5 @@
class Bubble < ApplicationRecord
include Assignable, Boostable, Colored, Commentable, Eventable,
include Assignable, Boostable, Colored, Commentable, Engageable, Eventable,
Messages, Notifiable, Pinnable, Poppable, Scorable, Searchable, Staged, Statuses, Taggable, Watchable
belongs_to :bucket, touch: true
+30
View File
@@ -0,0 +1,30 @@
module Bubble::Engageable
extend ActiveSupport::Concern
AUTO_POP_AFTER = 30.days
included do
has_one :engagement, dependent: :destroy, class_name: "Bubble::Engagement"
scope :doing, -> { joins(:engagement) }
scope :considering, -> { where.missing(:engagement) }
end
def doing?
engagement.present?
end
def considering?
!doing?
end
def engage
unless doing?
create_engagement!
end
end
def reconsider
engagement&.destroy
end
end
+3
View File
@@ -0,0 +1,3 @@
class Bubble::Engagement < ApplicationRecord
belongs_to :bubble, class_name: "::Bubble"
end
@@ -0,0 +1,9 @@
class CreateBubbleEngagements < ActiveRecord::Migration[8.1]
def change
create_table :bubble_engagements do |t|
t.references :bubble, index: true
t.timestamps
end
end
end
Generated
+8 -1
View File
@@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema[8.1].define(version: 2025_03_27_191456) do
ActiveRecord::Schema[8.1].define(version: 2025_04_03_094604) do
create_table "accesses", force: :cascade do |t|
t.integer "bucket_id", null: false
t.datetime "created_at", null: false
@@ -94,6 +94,13 @@ ActiveRecord::Schema[8.1].define(version: 2025_03_27_191456) do
t.index ["bubble_id"], name: "index_assignments_on_bubble_id"
end
create_table "bubble_engagements", force: :cascade do |t|
t.integer "bubble_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["bubble_id"], name: "index_bubble_engagements_on_bubble_id"
end
create_table "bubbles", force: :cascade do |t|
t.float "activity_score", default: 0.0, null: false
t.datetime "activity_score_at"
+372 -339
View File
File diff suppressed because it is too large Load Diff
+2
View File
@@ -0,0 +1,2 @@
logo_engagement:
bubble: logo
+29
View File
@@ -0,0 +1,29 @@
require "test_helper"
class Bubble::EngageableTest < ActiveSupport::TestCase
test "check the engagement status of a bubble" do
assert bubbles(:logo).doing?
assert_not bubbles(:shipping).doing?
assert_not bubbles(:logo).considering?
assert bubbles(:shipping).considering?
end
test "change the engagement" do
assert_changes -> { bubbles(:shipping).reload.doing? }, to: true do
bubbles(:shipping).engage
end
assert_changes -> { bubbles(:logo).reload.doing? }, to: false do
bubbles(:logo).reconsider
end
end
test "scopes" do
assert_includes Bubble.doing, bubbles(:logo)
assert_not_includes Bubble.doing, bubbles(:shipping)
assert_includes Bubble.considering, bubbles(:shipping)
assert_not_includes Bubble.considering, bubbles(:logo)
end
end