Skip to content

Commit

Permalink
View/edit/update programs
Browse files Browse the repository at this point in the history
  • Loading branch information
smellsblue committed May 10, 2023
1 parent dc0fcde commit 8314cfb
Show file tree
Hide file tree
Showing 12 changed files with 167 additions and 5 deletions.
44 changes: 44 additions & 0 deletions app/controllers/programs_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
class ProgramsController < ApplicationController
before_action :authenticate_user!
require_permission :can_view_programs?
require_permission :can_edit_programs?, only: %i[create update]
active_tab "inventory"

def index
@programs = Program.all.to_a
end

def new
@program = Program.new
end

def create
@program = Program.new
@program.name = params[:name]
@program.initialized_name = params[:initialized_name]
@program.external_id = params[:external_id]
@program.external_class_id = params[:external_class_id]
@program.save!
redirect_to programs_path, flash: { success: "Program successfully created!" }
rescue ActiveRecord::RecordNotUnique
flash.now[:error] = "Error, duplicate record detected!"
render :new
end

def show
@program = Program.find(params[:id])
end

def update
@program = Program.find(params[:id])
@program.name = params[:name]
@program.initialized_name = params[:initialized_name]
@program.external_id = params[:external_id]
@program.external_class_id = params[:external_class_id]
@program.save!
redirect_to programs_path, flash: { success: "Program successfully updated!" }
rescue ActiveRecord::RecordNotUnique
flash.now[:error] = "Error, duplicate record detected!"
render :show
end
end
13 changes: 13 additions & 0 deletions app/models/concerns/users/program_manipulator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module Users
module ProgramManipulator
extend ActiveSupport::Concern

def can_view_programs?
super_admin?
end

def can_edit_programs?
super_admin?
end
end
end
4 changes: 0 additions & 4 deletions app/models/program.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@ class Program < ApplicationRecord
def self.alphabetical
order(Arel.sql("LOWER(name)"))
end

def initialized_name
name.gsub(/\b(\w)\w*?\b/, "\\1").gsub(/[\s\-]/, "")
end
end
1 change: 1 addition & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class User < ApplicationRecord
include Users::ItemManipulator
include Users::OrderManipulator
include Users::OrganizationManipulator
include Users::ProgramManipulator
include Users::PurchaseManipulator
include Users::PurchaseShipmentManipulator
include Users::ReportManipulator
Expand Down
4 changes: 4 additions & 0 deletions app/views/items/_tabs.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
<%= tab("Racks", bin_locations_path, params[:controller] == "bin_locations") %>
<% end %>
<% if current_user.can_view_programs? %>
<%= tab("Programs", programs_path, params[:controller] == "programs") %>
<% end %>
<% if current_user.can_view_item_program_ratios? %>
<%= tab("Program Ratios", item_program_ratios_path, params[:controller] == "item_program_ratios") %>
<% end %>
Expand Down
31 changes: 31 additions & 0 deletions app/views/programs/_program_fields.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label class="control-label" for="program-name">Program Name</label>
<input type="text" class="form-control" id="program-name" name="name" placeholder="Program name" value="<%= program.name %>" data-guard="required" autocomplete="off" />
</div>
</div>

<div class="col-xs-3">
<div class="form-group">
<label class="control-label" for="program-external-id">External Id</label>
<input type="text" class="form-control" id="program-external-id" name="external_id" placeholder="External Id" value="<%= program.external_id %>" data-guard="required int" autocomplete="off" />
</div>
</div>

<div class="col-xs-3">
<div class="form-group">
<label class="control-label" for="program-external-id">External Class Id</label>
<input type="text" class="form-control" id="program-external-class-id" name="external_class_id" placeholder="External Class Id" value="<%= program.external_class_id %>" data-guard="required int" autocomplete="off" />
</div>
</div>
</div>

<div class="row">
<div class="col-xs-6">
<div class="form-group">
<label class="control-label" for="program-initials">Initials</label>
<input type="text" class="form-control" id="program-initials" name="initialized_name" placeholder="Program name initials" value="<%= program.initialized_name %>" data-guard="required string" data-guard-string-max="8" autocomplete="off" />
</div>
</div>
</div>
32 changes: 32 additions & 0 deletions app/views/programs/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<% content_for :title, "Programs" %>
<% content_for :tabs, render("items/tabs") %>
<% content_for :content do %>
<% if current_user.can_edit_programs? %>
<h4 class="button-height">
<%= link_to "Create Program", new_program_path, class: "btn btn-primary bottom15 pull-right" %>
</h4>
<% end %>

<table class="table table-hover table-striped data-table">
<thead>
<tr>
<th class="sort-asc">Name</th>
<th>Initials</th>
<th>External Id</th>
<th>External Class Id</th>
</tr>
</thead>

<tbody>
<% @programs.each do |program| %>
<tr data-href="<%= program_path(program) %>">
<td><%= program.name %></td>
<td><%= program.initialized_name %></td>
<td><%= program.external_id %></td>
<td><%= program.external_class_id %></td>
</tr>
<% end %>
</tbody>
</table>
<% end %>
10 changes: 10 additions & 0 deletions app/views/programs/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% content_for :title, "Create Program" %>
<% content_for :tabs, render("items/tabs") %>
<% content_for :content do %>
<%= form_for @program, data: { live_guarded: true } do |f| %>
<%= render partial: "programs/program_fields", locals: { fields: f, program: @program } %>
<%= f.submit "Save", class: "btn btn-primary" %>
<%= link_to "Cancel", programs_path, class: "btn btn-link" %>
<% end %>
<% end %>
10 changes: 10 additions & 0 deletions app/views/programs/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<% content_for :title, "Edit Program #{@program.name}" %>
<% content_for :tabs, render("items/tabs") %>
<% content_for :content do %>
<%= form_for @program, data: { live_guarded: true } do |f| %>
<%= render partial: "programs/program_fields", locals: { fields: f, program: @program } %>
<%= f.submit "Save", class: "btn btn-primary" %>
<%= link_to "Cancel", programs_path, class: "btn btn-link" %>
<% end %>
<% end %>
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@
end
end

resources :programs, only: %i[index new create show update]

resources :purchases, only: %i[index new create edit show update] do
collection do
get :closed, :canceled
Expand Down
17 changes: 17 additions & 0 deletions db/migrate/20230510024940_add_initialized_name_to_programs.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class AddInitializedNameToPrograms < ActiveRecord::Migration[5.2]
def up
add_column :programs, :initialized_name, :string, limit: 8
add_index :programs, :initialized_name, unique: true

Program.all.to_a.each do |program|
program.initialized_name = program.name.gsub(/\b(\w)\w*?\b/, "\\1").gsub(/[\s\-]/, "")
program.save!
end

change_column_null :programs, :initialized_name, false
end

def down
remove_column :programs, :initialized_name
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20220426061926) do
ActiveRecord::Schema.define(version: 2023_05_10_024940) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -284,6 +284,8 @@
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.integer "external_class_id", null: false
t.string "initialized_name", limit: 8, null: false
t.index ["initialized_name"], name: "index_programs_on_initialized_name", unique: true
end

create_table "purchase_details", id: :serial, force: :cascade do |t|
Expand Down

0 comments on commit 8314cfb

Please sign in to comment.