Skip to content

Commit

Permalink
Merge pull request #194 from camsys/quarter3
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
mathmerized authored Oct 19, 2018
2 parents 5c44678 + af14227 commit 2ef3876
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 49 deletions.
2 changes: 1 addition & 1 deletion app/controllers/asset_events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ def fire_workflow_event
if asset_event_class.name == 'EarlyDispositionRequestUpdateEvent' && event_name == "approve_via_transfer"
is_redirected = true
# we do not want to fire approval of the application event approval
redirect_to new_inventory_asset_event_path(@asset_event.asset, :event_type => DispositionUpdateEvent.asset_event_type.id, :transferred => 1, :causal_asset_event_id => @asset_event.object_key, :causal_asset_event_name => event_name)
redirect_to new_inventory_asset_event_path(@asset_event.send(Rails.application.config.asset_base_class_name.underscore), :event_type => DispositionUpdateEvent.asset_event_type.id, :transferred => 1, :causal_asset_event_id => @asset_event.object_key, :causal_asset_event_name => event_name)
elsif @asset_event.fire_state_event(event_name)
event = WorkflowEvent.new
event.creator = current_user
Expand Down
21 changes: 3 additions & 18 deletions app/models/abilities/authorized_asset_event_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ def initialize(user, organization_ids=[])
#-------------------------------------------------------------------------
# Can manage asset events if the asset is owned by their organization
can :manage, AssetEvent do |ae|
ae.asset_event_type.try(:active) && organization_ids.include?(ae.asset.try(:organization_id))
ae.asset_event_type.try(:active) && organization_ids.include?(ae.send(Rails.application.config.asset_base_class_name.underscore).try(:organization_id))
end

cannot :create, DispositionUpdateEvent do |ae|
!ae.asset.try(:disposable?,false)
!ae.send(Rails.application.config.asset_base_class_name.underscore).try(:disposable?,false)
end


cannot :create, EarlyDispositionRequestUpdateEvent do |ae|
!ae.asset.try(:eligible_for_early_disposition_request?)
!ae.send(Rails.application.config.asset_base_class_name.underscore).try(:eligible_for_early_disposition_request?)
end

cannot [:approve, :reject, :approve_via_transfer], EarlyDispositionRequestUpdateEvent
Expand All @@ -27,21 +27,6 @@ def initialize(user, organization_ids=[])
ae.state != 'new'
end


# Can manage asset events if the asset is owned by their organization
can :manage, AssetEvent do |ae|
ae.asset_event_type.try(:active) && organization_ids.include?(ae.transam_asset.try(:organization_id))
end

cannot :create, DispositionUpdateEvent do |ae|
!ae.transam_asset.try(:disposable?,false)
end


cannot :create, EarlyDispositionRequestUpdateEvent do |ae|
!ae.transam_asset.try(:eligible_for_early_disposition_request?)
end

end
end
end
19 changes: 3 additions & 16 deletions app/models/abilities/manager_asset_event_ability.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,19 @@ def initialize(user)
#-------------------------------------------------------------------------
# managers can manage asset events if the asset's organization is in their list
can :manage, AssetEvent do |ae|
ae.asset_event_type.try(:active) && user.organization_ids.include?(ae.asset.try(:organization_id))
ae.asset_event_type.try(:active) && user.organization_ids.include?(ae.send(Rails.application.config.asset_base_class_name.underscore).try(:organization_id))
end

can :manage, EarlyDispositionRequestUpdateEvent do |ae|
ae.asset_event_type.try(:active)
end

cannot :create, DispositionUpdateEvent do |ae|
!ae.asset.try(:disposable?,true)
!ae.send(Rails.application.config.asset_base_class_name.underscore).try(:disposable?,true)
end

cannot :create, EarlyDispositionRequestUpdateEvent do |ae|
!ae.asset.try(:eligible_for_early_disposition_request?)
end

# managers can manage asset events if the asset's organization is in their list
can :manage, AssetEvent do |ae|
ae.asset_event_type.try(:active) && user.organization_ids.include?(ae.transam_asset.try(:organization_id))
end

cannot :create, DispositionUpdateEvent do |ae|
!ae.transam_asset.try(:disposable?,true)
end

cannot :create, EarlyDispositionRequestUpdateEvent do |ae|
!ae.transam_asset.try(:eligible_for_early_disposition_request?)
!ae.send(Rails.application.config.asset_base_class_name.underscore).try(:eligible_for_early_disposition_request?)
end

end
Expand Down
2 changes: 1 addition & 1 deletion app/models/concerns/maintainable_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ module MaintainableAsset
#---------------------------------------------------------------------------

# each asset has zero or more condition updates
has_many :maintenance_updates, -> {where :asset_event_type_id => MaintenanceUpdateEvent.asset_event_type.id }, :foreign_key => :asset_id, :class_name => "MaintenanceUpdateEvent"
has_many :maintenance_updates, -> {where :asset_event_type_id => MaintenanceUpdateEvent.asset_event_type.id }, :foreign_key => Rails.application.config.asset_base_class_name.foreign_key, :class_name => "MaintenanceUpdateEvent"
#---------------------------------------------------------------------------
# Validations
#---------------------------------------------------------------------------
Expand Down
33 changes: 33 additions & 0 deletions app/models/maintenance_update_event.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ class MaintenanceUpdateEvent < AssetEvent

# Callbacks
after_initialize :set_defaults
after_save :update_asset

# Associations

# Condition of the asset
belongs_to :maintenance_type

validates :maintenance_type, :presence => true
validate :monotonically_increasing_mileage

#------------------------------------------------------------------------------
# Scopes
Expand Down Expand Up @@ -66,4 +68,35 @@ def set_defaults
self.asset_event_type ||= AssetEventType.find_by_class_name(self.name)
end

def update_asset
if current_mileage.present?
typed_asset = TransamAsset.get_typed_asset(transam_asset)
typed_asset.mileage_updates.create(event_date: self.event_date, current_mileage: self.current_mileage) if (typed_asset.respond_to? :mileage_updates)
end
end

# Ensure that the mileage is between the previous (if any) and the following (if any)
# Mileage must increase OR STAY THE SAME over time
def monotonically_increasing_mileage
if transam_asset
previous_mileage_update = transam_asset.asset_events
.where.not(current_mileage: nil)
.where("event_date < ? OR (event_date = ? AND created_at < ?)", self.event_date, self.event_date, (self.new_record? ? Time.current : self.created_at) ) # Define a window that runs up to this event
.where('object_key != ?', self.object_key)
.order(:event_date, :created_at => :asc).last
next_mileage_update = transam_asset.asset_events
.where.not(current_mileage: nil)
.where('event_date > ? OR (event_date = ? AND created_at > ?)', self.event_date, self.event_date, (self.new_record? ? Time.current : self.created_at )) # Define a window that backs up to this event
.where('object_key != ?', self.object_key)
.order(:event_date, :created_at => :desc).first

if previous_mileage_update
errors.add(:current_mileage, "can't be less than last update (#{previous_mileage_update.current_mileage})") if current_mileage < previous_mileage_update.current_mileage
end
if next_mileage_update
errors.add(:current_mileage, "can't be more than next update (#{next_mileage_update.current_mileage})") if current_mileage > next_mileage_update.current_mileage
end
end
end

end
21 changes: 14 additions & 7 deletions app/models/transam_asset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -264,14 +264,16 @@ def searchable_fields
end

def event_classes
typed_asset = TransamAsset.get_typed_asset(self)

a = []
# Use reflection to return the list of has many associatiopns and filter those which are
# events
very_specific.class.reflect_on_all_associations(:has_many).each do |assoc|
typed_asset.class.reflect_on_all_associations(:has_many).each do |assoc|
a << assoc.klass if assoc.class_name.end_with? 'UpdateEvent'
end

if very_specific.class.superclass.name != "TransamAssetRecord"
if typed_asset.class.superclass.name != "TransamAssetRecord"
klass = very_specific.class.superclass
klass.reflect_on_all_associations(:has_many).each do |assoc|
a << assoc.klass if assoc.class_name.end_with? 'UpdateEvent'
Expand All @@ -282,11 +284,13 @@ def event_classes
end

def asset_events(unscoped=false)
typed_asset = TransamAsset.get_typed_asset(self)

events = []
event_classes.each do |e|
assoc_name = e.name.gsub('Event', '').underscore.pluralize
assoc_name = 'early_disposition_requests' if assoc_name == 'early_disposition_request_updates'
events << very_specific.send(assoc_name).ids
events << typed_asset.send(assoc_name).ids
end
if unscoped
AssetEvent.unscoped.where(id: events.flatten)
Expand Down Expand Up @@ -552,7 +556,9 @@ def update_asset_state

# see what metric we are using to determine the service life of the asset
class_name = policy_analyzer.get_service_life_calculation_type.class_name
update_columns(policy_replacement_year: calculate(TransamAsset.get_typed_asset(self), class_name))

new_policy_replacement_year = calculate(TransamAsset.get_typed_asset(self), class_name)
update_columns(policy_replacement_year: new_policy_replacement_year) if old_policy_replacement_year != new_policy_replacement_year

if self.scheduled_replacement_year.nil? or self.scheduled_replacement_year == old_policy_replacement_year
Rails.logger.debug "Setting scheduled replacement year to #{policy_replacement_year}"
Expand All @@ -572,9 +578,9 @@ def update_asset_state
# is in backlog and update the scheduled replacement year to the first planning
# year
if self.policy_replacement_year < current_planning_year_year
update_columns(scheduled_replacement_year: current_planning_year_year, in_backlog: true)
update_columns(in_backlog: true)
else
update_columns(in_backlog: false)
update_columns(in_backlog: false) if self.in_backlog != false
end

Rails.logger.debug "New scheduled_replacement_year = #{self.scheduled_replacement_year}"
Expand All @@ -583,7 +589,8 @@ def update_asset_state
calculator_instance = class_name.constantize.new
start_date = start_of_fiscal_year(scheduled_replacement_year) unless scheduled_replacement_year.blank?
Rails.logger.debug "Start Date = #{start_date}"
update_columns(scheduled_replacement_cost: (calculator_instance.calculate_on_date(self, start_date)+0.5).to_i)
get_sched_cost = (calculator_instance.calculate_on_date(self, start_date)+0.5).to_i
update_columns(scheduled_replacement_cost: get_sched_cost) if get_sched_cost != self.scheduled_replacement_cost

#self.early_replacement_reason = nil if check_early_replacement && !is_early_replacement?
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/asset_events/_actions.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
%i.fa.fa-fw.fa-trash-o
= " Remove this record"

- if @asset_event.class.name == "EarlyDispositionRequestUpdateEvent"
- if @asset_event.respond_to? :allowable_events
%li.divider
- @asset_event.allowable_events.each do |evt|
- if can? evt.to_sym, @asset_event
%li
= link_to fire_workflow_event_inventory_asset_event_path(@asset_event.asset, @asset_event, :event => evt, :transferred => 1) do
= link_to fire_workflow_event_inventory_asset_event_path(@asset_event.send(Rails.application.config.asset_base_class_name.underscore), @asset_event, :event => evt) do
%i.fa.fa-fw{:class => get_workflow_event_icon(evt)}
-if evt == 'approve_via_transfer'
= 'Approve via Transfer'
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_inventory_nav.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
%i.fa.fa-tags.fa-fw
Manage Groups
- event_type = EarlyDispositionRequestUpdateEvent.asset_event_type
- can_view_early_disposition = (event_type.active and can?(:manage, EarlyDispositionRequestUpdateEvent))
- can_view_early_disposition = (event_type.active and can?(:approve, EarlyDispositionRequestUpdateEvent))
- if can_view_early_disposition || can?(:accept_transfers, Rails.application.config.asset_base_class_name.constantize)
- early_events = Rails.application.config.asset_base_class_name.constantize.where(organization_id: @organization_list).joins(:early_disposition_requests).where(asset_events: {state: 'new'}).count
- transferred_assets = Rails.application.config.asset_base_class_name.constantize.where(organization_id: @organization_list).where('asset_tag = object_key').count
Expand Down
2 changes: 1 addition & 1 deletion app/views/shared/_table_formatters.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
};

function asset_view_manufacturer(value, data){
if (data.transam_asset_manufacturer_name == "Other")
if (data.transam_asset_manufacturer_name == "Other (Describe)")
return data.transam_asset_other_manufacturer
else
return data.transam_asset_manufacturer_code + ' - ' + data.transam_asset_manufacturer_name
Expand Down
4 changes: 3 additions & 1 deletion app/views/users/_core_authorization_fields_scripts.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@

$('btn.selector-clearall').hide();

$('#user_organization_id').change();
if (parseInt('#{params[:organization_id]}') > 0) { // new user for a specific org
$('#user_organization_id').change();
}
all_org_updates($('#user_organization_id').val());

transam.make_same_height('.form-part');
Expand Down
2 changes: 1 addition & 1 deletion lib/transam_core/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module TransamCore
VERSION = "2.3.10"
VERSION = "2.3.12"
end

0 comments on commit 2ef3876

Please sign in to comment.