Skip to content

Adds fixed items model/assoc to activerecord-gitlab gem

What does this MR do and why?

Adds fixed items model and associations to activerecord-gitlab gem

Model concern allows classes with fixed items to have active record like methods (all, where, find_by, find).

The has one association can be used on models to establish an association to a fixed items model.

This is an extract from the custom statuses for work items POC where we'll use fixed items models to host system defined statuses and lifecycles.

  1. Draft: POC Static default status for work items (!178180 - closed)
  2. See the last refactor to move this to the gem in the POC
  3. Usage on WorkItems::Statuses::SystemDefined::Status in POC
  4. Usage of the association in WorkItems::Statuses::CurrentStatus in POC

Contributes to BE: Create tables to support Status widget (#498393 - closed)

Examples

class StaticModel
  include ActiveModel::Model
  include ActiveModel::Attributes
  include ActiveRecord::FixedItemsModel::Model

  ITEMS = [
    {
      id: 1,
      name: 'To do'
    }
  ]

  attribute :id, :integer
  attribute :name, :string
end

StaticModel.find(1)
StaticModel.where(name: 'To do')
StaticModel.find_by(name: 'To do')
StaticModel.all

class MyModel < ApplicationRecord
  include ActiveRecord::FixedItemsModel::HasOne

  belongs_to_fixed_items :static_model, fixed_items_class: StaticModel
end

m = MyModel.last
m.static_model # Returns fixed items model instance
m.static_model = StaticModel.find(1)
m.static_model_id = 1 # still possible
m.static_model? # Bool

References

Please include cross links to any resources that are relevant to this MR. This will give reviewers and future readers helpful context to give an efficient review of the changes introduced.

MR acceptance checklist

Please evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

Run the specs locally:

  1. Go to gem folder cd gems/activerecord-gitlab
  2. bundle install
  3. bundle exec rspec
Edited by Marc Saleiko

Merge request reports

Loading