summaryrefslogtreecommitdiff
path: root/lib/static_model.rb
diff options
context:
space:
mode:
authorDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-01-02 23:35:11 +0200
committerDmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>2013-01-02 23:35:11 +0200
commitcac7723451e575ce39a6930990178450a2a972f0 (patch)
tree32b258ab1929ac710665b19a7f8b62addad83538 /lib/static_model.rb
parent1b25a8f4374363d546d4a58f47c6fe00c3b3af07 (diff)
downloadgitlab-ce-cac7723451e575ce39a6930990178450a2a972f0.tar.gz
Get rid of roles
Diffstat (limited to 'lib/static_model.rb')
-rw-r--r--lib/static_model.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/static_model.rb b/lib/static_model.rb
new file mode 100644
index 00000000000..5b64be1f041
--- /dev/null
+++ b/lib/static_model.rb
@@ -0,0 +1,47 @@
+# Provides an ActiveRecord-like interface to a model whose data is not persisted to a database.
+module StaticModel
+ extend ActiveSupport::Concern
+
+ module ClassMethods
+ # 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)
+ 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