Skip to content

Commit

Permalink
chore: filling in tests, cleaning things up
Browse files Browse the repository at this point in the history
  • Loading branch information
aviemet committed Dec 7, 2024
1 parent ea3294e commit 893fb1b
Show file tree
Hide file tree
Showing 56 changed files with 2,692 additions and 2,625 deletions.
3 changes: 3 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -270,3 +270,6 @@ Style/HashSyntax:

Metrics:
Enabled: false

RSpec/NestedGroups:
Enabled: false
29 changes: 29 additions & 0 deletions app/controllers/api/accessories_controller.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1,31 @@
class Api::AccessoriesController < Api::ApiController
expose :accessories, -> { @active_company.accessories }
expose :accessory, scope: ->{ @active_company.accessories }

strong_params :accessory, permit: [:name, :location_id, :manager_id, :notes]

# @route GET /api/accessories (api_accessories)
def index
render json: accessories.includes_associated.render
end

# @route GET /api/accessories/:id (api_accessory)
def show
render json: accessory.render
end

# @route GET /api/options/accessories (api_accessories_options)
def options
render json: accessories.render(view: :options)
end

# @route PATCH /api/accessories/:id (api_accessory)
# @route PUT /api/accessories/:id (api_accessory)
def update
if accessory.update(accessory_params)
render json: accessory.render, status: :created
else
render json: { errors: accessory.errors }, status: :see_other
end
end
end
8 changes: 2 additions & 6 deletions app/controllers/api/assets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ class Api::AssetsController < Api::ApiController
expose :assets, -> { @active_company.assets }
expose :asset, scope: ->{ @active_company.assets }

strong_params :asset, permit: [:name, :location_id, :manager_id, :notes]

# @route GET /api/assets (api_assets)
def index
render json: assets.includes_associated.render
Expand Down Expand Up @@ -36,10 +38,4 @@ def update
render json: { errors: asset.errors }, status: :see_other
end
end

private

def asset_params
params.require(:asset).permit(:name, :location_id, :manager_id, :notes)
end
end
12 changes: 4 additions & 8 deletions app/controllers/api/nics_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
class Api::NicsController < Api::ApiController
expose :nic, id: ->{ params[:slug] }, scope: ->{ @active_company.nics.includes_associated }, find_by: :slug

strong_params :nic, permit: [:mac, :nic_type, :item_id]

# @route POST /api/hardware/:item_id/nics (api_item_nics)
def create
nic.company = @active_company

if nic.save
render json: nic, status: :created
render json: nic.render, status: :created
else
render json: { errors: nic.errors }, status: :see_other
end
Expand All @@ -16,15 +18,9 @@ def create
# @route PUT /api/hardware/:item_id/nics/:id (api_item_nic)
def update
if nic.update(nic_params)
render json: nic, status: :created
render json: nic.render, status: :created
else
render json: { errors: nic.errors }, status: :see_other
end
end

private

def nic_params
params.require(:nic).permit(:mac, :nic_type, :item_id)
end
end
13 changes: 9 additions & 4 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class ApplicationController < ActionController::Base

# before_action :decode_id

# rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found

private

Expand All @@ -27,7 +27,12 @@ def first_run
# params[:asset_type] = id_parts[:model]
# end

# def record_not_found
# raise ActionController::RoutingError, 'Not Found'
# end
def record_not_found
Rails.logger.warn("RecordNotFound: #{params.inspect}")

render inertia: "Error", props: {
status: 404,
message: t('activerecord.errors.message.not_found')
}
end
end
112 changes: 112 additions & 0 deletions app/controllers/concerns/searchable_from_giving.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
# frozen_string_literal: true

module Searchable
extend ActiveSupport::Concern

included do
class_attribute :internal_sortable_fields
before_action :remove_empty_query_parameters

##
# Searches and sorts model using search params
# model: ActiveRecord object
##
def search(model)
sort(search_by_params(model), model)
end

def paginate(resource, key)
resource.page(params[:page] || 1).per(key ? current_user.limit(key) : nil)
end

