Commit db796737 authored by Alex Buijs's avatar Alex Buijs

Add scopes to OnboardingProgress model

parent 5e081c84
...@@ -22,6 +22,24 @@ class OnboardingProgress < ApplicationRecord ...@@ -22,6 +22,24 @@ class OnboardingProgress < ApplicationRecord
:repository_mirrored :repository_mirrored
].freeze ].freeze
scope :incomplete_actions, -> (actions) do
Array.wrap(actions).inject(self) { |scope, action| scope.where(column_name(action) => nil) }
end
scope :completed_actions, -> (actions) do
Array.wrap(actions).inject(self) { |scope, action| scope.where.not(column_name(action) => nil) }
end
scope :completed_actions_with_latest_in_range, -> (actions, range) do
actions = Array(actions)
if actions.size == 1
where(column_name(actions[0]) => range)
else
action_columns = actions.map { |action| arel_table[column_name(action)] }
completed_actions(actions).where(Arel::Nodes::NamedFunction.new('GREATEST', action_columns).between(range))
end
end
class << self class << self
def onboard(namespace) def onboard(namespace)
return unless root_namespace?(namespace) return unless root_namespace?(namespace)
...@@ -44,12 +62,12 @@ class OnboardingProgress < ApplicationRecord ...@@ -44,12 +62,12 @@ class OnboardingProgress < ApplicationRecord
where(namespace: namespace).where.not(action_column => nil).exists? where(namespace: namespace).where.not(action_column => nil).exists?
end end
private
def column_name(action) def column_name(action)
:"#{action}_at" :"#{action}_at"
end end
private
def root_namespace?(namespace) def root_namespace?(namespace)
namespace && namespace.root? namespace && namespace.root?
end end
......
...@@ -29,6 +29,67 @@ RSpec.describe OnboardingProgress do ...@@ -29,6 +29,67 @@ RSpec.describe OnboardingProgress do
end end
end end
describe 'scopes' do
describe '.incomplete_actions' do
subject { described_class.incomplete_actions(actions) }
let!(:no_actions_completed) { create(:onboarding_progress) }
let!(:one_action_completed_one_action_incompleted) { create(:onboarding_progress, "#{action}_at" => Time.current) }
context 'when given one action' do
let(:actions) { action }
it { is_expected.to eq [no_actions_completed] }
end
context 'when given an array of actions' do
let(:actions) { [action, :git_write] }
it { is_expected.to eq [no_actions_completed] }
end
end
describe '.completed_actions' do
subject { described_class.completed_actions(actions) }
let!(:one_action_completed_one_action_incompleted) { create(:onboarding_progress, "#{action}_at" => Time.current) }
let!(:both_actions_completed) { create(:onboarding_progress, "#{action}_at" => Time.current, git_write_at: Time.current) }
context 'when given one action' do
let(:actions) { action }
it { is_expected.to eq [one_action_completed_one_action_incompleted, both_actions_completed] }
end
context 'when given an array of actions' do
let(:actions) { [action, :git_write] }
it { is_expected.to eq [both_actions_completed] }
end
end
describe '.completed_actions_with_latest_in_range' do
subject { described_class.completed_actions_with_latest_in_range(actions, 1.day.ago.beginning_of_day..1.day.ago.end_of_day) }
let!(:one_action_completed_in_range_one_action_incompleted) { create(:onboarding_progress, "#{action}_at" => 1.day.ago.middle_of_day) }
let!(:git_write_action_completed_in_range) { create(:onboarding_progress, git_write_at: 1.day.ago.middle_of_day) }
let!(:both_actions_completed_latest_action_out_of_range) { create(:onboarding_progress, "#{action}_at" => 1.day.ago.middle_of_day, git_write_at: Time.current) }
let!(:both_actions_completed_latest_action_in_range) { create(:onboarding_progress, "#{action}_at" => 1.day.ago.middle_of_day, git_write_at: 2.days.ago.middle_of_day) }
context 'when given one action' do
let(:actions) { :git_write }
it { is_expected.to eq [git_write_action_completed_in_range] }
end
context 'when given an array of actions' do
let(:actions) { [action, :git_write] }
it { is_expected.to eq [both_actions_completed_latest_action_in_range] }
end
end
end
describe '.onboard' do describe '.onboard' do
subject(:onboard) { described_class.onboard(namespace) } subject(:onboard) { described_class.onboard(namespace) }
...@@ -104,4 +165,10 @@ RSpec.describe OnboardingProgress do ...@@ -104,4 +165,10 @@ RSpec.describe OnboardingProgress do
end end
end end
end end
describe '.column_name' do
subject { described_class.column_name(action) }
it { is_expected.to eq(:subscription_created_at) }
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment