Commit 8db2a59d0b6959a78ea7be4663dd9a858dff9f98

Authored by Robert Speicher
1 parent 877aa545

Add StaticModel role, and add it to Commit model

Instead of doing this:

    link_to(commit.id, project_commit_path(project, id: commit.id))
    Note.create(noteable_id: commit.id, noteable_type: "Commit", ...)

It lets us do this:

    link_to(commit.id, project_commit_path(project, commit))
    Note.create(noteable: commit, ...)
Showing 2 changed files with 39 additions and 8 deletions   Show diff stats
app/models/commit.rb
1 1 class Commit
2 2 include ActiveModel::Conversion
3 3 include Gitlab::Encode
  4 + include StaticModel
4 5 extend ActiveModel::Naming
5 6  
6 7 attr_accessor :commit
... ... @@ -22,8 +23,7 @@ class Commit
22 23 :to_patch,
23 24 to: :commit
24 25  
25   -
26   - class << self
  26 + class << self
27 27 def find_or_first(repo, commit_id = nil, root_ref)
28 28 commit = if commit_id
29 29 repo.commit(commit_id)
... ... @@ -85,7 +85,7 @@ class Commit
85 85 first = project.commit(to.try(:strip))
86 86 last = project.commit(from.try(:strip))
87 87  
88   - result = {
  88 + result = {
89 89 commits: [],
90 90 diffs: [],
91 91 commit: nil
... ... @@ -105,10 +105,6 @@ class Commit
105 105 end
106 106 end
107 107  
108   - def persisted?
109   - false
110   - end
111   -
112 108 def initialize(raw_commit, head = nil)
113 109 @commit = raw_commit
114 110 @head = head
... ... @@ -155,7 +151,7 @@ class Commit
155 151 prev_commit.try :id
156 152 end
157 153  
158   - def parents_count
  154 + def parents_count
159 155 parents && parents.count || 0
160 156 end
161 157 end
... ...
app/roles/static_model.rb 0 → 100644
... ... @@ -0,0 +1,35 @@
  1 +# Provides an ActiveRecord-like interface to a model whose data is not persisted to a database.
  2 +module StaticModel
  3 + extend ActiveSupport::Concern
  4 +
  5 + module ClassMethods
  6 + # Used by ActiveRecord's polymorphic association to set object_id
  7 + def primary_key
  8 + 'id'
  9 + end
  10 +
  11 + # Used by ActiveRecord's polymorphic association to set object_type
  12 + def base_class
  13 + self
  14 + end
  15 + end
  16 +
  17 + # Used by AR for fetching attributes
  18 + #
  19 + # Pass it along if we respond to it.
  20 + def [](key)
  21 + send(key) if respond_to?(key)
  22 + end
  23 +
  24 + def to_param
  25 + id
  26 + end
  27 +
  28 + def persisted?
  29 + false
  30 + end
  31 +
  32 + def destroyed?
  33 + false
  34 + end
  35 +end
... ...