gitlab-org--gitlab-foss/lib/static_model.rb
gfyoung d598e4fd93 Enable more frozen string in lib/**/*.rb
Enables frozen for the following:

* lib/*.rb
* lib/banzai/**/*.rb
* lib/bitbucket/**/*.rb
* lib/constraints/**/*.rb
* lib/container_registry/**/*.rb
* lib/declarative_policy/**/*.rb

Partially addresses #47424.
2018-10-06 17:02:50 -07:00

49 lines
847 B
Ruby

# frozen_string_literal: true
# Provides an ActiveRecord-like interface to a model whose data is not persisted to a database.
module StaticModel
extend ActiveSupport::Concern
class_methods do
# Used by ActiveRecord's polymorphic association to set object_id
def primary_key
'id'
end
# Used by ActiveRecord's polymorphic association to set object_type
def base_class
self
end
end
# Used by AR for fetching attributes
#
# Pass it along if we respond to it.
def [](key)
send(key) if respond_to?(key) # rubocop:disable GitlabSecurity/PublicSend
end
def to_param
id
end
def new_record?
false
end
def persisted?
false
end
def destroyed?
false
end
def ==(other)
if other.is_a? ::StaticModel
id == other.id
else
super
end
end
end