Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master' into would-you-like-to-t…
Browse files Browse the repository at this point in the history
…ake-a-survey

Conflicts:
	db/schema.rb
  • Loading branch information
smellsblue committed May 10, 2023
2 parents a97581e + 0a3d7ab commit 0a46704
Show file tree
Hide file tree
Showing 15 changed files with 178 additions and 12 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
2 changes: 1 addition & 1 deletion app/models/item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def deleted?
end

def past_total_value(time)
past_version = version_at(time)
past_version = paper_trail.version_at(time)
return nil if past_version.blank? || past_version.value.nil?
past_version.current_quantity * past_version.value
end
Expand Down
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: 2023_03_14_075139) 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 @@ -293,6 +293,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
12 changes: 6 additions & 6 deletions db/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@
Address.delete_all

# Create Programs
resource_closets = Program.find_or_create_by(name: "Resource Closets", external_id: 6, external_class_id: 1)
Program.find_or_create_by(name: "Beyond the Closet", external_id: 12, external_class_id: 2)
Program.find_or_create_by(name: "Pack-It-Forward", external_id: 5, external_class_id: 3)
Program.find_or_create_by(name: "Operation Esteem", external_id: 9, external_class_id: 5)
Program.find_or_create_by(name: "Dress for Dignity", external_id: 2, external_class_id: 4)
Program.find_or_create_by(name: "Beautification Projects", external_id: 1, external_class_id: 6)
resource_closets = Program.find_or_create_by(name: "Resource Closets", initialized_name: "RC", external_id: 6, external_class_id: 1) # rubocop:disable Layout/LineLength
Program.find_or_create_by(name: "Beyond the Closet", initialized_name: "BtC", external_id: 12, external_class_id: 2)
Program.find_or_create_by(name: "Pack-It-Forward", initialized_name: "PIF", external_id: 5, external_class_id: 3)
Program.find_or_create_by(name: "Operation Esteem", initialized_name: "OE", external_id: 9, external_class_id: 5)
Program.find_or_create_by(name: "Dress for Dignity", initialized_name: "DfD", external_id: 2, external_class_id: 4)
Program.find_or_create_by(name: "Beautification Projects", initialized_name: "BP", external_id: 1, external_class_id: 6)

# Create organizations
org_stanford = Organization.create(name: "Stanford Hospital", phone_number: "(650) 723-4000",
Expand Down
4 changes: 4 additions & 0 deletions spec/fixtures/programs.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
resource_closets:
name: Resource Closets
initialized_name: RC
external_class_id: 1

pack_it_forward:
name: Pack-It-Forward
initialized_name: PIF
external_class_id: 3

dress_for_dignity:
name: Dress for Dignity
initialized_name: DFD
external_class_id: 4

beautification_projects:
name: Beautification Projects
initialized_name: BP
external_class_id: 6

0 comments on commit 0a46704

Please sign in to comment.