Skip to content

Commit

Permalink
Refactor repos browsing.
Browse files Browse the repository at this point in the history
Getting ready to support branching and directories.
  • Loading branch information
Alwahsh committed Apr 13, 2015
1 parent 6af034e commit fe6dd21
Show file tree
Hide file tree
Showing 15 changed files with 376 additions and 255 deletions.
61 changes: 1 addition & 60 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
class ApplicationController < ActionController::Base
include Escape
include SessionsHelper
include PathsHelper
protect_from_forgery
before_action :configure_devise_permitted_parameters, if: :devise_controller?
before_action :return_current_user_projects
Expand All @@ -17,55 +18,6 @@ def return_current_user_projects
@projects = current_user.projects if user_signed_in?
end

# Returns an array containing the configured width and height for thumbnails.
def thumbnail_size
Glitter::Application.config.thumbnail_geometry
end

# Generates a thumbnail for a commit in the appropriate place.
def generate_thumbnail(project, imagefile, commit_id)
image = Magick::Image.read(
"#{project.data_path}/satellite/#{imagefile}"
).first
image.scale(
thumbnail_size[0],
thumbnail_size[1]
).write project.thumbnail_for(commit_id, true)
end

def images_commit(project, image_files)
return unless user_signed_in?
repo = project.satelliterepo
image_files.each do |f|
tmp = f.tempfile
file = File.join project.satellitedir, f.original_filename
FileUtils.cp tmp.path, file
repo.index.add f.original_filename
end
commit_id = satellite_commit(
repo,
get_message(image_files)
)
generate_thumbnail project, image_files.last.original_filename, commit_id
project.pushtobare
end

def get_message(image_files)
names = image_files.map { |i| i.original_filename}
"Add #{view_context.pluralize(image_files.size, 'image')}:" \
" #{names.to_sentence}"
end

# TODO: refactor satellite_commit and satellite_delete

def satellite_commit(repo, message)
author = current_user.git_author_params
options = rugged_commit_options(author, repo, message)
commit_id = Rugged::Commit.create repo, options
repo.index.write
commit_id
end

def satellite_delete(repo, file_name)
repo.index.remove file_name
author = current_user.git_author_params
Expand All @@ -74,17 +26,6 @@ def satellite_delete(repo, file_name)
repo.index.write
end

def rugged_commit_options(author, repo, message)
{
author: author,
committer: author,
tree: repo.index.write_tree(repo),
update_ref: 'HEAD',
message: message,
parents: repo.empty? ? [] : [repo.head.target].compact
}
end

def pg(things, num)
things.paginate(page: params[:page], per_page: num) unless things.nil?
end
Expand Down
1 change: 0 additions & 1 deletion app/controllers/issues_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
class IssuesController < ApplicationController
include ApplicationHelper
before_action :get_context

def index
Expand Down
74 changes: 31 additions & 43 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def destroy
redirect_to dashboard_path
else
flash[:error] = "You don't have permission for this command!"
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end
end

Expand All @@ -59,7 +59,7 @@ def create
notification.victims << current_user.followers
notification.save!
end
redirect_to project.urlbase
redirect_to user_project_path project.user, project
else
flash[:alert] = "Didn't save project!"
redirect_to new_project_path
Expand All @@ -81,7 +81,7 @@ def follow
flash[:notice] = "You're the owner of this project, " \
'you automatically receive updates.'
end
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end

# DELETE /user_id/id/unfollow
Expand All @@ -93,7 +93,7 @@ def unfollow
flash[:notice] = 'You were not following ' \
"#{@user.username}/#{@project.name}"
end
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end

def tree_at(revision, path)
Expand All @@ -102,14 +102,19 @@ def tree_at(revision, path)
path == '/' ? tree.oid : tree.path(path)[:oid]
end

def show_blob_content
# GET /user/project/blob/branch_or_commit_oid/destination
def blob
barerepo = @project.barerepo
@blob = barerepo.blob_at barerepo.last_commit.oid, params[:destination]
oid = params[:oid]
@branch = barerepo.branches[oid]
oid = barerepo.branches[oid].target.oid if @branch
@blob = @project.blob oid, params[:destination]
@enc_blob_text = Base64.encode64 @blob.text
@comments = Comment.where(
polycomment_type: 'blob',
polycomment_id: @blob.oid
)
@comments = pg @comments, 10
@comment = Comment.new
@ajax = params[:page].nil? || params[:page] == 1
end
Expand All @@ -121,9 +126,7 @@ def show
headtree = barerepo.lookup barerepo.last_commit.tree_id
# TODO: what if there are trees inside this tree?
headtree.each do |blob|
link = File.join @project.urlbase, 'master', blob[:name]
@images.push({
link: link,
name: blob[:name],
url: @project.imageurl(blob[:name])
})
Expand Down Expand Up @@ -208,23 +211,6 @@ def tree
render 'commit'
end

