Skip to content

Commit

Permalink
rework validations for management reports
Browse files Browse the repository at this point in the history
  • Loading branch information
jgreben committed Mar 4, 2020
1 parent 995d89e commit b7f791f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 27 deletions.
24 changes: 12 additions & 12 deletions app/models/encumbrance_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,8 @@ class EncumbranceReport < ApplicationRecord
attr_accessor :fund_select, :show_dates, :fund, :fund_begin, :email, :status,
:date_ran, :date_request, :output_file, :fund_acct

validates :email, :status, :date_request, :output_file, presence: true
validates :email, format: { with: Rails.configuration.email_pattern }, allow_blank: true
validates :fund, presence: true, if: :blank_fund_begin?
validates :fund_begin, presence: true, if: :blank_fund?
validate :email_format
validate :fund_selection_present

before_save :set_fund, :write_dates, :set_email, :set_status, :set_output_file

Expand All @@ -21,14 +19,6 @@ def kickoff

private

def blank_fund_begin?
fund_begin.blank?
end

def blank_fund?
fund.blank?
end

def set_fund
if fund.present?
self[:fund_acct] = fund.join(',')
Expand All @@ -52,4 +42,14 @@ def set_status
def set_output_file
self[:output_file] = output_file
end

def email_format
message = 'Email address is missing or not in a correct format'
errors.add(:base, message) unless email.match(Rails.configuration.email_pattern)
end

def fund_selection_present
message = 'Select either a single Fund ID/PTA or a fund that begins with an ID/PTA number'
errors.add(:base, message) unless fund.present? || fund_begin.present?
end
end
16 changes: 12 additions & 4 deletions app/models/expenditures_with_circ_stats_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ class ExpendituresWithCircStatsReport < ApplicationRecord
:fy_start, :fy_end, :cal_start, :cal_end, :pd_start, :pd_end,
:lib_array, :libraries, :format_array, :formats

validates :fund, presence: true, if: :blank_fund_begin?
validates :fund_begin, presence: true, if: :blank_fund?
validates :lib_array, presence: true
validates :format_array, presence: true
validates :date_type, inclusion: %w[fiscal calendar paydate]
validates :start_date_present?, inclusion: { in: [true], message: 'Please choose a start date for the report.' }
validate :email_format
validate :fund_selection_present

before_save :set_fund, :write_lib, :write_fmt, :check_dates

Expand Down Expand Up @@ -46,4 +44,14 @@ def start_date_present?
pd_start.present?
end
end

def email_format
message = 'Email address is missing or not in a correct format'
errors.add(:base, message) unless email.match(Rails.configuration.email_pattern)
end

def fund_selection_present
message = 'Select either a single Fund ID/PTA or a fund that begins with an ID/PTA number'
errors.add(:base, message) unless fund.present? || fund_begin.present?
end
end
28 changes: 18 additions & 10 deletions app/models/shelf_selection_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,16 @@ class ShelfSelectionReport
include ActiveModel::Model
extend ActiveModel::Callbacks
include ActiveModel::Validations
include ActiveModel::Validations::Callbacks
attr_accessor :email, :lib, :loc_array, :cloc_diff, :format_array, :itype_array,
:icat1_array, :lang, :min_yr, :max_yr, :min_circ, :max_circ,
:shadowed, :digisent, :url, :mhlds, :has_dups, :multvol, :multcop,
:no_boundw, :range_type, :call_alpha, :subj_name, :save_opt,
:search_name, :call_lo, :call_hi, :user_id

validates :email, :loc_array, presence: true
validates :loc_array, length: { minimum: 2, message: "can't be empty" }
validates :call_lo, presence: true, if: :lc_range_type?
validates :format_array, :itype_array, length: { minimum: 2, message: "can't be empty" }, if: :lc_range_type?
validate :home_location_present
validate :call_lo_present
validate :classic_call_lo_and_hi, if: :classic_range_type?
validates :itype_array, length: { minimum: 2, message: "can't be empty" }, if: :classic_range_type?
validates :format_array, length: { minimum: 2, message: "can't be empty" }, if: :other_range_type?
validates :format_array, length: { minimum: 2, message: "can't be empty" }, if: :other_range_type?
validates :subj_name, length: { maximum: 80, message: 'no more than 80 characters' }
validates :email, format: { with: Rails.configuration.email_pattern }, allow_blank: true
validate :email_format

def self.generic_options
[["Doesn't matter", 0], ['INCLUDE only', 1], ['EXCLUDE', 2]]
Expand Down Expand Up @@ -53,4 +46,19 @@ def classic_call_lo_and_hi
message = 'Low and hi callnum range must be all digits, with lo lower than hi.'
errors.add(:base, message) unless call_lo =~ /^[0-9]+$/ && call_hi =~ /^[0-9]+$/ && lo_integer < hi_integer
end

def home_location_present
message = "Select a Home Location for #{lib}"
errors.add(:base, message) unless loc_array.size > 1
end

def call_lo_present
message = 'Provide a lower letter range for a LC call number'
errors.add(:base, message) if lc_range_type? && call_lo.empty?
end

def email_format
message = 'Email address is missing or not in a correct format'
errors.add(:base, message) unless email.match(Rails.configuration.email_pattern)
end
end
2 changes: 1 addition & 1 deletion app/views/shelf_selection_reports/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
<div class='form-group row'>
<%= f.label :subj_name, 'Name of this report\'s selection criteria. Leave blank to not use.', class: 'col-sm-10 form-control-label' %>
<div class='col-sm-10'>
<%= f.text_field :subj_name, class: 'form-control' %>
<%= f.text_field :subj_name, maxlength: 80, class: 'form-control' %>
<em>The control allows entering up to 80 characters.</em>
</div>
</div>
Expand Down

0 comments on commit b7f791f

Please sign in to comment.