Class: DesignManagement::DesignAtVersion

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Validations, Gitlab::Utils::StrongMemoize, GlobalID::Identification
Defined in:
app/models/design_management/design_at_version.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(design: nil, version: nil) ⇒ DesignAtVersion

Returns a new instance of DesignAtVersion.


20
21
22
# File 'app/models/design_management/design_at_version.rb', line 20

def initialize(design: nil, version: nil)
  @design, @version = design, version
end

Instance Attribute Details

#designObject (readonly)

Returns the value of attribute design


12
13
14
# File 'app/models/design_management/design_at_version.rb', line 12

def design
  @design
end

#versionObject (readonly)

Returns the value of attribute version


11
12
13
# File 'app/models/design_management/design_at_version.rb', line 11

def version
  @version
end

Class Method Details

.find(ids) ⇒ Object


53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'app/models/design_management/design_at_version.rb', line 53

def self.find(ids)
  pairs = ids.map { |id| id.split('.').map(&:to_i) }

  design_ids = pairs.map(&:first).uniq
  version_ids = pairs.map(&:second).uniq

  designs = ::DesignManagement::Design
    .where(id: design_ids)
    .index_by(&:id)

  versions = ::DesignManagement::Version
    .where(id: version_ids)
    .index_by(&:id)

  pairs.map do |(design_id, version_id)|
    design = designs[design_id]
    version = versions[version_id]

    obj = new(design: design, version: version)

    obj if obj.valid?
  end.compact
end

.instantiate(attrs) ⇒ Object


24
25
26
# File 'app/models/design_management/design_at_version.rb', line 24

def self.instantiate(attrs)
  new(attrs).tap { |obj| obj.validate! }
end

.lazy_find(id) ⇒ Object


45
46
47
48
49
50
51
# File 'app/models/design_management/design_at_version.rb', line 45

def self.lazy_find(id)
  BatchLoader.for(id).batch do |ids, callback|
    find(ids).each do |record|
      callback.call(record.id, record)
    end
  end
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?


37
38
39
40
41
# File 'app/models/design_management/design_at_version.rb', line 37

def ==(other)
  return false unless other && self.class == other.class

  other.id == id
end

#deleted?Boolean

Returns:

  • (Boolean)

87
88
89
# File 'app/models/design_management/design_at_version.rb', line 87

def deleted?
  action&.deletion?
end

#idObject

The ID, needed by GraphQL types and as part of the Lazy-fetch protocol, includes information about both the design and the version.

The particular format is not interesting, and should be treated as opaque by all callers.


33
34
35
# File 'app/models/design_management/design_at_version.rb', line 33

def id
  "#{design.id}.#{version.id}"
end

#not_created_yet?Boolean

Returns:

  • (Boolean)

91
92
93
# File 'app/models/design_management/design_at_version.rb', line 91

def not_created_yet?
  action.nil?
end

#statusObject


77
78
79
80
81
82
83
84
85
# File 'app/models/design_management/design_at_version.rb', line 77

def status
  if not_created_yet?
    :not_created_yet
  elsif deleted?
    :deleted
  else
    :current
  end
end