Skip to content

Commit

Permalink
Merge pull request #132 from leouofa/pillar-topic-population
Browse files Browse the repository at this point in the history
Added Pillar Topic Population
  • Loading branch information
leouofa committed Jun 29, 2024
2 parents bdfcea0 + 17f9041 commit 93a49bf
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 4 deletions.
5 changes: 4 additions & 1 deletion app/jobs/pillar_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ def perform(*_args)
# [x] Add Pillar Columns for Each Pillar
Pillars::IterateThroughPillarsJob.perform_now

# [ ] Create Topics for each Pillar Column
# [x] Create Topics for each Pillar Column
Pillars::IterateThroughPillarColumnsJob.perform_now

# [x] Creates Pillar Topics from Pillar Columns
Pillars::CreatePillarTopicsJob.perform_now

# [x] Adds Articles for each Pillar Columns
# Articles::IterateThroughPillarColumnsJob.perform_now
ensure
Expand Down
22 changes: 22 additions & 0 deletions app/jobs/pillars/create_pillar_topics_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
class Pillars::CreatePillarTopicsJob < ApplicationJob
queue_as :default

def perform(*args)
PillarColumn.with_topics.unprocessed.each do |pillar_column|
process_pillar_column(pillar_column:)
end
end

private

def process_pillar_column(pillar_column:)
pillar_column.topics.each do |pillar_topic|
create_pillar_topic(pillar_column: ,pillar_topic:)
end
pillar_column.update(processed: true)
end

def create_pillar_topic(pillar_column:, pillar_topic:)
PillarTopic.create!(pillar_column: pillar_column, title: pillar_topic["title"], summary: pillar_topic["summary"])
end
end
10 changes: 9 additions & 1 deletion app/models/pillar_column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,14 @@
# created_at :datetime not null
# updated_at :datetime not null
# topics :text
#
# processed :boolean default(FALSE)

################ LOGIC #################
# - The `processed` value is set to true once the pillar column has been parsed and the feed items have been created.
# * This ensures that the column only gets processed once.
# * The value is set by the create_pillar_topics_job.rb job.
########################################

class PillarColumn < ApplicationRecord
belongs_to :pillar
has_many :articles
Expand All @@ -21,4 +28,5 @@ class PillarColumn < ApplicationRecord

scope :without_topics, -> { where(topics: nil) }
scope :with_topics, -> { where.not(topics: nil) }
scope :unprocessed, -> { where(processed: false) }
end
5 changes: 5 additions & 0 deletions db/migrate/20240629161427_add_processed_to_pillar_column.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddProcessedToPillarColumn < ActiveRecord::Migration[7.0]
def change
add_column :pillar_columns, :processed, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions spec/factories/pillar_columns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# created_at :datetime not null
# updated_at :datetime not null
# topics :text
# processed :boolean default(FALSE)
#
FactoryBot.define do
factory :pillar_column do
Expand Down
24 changes: 24 additions & 0 deletions spec/jobs/pillars/create_pillar_topics_job_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
require 'rails_helper'

RSpec.describe Pillars::CreatePillarTopicsJob, type: :job do
let!(:pillar_column_with_topics) { create(:pillar_column, topics: [{ "title" => "Topic 1", "summary" => "Summary 1" }, { "title" => "Topic 2", "summary" => "Summary 2" }]) }
let!(:pillar_column_without_topics) { create(:pillar_column, topics: nil) }
let!(:processed_pillar_column) { create(:pillar_column, topics: [{ "title" => "Topic 3", "summary" => "Summary 3" }], processed: true) }

describe '#perform' do
it 'processes unprocessed pillar columns with topics' do
expect { described_class.perform_now }.to change { PillarTopic.count }.by(2)

pillar_column_with_topics.reload
expect(pillar_column_with_topics.processed).to be_truthy
end

it 'does not process already processed pillar columns' do
expect { described_class.perform_now }.not_to(change { processed_pillar_column.pillar_topics.count })
end

it 'does not process pillar columns without topics' do
expect { described_class.perform_now }.not_to(change { pillar_column_without_topics.pillar_topics.count })
end
end
end
2 changes: 1 addition & 1 deletion spec/models/pillar_column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# created_at :datetime not null
# updated_at :datetime not null
# topics :text
# processed :boolean default(FALSE)
#
# spec/models/pillar_column_spec.rb

require 'rails_helper'

Expand Down

0 comments on commit 93a49bf

Please sign in to comment.