Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/project avatar #8

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added app/assets/images/no_project_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions app/assets/javascripts/project.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,13 @@ $ ->
$("a[href=" + defaultView + "]").tab "show"
else
$("a[data-toggle='tab']:first").tab "show"

# avatar
$('.js-choose-project-avatar-button').bind "click", ->
form = $(this).closest("form")
form.find(".js-project-avatar-input").click()

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

foo

$('.js-project-avatar-input').bind "change", ->
form = $(this).closest("form")
filename = $(this).val().replace(/^.*[\\\/]/, '')
form.find(".js-avatar-filename").text(filename)
13 changes: 13 additions & 0 deletions app/assets/stylesheets/generic/avatar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,16 @@
&.s90 { width: 90px; height: 90px; margin-right: 15px; }
&.s160 { width: 160px; height: 160px; margin-right: 20px; }
}

.identicon {
text-align: center;
vertical-align: top;

&.s16 { font-size: 12px; line-height: 1.33; }
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

er

&.s24 { font-size: 18px; line-height: 1.33; }
&.s26 { font-size: 20px; line-height: 1.33; }
&.s32 { font-size: 24px; line-height: 1.33; }
&.s60 { font-size: 45px; line-height: 1.33; }
&.s90 { font-size: 68px; line-height: 1.33; }
&.s160 { font-size: 120px; line-height: 1.33; }
}
19 changes: 16 additions & 3 deletions app/assets/stylesheets/sections/dashboard.scss
Original file line number Diff line number Diff line change
Expand Up @@ -94,20 +94,33 @@
overflow: hidden;
}

.project-avatar {
float: left;
}

.project-access-icon {
margin-left: 10px;
float: left;
margin-right: 15px;
font-size: 20px;
margin-bottom: 15px;

border: 1px solid #EEE;
padding: 8px 12px;
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reer

border-radius: 20px;
background: #f5f5f5;
text-align: center;
position: relative;
left: -32px;
top: 38px;
i {
color: #888;
}
}

.dash-project-avatar {
float: left;
}
.dash-project-access-icon {
float: left;
margin-right: 3px;
color: #999;
width: 16px;
}
29 changes: 29 additions & 0 deletions app/controllers/projects/avatars_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
class Projects::AvatarsController < Projects::ApplicationController
layout 'project'

before_filter :project

def show
@blob = @project.repository.blob_at_branch('master', @project.avatar_in_git)
if @blob
headers['X-Content-Type-Options'] = 'nosniff'
send_data(
@blob.data,
type: @blob.mime_type,
disposition: 'inline',
filename: @blob.name
)
else
not_found!
end
end

def destroy
@project.remove_avatar!

@project.save
@project.reset_events_cache

redirect_to edit_project_path(@project)
end
end
27 changes: 27 additions & 0 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,33 @@ def current_action?(*args)
args.any? { |v| v.to_s.downcase == action_name }
end

def project_icon(project_id, options = {})
project = Project.find_with_namespace(project_id)
if project.avatar.present?
image_tag project.avatar.url, options
elsif options[:only_uploaded]
image_tag '/assets/no_project_icon.png', options
elsif project.avatar_in_git
image_tag project_avatar_path(project), options
else # generated icon
project_identicon(project, options)
end
end

def project_identicon(project, options = {})
options[:class] ||= ''
options[:class] << ' identicon'
bg_color = Digest::MD5.hexdigest(project.name)[0, 6]
brightness = bg_color[0, 2].hex + bg_color[2, 2].hex + bg_color[4, 2].hex
text_color = (brightness > 375) ? '#000' : '#fff'
content_tag(:div,
class: options[:class],
style: "background-color: ##{bg_color}; "\
"color: #{text_color}") do
project.name[0, 1].upcase
end
end

def group_icon(group_path)
group = Group.find_by(path: group_path)
if group && group.avatar.present?
Expand Down
21 changes: 21 additions & 0 deletions app/models/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
# import_status :string(255)
# repository_size :float default(0.0)
# star_count :integer default(0), not null
# avatar :string(255)
#

