Permalink
Browse files

Add admin functions to users & licenses

  • Loading branch information...
marktabler committed Jun 10, 2013
1 parent 3cc99ce commit e4fc26c453f5678745e024d9bc36141afbf993bf
@@ -12,6 +12,17 @@ def update
return redirect_to admin_licenses_path
end
+ def create
+ @license = License.create(license_params)
+ return redirect_to admin_licenses_path
+ end
+
+ def destroy
+ @license = License.find(params[:id])
+ @license.destroy
+ return redirect_to admin_licenses_path
+ end
+
private
def license_params
@@ -2,4 +2,30 @@ class Admin::MachinesController < ApplicationController
before_filter :admin_required
+ def edit
+ @machine = Machine.find(params[:id])
+ end
+
+ def update
+ @machine = Machine.find(params[:id])
+ @machine.update_attributes(machine_params)
+ return redirect_to admin_machines_path
+ end
+
+ def create
+ @machine = Machine.create(machine_params)
+ return redirect_to admin_machines_path
+ end
+
+ def destroy
+ @machine = Machine.find(params[:id])
+ @machine.destroy
+ return redirect_to admin_machines_path
+ end
+
+ private
+
+ def machine_params
+ params.require(:machine).permit(:barcode)
+ end
end
@@ -2,4 +2,43 @@ class Admin::UsersController < ApplicationController
before_filter :admin_required
+ def edit
+ @user = User.find(params[:id])
+ end
+
+ def update
+ @user = User.find(params[:id])
+ @user.update_attributes(user_params)
+ return redirect_to admin_users_path
+ end
+
+ def create
+ @user = User.create(user_params)
+ return redirect_to admin_users_path
+ end
+
+ def promote
+ @user = User.find(params[:id])
+ @user.update_attribute(:admin, true)
+ return redirect_to admin_users_path
+ end
+
+ def demote
+ @user = User.find(params[:id])
+ @user.update_attribute(:admin, false) unless @user == current_user
+ return redirect_to admin_users_path
+ end
+
+ def destroy
+ @user = User.find(params[:id])
+ @user.destroy
+ return redirect_to admin_users_path
+ end
+
+ private
+
+ def user_params
+ params.require(:user).permit(:name, :pincode)
+ end
+
end
@@ -4,7 +4,7 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
def current_user
- @user ||= User.find(session[:user_id]) if session[:user_id]
+ @current_user ||= User.find(session[:user_id]) if session[:user_id]
end
helper_method :current_user
View
@@ -3,6 +3,8 @@ class Machine < ActiveRecord::Base
has_many :active_licenses, dependent: :destroy
has_many :licenses, through: :active_licenses
+ default_scope { order("barcode ASC") }
+
def install(license)
active_licenses.create(license_id: license.id)
end
@@ -4,4 +4,12 @@
<%= f.label :capacity %>
<%= f.text_field :capacity %>
<%= f.submit %>
-<% end %>
+<% end %>
+
+<hr>
+<h4>Installed on:</h4>
+<ul>
+ <% @license.machines.each do |machine| %>
+ <%= link_to machine.barcode, machine_path(machine) %>
+ <% end %>
+</ul>
@@ -1,5 +1,15 @@
<h2>Administer Licenses</h2>
-<table>
+<h4>Create</h4>
+<%= form_for License.new, url: admin_licenses_path, method: :post do |f| %>
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+ <%= f.label :capacity %>
+ <%= f.text_field :capacity %>
+ <%= f.submit %>
+<% end %>
+<h4>Manage</h4>
+<hr>
+<table class="table">
<tr>
<th>Name</th>
<th>Total</th>
@@ -0,0 +1,21 @@
+<h2>Administer Assets</h2>
+<h4>Create</h4>
+<%= form_for Machine.new, url: admin_machines_path, method: :post do |f| %>
+ <%= f.label :barcode %>
+ <%= f.text_field :barcode %>
+ <%= f.submit %>
+<% end %>
+<h4>Manage</h4>
+<hr>
+<table class="table">
+ <tr>
+ <th>Barcode</th>
+ <th>Delete</th>
+ </tr>
+ <% Machine.all.each do |machine| %>
+ <tr>
+ <td><%= link_to machine.barcode, machine_path(machine) %></td>
+ <td><%= link_to "Delete", admin_machine_path(machine), method: :delete, data: { confirm: "Delete #{machine.barcode} and all installation records? This action cannot be undone."} %></td>
+ </tr>
+ <% end %>
+</table>
@@ -0,0 +1,15 @@
+<%= form_for @user, url: admin_user_path(@user), method: :put do |f| %>
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+ <%= f.label :pincode, "Pin" %>
+ <%= f.text_field :pincode %>
+ <%= f.submit %>
+<% end %>
+
+<% unless @user == current_user %>
+ <% if @user.admin? %>
+ <%= button_to "Demote", admin_demote_user_path(@user), method: :post %>
+ <% else %>
+ <%= button_to "Promote", admin_promote_user_path(@user), method: :post %>
+ <% end %>
+<% end %>
@@ -0,0 +1,27 @@
+<h2>Administer Users</h2>
+<h4>Create</h4>
+<%= form_for User.new, url: admin_users_path, method: :post do |f| %>
+ <%= f.label :name %>
+ <%= f.text_field :name %>
+ <%= f.label :pincode, "Pin" %>
+ <%= f.text_field :pincode %>
+ <%= f.submit %>
+<% end %>
+<h4>Manage</h4>
+<hr>
+<table class="table">
+ <tr>
+ <th>Name</th>
+ <th>Pin</th>
+ <th>Admin<th>
+ <th>Delete</th>
+ </tr>
+ <% User.all.each do |user| %>
+ <tr>
+ <td><%= link_to user.name, edit_admin_user_path(user) %></td>
+ <td><%= user.pincode %></td>
+ <td><%= user.admin? ? "Yes" : "No" %></td>
+ <td><%= link_to "Delete", admin_user_path(user), method: :delete, data: { confirm: "Delete #{user.name}? This action cannot be undone."} %></td>
+ </tr>
+ <% end %>
+</table>
@@ -1,5 +1,5 @@
<h2>Licenses</h2>
-<table>
+<table class="table">
<tr>
<th>Name</th>
<th>Total</th>
@@ -1,5 +1,5 @@
<h4>Asset ID <%= @machine.barcode %></h4>
-<table>
+<table class="table">
<tr>
<th>Title</th>
<th>Remove</th>
View
@@ -3,6 +3,8 @@
post '/login' => 'sessions#create'
get '/login' => 'sessions#new'
delete '/logout' => 'sessions#destroy'
+ post '/admin/promote/:id' => 'admin/users#promote', as: "admin_promote_user"
+ post '/admin/demote/:id' => 'admin/users#demote', as: "admin_demote_user"
get '/machines/search' => 'machines#search', as: 'machine_search'

0 comments on commit e4fc26c

Please sign in to comment.