[#345] remove url_for hack

This commit is contained in:
Alexander Machehin 2014-03-13 22:35:00 +06:00
parent 5aa5997203
commit 718c8bb08c
20 changed files with 41 additions and 59 deletions

View File

@ -6,29 +6,19 @@ RosaABF.controller('ProjectScheduleController', ['$scope', '$http', function($sc
'1': 'autostart_statuses.1', '1': 'autostart_statuses.1',
'2': 'autostart_statuses.2' '2': 'autostart_statuses.2'
}; };
$scope.project = null;
$scope.owner = null;
$scope.items = []; $scope.items = [];
$scope.init = function(name_with_owner) {
var arr = name_with_owner.split('/');
$scope.owner = arr[0];
$scope.project = arr[1];
}
$scope.updateStatus = function() { $scope.updateStatus = function() {
$http.put( $http.put(
Routes.project_path($scope.owner, $scope.project), Routes.project_path($scope.name_with_owner),
{project: {autostart_status: $scope.autostart_status}, format: 'json'} {project: {autostart_status: $scope.autostart_status}, format: 'json'}
); );
} }
$scope.updateSchedule = function(obj) { $scope.updateSchedule = function(obj) {
$http.put( $http.put(
Routes.project_schedule_path($scope.owner, $scope.project), Routes.project_schedule_path($scope.name_with_owner),
{enabled: obj.enabled, auto_publish: obj.auto_publish, repository_id: obj.repository_id, format: 'json'} {enabled: obj.enabled, auto_publish: obj.auto_publish, repository_id: obj.repository_id, format: 'json'}
); );
} }
}]); }]);

View File

@ -35,8 +35,8 @@ var BuildList = function(atts, dictionary) {
self.version_link_text = self.commit_hash || self.project_version; self.version_link_text = self.commit_hash || self.project_version;
self.version_link_url = Routes.commit_path(self.project.owner, self.project.name, self.version_link_text); self.version_link_url = Routes.commit_path(self.project.owner, self.project.name, self.version_link_text);
} }
self.project.url = Routes.project_path(self.project.owner, self.project.name);
self.project.name_with_owner = self.project.owner + '/' + self.project.name; self.project.name_with_owner = self.project.owner + '/' + self.project.name;
self.project.url = Routes.project_path(self.project.name_with_owner);
} }
if (self.user) if (self.user)

View File

@ -67,8 +67,6 @@ class Platforms::ProductsController < Platforms::BaseController
protected protected
def set_project def set_project
args = params[:src_project].try(:split, '/') || [] @product.project = Project.find_by_owner_and_name params[:src_project]
@product.project = (args.length == 2) ?
Project.find_by_owner_and_name(*args) : nil
end end
end end

View File

@ -12,7 +12,7 @@ class Projects::BaseController < ApplicationController
end end
def find_project def find_project
@project = Project.find_by_owner_and_name!(params[:owner_name], params[:project_name]) if params[:owner_name].present? && params[:project_name].present? @project = Project.find_by_owner_and_name! params[:owner_with_name]
end end
def init_statistics def init_statistics

View File

@ -152,8 +152,7 @@ class Projects::PullRequestsController < Projects::BaseController
end end
def find_destination_project bang=true def find_destination_project bang=true
args = params[:to_project].try(:split, '/') || [] project = Project.find_by_owner_and_name params[:to_project]
project = (args.length == 2) ? Project.find_by_owner_and_name(*args) : nil
raise ActiveRecord::RecordNotFound if bang && !project raise ActiveRecord::RecordNotFound if bang && !project
project || @project project || @project
end end

View File

@ -3,8 +3,7 @@ class Users::UsersController < Users::BaseController
before_filter :find_user_by_key, only: [:allowed, :discover] before_filter :find_user_by_key, only: [:allowed, :discover]
def allowed def allowed
owner_name, project_name = params[:project].split '/' project = Project.find_by_owner_and_name! params[:project]
project = Project.find_by_owner_and_name!(owner_name, project_name ? project_name : '!')
action = case params[:action_type] action = case params[:action_type]
when 'git-upload-pack' when 'git-upload-pack'
then :read then :read

View File

