Skip to content
This repository was archived by the owner on Jul 24, 2020. It is now read-only.

Commit 3432110

Browse files
committed
Merge pull request #660 from YaleSTC/190_refactor_views_users_new
Refactor UsersController#new (controller and view)
2 parents e5914f8 + 14d21cb commit 3432110

File tree

4 files changed

+59
-43
lines changed

4 files changed

+59
-43
lines changed

app/controllers/users_controller.rb

+34-8
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,43 @@ def show
4343
end
4444

4545
def new
46-
if can? :create, User
47-
if params[:possible_netid]
48-
@user = User.new(User.search_ldap(params[:possible_netid]))
49-
else
50-
@user = User.new
51-
end
52-
else
46+
@can_edit_login = current_user.present? && (can? :create, User) # used in view
47+
48+
if current_user.nil?
49+
# This is a new user -> create an account for them
5350
@user = User.new(User.search_ldap(session[:cas_user]))
5451
@user.login = session[:cas_user] #default to current login
52+
53+
# TODO: What should it render?
54+
@partial_to_render = 'form'
55+
else
56+
# Someone with permissions is creating a new user
57+
ldap_result = User.search_ldap(params[:possible_netid])
58+
@user = User.new(ldap_result)
59+
60+
# Does netID exist?
61+
if ldap_result.nil?
62+
@message = 'Sorry, the netID that you entered does not exist.
63+
You cannot create a user profile without a valid netID.'
64+
render :new and return
65+
end
66+
67+
# Is there a user record already?
68+
if User.exists?(login: params[:possible_netid])
69+
@message = 'You cannot create a new user, as the netID you entered
70+
is already associated with a user. If you would like to reserve for
71+
them, please select their name from the drop-down options in the cart.'
72+
render :new and return
73+
end
74+
75+
# With existing netID and no user record, what's the context of creation?
76+
# FIXME: can the check be replaced by params[:from_cart].present?
77+
if params[:from_cart] == 'true'
78+
@partial_to_render = 'short_form' # Display short_form
79+
else
80+
@partial_to_render = 'form' # Display (normal) form
81+
end
5582
end
56-
@can_edit_login = (can? :create, User)
5783
end
5884

5985
def create

app/models/user.rb

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ def checked_out_models
7676
end
7777

7878
def self.search_ldap(login)
79+
return nil if login.blank?
80+
7981
ldap = Net::LDAP.new(host: "directory.yale.edu", port: 389)
8082
filter = Net::LDAP::Filter.eq("uid", login)
8183
attrs = ["givenname", "sn", "eduPersonNickname", "telephoneNumber", "uid",

app/views/users/new.html.erb

+6-20
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,11 @@
11
<% title "New User" unless params[:from_cart] == "true" %>
22

3-
<% if params[:from_cart] == "true" && User.search_ldap(params[:possible_netid]) && !User.exists?(login: params[:possible_netid]) %>
4-
<%= render :partial => 'short_form' %>
5-
<% elsif params[:from_cart] == "true" && !User.search_ldap(params[:possible_netid]) %>
6-
<div class="span5">
7-
<p>Sorry, the netID that you entered does not exist.
8-
You cannot create a user profile without a valid netID.</p>
9-
</div>
10-
<div class="span5">
11-
</div>
12-
<% elsif params[:from_cart] == "true" && User.exists?(login: params[:possible_netid]) %>
13-
<div class="span5">
14-
<p>This user already exists! If you would like to reserve for them, please select their name from the drop-down
15-
options.</p>
16-
</div>
3+
<% if flash[:notice].nil? && flash[:error].nil? %>
4+
<%= render :partial => @partial_to_render %>
5+
<%= content_tag(:p, link_to("Back to List", users_path)) if current_user && params[:from_cart] != "true" %>
6+
<% else %>
177
<div class="span5">
8+
<%= content_tag(:p, @message) %>
189
</div>
19-
<% else %>
20-
<%= render :partial => 'form' %>
21-
<% end%>
22-
23-
<% if current_user && params[:from_cart] != "true" %>
24-
<p><%= link_to "Back to List", users_path %></p>
10+
<div class="span5"></div>
2511
<% end %>

app/views/users/show.html.erb

+17-15
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,14 @@
5353

5454
<div id="user_stats" class="span5">
5555

56-
<% @show_equipment.each do |key, value| %>
57-
<%= content_tag :div, :id => "#{key}_count", :class => 'span2' do %>
58-
<%= content_tag :h4, key.to_s.titleize %>
59-
<%= content_tag :div, :class => "#{key}_num" do %>
60-
<%= content_tag :i, nil, :class => stats_icons(key) %>
61-
<%= value.size %>
56+
<%# @show_equipment has reservation status (:overdue, ...) as key, full
57+
reservations in question as value %>
58+
<% @show_equipment.each do |status, reservations| %>
59+
<%= content_tag :div, :id => "#{status}_count", :class => 'span2' do %>
60+
<%= content_tag :h4, status.to_s.titleize %>
61+
<%= content_tag :div, :class => "#{status}_num" do %>
62+
<%= content_tag :i, nil, :class => stats_icons(status) %>
63+
<%= reservations.size %>
6264
<% end %>
6365
<% end %>
6466
<% end %>
@@ -98,22 +100,22 @@
98100
<div class="tabbable tabs-left span9">
99101

100102
<ul class="nav nav-tabs">
101-
<% @show_equipment.each do |key, value| %>
102-
<%= content_tag :li, :class => active_tab(key) do %>
103-
<%= link_to "##{key.to_s}", :class => 'tab-text', :"data-toggle" => 'tab' do %>
104-
<%= key.to_s.titleize %>
105-
<%= content_tag :span, value.size, :class => 'badge tab-badge' %>
103+
<% @show_equipment.each do |status, reservations| %>
104+
<%= content_tag :li, :class => active_tab(status) do %>
105+
<%= link_to "##{status.to_s}", :class => 'tab-text', :"data-toggle" => 'tab' do %>
106+
<%= status.to_s.titleize %>
107+
<%= content_tag :span, reservations.size, :class => 'badge tab-badge' %>
106108
<% end %>
107109
<% end %>
108110
<% end %>
109111
</ul>
110112

111113
<div class="tab-content">
112-
<% @show_equipment.each do |key, value| %>
114+
<% @show_equipment.each do |status, reservations| %>
113115
<p>
114-
<%= content_tag :div, {:class => "tab-pane #{active_tab(key)}", :id => key}, false do %>
115-
<% unless value.empty? %>
116-
<%= render partial: 'history_table', locals: { :key => key, :value => value } %>
116+
<%= content_tag :div, {:class => "tab-pane #{active_tab(status)}", :id => status}, false do %>
117+
<% unless reservations.empty? %>
118+
<%= render partial: 'history_table', locals: { :key => status, :value => reservations } %>
117119
<% end %>
118120
<% end %>
119121
</p>

0 commit comments

Comments
 (0)