debian-mirror-gitlab/lib/static_model.rb

46 lines
828 B
Ruby
Raw Normal View History

2018-12-13 13:39:08 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
# Provides an ActiveRecord-like interface to a model whose data is not persisted to a database.
module StaticModel
extend ActiveSupport::Concern
2018-11-20 20:47:30 +05:30
class_methods do
2014-09-02 18:07:02 +05:30
# 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)
2018-03-17 18:26:18 +05:30
send(key) if respond_to?(key) # rubocop:disable GitlabSecurity/PublicSend
2014-09-02 18:07:02 +05:30
end
def to_param
id
end
def new_record?
false
end
def persisted?
false
end
def destroyed?
false
end
def ==(other)
2020-05-24 23:13:21 +05:30
other.present? && other.is_a?(self.class) && id == other.id
2014-09-02 18:07:02 +05:30
end
end