Commit d6363e935933551b7f9edb4063d0b0fbcea3c824

Authored by Dmitriy Zaporozhets
1 parent d683ce5c

Admin Group scaffold -> new, show, index

app/controllers/admin/groups_controller.rb 0 → 100644
... ... @@ -0,0 +1,66 @@
  1 +class Admin::GroupsController < AdminController
  2 + before_filter :group, only: [:edit, :show, :update, :destroy, :project_update]
  3 +
  4 + def index
  5 + @groups = Group.scoped
  6 + @groups = @groups.search(params[:name]) if params[:name].present?
  7 + @groups = @groups.page(params[:page]).per(20)
  8 + end
  9 +
  10 + def show
  11 + @projects = Project.scoped
  12 + @projects = @projects.not_in_group(@group) if @group.projects.present?
  13 + @projects = @projects.all
  14 + end
  15 +
  16 + def new
  17 + @group = Group.new
  18 + end
  19 +
  20 + def edit
  21 + end
  22 +
  23 + def create
  24 + @group = Group.new(params[:group])
  25 + @group.owner = current_user
  26 +
  27 + if @group.save
  28 + redirect_to [:admin, @group], notice: 'Group was successfully created.'
  29 + else
  30 + render action: "new"
  31 + end
  32 + end
  33 +
  34 + def update
  35 + owner_id = params[:group].delete(:owner_id)
  36 +
  37 + if owner_id
  38 + @group.owner = User.find(owner_id)
  39 + end
  40 +
  41 + if @group.update_attributes(params[:group])
  42 + redirect_to [:admin, @group], notice: 'Group was successfully updated.'
  43 + else
  44 + render action: "edit"
  45 + end
  46 + end
  47 +
  48 + def project_update
  49 + project_ids = params[:project_ids]
  50 + Project.where(id: project_ids).update_all(group_id: @group.id)
  51 +
  52 + redirect_to :back, notice: 'Group was successfully updated.'
  53 + end
  54 +
  55 + def destroy
  56 + @group.destroy
  57 +
  58 + redirect_to groups_url, notice: 'Group was successfully deleted.'
  59 + end
  60 +
  61 + private
  62 +
  63 + def group
  64 + @group = Group.find_by_code(params[:id])
  65 + end
  66 +end
... ...
app/models/group.rb
... ... @@ -19,4 +19,10 @@ class Group &lt; ActiveRecord::Base
19 19 validates :name, presence: true, uniqueness: true
20 20 validates :code, presence: true, uniqueness: true
21 21 validates :owner_id, presence: true
  22 +
  23 + delegate :name, to: :owner, allow_nil: true, prefix: true
  24 +
  25 + def to_param
  26 + code
  27 + end
22 28 end
... ...
app/models/project.rb
... ... @@ -26,9 +26,12 @@ class Project &lt; ActiveRecord::Base
26 26 has_many :wikis, dependent: :destroy
27 27 has_many :protected_branches, dependent: :destroy
28 28  
  29 + delegate :name, to: :owner, allow_nil: true, prefix: true
  30 +
29 31 # Scopes
30 32 scope :public_only, where(private_flag: false)
31   - scope :without_user, lambda { |user| where("id not in (:ids)", ids: user.projects.map(&:id) ) }
  33 + scope :without_user, ->(user) { where("id not in (:ids)", ids: user.projects.map(&:id) ) }
  34 + scope :not_in_group, ->(group) { where("id not in (:ids)", ids: group.project_ids ) }
