Commit 8db2a59d0b6959a78ea7be4663dd9a858dff9f98
1 parent
877aa545
Exists in
master
and in
4 other branches
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 | class Commit | 1 | class Commit |
| 2 | include ActiveModel::Conversion | 2 | include ActiveModel::Conversion |
| 3 | include Gitlab::Encode | 3 | include Gitlab::Encode |
| 4 | + include StaticModel | ||
| 4 | extend ActiveModel::Naming | 5 | extend ActiveModel::Naming |
| 5 | 6 | ||
| 6 | attr_accessor :commit | 7 | attr_accessor :commit |
| @@ -22,8 +23,7 @@ class Commit | @@ -22,8 +23,7 @@ class Commit | ||
| 22 | :to_patch, | 23 | :to_patch, |
| 23 | to: :commit | 24 | to: :commit |
| 24 | 25 | ||
| 25 | - | ||
| 26 | - class << self | 26 | + class << self |
| 27 | def find_or_first(repo, commit_id = nil, root_ref) | 27 | def find_or_first(repo, commit_id = nil, root_ref) |
| 28 | commit = if commit_id | 28 | commit = if commit_id |
| 29 | repo.commit(commit_id) | 29 | repo.commit(commit_id) |
| @@ -85,7 +85,7 @@ class Commit | @@ -85,7 +85,7 @@ class Commit | ||
| 85 | first = project.commit(to.try(:strip)) | 85 | first = project.commit(to.try(:strip)) |
| 86 | last = project.commit(from.try(:strip)) | 86 | last = project.commit(from.try(:strip)) |
| 87 | 87 | ||
| 88 | - result = { | 88 | + result = { |
| 89 | commits: [], | 89 | commits: [], |
| 90 | diffs: [], | 90 | diffs: [], |
| 91 | commit: nil | 91 | commit: nil |
| @@ -105,10 +105,6 @@ class Commit | @@ -105,10 +105,6 @@ class Commit | ||
| 105 | end | 105 | end |
| 106 | end | 106 | end |
| 107 | 107 | ||
| 108 | - def persisted? | ||
| 109 | - false | ||
| 110 | - end | ||
| 111 | - | ||
| 112 | def initialize(raw_commit, head = nil) | 108 | def initialize(raw_commit, head = nil) |
| 113 | @commit = raw_commit | 109 | @commit = raw_commit |
| 114 | @head = head | 110 | @head = head |
| @@ -155,7 +151,7 @@ class Commit | @@ -155,7 +151,7 @@ class Commit | ||
| 155 | prev_commit.try :id | 151 | prev_commit.try :id |
| 156 | end | 152 | end |
| 157 | 153 | ||
| 158 | - def parents_count | 154 | + def parents_count |
| 159 | parents && parents.count || 0 | 155 | parents && parents.count || 0 |
| 160 | end | 156 | end |
| 161 | end | 157 | end |
| @@ -0,0 +1,35 @@ | @@ -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 |