Minor design fix. Refactor register requests. Refactor case insensitive search. Other minor fixes and code cleanup. Refacor and improve owner constraint. Refs #263

This commit is contained in:
Pavel Chipiga 2012-05-03 17:08:02 +03:00
parent f3cc6a03a5
commit abb60a503a
12 changed files with 45 additions and 39 deletions

View File

@ -65,7 +65,7 @@ header div.middle {
background: image-url("top-middle.png");
float: left;
height: 46px;
width: 912px;
width: 913px;
}
header div.right {

View File

@ -5,25 +5,17 @@ class Admin::RegisterRequestsController < Admin::BaseController
end
def update
if params[:update_type].present? and params[:request_ids].present?
updates = RegisterRequest.where(:id => params[:request_ids])
case params[:update_type]
when 'approve' # see approve method
updates.each {|req| req.update_attributes(:approved => true, :rejected => false)}
when 'reject' # see reject method
updates.each {|req| req.update_attributes(:approved => false, :rejected => true)}
end
end
RegisterRequest.where(:id => params[:request_ids]).each(&params[:update_type].to_sym) if params[:update_type].present? && params[:request_ids].present?
redirect_to :action => :index
end
def approve
@register_request.update_attributes(:approved => true, :rejected => false)
@register_request.approve
redirect_to :action => :index
end
def reject
@register_request.update_attributes(:approved => false, :rejected => true)
@register_request.reject
redirect_to :action => :index
end
end

View File

@ -7,7 +7,7 @@ class Groups::BaseController < ApplicationController
def find_group
if group_id = params[:owner_name] || params[:group_id] || params[:id]
@group = Group.find_by_owner_name! group_id
@group = Group.find_by_insensitive_uname! group_id
end
end
end

View File

@ -1,7 +1,7 @@
# -*- encoding : utf-8 -*-
class Groups::MembersController < Groups::BaseController
is_related_controller!
belongs_to :group, :finder => 'find_by_owner_name!', :optional => true
belongs_to :group, :finder => 'find_by_insensitive_uname!', :optional => true
before_filter lambda { authorize! :manage_members, @group }
@ -9,16 +9,15 @@ class Groups::MembersController < Groups::BaseController
end
def update
params['user'].keys.each { |user_id|
params['user'].keys.each do |user_id|
role = params['user'][user_id]
if relation = parent.actors.where(:actor_id => user_id, :actor_type => 'User') #find_by_actor_id_and_actor_type(user_id, 'User')
relation.update_all(:role => role) if parent.owner.id.to_s != user_id
else
relation = parent.actors.build(:actor_id => user_id, :actor_type => 'User', :role => role)
relation.save!
end
} if params['user']
end if params['user']
if parent.save
flash[:notice] = t("flash.members.successfully_changed")
else
@ -29,9 +28,9 @@ class Groups::MembersController < Groups::BaseController
def remove
all_user_ids = []
params['user_remove'].keys.each { |user_id|
params['user_remove'].keys.each do |user_id|
all_user_ids << user_id if params['user_remove'][user_id] == ["1"] && parent.owner.id.to_s != user_id
} if params['user_remove']
end if params['user_remove']
all_user_ids.each do |user_id|
u = User.find(user_id)
Relation.by_actor(u).by_target(parent).each {|r| r.destroy}

View File

@ -7,7 +7,7 @@ class Users::BaseController < ApplicationController
def find_user
if user_id = params[:owner_name] || params[:user_id] || params[:id]
@user = User.find_by_owner_name! user_id
@user = User.find_by_insensitive_uname! user_id
end
end
end

View File

@ -2,9 +2,9 @@
class Group < ActiveRecord::Base
belongs_to :owner, :class_name => 'User'
has_many :relations, :as => :actor, :dependent => :destroy
has_many :actors, :as => :target, :class_name => 'Relation'
has_many :targets, :as => :actor, :class_name => 'Relation'
has_many :relations, :as => :actor, :dependent => :destroy, :dependent => :destroy
has_many :actors, :as => :target, :class_name => 'Relation', :dependent => :destroy
has_many :targets, :as => :actor, :class_name => 'Relation', :dependent => :destroy
has_many :members, :through => :actors, :source => :actor, :source_type => 'User', :autosave => true
has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true

View File

@ -36,7 +36,7 @@ class Platform < ActiveRecord::Base
scope :main, where(:platform_type => 'main')
scope :personal, where(:platform_type => 'personal')
attr_accessible :name, :distrib_type, :parent_platform_id, :platform_type, :owner, :visibility, :description, :released #, :owner_id, :owner_type
attr_accessible :name, :distrib_type, :parent_platform_id, :platform_type, :owner, :visibility, :description, :released
attr_readonly :name, :distrib_type, :parent_platform_id, :platform_type
include Modules::Models::Owner

View File

@ -59,6 +59,7 @@ class Project < ActiveRecord::Base
owner = User.find_by_uname(owner_name) || Group.find_by_uname(owner_name) || User.by_uname(owner_name).first || Group.by_uname(owner_name).first and
scoped = where(:owner_id => owner.id, :owner_type => owner.class) and
scoped.find_by_name(project_name) || scoped.by_name(project_name).first
# owner.projects.find_by_name(project_name) || owner.projects.by_name(project_name).first # TODO force this work?
end
def self.find_by_owner_and_name!(owner_name, project_name)

View File

@ -11,6 +11,14 @@ class RegisterRequest < ActiveRecord::Base
validates :email, :presence => true, :uniqueness => {:case_sensitive => false}, :format => { :with => /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i }
def approve
update_attributes(:approved => true, :rejected => false)
end
def reject
update_attributes(:approved => false, :rejected => true)
end
protected
def generate_token

View File

@ -10,6 +10,16 @@ Rosa::Application.routes.draw do
get '/forbidden' => 'pages#forbidden', :as => 'forbidden'
get '/terms-of-service' => 'pages#tos', :as => 'tos'
get '/activity_feeds.:format' => 'activity_feeds#index', :as => 'atom_activity_feeds', :format => /atom/
if APP_CONFIG['anonymous_access']
authenticated do
root :to => 'activity_feeds#index'
end
root :to => 'pages#root'
else
root :to => 'activity_feeds#index'
end
scope :module => 'platforms' do
resources :platforms do
resources :private_users, :except => [:show, :destroy, :update]
@ -109,7 +119,7 @@ Rosa::Application.routes.draw do
constraints OwnerConstraint.new(User) do
get '/' => 'users/profile#show', :as => :user
end
constraints OwnerConstraint.new(Group) do
constraints OwnerConstraint.new(Group, true) do
get '/' => 'groups/profile#show', :as => :group_profile
end
scope ':project_name', :as => 'project', :module => 'projects' do
@ -190,14 +200,4 @@ Rosa::Application.routes.draw do
get '/archive/:format/tree/:treeish' => "git/trees#archive", :defaults => {:treeish => :master}, :as => :archive, :format => /zip|tar/
end
end
get '/activity_feeds.:format' => 'activity_feeds#index', :as => 'atom_activity_feeds', :format => /atom/
if APP_CONFIG['anonymous_access']
authenticated do
root :to => 'activity_feeds#index'
end
root :to => 'pages#root'
else
root :to => 'activity_feeds#index'
end
end

View File

@ -1,10 +1,12 @@
# -*- encoding : utf-8 -*-
class OwnerConstraint
def initialize(class_name)
def initialize(class_name, bang = false)
@class_name = class_name
@finder = 'find_by_insensitive_uname'
@finder << '!' if bang
end
def matches?(request)
!!(@class_name.find_by_uname(request.params[:owner_name]) || @class_name.by_uname(request.params[:owner_name]).first)
@class_name.send(@finder, request.params[:owner_name]).present?
end
end

View File

@ -29,8 +29,12 @@ module Modules
end
module ClassMethods
def find_by_owner_name!(uname)
by_uname(uname).first!
def find_by_insensitive_uname(uname)
find_by_uname(uname) || by_uname(uname).first
end
def find_by_insensitive_uname!(uname)
find_by_insensitive_uname(uname) or raise ActiveRecord::RecordNotFound
end
end
end