32 35  
33 36 def self.active
34 37 joins(:issues, :notes, :merge_requests).order("issues.created_at, notes.created_at, merge_requests.created_at DESC")
... ...
app/views/admin/groups/_form.html.haml 0 → 100644
... ... @@ -0,0 +1,19 @@
  1 += form_for [:admin, @group] do |f|
  2 + - if @group.errors.any?
  3 + .alert-message.block-message.error
  4 + %span= @group.errors.full_messages.first
  5 + .clearfix.group_name_holder
  6 + = f.label :name do
  7 + Group name is
  8 + .input
  9 + = f.text_field :name, placeholder: "Example Group", class: "xxlarge"
  10 + .clearfix
  11 + = f.label :code do
  12 + URL
  13 + .input
  14 + .input-prepend
  15 + %span.add-on= web_app_url
  16 + = f.text_field :code, placeholder: "example"
  17 +
  18 + .form-actions
  19 + = f.submit 'Create group', class: "btn primary"
... ...
app/views/admin/groups/index.html.haml 0 → 100644
... ... @@ -0,0 +1,24 @@
  1 +%h3.page_title
  2 + Groups
  3 + = link_to 'New Group', new_admin_group_path, class: "btn small right"
  4 +%br
  5 += form_tag admin_groups_path, method: :get, class: 'form-inline' do
  6 + = text_field_tag :name, params[:name], class: "xlarge"
  7 + = submit_tag "Search", class: "btn submit primary"
  8 +
  9 +%table
  10 + %thead
  11 + %th Name
  12 + %th Path
  13 + %th Projects
  14 + %th Edit
  15 + %th.cred Danger Zone!
  16 +
  17 + - @groups.each do |group|
  18 + %tr
  19 + %td= link_to group.name, [:admin, group]
  20 + %td= group.path
  21 + %td= group.projects.count
  22 + %td= link_to 'Edit', edit_admin_group_path(group), id: "edit_#{dom_id(group)}", class: "btn small"
  23 + %td.bgred= link_to 'Destroy', [:admin, group], confirm: "REMOVE #{group.name}? Are you sure?", method: :delete, class: "btn small danger"
  24 += paginate @groups, theme: "admin"
... ...
app/views/admin/groups/new.html.haml 0 → 100644
... ... @@ -0,0 +1,3 @@
  1 +%h3.page_title New Group
  2 +%br
  3 += render 'form'
... ...
app/views/admin/groups/show.html.haml 0 → 100644
... ... @@ -0,0 +1,48 @@
  1 +%h3.page_title
  2 + Group: #{@group.name}
  3 + = link_to edit_admin_group_path(@group), class: "btn right" do
  4 + %i.icon-edit
  5 + Edit
  6 +
  7 +%br
  8 +%table.zebra-striped
  9 + %thead
  10 + %tr
  11 + %th Group
  12 + %th
  13 + %tr
  14 + %td
  15 + %b
  16 + Name:
  17 + %td
  18 + = @group.name
  19 + %tr
  20 + %td
  21 + %b
  22 + Code:
  23 + %td
  24 + = @group.code
  25 + %tr
  26 + %td
  27 + %b
  28 + Owner:
  29 + %td
  30 + = @group.owner_name
  31 +.ui-box
  32 + %h5
  33 + Projects
  34 + %small
  35 + (#{@group.projects.count})
  36 + %ul.unstyled
  37 + - @group.projects.each do |project|
  38 + %li.wll
  39 + %strong
  40 + = link_to project.name, [:admin, project]
  41 +
  42 +%br
  43 +%h3 Add new project
  44 +%br
  45 += form_tag project_update_admin_group_path(@group), class: "bulk_import", method: :put do
  46 + = select_tag :project_ids, options_from_collection_for_select(@projects , :id, :name), multiple: true, data: {placeholder: 'Select projects'}, class: 'chosen span5'
  47 + .form-actions
  48 + = submit_tag 'Add', class: "btn primary"
... ...
config/routes.rb
... ... @@ -43,6 +43,11 @@ Gitlab::Application.routes.draw do
43 43 put :unblock
44 44 end
45 45 end
  46 + resources :groups, constraints: { id: /[^\/]+/ } do
  47 + member do
  48 + put :project_update
  49 + end
  50 + end
46 51 resources :projects, constraints: { id: /[^\/]+/ } do
47 52 member do
48 53 get :team
... ...