@ -11,6 +11,7 @@ class Project < ActiveRecord::Base
VISIBILITIES = ['open', 'hidden'] VISIBILITIES = ['open', 'hidden']
MAX_OWN_PROJECTS = 32000 MAX_OWN_PROJECTS = 32000
NAME_REGEXP = /[\w\-\+\.]+/ NAME_REGEXP = /[\w\-\+\.]+/
OWNER_AND_NAME_REGEXP = /#{User::NAME_REGEXP.source}\/#{NAME_REGEXP.source}/
belongs_to :owner, polymorphic: true, counter_cache: :own_projects_count belongs_to :owner, polymorphic: true, counter_cache: :own_projects_count
belongs_to :maintainer, class_name: 'User' belongs_to :maintainer, class_name: 'User'
@ -37,7 +38,7 @@ class Project < ActiveRecord::Base
validates :name, uniqueness: { scope: [:owner_id, :owner_type], case_sensitive: false }, validates :name, uniqueness: { scope: [:owner_id, :owner_type], case_sensitive: false },
presence: true, presence: true,
format: { with: /\A#{NAME_REGEXP}\z/, format: { with: /\A#{NAME_REGEXP.source}\z/,
message: I18n.t("activerecord.errors.project.uname") } message: I18n.t("activerecord.errors.project.uname") }
validates :maintainer_id, presence: true, unless: :new_record? validates :maintainer_id, presence: true, unless: :new_record?
validates :url, presence: true, format: { with: /\Ahttps?:\/\/[\S]+\z/ }, if: :mass_import validates :url, presence: true, format: { with: /\Ahttps?:\/\/[\S]+\z/ }, if: :mass_import
@ -95,13 +96,15 @@ class Project < ActiveRecord::Base
attr_accessor :url, :srpms_list, :mass_import, :add_to_repository_id attr_accessor :url, :srpms_list, :mass_import, :add_to_repository_id
class << self class << self
def find_by_owner_and_name(owner_name, project_name) def find_by_owner_and_name(first, last = nil)
where(owner_uname: owner_name, name: project_name).first || arr = first.try(:split, '/') || []
by_owner_and_name(owner_name, project_name).first arr = (arr << last).compact
return nil if arr.length != 2
where(owner_uname: arr.first, name: arr.last).first || by_owner_and_name(*arr).first
end end
def find_by_owner_and_name!(owner_name, project_name) def find_by_owner_and_name!(first, last = nil)
find_by_owner_and_name(owner_name, project_name) or raise ActiveRecord::RecordNotFound find_by_owner_and_name(first, last) or raise ActiveRecord::RecordNotFound
end end
end end
@ -114,7 +117,7 @@ class Project < ActiveRecord::Base
end end
def to_param def to_param
name "#{owner_uname}/#{name}"
end end
def all_members def all_members

View File

@ -8,6 +8,7 @@ class User < Avatar
EXTENDED_ROLES = ROLES | ['system'] EXTENDED_ROLES = ROLES | ['system']
LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']] LANGUAGES_FOR_SELECT = [['Russian', 'ru'], ['English', 'en']]
LANGUAGES = LANGUAGES_FOR_SELECT.map(&:last) LANGUAGES = LANGUAGES_FOR_SELECT.map(&:last)
NAME_REGEXP = /[a-z0-9_]+/
devise :database_authenticatable, :registerable, :omniauthable, devise :database_authenticatable, :registerable, :omniauthable,
:recoverable, :rememberable, :validatable, :lockable, :confirmable :recoverable, :rememberable, :validatable, :lockable, :confirmable
@ -40,7 +41,7 @@ class User < Avatar
has_many :ssh_keys, dependent: :destroy has_many :ssh_keys, dependent: :destroy
validates :uname, presence: true, uniqueness: { case_sensitive: false }, validates :uname, presence: true, uniqueness: { case_sensitive: false },
format: { with: /\A[a-z0-9_]+\z/ }, reserved_name: true format: { with: /\A#{NAME_REGEXP.source}\z/ }, reserved_name: true
validate { errors.add(:uname, :taken) if Group.by_uname(uname).present? } validate { errors.add(:uname, :taken) if Group.by_uname(uname).present? }
validates :role, inclusion: { in: EXTENDED_ROLES }, allow_blank: true validates :role, inclusion: { in: EXTENDED_ROLES }, allow_blank: true
validates :language, inclusion: { in: LANGUAGES }, allow_blank: true validates :language, inclusion: { in: LANGUAGES }, allow_blank: true

View File

@ -4,7 +4,8 @@
.text .text
%span %span
= raw t('notifications.bodies.build_task', id: build_list_id, task_link: build_list_path(build_list_id)) = raw t('notifications.bodies.build_task', id: build_list_id, task_link: build_list_path(build_list_id))
= raw t('notifications.bodies.project', project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t('notifications.bodies.project', project_link: link_to(name_with_owner, name_with_owner) )
- message, error = case status - message, error = case status
- when BuildList::BUILD_PENDING - when BuildList::BUILD_PENDING
- ['pending', nil] - ['pending', nil]

View File

@ -5,7 +5,8 @@
%span %span
-_user_link = defined?(user_email) ? user_link(user, defined?(user_name) ? user_name : user_email) : nil -_user_link = defined?(user_email) ? user_link(user, defined?(user_name) ? user_name : user_email) : nil
= t('notifications.bodies.delete_branch', branch_name: branch_name, user_link: _user_link).html_safe = t('notifications.bodies.delete_branch', branch_name: branch_name, user_link: _user_link).html_safe
= raw t("notifications.bodies.project", project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t("notifications.bodies.project", project_link: link_to(name_with_owner, project_path(name_with_owner)) )
.both .both
= datetime_moment activity_feed.created_at, tag: :span, class: 'date' = datetime_moment activity_feed.created_at, tag: :span, class: 'date'
.both .both

View File

@ -5,7 +5,8 @@
%span %span
-_user_link = defined?(user_email) ? user_link(user, defined?(user_name) ? user_name : user_email) : nil -_user_link = defined?(user_email) ? user_link(user, defined?(user_name) ? user_name : user_email) : nil
= raw t("notifications.bodies.#{change_type}_branch", {branch_name: branch_name, user_link: _user_link}) = raw t("notifications.bodies.#{change_type}_branch", {branch_name: branch_name, user_link: _user_link})
= raw t("notifications.bodies.project", project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t("notifications.bodies.project", project_link: link_to(name_with_owner, project_path(name_with_owner)) )
.both .both
= datetime_moment activity_feed.created_at, tag: :span, class: 'date' = datetime_moment activity_feed.created_at, tag: :span, class: 'date'
.both .both

View File

@ -2,7 +2,8 @@
.text .text
%span %span
= raw t("notifications.bodies.issue_assign_notification", { issue_link: link_to(issue_title, project_issue_path(project_owner, project_name, issue_serial_id))}) = raw t("notifications.bodies.issue_assign_notification", { issue_link: link_to(issue_title, project_issue_path(project_owner, project_name, issue_serial_id))})
= raw t("notifications.bodies.project", project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t("notifications.bodies.project", project_link: link_to(name_with_owner, project_path(name_with_owner)) )
.both .both
= datetime_moment activity_feed.created_at, tag: :span, class: 'date' = datetime_moment activity_feed.created_at, tag: :span, class: 'date'
.both .both

View File

@ -5,7 +5,8 @@
%span %span
= raw t("notifications.bodies.new_comment_notification.title", user_link: user_link(user, user_name)) = raw t("notifications.bodies.new_comment_notification.title", user_link: user_link(user, user_name))
= raw t("notifications.bodies.new_comment_notification.commit_content", {commit_link: link_to(commit_message, commit_path(project_owner, project_name, commit_id) + "#comment#{comment_id}")}) = raw t("notifications.bodies.new_comment_notification.commit_content", {commit_link: link_to(commit_message, commit_path(project_owner, project_name, commit_id) + "#comment#{comment_id}")})
= raw t("notifications.bodies.project", project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t("notifications.bodies.project", project_link: link_to(name_with_owner, project_path(name_with_owner)) )
.both .both
= datetime_moment activity_feed.created_at, tag: :span, class: 'date' = datetime_moment activity_feed.created_at, tag: :span, class: 'date'
.both .both

View File

@ -5,7 +5,8 @@
%span %span
= raw t("notifications.bodies.new_comment_notification.title", {user_link: user_link(user, user_name)}) = raw t("notifications.bodies.new_comment_notification.title", {user_link: user_link(user, user_name)})
= raw t("notifications.bodies.new_comment_notification.content", {issue_link: link_to(issue_title, project_issue_path(project_owner, project_name, issue_serial_id) + "#comment#{comment_id}")}) = raw t("notifications.bodies.new_comment_notification.content", {issue_link: link_to(issue_title, project_issue_path(project_owner, project_name, issue_serial_id) + "#comment#{comment_id}")})
= raw t("notifications.bodies.project", project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t("notifications.bodies.project", project_link: link_to(name_with_owner, project_path(name_with_owner)) )
.both .both
= datetime_moment activity_feed.created_at, tag: :span, class: 'date' = datetime_moment activity_feed.created_at, tag: :span, class: 'date'
.both .both

View File

@ -4,7 +4,8 @@
.text .text
%span %span
= raw t("notifications.bodies.new_issue_notification", { user_link: user_link(user, user_name), issue_link: project_issue_path(project_owner, project_name, issue_serial_id)}) = raw t("notifications.bodies.new_issue_notification", { user_link: user_link(user, user_name), issue_link: project_issue_path(project_owner, project_name, issue_serial_id)})
= raw t("notifications.bodies.project", project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t("notifications.bodies.project", project_link: link_to(name_with_owner, project_path(name_with_owner)) )
.both .both
= datetime_moment activity_feed.created_at, tag: :span, class: 'date' = datetime_moment activity_feed.created_at, tag: :span, class: 'date'
.both .both

View File

@ -4,7 +4,8 @@
.text .text
%span %span
= raw t("notifications.bodies.wiki_new_commit_notification", {user_link: user_link(user, user_name), history_link: link_to("wiki", history_project_wiki_index_path(project_owner, project_name))}) = raw t("notifications.bodies.wiki_new_commit_notification", {user_link: user_link(user, user_name), history_link: link_to("wiki", history_project_wiki_index_path(project_owner, project_name))})
= raw t("notifications.bodies.project", project_link: link_to("#{project_owner}/#{project_name}", project_path(project_owner, project_name)) ) - name_with_owner = "#{project_owner}/#{project_name}"
= raw t("notifications.bodies.project", project_link: link_to(name_with_owner, project_path(name_with_owner)) )
.both .both
= datetime_moment activity_feed.created_at, tag: :span, class: 'date' = datetime_moment activity_feed.created_at, tag: :span, class: 'date'
.both .both

View File

@ -20,10 +20,10 @@
%li %li
= image_tag 'square.png' = image_tag 'square.png'
= link_to t('bottom_menu.developer_api'), t('bottom_menu.developer_api_url') = link_to t('bottom_menu.developer_api'), t('bottom_menu.developer_api_url')
-if Project.find_by_owner_and_name 'abf', 'abf-ideas' -if pr = Project.find_by_owner_and_name('abf/abf-ideas')
%li %li
= image_tag 'square.png' = image_tag 'square.png'
= link_to t('bottom_menu.abf_ideas'), project_issues_url('abf', 'abf-ideas') = link_to t('bottom_menu.abf_ideas'), project_issues_url(pr)
%li %li
= image_tag 'square.png' = image_tag 'square.png'
= link_to t('bottom_menu.abf_blog'), t('bottom_menu.abf_blog_url') = link_to t('bottom_menu.abf_blog'), t('bottom_menu.abf_blog_url')

View File

@ -292,7 +292,7 @@ Rosa::Application.routes.draw do
get :mass_import get :mass_import
end end
end end
scope ':owner_name/:project_name', constraints: {project_name: Project::NAME_REGEXP} do # project scope ':owner_with_name', constraints: { owner_with_name: Project::OWNER_AND_NAME_REGEXP } do # project
scope as: 'project' do scope as: 'project' do
resources :wiki do resources :wiki do
collection do collection do
@ -365,7 +365,7 @@ Rosa::Application.routes.draw do
get '/tags' => "git/trees#tags", as: :tags get '/tags' => "git/trees#tags", as: :tags
# Branches # Branches
get '/branches' => "git/trees#branches", as: :branches get '/branches' => "git/trees#branches", as: :branches
get '/branches/:treeish' => "git/trees#branches", as: :treeish_branch get '/branches/:treeish' => "git/trees#branches", as: :branch
delete '/branches/:treeish' => "git/trees#destroy", as: :destroy_branch delete '/branches/:treeish' => "git/trees#destroy", as: :destroy_branch
put '/branches/:treeish' => "git/trees#restore_branch", as: :restore_branch put '/branches/:treeish' => "git/trees#restore_branch", as: :restore_branch
post '/branches' => "git/trees#create", as: :create_branch post '/branches' => "git/trees#create", as: :create_branch

View File

@ -1,16 +0,0 @@
module ActionDispatch
module Routing
module UrlFor
def url_for_with_defaults(options = nil)
if options.kind_of?(Hash)
if project = options[:owner_name] and project.is_a?(Project) # for project routes
# set the correct owner and name
options[:owner_name], options[:project_name] = project.owner, project
end
end
url_for_without_defaults(options)
end
alias_method_chain :url_for, :defaults
end
end
end

View File

@ -22,7 +22,7 @@ module Rosa
def self.matches?(request) def self.matches?(request)
if (params = request.path_parameters) && params[:treeish] # parse existing branch (tag) and path if (params = request.path_parameters) && params[:treeish] # parse existing branch (tag) and path
branch_or_tag = begin branch_or_tag = begin
(p = Project.find_by_owner_and_name params[:owner_name], params[:project_name]) && (p = Project.find_by_owner_and_name params[:owner_and_name]) &&
p.repo.branches_and_tags.map(&:name).sort{|a,b| b.length <=> a.length}.detect{|b| params[:treeish].start_with?(b)} || p.repo.branches_and_tags.map(&:name).sort{|a,b| b.length <=> a.length}.detect{|b| params[:treeish].start_with?(b)} ||
params[:treeish].split('/').first params[:treeish].split('/').first
end end