Skip to content
This repository has been archived by the owner on Mar 8, 2021. It is now read-only.

Commit

Permalink
Merge pull request #30 from sfc-rg/feature/grouping
Browse files Browse the repository at this point in the history
Grouping
  • Loading branch information
miyukki committed Sep 10, 2015
2 parents 5ce3adf + 646b535 commit 1ce3724
Show file tree
Hide file tree
Showing 26 changed files with 196 additions and 13 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ gem 'jquery-rails'
gem 'bourbon'
gem 'neat'
gem 'font-awesome-sass', '~> 4.3.0'
gem 'nested_form'

gem 'mysql2', '0.3.18'
gem 'bcrypt', '3.1.10'
Expand Down
2 changes: 2 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ GEM
neat (1.7.2)
bourbon (>= 4.0)
sass (>= 3.3)
nested_form (0.3.2)
net-ldap (0.11)
net-scp (1.2.1)
net-ssh (>= 2.6.5)
Expand Down Expand Up @@ -297,6 +298,7 @@ DEPENDENCIES
json_expressions (~> 0.8.3)
mysql2 (= 0.3.18)
neat
nested_form
net-ldap
omniauth (~> 1.2.2)
omniauth-slack (~> 2.0.0)
Expand Down
1 change: 1 addition & 0 deletions app/assets/javascripts/application.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,6 @@
//
//= require jquery
//= require jquery_ujs
//= require jquery_nested_form
//= require turbolinks
//= require_tree .
3 changes: 3 additions & 0 deletions app/assets/javascripts/groups.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/groups.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Place all the styles related to the groups controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/
22 changes: 21 additions & 1 deletion app/assets/stylesheets/settings.scss
Original file line number Diff line number Diff line change
@@ -1,4 +1,24 @@
.ldap-form {
.setting-form {
margin: 0 auto;
max-width: 400px;
}

.fields {
position: relative;
}

.remove_nested_fields {
position: absolute;
left: 410px;
top: 6px;
}

select {
background-color: white;
border: 1px solid #ddd;
border-radius: 3px;
box-shadow: inset 0 1px 3px rgba(0, 0, 0, 0.06);
box-sizing: border-box;
width: 100%;
height: 36px;
}
16 changes: 16 additions & 0 deletions app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class GroupsController < ApplicationController
def index
@groups = Group.all
end

def create
Group.new(group_params).save
redirect_to groups_path
end

private

def group_params
params.require(:group).permit(:name, :kind)
end
end
14 changes: 13 additions & 1 deletion app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class SettingsController < ApplicationController
def edit_profile
end

def update_profile
def update_ldap
ldap = params.require(:ldap).permit(:username, :password)

unless LdapSupport.ldap_bind(ldap[:username], ldap[:password])
Expand All @@ -20,4 +20,16 @@ def update_profile

redirect_to edit_profile_path
end

def update_profile
@current_user.group_users.destroy_all
@current_user.update(user_params)
redirect_to edit_profile_path
end

private

def user_params
params.require(:user).permit(group_users_attributes: [:group_id, :_destroy])
end
end
2 changes: 2 additions & 0 deletions app/helpers/groups_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module GroupsHelper
end
3 changes: 3 additions & 0 deletions app/helpers/settings_helper.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
module SettingsHelper
def group_list
Group.all.map { |t| [t.name, t.id] }
end
end
7 changes: 7 additions & 0 deletions app/models/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class Group < ActiveRecord::Base
enum kind: { other: 0, kg: 10, rg: 20 }
has_many :group_users
has_many :users, through: :group_users

validates :name, uniqueness: true
end
4 changes: 4 additions & 0 deletions app/models/group_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
class GroupUser < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
6 changes: 5 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
class User < ActiveRecord::Base
has_one :slack_credential
has_one :ldap_credential
has_many :group_users
has_many :groups, through: :group_users

accepts_nested_attributes_for :group_users, allow_destroy: true

def active?
self.ldap_credential.present?
self.ldap_credential.present? && self.groups.present?
end
end
20 changes: 20 additions & 0 deletions app/views/groups/index.html.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
.content
%h1 グループ一覧
%table
%tr
%th #
%th Kind
%th Name
%th Members
- @groups.each do |group|
%tr
%td= group.id
%td= group.kind.to_s
%td= group.name
%td= group.users.count

%h1 グループ追加
= form_for Group.new do |f|
= f.select :kind, Group.kinds.keys
= f.text_field :name
= f.submit
8 changes: 5 additions & 3 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
= link_to 'Thesis', thesis_path
- if @current_user
.inner-right
.user
.icon= image_tag @current_user.icon_url
.name= @current_user.nickname
.control
= link_to edit_profile_path do
.user
.icon= image_tag @current_user.icon_url
.name= @current_user.nickname

- if content_for?(:header)
.header
Expand Down
20 changes: 16 additions & 4 deletions app/views/settings/edit_profile.html.haml
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
.content
%h2
rg-net のアカウント
rg-net アカウント連携
= "(#{@current_user.ldap_credential.present? ? '認証済み' : '未認証'})"
- if @current_user.ldap_credential.present?
.ldap-form
.setting-form
%label Username
= text_field :ldap, :username, disabled: true, value: @current_user.ldap_credential.uid
%label Student Id
= text_field :ldap, :username, disabled: true, value: @current_user.ldap_credential.student_id
%p この情報が間違っている場合は #coordinator まで連絡してください。
- else
= form_tag update_profile_path, method: :patch, class: 'ldap-form' do
= form_tag update_ldap_path, method: :patch, class: 'setting-form' do
%label Username
= text_field :ldap, :username, placeholder: 'username'
%label Password
= password_field :ldap, :password, placeholder: 'password'
= submit_tag '設定'
= submit_tag 'アカウント連携をする'

%h2
KG設定
= nested_form_for @current_user, url: update_profile_path, class: 'setting-form' do |f|
.setting-form
= f.fields_for :group_users do |group_form|
= group_form.link_to_remove do
%i.fa.fa-trash
= group_form.select :group_id, group_list
= f.link_to_add '追加', :group_users
= f.submit 'KG設定を更新する'
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
get '/wip_term' => 'pre_built_pages#wip_term'
get '/thesis' => 'pre_built_pages#thesis'

resources :groups, only: [:index, :create]

scope :settings do
get '/profile' => 'settings#edit_profile', as: :edit_profile
patch '/profile' => 'settings#update_profile', as: :update_profile
patch '/profile/ldap' => 'settings#update_ldap', as: :update_ldap
end

scope :search do
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20150906015824_create_groups_and_group_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class CreateGroupsAndGroupUsers < ActiveRecord::Migration
def change
create_table :groups do |t|
t.string :name
t.integer :kind, null: false

t.timestamps null: false
end

create_table :group_users do |t|
t.references :group
t.references :user

t.timestamps null: false
end
end
end
16 changes: 15 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20150905135019) do
ActiveRecord::Schema.define(version: 20150906015824) do

create_table "comments", force: :cascade do |t|
t.integer "user_id"
Expand All @@ -24,6 +24,20 @@
add_index "comments", ["page_id"], name: "index_comments_on_page_id"
add_index "comments", ["user_id"], name: "index_comments_on_user_id"

create_table "group_users", force: :cascade do |t|
t.integer "group_id"
t.integer "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "groups", force: :cascade do |t|
t.string "name"
t.integer "kind", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

create_table "ldap_credentials", force: :cascade do |t|
t.integer "user_id"
t.string "uid"
Expand Down
4 changes: 4 additions & 0 deletions spec/controllers/groups_controller_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
require 'rails_helper'

RSpec.describe GroupsController, type: :controller do
end
4 changes: 2 additions & 2 deletions spec/controllers/settings_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
render_views
let(:user) { FactoryGirl.create(:user) }

describe '#update_profile' do
describe '#update_ldap' do
let(:username) { 'miyukki' }
let(:password) { 'p@ssw0rd' }
let(:student_id) { 71349640 }
Expand All @@ -21,7 +21,7 @@
session[:user_id] = user.id # logged in
allow_any_instance_of(Net::LDAP).to receive(:bind).and_return(ldap_bind_result)
allow_any_instance_of(Net::LDAP).to receive_message_chain(:search, :first).and_return(ldap_info_result)
patch :update_profile, ldap: { username: username, password: password }
patch :update_ldap, ldap: { username: username, password: password }
end

context 'when correct credential' do
Expand Down
4 changes: 4 additions & 0 deletions spec/factories/group_users.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryGirl.define do
factory :group_user do
end
end
4 changes: 4 additions & 0 deletions spec/factories/groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FactoryGirl.define do
factory :group do
end
end
15 changes: 15 additions & 0 deletions spec/helpers/groups_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'rails_helper'

# Specs in this file have access to a helper object that includes
# the GroupsHelper. For example:
#
# describe GroupsHelper do
# describe "string concat" do
# it "concats two strings with spaces" do
# expect(helper.concat_strings("this","that")).to eq("this that")
# end
# end
# end
RSpec.describe GroupsHelper, type: :helper do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/group_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe Group, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end
5 changes: 5 additions & 0 deletions spec/models/group_user_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
require 'rails_helper'

RSpec.describe GroupUser, type: :model do
pending "add some examples to (or delete) #{__FILE__}"
end

0 comments on commit 1ce3724

Please sign in to comment.