BaseContext
Controllers refactoring with contexts Move commit compare logic to model
This commit is contained in:
parent
3c6daec4b1
commit
3063af5adc
8
app/contexts/base_context.rb
Normal file
8
app/contexts/base_context.rb
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
class BaseContext
|
||||||
|
attr_accessor :project, :current_user, :params
|
||||||
|
|
||||||
|
def initialize(project, user, params)
|
||||||
|
@project, @current_user, @params = project, user, params.dup
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
26
app/contexts/commit_load.rb
Normal file
26
app/contexts/commit_load.rb
Normal file
|
@ -0,0 +1,26 @@
|
||||||
|
class CommitLoad < BaseContext
|
||||||
|
def execute
|
||||||
|
result = {
|
||||||
|
:commit => nil,
|
||||||
|
:suppress_diff => false,
|
||||||
|
:line_notes => [],
|
||||||
|
:notes_count => 0,
|
||||||
|
:note => nil
|
||||||
|
}
|
||||||
|
|
||||||
|
commit = project.commit(params[:id])
|
||||||
|
|
||||||
|
if commit
|
||||||
|
commit = CommitDecorator.decorate(commit)
|
||||||
|
line_notes = project.commit_line_notes(commit)
|
||||||
|
|
||||||
|
result[:suppress_diff] = true if commit.diffs.size > 200 && !params[:force_show_diff]
|
||||||
|
result[:commit] = commit
|
||||||
|
result[:note] = project.build_commit_note(commit)
|
||||||
|
result[:line_notes] = line_notes
|
||||||
|
result[:notes_count] = line_notes.count + project.commit_notes(commit).count
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
|
end
|
16
app/contexts/merge_requests_load.rb
Normal file
16
app/contexts/merge_requests_load.rb
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
class MergeRequestsLoad < BaseContext
|
||||||
|
def execute
|
||||||
|
type = params[:f].to_i
|
||||||
|
|
||||||
|
merge_requests = project.merge_requests
|
||||||
|
|
||||||
|
merge_requests = case type
|
||||||
|
when 1 then merge_requests
|
||||||
|
when 2 then merge_requests.closed
|
||||||
|
when 3 then merge_requests.opened.assigned(current_user)
|
||||||
|
else merge_requests.opened
|
||||||
|
end.page(params[:page]).per(20)
|
||||||
|
|
||||||
|
merge_requests.includes(:author, :project).order("closed, created_at desc")
|
||||||
|
end
|
||||||
|
end
|
30
app/contexts/notes_load.rb
Normal file
30
app/contexts/notes_load.rb
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
class NotesLoad < BaseContext
|
||||||
|
def execute
|
||||||
|
target_type = params[:target_type]
|
||||||
|
target_id = params[:target_id]
|
||||||
|
first_id = params[:first_id]
|
||||||
|
last_id = params[:last_id]
|
||||||
|
|
||||||
|
|
||||||
|
@notes = case target_type
|
||||||
|
when "commit"
|
||||||
|
then project.commit_notes(project.commit(target_id)).fresh.limit(20)
|
||||||
|
when "snippet"
|
||||||
|
then project.snippets.find(target_id).notes
|
||||||
|
when "wall"
|
||||||
|
then project.common_notes.order("created_at DESC").fresh.limit(50)
|
||||||
|
when "issue"
|
||||||
|
then project.issues.find(target_id).notes.inc_author.order("created_at DESC").limit(20)
|
||||||
|
when "merge_request"
|
||||||
|
then project.merge_requests.find(target_id).notes.inc_author.order("created_at DESC").limit(20)
|
||||||
|
end
|
||||||
|
|
||||||
|
@notes = if last_id
|
||||||
|
@notes.where("id > ?", last_id)
|
||||||
|
elsif first_id
|
||||||
|
@notes.where("id < ?", first_id)
|
||||||
|
else
|
||||||
|
@notes
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
|
@ -26,43 +26,31 @@ class CommitsController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@commit = project.commit(params[:id])
|
result = CommitLoad.new(project, current_user, params).execute
|
||||||
|
|
||||||
git_not_found! and return unless @commit
|
@commit = result[:commit]
|
||||||
|
|
||||||
@commit = CommitDecorator.decorate(@commit)
|
if @commit
|
||||||
|
@suppress_diff = result[:suppress_diff]
|
||||||
@note = @project.build_commit_note(@commit)
|
@note = result[:note]
|
||||||
|
@line_notes = result[:line_notes]
|
||||||
|
@notes_count = result[:notes_count]
|
||||||
@comments_allowed = true
|
@comments_allowed = true
|
||||||
@line_notes = project.commit_line_notes(@commit)
|
else
|
||||||
|
return git_not_found!
|
||||||
@notes_count = @line_notes.count + project.commit_notes(@commit).count
|
|
||||||
|
|
||||||
if @commit.diffs.size > 200 && !params[:force_show_diff]
|
|
||||||
@suppress_diff = true
|
|
||||||
end
|
end
|
||||||
|
|
||||||
rescue Grit::Git::GitTimeout
|
rescue Grit::Git::GitTimeout
|
||||||
render "huge_commit"
|
render "huge_commit"
|
||||||
end
|
end
|
||||||
|
|
||||||
def compare
|
def compare
|
||||||
first = project.commit(params[:to].try(:strip))
|
result = Commit.compare(project, params[:from], params[:to])
|
||||||
last = project.commit(params[:from].try(:strip))
|
|
||||||
|
|
||||||
@diffs = []
|
@commits = result[:commits]
|
||||||
@commits = []
|
@commit = result[:commit]
|
||||||
|
@diffs = result[:diffs]
|
||||||
@line_notes = []
|
@line_notes = []
|
||||||
|
|
||||||
if first && last
|
|
||||||
commits = [first, last].sort_by(&:created_at)
|
|
||||||
younger = commits.first
|
|
||||||
older = commits.last
|
|
||||||
|
|
||||||
|
|
||||||
@commits = project.repo.commits_between(younger.id, older.id).map {|c| Commit.new(c)}
|
|
||||||
@diffs = project.repo.diff(younger.id, older.id) rescue []
|
|
||||||
@commit = Commit.new(older)
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def patch
|
def patch
|
||||||
|
|
|
@ -2,11 +2,8 @@ class DashboardController < ApplicationController
|
||||||
respond_to :html
|
respond_to :html
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@projects = current_user.projects.includes(:events).order("events.created_at DESC")
|
@projects = current_user.projects_with_events.page(params[:page]).per(40)
|
||||||
@projects = @projects.page(params[:page]).per(40)
|
@events = Event.recent_for_user(current_user).limit(20)
|
||||||
|
|
||||||
@events = Event.where(:project_id => current_user.projects.map(&:id)).recent.limit(20)
|
|
||||||
|
|
||||||
@last_push = current_user.recent_push
|
@last_push = current_user.recent_push
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
|
|
|
@ -24,16 +24,7 @@ class MergeRequestsController < ApplicationController
|
||||||
|
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@merge_requests = @project.merge_requests
|
@merge_requests = MergeRequestsLoad.new(project, current_user, params).execute
|
||||||
|
|
||||||
@merge_requests = case params[:f].to_i
|
|
||||||
when 1 then @merge_requests
|
|
||||||
when 2 then @merge_requests.closed
|
|
||||||
when 3 then @merge_requests.opened.assigned(current_user)
|
|
||||||
else @merge_requests.opened
|
|
||||||
end.page(params[:page]).per(20)
|
|
||||||
|
|
||||||
@merge_requests = @merge_requests.includes(:author, :project).order("closed, created_at desc")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def show
|
def show
|
||||||
|
|
|
@ -40,25 +40,6 @@ class NotesController < ApplicationController
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def notes
|
def notes
|
||||||
@notes = case params[:target_type]
|
@notes = NotesLoad.new(project, current_user, params).execute
|
||||||
when "commit"
|
|
||||||
then project.commit_notes(project.commit((params[:target_id]))).fresh.limit(20)
|
|
||||||
when "snippet"
|
|
||||||
then project.snippets.find(params[:target_id]).notes
|
|
||||||
when "wall"
|
|
||||||
then project.common_notes.order("created_at DESC").fresh.limit(50)
|
|
||||||
when "issue"
|
|
||||||
then project.issues.find(params[:target_id]).notes.inc_author.order("created_at DESC").limit(20)
|
|
||||||
when "merge_request"
|
|
||||||
then project.merge_requests.find(params[:target_id]).notes.inc_author.order("created_at DESC").limit(20)
|
|
||||||
end
|
|
||||||
|
|
||||||
@notes = if params[:last_id]
|
|
||||||
@notes.where("id > ?", params[:last_id])
|
|
||||||
elsif params[:first_id]
|
|
||||||
@notes.where("id < ?", params[:first_id])
|
|
||||||
else
|
|
||||||
@notes
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -80,6 +80,29 @@ class Commit
|
||||||
def commits_between(repo, from, to)
|
def commits_between(repo, from, to)
|
||||||
repo.commits_between(from, to).map { |c| Commit.new(c) }
|
repo.commits_between(from, to).map { |c| Commit.new(c) }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def compare(project, from, to)
|
||||||
|
first = project.commit(to.try(:strip))
|
||||||
|
last = project.commit(from.try(:strip))
|
||||||
|
|
||||||
|
result = {
|
||||||
|
:commits => [],
|
||||||
|
:diffs => [],
|
||||||
|
:commit => nil
|
||||||
|
}
|
||||||
|
|
||||||
|
if first && last
|
||||||
|
commits = [first, last].sort_by(&:created_at)
|
||||||
|
younger = commits.first
|
||||||
|
older = commits.last
|
||||||
|
|
||||||
|
result[:commits] = project.repo.commits_between(younger.id, older.id).map {|c| Commit.new(c)}
|
||||||
|
result[:diffs] = project.repo.diff(younger.id, older.id) rescue []
|
||||||
|
result[:commit] = Commit.new(older)
|
||||||
|
end
|
||||||
|
|
||||||
|
result
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def persisted?
|
def persisted?
|
||||||
|
|
|
@ -28,6 +28,10 @@ class Event < ActiveRecord::Base
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.recent_for_user user
|
||||||
|
where(:project_id => user.projects.map(&:id)).recent
|
||||||
|
end
|
||||||
|
|
||||||
# Next events currently enabled for system
|
# Next events currently enabled for system
|
||||||
# - push
|
# - push
|
||||||
# - new issue
|
# - new issue
|
||||||
|
|
|
@ -55,4 +55,8 @@ module Account
|
||||||
# Take only latest one
|
# Take only latest one
|
||||||
events = events.recent.limit(1).first
|
events = events.recent.limit(1).first
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def projects_with_events
|
||||||
|
projects.includes(:events).order("events.created_at DESC")
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue