From f76a3aee39ad8f709faa0a3013eec0c48dc13b59 Mon Sep 17 00:00:00 2001 From: George Vinogradov Date: Mon, 17 Oct 2011 00:39:45 +0400 Subject: [PATCH] Added groups representation. --- app/controllers/groups_controller.rb | 50 +++++++++++++++++++ app/models/group.rb | 20 +++++++- app/views/groups/_form.html.haml | 13 +++++ app/views/groups/_list.html.haml | 24 +++++++++ app/views/groups/_sidebar.html.haml | 5 ++ app/views/groups/edit.html.haml | 12 +++++ app/views/groups/index.html.haml | 7 +++ app/views/groups/new.html.haml | 11 ++++ app/views/groups/show.html.haml | 39 +++++++++++++++ app/views/layouts/application.html.haml | 4 ++ app/views/platforms/_list.html.haml | 28 +++++++++++ app/views/projects/_list.html.haml | 28 +++++++++++ app/views/repositories/_list.html.haml | 28 +++++++++++ .../20111016130557_add_uname_to_groups.rb | 9 ++++ 14 files changed, 277 insertions(+), 1 deletion(-) create mode 100644 app/controllers/groups_controller.rb create mode 100644 app/views/groups/_form.html.haml create mode 100644 app/views/groups/_list.html.haml create mode 100644 app/views/groups/_sidebar.html.haml create mode 100644 app/views/groups/edit.html.haml create mode 100644 app/views/groups/index.html.haml create mode 100644 app/views/groups/new.html.haml create mode 100644 app/views/groups/show.html.haml create mode 100644 app/views/platforms/_list.html.haml create mode 100644 app/views/projects/_list.html.haml create mode 100644 app/views/repositories/_list.html.haml create mode 100644 db/migrate/20111016130557_add_uname_to_groups.rb diff --git a/app/controllers/groups_controller.rb b/app/controllers/groups_controller.rb new file mode 100644 index 000000000..5e47b5a78 --- /dev/null +++ b/app/controllers/groups_controller.rb @@ -0,0 +1,50 @@ +# coding: UTF-8 + +class GroupsController < ApplicationController + before_filter :authenticate_user! + + before_filter :find_group, :only => [:show, :edit, :update, :destroy] + + def index + @groups = Group.paginate(:page => params[:page], :per_page => 15) + end + + def show + @platforms = @group.platforms.paginate(:page => params[:platform_page], :per_page => 10) + @repositories = @group.repositories.paginate(:page => params[:repository_page], :per_page => 10) + @projects = @group.projects.paginate(:page => params[:project_page], :per_page => 10) + end + + def edit + end + + def destroy + @user.destroy + + flash[:notice] = t("flash.group.destroyed") + redirect_to groups_path + end + + def new + @group = Group.new + end + + def create + @group = Group.new params[:group] + @group.owner = current_user + @group.members << current_user + if @group.save + flash[:notice] = t('flash.group.saved') + redirect_to edit_group_path(@group) + else + flash[:error] = t('flash.group.save_error') + render :action => :new + end + end + + private + def find_group + @group = Group.find(params[:id]) + end + +end diff --git a/app/models/group.rb b/app/models/group.rb index de53d5c40..3866664f9 100644 --- a/app/models/group.rb +++ b/app/models/group.rb @@ -1,11 +1,29 @@ class Group < ActiveRecord::Base + + validates :name, :uname, :owner_id, :presence => true + validates :name, :uname, :uniqueness => true + belongs_to :owner, :class_name => 'User' has_many :objects, :as => :target, :class_name => 'Relation' has_many :targets, :as => :object, :class_name => 'Relation' - has_many :users, :through => :objects, :source => :object, :source_type => 'User', :autosave => true + has_many :members, :through => :objects, :source => :object, :source_type => 'User', :autosave => true has_many :projects, :through => :targets, :source => :target, :source_type => 'Project', :autosave => true has_many :platforms, :through => :targets, :source => :target, :source_type => 'Platform', :autosave => true has_many :repositories, :through => :targets, :source => :target, :source_type => 'Repository', :autosave => true + + def roles_of(user) + objects.where(:object_id => user.id, :object_type => user.class).map {|rel| rel.role}.reject {|r| r.nil?} + end + + def add_role(user, role) + roles = objects.where(:object_id => user.id, :object_type => user.class).map {|rel| rel.role}.reject {|r| r.nil?} + unless roles.include? role + rel = Relation.create(:object_type => user.class.to_s, :object_id => user.id, + :target_type => self.class.to_s, :target_id => id) + rel.role = role + rel.save + end + end end diff --git a/app/views/groups/_form.html.haml b/app/views/groups/_form.html.haml new file mode 100644 index 000000000..7fe4a3e9a --- /dev/null +++ b/app/views/groups/_form.html.haml @@ -0,0 +1,13 @@ +.group + = f.label :name, t("activerecord.attributes.group.name"), :class => :label + = f.text_field :name, :class => 'text_field' +.group + = f.label :uname, t("activerecord.attributes.group.uname"), :class => :label + = f.text_field :uname, :class => 'text_field' + +.group.navform.wat-cf + %button.button{:type => "submit"} + = image_tag("web-app-theme/icons/tick.png", :alt => t("layout.save")) + = t("layout.save") + %span.text_button_padding= t("layout.or") + = link_to t("layout.cancel"), users_path, :class => "text_button_padding link_button" diff --git a/app/views/groups/_list.html.haml b/app/views/groups/_list.html.haml new file mode 100644 index 000000000..d66c0364e --- /dev/null +++ b/app/views/groups/_list.html.haml @@ -0,0 +1,24 @@ +.content + %h2.title + = t("layout.groups.list_header") + .inner + %table.table + %tr + %th.first ID + %th= t("activerecord.attributes.group.name") + %th= t("activerecord.attributes.group.owner") + %th.last   + - @groups.each do |group| + %tr{:class => cycle("odd", "even")} + %td + = group.id + %td + = link_to group.name, group_path(group) + %td + = link_to group.owner.name, user_path(group.owner) + %td.last + #{link_to t("layout.show"), group_path(group)} | #{link_to t("layout.edit"), edit_group_path(group)} | #{link_to t("layout.delete"), group_path(group), :method => :delete, :confirm => t("layout.groups.confirm_delete")} + .actions-bar.wat-cf + .actions + - if !!paginate + = paginate diff --git a/app/views/groups/_sidebar.html.haml b/app/views/groups/_sidebar.html.haml new file mode 100644 index 000000000..83cef0324 --- /dev/null +++ b/app/views/groups/_sidebar.html.haml @@ -0,0 +1,5 @@ +.block.notice + %h3= t("layout.groups.members") + .content + - @group.members.uniq.each do |member| + %p= link_to member.name + " (#{@group.roles_of(member).map{|r| r.name}.join(', ')})", user_path(member) diff --git a/app/views/groups/edit.html.haml b/app/views/groups/edit.html.haml new file mode 100644 index 000000000..5d29b63b7 --- /dev/null +++ b/app/views/groups/edit.html.haml @@ -0,0 +1,12 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first= link_to t("layout.groups.list"), groups_path + %li= link_to t("layout.groups.new"), new_group_path + %li.active= link_to t("layout.groups.edit"), edit_group_path + .content + %h2.title= t("layout.groups.edit_header") + .inner + = form_for @group, :url => group_path(@group), :html => { :class => :form } do |f| + = render :partial => "form", :locals => {:f => f} +- content_for :sidebar, render(:partial => 'sidebar') diff --git a/app/views/groups/index.html.haml b/app/views/groups/index.html.haml new file mode 100644 index 000000000..005f4b168 --- /dev/null +++ b/app/views/groups/index.html.haml @@ -0,0 +1,7 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first.active= link_to t("layout.groups.list"), users_path + %li= link_to t("layout.groups.new"), new_group_path + =render :partial => 'list', :object => @groups, :locals => {:paginate => will_paginate(@groups)} +-# content_for :sidebar, render(:partial => 'sidebar') diff --git a/app/views/groups/new.html.haml b/app/views/groups/new.html.haml new file mode 100644 index 000000000..04a96c3e0 --- /dev/null +++ b/app/views/groups/new.html.haml @@ -0,0 +1,11 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first= link_to t("layout.groups.list"), groups_path + %li.active= link_to t("layout.groups.new"), new_group_path + .content + %h2.title= t("layout.groups.new_header") + .inner + = form_for :group, :url => groups_path, :html => { :class => :form } do |f| + = render :partial => "form", :locals => {:f => f} +-# content_for :sidebar, render(:partial => 'sidebar') diff --git a/app/views/groups/show.html.haml b/app/views/groups/show.html.haml new file mode 100644 index 000000000..f905d1a12 --- /dev/null +++ b/app/views/groups/show.html.haml @@ -0,0 +1,39 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first= link_to t("layout.groups.list"), groups_path + %li= link_to t("layout.groups.new"), new_group_path + %li.active= link_to t("layout.groups.show"), group_path + .content + .inner + %p + %b + Id + \: + = @group.id + %p + %b + = t("activerecord.attributes.group.name") + \: + = @group.name + %p + %b + = t("activerecord.attributes.group.owner") + \: + = link_to @group.owner.name, user_path(@group.owner) + %p + %b + = t("activerecord.attributes.group.created_at") + \: + = @group.created_at + .wat-cf + = link_to image_tag("web-app-theme/icons/application_edit.png", :alt => t("layout.edit")) + " " + t("layout.edit"), edit_group_path(@group), :class => "button" + = link_to image_tag("web-app-theme/icons/cross.png", :alt => t("layout.delete")) + " " + t("layout.delete"), group_path(@group), :method => "delete", :class => "button", :confirm => t("layout.groups.confirm_delete") + += render :partial => 'platforms/list', :object => @platforms + += render :partial => 'repositories/list', :object => @repositories + += render :partial => 'projects/list', :object => @projects + +- content_for :sidebar, render(:partial => 'sidebar') diff --git a/app/views/layouts/application.html.haml b/app/views/layouts/application.html.haml index 5f3ed52f2..f03c87859 100644 --- a/app/views/layouts/application.html.haml +++ b/app/views/layouts/application.html.haml @@ -13,11 +13,15 @@ #user-navigation %ul.wat-cf %li + = t("layout.logged_in_as") + ' ' + current_user.name +   |   = link_to t('layout.logout'), destroy_user_session_path, :class => "logout" #main-navigation %ul.wat-cf %li{:class => controller.controller_path == 'users' ? 'active' : '' } %a{:href => users_path}= t("layout.menu.users") + %li{:class => controller.controller_path == 'groups' ? 'active' : '' } + %a{:href => groups_path}= t("layout.menu.groups") %li{:class => controller.controller_path == 'platforms' ? 'active' : '' } %a{:href => platforms_path}= t("layout.menu.platforms") #wrapper.wat-cf diff --git a/app/views/platforms/_list.html.haml b/app/views/platforms/_list.html.haml new file mode 100644 index 000000000..1bd464eec --- /dev/null +++ b/app/views/platforms/_list.html.haml @@ -0,0 +1,28 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first.active= link_to t("layout.platforms.list"), platforms_path + %li= link_to t("layout.platforms.new"), new_platform_path + .content + %h2.title + = t("layout.platforms.list_header") + .inner + = form_tag '#', :method => :get do#platform_repository_path(@platform, @repository), :method => :get do + .group + = label_tag :query, t("layout.search_by_name"), :class => :label + = text_field_tag :query + %button.search{:type => "submit"} + = t("layout.search") + %table.table + %tr + %th.first= t("activerecord.attributes.platform.name") + %th.last   + - @platforms.each do |platform| + %tr{:class => cycle("odd", "even")} + %td + = link_to platform.name, platform_path(platform) + %td.last + #{link_to t("layout.show"), platform_path(platform)} | #{link_to t("layout.delete"), platform_path(platform), :method => :delete, :confirm => t("layout.platforms.confirm_delete")} + .actions-bar.wat-cf + .actions + = will_paginate @platforms, :param_name => :platform_page diff --git a/app/views/projects/_list.html.haml b/app/views/projects/_list.html.haml new file mode 100644 index 000000000..b90152316 --- /dev/null +++ b/app/views/projects/_list.html.haml @@ -0,0 +1,28 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first.active= link_to t("layout.projects.list"), '#'#platform_repository_path(@platform, @repository) + "#projects" + %li= link_to t("layout.projects.new"), '#'#new_platform_repository_project_path(@platform, @repository) + .content + %h2.title + = t("layout.projects.list_header") + .inner + = form_tag '#', :method => :get do#platform_repository_path(@platform, @repository), :method => :get do + .group + = label_tag :query, t("layout.search_by_name"), :class => :label + = text_field_tag :query + %button.search{:type => "submit"} + = t("layout.search") + %table.table + %tr + %th.first= t("activerecord.attributes.project.name") + %th.last   + - @projects.each do |project| + %tr{:class => cycle("odd", "even")} + %td + = link_to project.name, project_path(project) + %td.last + #{link_to t("layout.show"), project_path(project)} | #{link_to t("layout.delete"), project_path(project), :method => :delete, :confirm => t("layout.projects.confirm_delete")} + .actions-bar.wat-cf + .actions + = will_paginate @projects, :param_name => :project_page diff --git a/app/views/repositories/_list.html.haml b/app/views/repositories/_list.html.haml new file mode 100644 index 000000000..fe9dac995 --- /dev/null +++ b/app/views/repositories/_list.html.haml @@ -0,0 +1,28 @@ +.block + .secondary-navigation + %ul.wat-cf + %li.first.active= link_to t("layout.repositories.list"), '#'#platform_path(@platform) + "#repositories" + %li= link_to t("layout.repositories.new"), '#' #new_platform_repository_path(@platform) + .content + %h2.title + = t("layout.repositories.list_header") + .inner + = form_tag '#', :method => :get do#platform_repository_path(@platform, @repository), :method => :get do + .group + = label_tag :query, t("layout.search_by_name"), :class => :label + = text_field_tag :query + %button.search{:type => "submit"} + = t("layout.search") + %table.table + %tr + %th.first= t("activerecord.attributes.repository.name") + %th.last   + - @repositories.each do |repository| + %tr{:class => cycle("odd", "even")} + %td + = link_to repository.name, repository_path(repository) + %td.last + #{link_to t("layout.show"), repository_path(repository)} | #{link_to t("layout.delete"), repository_path(repository), :method => :delete, :confirm => t("layout.repositories.confirm_delete")} + .actions-bar.wat-cf + .actions + = will_paginate @repositories, :param_name => :repository_page diff --git a/db/migrate/20111016130557_add_uname_to_groups.rb b/db/migrate/20111016130557_add_uname_to_groups.rb new file mode 100644 index 000000000..37805aabf --- /dev/null +++ b/db/migrate/20111016130557_add_uname_to_groups.rb @@ -0,0 +1,9 @@ +class AddUnameToGroups < ActiveRecord::Migration + def self.up + add_column :groups, :uname, :string + end + + def self.down + remove_column :groups, :uname + end +end