# Given a filename, view the state of the file in master.
# Since we aren't doing much branch hopping on the app itself, this
# is useful for fetching the current state of a file in a project.

# TODO: /tree/master/file_name and generalize for /tree/branch/file_name

def masterbranch
@imageurl = File.join @project.satellitedir, params[:image_name]
@comments = Comment.where(
polycomment_type: 'file',
polycomment_id: params[:image_name]
)
@comments = pg @comments, 10
@comment = Comment.new
@ajax = params[:page].nil? || params[:page] == 1
end

def file_history
@bloblist = []
walker = Rugged::Walker.new @project.barerepo
Expand Down Expand Up @@ -254,7 +240,11 @@ def update

def file_upload
if params[:file]
if images_commit @project, params[:file]
if user_signed_in? && @project.add_images(
params[:branch] || 'master',
params[:file],
@user.git_author_params
)
sentence = view_context.pluralize(params[:file].size, 'image')
flash[:notice] = "Successfully added #{sentence}! How sparkly!"
else
Expand All @@ -263,25 +253,23 @@ def file_upload
else
flash[:alert] = 'No image selected!'
end
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end

def file_update
tmp = params[:file].tempfile
file = File.join @project.satellitedir, params[:image_name]
FileUtils.cp tmp.path, file
@project.satelliterepo.index.add params[:image_name]
if params[:file]
message = params[:message]
commit_id = satellite_commit @project.satelliterepo, message
generate_thumbnail @project, params[:image_name], commit_id
@project.pushtobare
flash[:notice] = "#{params[:image_name]} has been updated! Shiny!"
if @project.add_images(
params[:branch],
[params[:file]],
@user.git_author_params,
params[:message],
params[:image_path]
)
flash[:notice] = "#{params[:image_path]} has been updated! Shiny!"
else
flash[:alert] = "Unable to update #{params[:image_name]}. " \
flash[:alert] = "Unable to update #{params[:image_path]}. " \
'The server ponies are sad.'
end
redirect_to @project.urlbase
redirect_to project_blob_path @project, params[:image_path], params[:branch]
end

def file_delete
Expand All @@ -290,21 +278,21 @@ def file_delete
satellite_delete @project.satelliterepo, params[:image_name]
@project.pushtobare
flash[:notice] = "#{params[:image_name]} has been deleted!"
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end

def open
@pull = PullRequest.find params[:pull_id]
@pull.status = 'open'
@pull.save
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end

def close
@pull = PullRequest.find params[:pull_id]
@pull.status = 'closed'
@pull.save
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end

def fork
Expand All @@ -316,7 +304,7 @@ def fork
else
flash[:alert] = "Couldn't fork project. " \
"#{child.errors.full_messages.to_sentence}"
redirect_to @project.urlbase
redirect_to user_project_path @project.user, @project
end
end

Expand Down
61 changes: 0 additions & 61 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,65 +45,4 @@ def set_body_class
log_status = user_signed_in? ? 'logged_in' : 'not_logged_in'
"#{controller.controller_name} #{controller.action_name} #{log_status}"
end

# Path helpers

def project_issues_path(project)
user_project_issues_path project.user, project, params[:xid]
end

def issue_path(issue)
user_project_issue_path(
issue.project.user,
issue.project,
params[:xid],
issue
)
end

def close_issue_path(issue)
close_user_project_issue_path(
issue.project.user,
issue.project,
params[:xid],
issue
)
end

def reopen_issue_path(issue)
reopen_user_project_issue_path(
issue.project.user,
issue.project,
params[:xid],
issue
)
end

def project_commits_path(project)
commits_user_project_path project.user, project, params[:xid]
end

def project_network_path(project)
network_user_project_path project.user, project, params[:xid]
end

def project_newfile_path(project)
newfile_user_project_path project.user, project, params[:xid]
end

def project_fork_path(project)
fork_user_project_path project.user, project, params[:xid]
end

def project_settings_path(project)
settings_user_project_path project.user, project, params[:xid]
end

def project_follow_path(project)
follow_user_project_path project.user, project, params[:xid]
end

def project_unfollow_path(project)
unfollow_user_project_path project.user, project, params[:xid]
end
end
Loading

0 comments on commit fe6dd21

Please sign in to comment.