Permalink
Browse files

Merge pull request #2330 from ekylibre/refacto/validations

Cleaning up validations
  • Loading branch information...
Aquaj committed Nov 2, 2018
2 parents 7901264 + b34e38d commit 9315832f54d38d3d0e528a5e03778bf82b6704df
View
@@ -189,22 +189,16 @@ class Activity < Ekylibre::Record::Base
end
validate do
if family_item = Nomen::ActivityFamily[family]
if with_supports && variety = Nomen::Variety[support_variety] && family_item.support_variety
errors.add(:support_variety, :invalid) unless variety <= family_item.support_variety
end
if with_cultivation && variety = Nomen::Variety[cultivation_variety]
if family_item.cultivation_variety.present?
errors.add(:cultivation_variety, :invalid) unless variety <= family_item.cultivation_variety
end
end
end
errors.add :use_gradings, :checked_off_with_inspections if inspections.any? && !use_gradings
if use_gradings
unless measure_something?
errors.add :use_gradings, :checked_without_measures
end
errors.add :use_gradings, :checked_without_measures if use_gradings && !measure_something?
next unless family_item = Nomen::ActivityFamily[family]
if with_supports && variety = Nomen::Variety[support_variety] && family_item.support_variety
errors.add(:support_variety, :invalid) unless variety <= family_item.support_variety
end
next unless with_cultivation && variety = Nomen::Variety[cultivation_variety]
next unless family_item.cultivation_variety.present?
errors.add(:cultivation_variety, :invalid) unless variety <= family_item.cultivation_variety
true
end
@@ -62,8 +62,9 @@ class ActivityBudgetItem < Ekylibre::Record::Base
validates :variant_indicator, :variant_unit, length: { maximum: 500 }, allow_blank: true
# ]VALIDATORS]
validates :variant, presence: true
validates :unit_amount, presence: { message: :invalid }
validates :currency, match: { with: :activity_budget }
# delegate :supports_quantity, :supports_count, :support_indicator, :support_unit, to: :activity
delegate :size_indicator, :size_unit, to: :activity
delegate :currency, to: :activity_budget, prefix: true
delegate :name, to: :variant, prefix: true
@@ -77,14 +78,10 @@ class ActivityBudgetItem < Ekylibre::Record::Base
end
validate do
if currency && activity_budget
errors.add(:currency, :invalid) if currency != activity_budget_currency
end
#???: Why do we even have both if we check that they're always equals??
if currency && unit_currency
errors.add(:currency, :invalid) if currency != unit_currency
end
errors.add(:unit_amount, :invalid) if unit_amount.blank?
end
after_validation do
@@ -187,10 +187,7 @@ class ActivityProduction < Ekylibre::Record::Base
end
validate do
if plant_farming?
errors.add(:support_shape, :empty) if support_shape && support_shape.empty?
end
true
errors.add(:support_shape, :empty) if plant_farming? && support_shape && support_shape.empty?
end
after_save do
@@ -44,6 +44,7 @@ class ContractItem < Ekylibre::Record::Base
validates :pretax_amount, :quantity, :unit_pretax_amount, presence: true, numericality: { greater_than: -1_000_000_000_000_000, less_than: 1_000_000_000_000_000 }
validates :contract, :variant, presence: true
# ]VALIDATORS]
validates :quantity, exclusion: { in: [0], message: :invalid }
delegate :currency, to: :contract
sums :contract, :items, :pretax_amount
@@ -61,8 +62,4 @@ class ContractItem < Ekylibre::Record::Base
before_validation do
self.quantity ||= 0
end
validate do
errors.add(:quantity, :invalid) if self.quantity.zero?
end
end
@@ -58,6 +58,10 @@ class DebtTransfer < Ekylibre::Record::Base
validates :number, length: { maximum: 500 }, allow_blank: true
# ]VALIDATORS]
validates :debt_transfer_affair_id, uniqueness: { scope: :affair_id }
validates :amount, presence: { message: :empty }, exclusion: { in: [0], message: :empty }
validates :third, match: { with: :affair, middleman: :debt_transfer_affair }
validates :currency, match: { with: :affair, middleman: :debt_transfer_affair }
acts_as_affairable :third
@@ -75,12 +79,6 @@ class DebtTransfer < Ekylibre::Record::Base
end
end
validate do
errors.add(:third, :invalid) unless affair.third == debt_transfer_affair.third
errors.add(:currency, :invalid) unless affair.currency == debt_transfer_affair.currency
errors.add(:amount, :empty) unless amount.present? && (amount != 0)
end
before_destroy do
DebtTransfer.where(
affair: debt_transfer_affair,
View
@@ -173,6 +173,7 @@ class Entity < Ekylibre::Record::Base
validates :activity_code, length: { allow_nil: true, maximum: 30 }
validates :deliveries_conditions, :number, length: { allow_nil: true, maximum: 60 }
validates :iban, iban: true, allow_blank: true
validates :siret_number, siret_format: true, allow_blank: true
validates_attachment_content_type :picture, content_type: /image/
validates_delay_format_of :supplier_payment_delay
@@ -219,6 +220,7 @@ class Entity < Ekylibre::Record::Base
# end
full_name.strip!
# self.name = self.name.to_s.strip.downcase.gsub(/[^a-z0-9\.\_]/,'')
self.siret_number = siret_number&.strip
self.language = Preference[:language] if language.blank?
self.currency = Preference[:currency] if currency.blank?
self.country = Preference[:country] if country.blank?
@@ -229,17 +231,6 @@ class Entity < Ekylibre::Record::Base
self.supplier_payment_delay = '30 days' if supplier_payment_delay.blank?
end
validate do
if siret_number.present?
errors.add(:siret_number, :invalid) unless Luhn.valid?(siret_number.strip)
end
# if self.nature
# if self.nature.in_name and not self.last_name.match(/( |^)#{self.nature.title}( |$)/i)
# errors.add(:last_name, :missing_title, :title => self.nature.title)
# end
# end
end
before_save do
self.born_at ||= Time.new(2008, 1, 1) if of_company
end
View
@@ -99,6 +99,8 @@ class FixedAsset < Ekylibre::Record::Base
validates :name, uniqueness: true
validates :depreciation_method, inclusion: { in: depreciation_method.values }
validates :asset_account, :expenses_account, presence: true
validates :currency, match: { with: :journal, to_invalidate: :journal }
enumerize :depreciation_period, in: %i[monthly quarterly yearly], default: -> { Preference.get(:default_depreciation_period).value || Preference.set!(:default_depreciation_period, :yearly, :string) }
scope :drafts, -> { where(state: %w[draft]) }
@@ -154,15 +156,8 @@ class FixedAsset < Ekylibre::Record::Base
end
validate do
if currency && journal
errors.add(:journal, :invalid) if currency != journal.currency
end
if started_on
if self.stopped_on
unless self.stopped_on >= started_on
errors.add(:stopped_on, :posterior, to: started_on.l)
end
end
if started_on && self.stopped_on && stopped_on < started_on
errors.add(:stopped_on, :posterior, to: started_on.l)
end
true
end
View
@@ -57,6 +57,8 @@ class Gap < Ekylibre::Record::Base
validates :number, presence: true, length: { maximum: 500 }
validates :printed_at, presence: true, timeliness: { on_or_after: -> { Time.new(1, 1, 1).in_time_zone }, on_or_before: -> { Time.zone.now + 50.years } }
# ]VALIDATORS]
# No Gap can be registered, only subclasses because role is needed
validates :type, exclusion: { in: [Gap.name], message: :invalid }
acts_as_numbered
alias_attribute :label, :number
@@ -65,11 +67,6 @@ class Gap < Ekylibre::Record::Base
self.printed_at ||= Time.zone.now
end
validate do
# No Gap can be registered because role is needed
errors.add(:type, :invalid) if self.class == Gap
end
def printed_on
printed_at.to_date
end
View
@@ -39,17 +39,10 @@ class Identifier < Ekylibre::Record::Base
validates :nature, presence: true
validates :value, presence: true, length: { maximum: 500 }
# ]VALIDATORS]
validates :nature, inclusion: { in: -> (i) { i.net_service_reference.identifiers.map(&:to_s) }, if: -> (i) { i.net_service&.reference } }
delegate :reference, to: :net_service, prefix: true
validate do
if net_service && net_service_reference
unless net_service_reference.identifiers.include?(nature.to_sym)
errors.add(:nature, :inclusion)
end
end
end
def name
(nature ? Nomen::IdentifierNature[nature].human_name : :unknown.tl)
end
@@ -84,6 +84,9 @@ class IncomingPayment < Ekylibre::Record::Base
validates :payer, presence: true
validates :commission_account, presence: { if: :with_commission? }
validates :currency, match: { with: :mode }
validates :mode, match: { with: :deposit, to_invalidate: :deposit_id }, allow_blank: true
acts_as_numbered
acts_as_affairable :payer, dealt_at: :to_bank_at, class_name: 'SaleAffair'
@@ -120,15 +123,6 @@ class IncomingPayment < Ekylibre::Record::Base
true
end
validate do
if mode
errors.add(:currency, :invalid) if currency != mode.currency
if deposit
errors.add(:deposit_id, :invalid) if mode_id != deposit.mode_id
end
end
end
protect do
(deposit && deposit.protected_on_update?) ||
(journal_entry && journal_entry.closed?) ||
View
@@ -47,23 +47,15 @@ class Integration < Ekylibre::Record::Base
converter: proc { |parameters| ActionIntegration::Parameters.cipher(parameters) }
validate do
if ciphered_parameters_changed?
if integration_type
if authentication_mode == :check
check_connection attributes do |c|
c.redirect do
errors.add(:parameters, :check_redirected)
end
c.error do
errors.add(:parameters, :check_errored)
end
end
elsif authentication_mode == :check
list = parameters.keys.map(&:to_s)
unless parameters && !integration_type.parameters.detect { |p| list.include?(p.to_s) }
errors.add(:parameters, :check_errored)
end
end
next unless ciphered_parameters_changed?
next unless integration_type
next unless authentication_mode == :check
check_connection attributes do |c|
c.redirect do
errors.add(:parameters, :check_redirected)
end
c.error do
errors.add(:parameters, :check_errored)
end
end
end
@@ -117,14 +117,13 @@ class InterventionProductParameter < InterventionParameter
end
validate do
if intervention && intervention.procedure
if reference
if reference.handled? && quantity_handler?
errors.add(:quantity_handler, :invalid) unless reference.handler(quantity_handler)
end
elsif reference_name.present?
errors.add(:reference_name, :invalid)
next unless intervention && intervention.procedure
if reference
if reference.handled? && quantity_handler? && !reference.handler(quantity_handler)
errors.add(:quantity_handler, :invalid)
end
elsif reference_name.present?
errors.add(:reference_name, :invalid)
end
true
end
@@ -182,10 +182,6 @@ class JournalEntryItem < Ekylibre::Record::Base
#
validate do
# unless self.updateable?
# errors.add(:number, :closed_entry)
# return
# end
errors.add(:credit, :unvalid_amounts) if debit.nonzero? && credit.nonzero?
errors.add(:real_credit, :unvalid_amounts) if real_debit.nonzero? && real_credit.nonzero?
errors.add(:absolute_credit, :unvalid_amounts) if absolute_debit.nonzero? && absolute_credit.nonzero?
View
@@ -91,6 +91,7 @@ class Loan < Ekylibre::Record::Base
validates :loan_account, :interest_account, presence: true
validates :insurance_account, presence: { if: -> { updateable? && insurance_percentage.present? && insurance_percentage.nonzero? } }
validates :amount, numericality: { greater_than: 0 }
validates :currency, match: { with: :cash }
state_machine :state, initial: :draft do
state :draft
@@ -117,12 +118,6 @@ class Loan < Ekylibre::Record::Base
self.shift_duration ||= 0
end
validate do
if self.currency && cash
errors.add(:currency, :invalid) unless self.currency == cash.currency
end
end
after_save do
generate_repayments
if accountable_repayments_started_on
View
@@ -101,6 +101,8 @@ class Parcel < Ekylibre::Record::Base
validates :transporter, presence: { if: :delivery_mode_transporter? }
validates :storage, presence: { unless: :outgoing? }
validates :transporter, match: { with: :delivery, if: ->(p) { p.delivery&.transporter } }, allow_blank: true
scope :without_transporter, -> { with_delivery_mode(:transporter).where(transporter_id: nil) }
scope :with_delivery, -> { where(with_delivery: true) }
scope :to_deliver, -> { with_delivery.where(delivery_id: nil).where.not(state: :given) }
@@ -151,14 +153,6 @@ class Parcel < Ekylibre::Record::Base
self.pretax_amount = items.sum(:pretax_amount)
end
validate do
if delivery && delivery.transporter && transporter
if delivery.transporter != transporter
errors.add :transporter_id, :invalid
end
end
end
after_initialize do
if new_record? && incoming?
self.address ||= Entity.of_company.default_mail_address
@@ -48,6 +48,7 @@ class PayslipNature < Ekylibre::Record::Base
validates :currency, :journal, presence: true
validates :name, presence: true, uniqueness: true, length: { maximum: 500 }
# ]VALIDATORS]
validates :currency, match: { with: :journal }
selects_among_all
@@ -59,8 +60,4 @@ class PayslipNature < Ekylibre::Record::Base
protect on: :destroy do
payslips.any?
end
validate do
errors.add(:currency, :invalid) if journal && currency.present? && journal.currency != currency
end
end
View
@@ -296,11 +296,13 @@ class Product < Ekylibre::Record::Base
# ]VALIDATORS]
validates :derivative_of, :variety, length: { allow_nil: true, maximum: 120 }
validates :nature, :variant, :name, :uuid, presence: true
validates :nature, match: { with: :variant }
validates_attachment_content_type :picture, content_type: /image/
validate :born_at_in_interventions, if: ->(product) { product.born_at? && product.interventions_used_in.pluck(:started_at).any? }
validate :dead_at_in_interventions, if: ->(product) { product.dead_at? && product.interventions.pluck(:stopped_at).any? }
store :reading_cache, accessors: Nomen::Indicator.all, coder: ReadingsCoder
# [DEPRECATIONS[
@@ -373,9 +375,6 @@ def dead_at_in_interventions
end
validate do
if nature && variant
errors.add(:nature_id, :invalid) if variant.nature_id != nature_id
end
if dead_at && born_at
errors.add(:dead_at, :invalid) if dead_at < born_at
end
Oops, something went wrong.

0 comments on commit 9315832

Please sign in to comment.