class Project < ActiveRecord::Base
Expand Down Expand Up @@ -116,6 +117,12 @@ class Project < ActiveRecord::Base
validates :star_count, numericality: { greater_than_or_equal_to: 0 }
validate :check_limit, on: :create

validate :avatar_type,
if: ->(project) { project.avatar && project.avatar_changed? }
validates :avatar, file_size: { maximum: 100.kilobytes.to_i }

mount_uploader :avatar, AttachmentUploader

# Scopes
scope :without_user, ->(user) { where("projects.id NOT IN (:ids)", ids: user.authorized_projects.map(&:id) ) }
scope :without_team, ->(team) { team.projects.present? ? where("projects.id NOT IN (:ids)", ids: team.projects.map(&:id)) : scoped }
Expand Down Expand Up @@ -328,6 +335,19 @@ def ci_service
@ci_service ||= ci_services.select(&:activated?).first
end

def avatar_type
unless avatar.image?
errors.add :avatar, 'only images allowed'
end
end

def avatar_in_git
@avatar_file ||= 'logo.png' if repository.blob_at_branch('master', 'logo.png')
@avatar_file ||= 'logo.jpg' if repository.blob_at_branch('master', 'logo.jpg')
@avatar_file ||= 'logo.gif' if repository.blob_at_branch('master', 'logo.gif')
@avatar_file
end

# For compatibility with old code
def code
path
Expand Down Expand Up @@ -561,6 +581,7 @@ def hook_attrs
# Since we do cache @event we need to reset cache in special cases:
# * when project was moved
# * when project was renamed
# * when the project avatar changes
# Events cache stored like events/23-20130109142513.
# The cache key includes updated_at timestamp.
# Thus it will automatically generate a new fragment
Expand Down
3 changes: 3 additions & 0 deletions app/services/projects/fork_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ def execute
project.path = @from_project.path
project.namespace = current_user.namespace
project.creator = current_user
if @from_project.avatar && @from_project.avatar.image?
project.avatar = @from_project.avatar
end

# If the project cannot save, we do not want to trigger the project destroy
# as this can have the side effect of deleting a repo attached to an existing
Expand Down
2 changes: 2 additions & 0 deletions app/views/dashboard/_project.html.haml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
= link_to project_path(project), class: dom_class(project) do
.dash-project-avatar
= project_icon(project.to_param, alt: '', class: 'avatar s24')
.dash-project-access-icon
= visibility_level_icon(project.visibility_level)
%span.str-truncated
Expand Down
3 changes: 2 additions & 1 deletion app/views/dashboard/projects.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- @projects.each do |project|
%li.my-project-row
%h4.project-title
.project-avatar
= project_icon(project.to_param, alt: '', class: 'avatar s60')
.project-access-icon
= visibility_level_icon(project.visibility_level)
= link_to project_path(project), class: dom_class(project) do
Expand Down Expand Up @@ -70,4 +72,3 @@
.nothing-here-block There are no projects here.
.bottom
= paginate @projects, theme: "gitlab"

3 changes: 2 additions & 1 deletion app/views/projects/_home_panel.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
- empty_repo = @project.empty_repo?
.project-home-panel{:class => ("empty-project" if empty_repo)}
.project-home-row
.project-avtatar
= project_icon(@project.to_param, alt: '', class: 'avatar s32')
.project-home-desc
- if @project.description.present?
= escaped_autolink(@project.description)
Expand All @@ -22,7 +24,6 @@
- else
= link_to fork_project_path(@project), title: "Fork project", method: "POST" do
= link_to_toggle_fork

.star-buttons
%span.star.js-toggler-container{class: @show_star ? 'on' : ''}
- if current_user
Expand Down
28 changes: 27 additions & 1 deletion app/views/projects/edit.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
%p.light Some settings, such as "Transfer Project", are hidden inside the danger area below.
%hr
.panel-body
= form_for @project, remote: true, html: { class: "edit_project form-horizontal" } do |f|
= form_for @project, remote: true, html: { multipart: true, class: "edit_project form-horizontal" }, authenticity_token: true do |f|