def pagination_data(model)
return if !model.respond_to? :total_pages

{
pages: model.total_pages,
limit: model.limit_value,
current_page: model.current_page,
next_page: model.next_page,
prev_page: model.prev_page,
is_first_page: model.first_page?,
is_last_page: model.last_page?
}
end
end

class_methods do
def sortable_fields(fields)
self.internal_sortable_fields = fields
end
end

private

##
# Filters ActiveRecord relation by search params
##
def search_by_params(model)
return model unless params[:search]

model.where(id: model.search(params[:search]).pluck(:id))
end

##
# Sorts ActiveRecord relation by sort params
##
def sort(obj, model)
# With empty sort params, don't sort
return obj unless params[:sort]

# Sort using db query
obj.order(sort_string(model))
end

##
# Returns a string to be used in an `order` statement
##
def sort_string(model)
return unless self.class.internal_sortable_fields&.include?(params[:sort])

field_type = get_field_type(model, params[:sort])
# Don't error if field doesn't exist on model
return if field_type.nil?

sort_str = params[:sort].to_s
"#{sort_str} #{direction}"
end

##
# Returns the data type of a database field
##
def get_field_type(model, column)
# if `column` is in the form 'model.field', or further chained such as 'model1.model2.field',
# ignore the passed `model` param and use the last chained model sent in `column`
split_fields = column.split(".")
if split_fields.length > 1
model = split_fields[-2].titleize.singularize.constantize
column = split_fields[-1]
end
model.column_for_attribute(column).type
end

def direction
%w(asc desc).freeze.include?(params[:direction]) ? params[:direction] : 'asc'
end

def remove_empty_query_parameters
# Filter out empty query parameters
non_empty_params = request.query_parameters.compact_blank

# Remove direction param if table is not sorted
if non_empty_params['direction'].present? && non_empty_params['sort'].blank?
non_empty_params.delete('direction')
end

return unless request.query_parameters.keys.length > non_empty_params.keys.length

# Rebuild the URL without empty query parameters
new_url = "#{request.path}?#{non_empty_params.to_param}"
redirect_to new_url
end
end
6 changes: 2 additions & 4 deletions app/controllers/items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class ItemsController < ApplicationController
expose :items, -> { search(@active_company.items.includes_associated, sortable_fields) }
expose :item, scope: ->{ @active_company.items }, find: ->(id, scope){ scope.includes_associated.find(id) }

strong_params :item, permit: [:name, :asset_tag, :serial, :cost, :cost_cents, :cost_currency, :notes, :department_id, :model_id, :vendor_id, :default_location_id, :parent_id, :status_label_id, :purchased_at, :requestable, nics: [:mac, :ip]]

before_action :handle_department_change, only: [:create, :update]

# @route GET /hardware (items)
Expand Down Expand Up @@ -141,8 +143,4 @@ def sortable_fields
def advanced_search_params
params.permit(:name, :asset_tag, :serial, :cost, :purchased_at, :requestable, model: [:id], vendor: [:id], manufacturer: [:id], department: [:id], category: [:id], created_at: [:start, :end, :type])
end

def item_params
@item_params ||= params.require(:item).permit(:name, :asset_tag, :serial, :cost, :cost_cents, :cost_currency, :notes, :department_id, :model_id, :vendor_id, :default_location_id, :parent_id, :status_label_id, :purchased_at, :requestable, nics: [:mac, :ip])
end
end
10 changes: 3 additions & 7 deletions app/controllers/status_labels_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,9 @@ def index

# @route GET /status_labels/:slug (status_label)
def show
if status_label.nil?
render inertia: "Error", props: { status: 404 }
else
render inertia: "StatusLabels/Show", props: {
status_label: -> { status_label.render }
}
end
render inertia: "StatusLabels/Show", props: {
status_label: -> { status_label.render }
}
end

# @route GET /status_labels/new (new_status_label)
Expand Down
Loading

0 comments on commit 893fb1b

Please sign in to comment.