Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add license manipulation for assets

  • Loading branch information...
commit 6e059ff6bd5ba0337a86454397d6257eea44550b 1 parent 2f71fa5
Mark Tabler authored
3  app/assets/javascripts/sessions.js.coffee
View
@@ -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  app/assets/stylesheets/sessions.css.scss
View
@@ -0,0 +1,3 @@
+// Place all the styles related to the sessions controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
13 app/controllers/application_controller.rb
View
@@ -2,4 +2,17 @@ class ApplicationController < ActionController::Base
# Prevent CSRF attacks by raising an exception.
# For APIs, you may want to use :null_session instead.
protect_from_forgery with: :exception
+
+ def current_user
+ @user ||= User.find(session[:user_id]) if session[:user_id]
+ end
+ helper_method :current_user
+
+ def login_required
+ return redirect_to login_path unless current_user
+ end
+
+ def admin_required
+ return redirect_to login_path unless current_user && current_user.admin?
+ end
end
17 app/controllers/licenses_controller.rb
View
@@ -1,2 +1,19 @@
class LicensesController < ApplicationController
+
+ before_filter :login_required
+
+
+ def install
+ machine = Machine.find(params[:machine_id])
+ license = License.find(params[:license_id])
+ machine.install(license)
+ return redirect_to machine_path(machine)
+ end
+
+ def uninstall
+ active_license = ActiveLicense.find(params[:id])
+ machine = active_license.machine
+ active_license.destroy
+ return redirect_to machine_path(machine)
+ end
end
15 app/controllers/machines_controller.rb
View
@@ -1,2 +1,17 @@
class MachinesController < ApplicationController
+
+ before_filter :login_required
+
+ def search
+ machine = Machine.find_by_barcode(params[:barcode])
+ if machine
+ return redirect_to machine_path(machine)
+ else
+ return redirect_to machines_path
+ end
+ end
+
+ def show
+ @machine = Machine.find(params[:id])
+ end
end
18 app/controllers/sessions_controller.rb
View
@@ -0,0 +1,18 @@
+class SessionsController < ApplicationController
+
+ def create
+ u = User.find_by_pincode(params[:pincode])
+ if u
+ session[:user_id] = u.id
+ return redirect_to licenses_path
+ else
+ return redirect_to login_path
+ end
+ end
+
+ def destroy
+ session[:user_id] = nil
+ return redirect_to login_path
+ end
+
+end
3  app/controllers/users_controller.rb
View
@@ -1,2 +1,5 @@
class UsersController < ApplicationController
+
+ before_filter :admin_required
+
end
5 app/helpers/licenses_helper.rb
View
@@ -1,2 +1,7 @@
module LicensesHelper
+
+ def install_options(machine)
+ licenses = License.all - machine.licenses
+ options_for_select(licenses.collect {|l| [l.name, l.id]})
+ end
end
2  app/helpers/sessions_helper.rb
View
@@ -0,0 +1,2 @@
+module SessionsHelper
+end
5 app/models/active_license.rb
View
@@ -2,5 +2,8 @@ class ActiveLicense < ActiveRecord::Base
belongs_to :machine
belongs_to :license
-
+
+ def name
+ license.name
+ end
end
4 app/models/user.rb
View
@@ -1,4 +1,6 @@
class User < ActiveRecord::Base
-
+ def admin?
+ admin
+ end
end
1  app/views/layouts/_nav.html.erb
View
@@ -0,0 +1 @@
+<%= link_to "View Licenses", licenses_path %> | <%= link_to "View Assets", machines_path %>
6 app/views/layouts/application.html.erb
View
@@ -7,7 +7,11 @@
<%= csrf_meta_tags %>
</head>
<body>
-
+<% if current_user %>
+ <h3>Signed in as <%= current_user.name %> | <%= link_to 'Sign out', '/logout', method: :delete %></h3>
+ <%= render partial: 'layouts/nav' %>
+ <br><br>
+<% end %>
<%= yield %>
</body>
1  app/views/licenses/index.html.erb
View
@@ -0,0 +1 @@
+<h2>Licenses</h2>
5 app/views/machines/_search.html.erb
View
@@ -0,0 +1,5 @@
+<%= form_tag machine_search_path, method: :get do %>
+ <%= label_tag :barcode, "Asset Barcode" %>
+ <%= text_field_tag :barcode %>
+ <%= submit_tag "Search" %>
+<% end %>
1  app/views/machines/index.html.erb
View
@@ -0,0 +1 @@
+<%= render partial: 'search' %>
24 app/views/machines/show.html.erb
View
@@ -0,0 +1,24 @@
+<h4>Asset ID <%= @machine.barcode %></h4>
+<table>
+ <tr>
+ <th>Title</th>
+ <th>Remove</th>
+ </tr>
+ <% @machine.active_licenses.each do |active_license| %>
+ <tr>
+ <td><%= active_license.name %></td>
+ <td><%= button_to "Uninstall", uninstall_path(active_license), method: :delete %></td>
+ </tr>
+ <% end %>
+</table>
+
+<h4>Install</h4>
+<%= form_tag install_path, method: :post do %>
+ <%= select_tag :license_id, install_options(@machine)%>
+ <%= hidden_field_tag :machine_id, @machine.id %>
+ <%= submit_tag "Install" %>
+<% end %>
+
+<hr>
+<h4>New Search</h4>
+<%= render partial: 'search' %>
5 app/views/sessions/new.html.erb
View
@@ -0,0 +1,5 @@
+<%= form_tag '/login', method: :post do %>
+ <%= label_tag :pincode %>
+ <%= text_field_tag :pincode %>
+ <%= submit_tag "Log in" %>
+<% end %>
10 config/routes.rb
View
@@ -1,4 +1,14 @@
Norrington::Application.routes.draw do
+
+ post '/login' => 'sessions#create'
+ get '/login' => 'sessions#new'
+ delete '/logout' => 'sessions#destroy'
+
+ get '/machines/search' => 'machines#search', as: 'machine_search'
+
+ delete '/uninstall/:id' => 'licenses#uninstall', as: "uninstall"
+ post '/install' => 'licenses#install', as: "install"
+
resources :licenses
resources :users
7 test/controllers/sessions_controller_test.rb
View
@@ -0,0 +1,7 @@
+require 'test_helper'
+
+class SessionsControllerTest < ActionController::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end
4 test/helpers/sessions_helper_test.rb
View
@@ -0,0 +1,4 @@
+require 'test_helper'
+
+class SessionsHelperTest < ActionView::TestCase
+end
Please sign in to comment.
Something went wrong with that request. Please try again.