%fieldset
.form-group.project_name_holder
= f.label :name, class: 'control-label' do
Expand Down Expand Up @@ -80,6 +81,31 @@
= f.check_box :snippets_enabled
%span.descr Share code pastes with others out of git repository

%fieldset.features
%legend
Project avatar:
.form-group
.col-sm-2
.col-sm-10
= project_icon(@project.to_param, alt: '', class: 'avatar s160', only_uploaded: true)
%p.light
- if @project.avatar_in_git
Project avatar in repository: #{ @project.avatar_in_git }
%p.light
- if @project.avatar?
You can change your project avatar here
- else
You can upload an project avatar here
%a.choose-btn.btn.btn-small.js-choose-project-avatar-button
%i.icon-paper-clip
%span Choose File ...
&nbsp;
%span.file_name.js-avatar-filename File name...
= f.file_field :avatar, class: "js-project-avatar-input hidden"
.light The maximum file size allowed is 100KB.
- if @project.avatar?
%hr
= link_to 'Remove avatar', project_avatar_path(@project), data: { confirm: "Project avatar will be removed. Are you sure?"}, method: :delete, class: "btn btn-remove btn-small remove-avatar"

.form-actions
= f.submit 'Save changes', class: "btn btn-save"
Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@
post :preview
end
end

resource :avatar, only: [:show, :destroy]
end
end

Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20141025111418_add_avatar_to_projects.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddAvatarToProjects < ActiveRecord::Migration
def change
add_column :projects, :avatar, :string
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20141007100818) do
ActiveRecord::Schema.define(version: 20141025111418) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -262,6 +262,7 @@
t.string "import_status"
t.float "repository_size", default: 0.0
t.integer "star_count", default: 0, null: false
t.string "avatar"
end

add_index "projects", ["creator_id"], name: "index_projects_on_creator_id", using: :btree
Expand Down
13 changes: 13 additions & 0 deletions features/project/project.feature
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,19 @@ Feature: Project
And project "Shop" has push event
And I visit project "Shop" page

Scenario: I edit the project avatar
Given I visit edit project "Shop" page
When I change the project avatar
And I should see new project avatar
And I should see the "Remove avatar" button

Scenario: I remove the project avatar
Given I visit edit project "Shop" page
And I have an project avatar
When I remove my project avatar
Then I should see the default project avatar
And I should not see the "Remove avatar" button

@javascript
Scenario: I should see project activity
When I visit project "Shop" page
Expand Down
47 changes: 44 additions & 3 deletions features/steps/project/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,53 @@ class Spinach::Features::Project < Spinach::FeatureSteps
end

step 'change project path settings' do
fill_in "project_path", with: "new-path"
click_button "Rename"
fill_in 'project_path', with: 'new-path'
click_button 'Rename'
end

step 'I should see project with new path settings' do
project.path.should == "new-path"
project.path.should == 'new-path'
end

step 'I change the project avatar' do
attach_file(
:project_avatar,
File.join(Rails.root, 'public', 'gitlab_logo.png')
)
click_button 'Save changes'
@project.reload
end

step 'I should see new project avatar' do
@project.avatar.should be_instance_of AttachmentUploader
url = @project.avatar.url
url.should == "/uploads/project/avatar/#{ @project.id }/gitlab_logo.png"
end

step 'I should see the "Remove avatar" button' do
page.should have_link('Remove avatar')
end

step 'I have an project avatar' do
attach_file(
:project_avatar,
File.join(Rails.root, 'public', 'gitlab_logo.png')
)
click_button 'Save changes'
@project.reload
end

step 'I remove my project avatar' do
click_link 'Remove avatar'
@project.reload
end

step 'I should see the default project avatar' do
@project.avatar?.should be_false
end

step 'I should not see the "Remove avatar" button' do
page.should_not have_link('Remove avatar')
end

step 'I should see project "Shop" version' do
Expand Down
Loading