Skip to content

Instantly share code, notes, and snippets.

@amitpatelx
Created November 25, 2021 08:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amitpatelx/3db0a0cd9e0e37d32c3f86ab9606da46 to your computer and use it in GitHub Desktop.
Save amitpatelx/3db0a0cd9e0e37d32c3f86ab9606da46 to your computer and use it in GitHub Desktop.
Custom Precondition for Granite
1) BA::SkillName::Update preconditions when in merge flow when merge is broken is expected not to satisfy preconditions
Failure/Error: it { is_expected.not_to satisfy_preconditions.with_message(broken_merge_msg) }
#<BA::SkillName::Update skill_name: #<ReferencesOne #<SkillName id: 1001, created_at: nil, editor_c...>, skills: #<EmbedsMany []>, remaining_skills: #<EmbedsMany [#<BA::SkillName::Update::EmbededSkill vertica...]>, skill_name_id: 1001, name: "Skill 1", skill_page_slug: "corrin-blanda-1", selected_vertical: nil, new_name: "Skill 2", after_merge_confirmation: false> does not implement: broken_merge?
module BA
module SkillName
class Update
class MergePreconditions < Granite::Action::Precondition
description 'Must be mergeable'
def call(**)
decline_with(:merge_into_unchecked_skill) and return unless new_skill_name.checked?
if broken_merge?
decline_with(:merging_with_descendant) and return if merging_with_descendant?
decline_with(:non_obsolete_bonus_program_reference) and return if merging_from_bonus_program_in_use?
decline_with(:broken_merge)
end
end
def broken_merge?
skill_mergers_hash.values.any?(&:invalid?)
end
# Checks if there is any embedded skill that would be merged with it's own descendant
def merging_with_descendant?
skill_mergers_hash.values.any? do |skill_merger|
skill_merger.failed_preconditions.include?(:merging_with_descendant)
end
end
def merging_from_bonus_program_in_use?
skill_mergers_hash.values.any? do |skill_merger|
skill_merger.failed_preconditions.include?(:non_obsolete_bonus_program_reference)
end
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment