Fixed groups creation.
This commit is contained in:
parent
7023dda7e0
commit
f154ccc132
|
@ -1,12 +1,27 @@
|
|||
# coding: UTF-8
|
||||
class GroupsController < ApplicationController
|
||||
is_related_controller!
|
||||
|
||||
belongs_to :user, :optional => true
|
||||
|
||||
before_filter :authenticate_user!
|
||||
before_filter :find_group, :only => [:show, :edit, :update, :destroy]
|
||||
|
||||
load_and_authorize_resource
|
||||
|
||||
def index
|
||||
@groups = Group.paginate(:page => params[:group_page])
|
||||
puts parent.inspect
|
||||
@groups = if parent? and !parent.nil?
|
||||
parent.groups
|
||||
else
|
||||
Group
|
||||
end.accessible_by(current_ability)
|
||||
|
||||
@groups = if params[:query]
|
||||
@groups.where(["name LIKE ?", "%#{params[:query]}%"])
|
||||
else
|
||||
@groups
|
||||
end.paginate(:page => params[:group_page])
|
||||
end
|
||||
|
||||
def show
|
||||
|
@ -24,13 +39,18 @@ class GroupsController < ApplicationController
|
|||
|
||||
def create
|
||||
@group = Group.new params[:group]
|
||||
@group.owner = current_user
|
||||
@group.members << current_user
|
||||
if @group.save
|
||||
@group.owner = if parent? and parent.is_a? User
|
||||
parent
|
||||
else
|
||||
current_user
|
||||
end
|
||||
|
||||
if @group.save!
|
||||
flash[:notice] = t('flash.group.saved')
|
||||
redirect_to edit_group_path(@group)
|
||||
else
|
||||
flash[:error] = t('flash.group.save_error')
|
||||
flash[:warning] = @project.errors[:base]
|
||||
render :action => :new
|
||||
end
|
||||
end
|
||||
|
|
|
@ -20,6 +20,14 @@ class Group < ActiveRecord::Base
|
|||
|
||||
delegate :ssh_key, :to => :owner
|
||||
|
||||
after_create :add_owner_to_members
|
||||
|
||||
include Modules::Models::PersonalRepository
|
||||
include Modules::Models::Owner
|
||||
|
||||
protected
|
||||
def add_owner_to_members
|
||||
Relation.create_with_role(self.owner, self, 'admin')
|
||||
# members << self.owner if !members.exists?(:id => self.owner.id)
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,6 +5,21 @@ class Relation < ActiveRecord::Base
|
|||
ROLES = %w[reader writer admin]
|
||||
validates :role, :inclusion => {:in => ROLES}
|
||||
|
||||
before_validation :add_default_role
|
||||
|
||||
scope :by_object, lambda {|obj| {:conditions => ['object_id = ? AND object_type = ?', obj.id, obj.class.to_s]}}
|
||||
scope :by_target, lambda {|tar| {:conditions => ['target_id = ? AND target_type = ?', tar.id, tar.class.to_s]}}
|
||||
|
||||
def self.create_with_role(object, target, role)
|
||||
r = new
|
||||
r.object = object
|
||||
r.target = target
|
||||
r.role = role
|
||||
r.save
|
||||
end
|
||||
|
||||
protected
|
||||
def add_default_role
|
||||
self.role = ROLES.first if role.nil? || role.empty?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -6,7 +6,9 @@ Rosa::Application.routes.draw do
|
|||
get '/users/auth/:provider' => 'users/omniauth_callbacks#passthru'
|
||||
end
|
||||
|
||||
resources :users
|
||||
resources :users do
|
||||
resources :groups, :only => [:new, :create, :index]
|
||||
end
|
||||
|
||||
resources :event_logs, :only => :index
|
||||
|
||||
|
|
Loading…
Reference in New Issue