diff options
Diffstat (limited to 'app/models/design_management')
-rw-r--r-- | app/models/design_management/design.rb | 36 | ||||
-rw-r--r-- | app/models/design_management/design_collection.rb | 4 |
2 files changed, 39 insertions, 1 deletions
diff --git a/app/models/design_management/design.rb b/app/models/design_management/design.rb index 0dca6333fa1..deda814d689 100644 --- a/app/models/design_management/design.rb +++ b/app/models/design_management/design.rb @@ -9,6 +9,7 @@ module DesignManagement include Referable include Mentionable include WhereComposite + include RelativePositioning belongs_to :project, inverse_of: :designs belongs_to :issue @@ -75,9 +76,23 @@ module DesignManagement join = designs.join(actions) .on(actions[:design_id].eq(designs[:id])) - joins(join.join_sources).where(actions[:event].not_eq(deletion)).order(:id) + joins(join.join_sources).where(actions[:event].not_eq(deletion)) end + scope :ordered, -> (project) do + # TODO: Always order by relative position after the feature flag is removed + # https://gitlab.com/gitlab-org/gitlab/-/issues/34382 + if Feature.enabled?(:reorder_designs, project, default_enabled: true) + # We need to additionally sort by `id` to support keyset pagination. + # See https://gitlab.com/gitlab-org/gitlab/-/merge_requests/17788/diffs#note_230875678 + order(:relative_position, :id) + else + in_creation_order + end + end + + scope :in_creation_order, -> { reorder(:id) } + scope :with_filename, -> (filenames) { where(filename: filenames) } scope :on_issue, ->(issue) { where(issue_id: issue) } @@ -87,6 +102,14 @@ module DesignManagement # A design is current if the most recent event is not a deletion scope :current, -> { visible_at_version(nil) } + def self.relative_positioning_query_base(design) + default_scoped.on_issue(design.issue_id) + end + + def self.relative_positioning_parent_column + :issue_id + end + def status if new_design? :new @@ -196,6 +219,17 @@ module DesignManagement project end + def immediately_before?(next_design) + return false if next_design.relative_position <= relative_position + + interloper = self.class.on_issue(issue).where( + "relative_position <@ int4range(?, ?, '()')", + *[self, next_design].map(&:relative_position) + ) + + !interloper.exists? + end + private def head_version diff --git a/app/models/design_management/design_collection.rb b/app/models/design_management/design_collection.rb index 18d1541e9c7..96d5f4c2419 100644 --- a/app/models/design_management/design_collection.rb +++ b/app/models/design_management/design_collection.rb @@ -10,6 +10,10 @@ module DesignManagement @issue = issue end + def ==(other) + other.is_a?(self.class) && issue == other.issue + end + def find_or_create_design!(filename:) designs.find { |design| design.filename == filename } || designs.safe_find_or_create_by!(project: project, filename: filename) |