Module: Submission::ProjectValidation

Included in:
Order
Defined in:
app/models/submission/project_validation.rb

Overview

rubocop:todo Style/Documentation

Constant Summary collapse

Error =
Class.new(StandardError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object


2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# File 'app/models/submission/project_validation.rb', line 2

def self.included(base)
  base.class_eval do
    # We probably want to move this validation
    validates_each(:project, if: :checking_project?) do |record, _attr, project|
      record.errors.add(:base, "Project #{project.name} is not approved")                 unless project.approved?
      record.errors.add(:base, "Project #{project.name} is not active")                   unless project.active?
      record.errors.add(:base, "Project #{project.name} does not have a budget division") unless project.actionable?
    end

    validates_each(:project, if: :validating?) do |record, _attr, project|
      unless project.submittable?
        record.errors.add(:base,
                          "Project #{project.name} is not suitable for submission: #{project.errors.full_messages.join('; ')}")
      end
    end

    before_create :confirm_validity!
  end
end

Instance Method Details

#checking_project?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'app/models/submission/project_validation.rb', line 22

def checking_project?
  validating? && project.enforce_quotas?
end

#save_after_unmarshallingObject

Hack to be able to build order from pulled data


55
56
57
58
59
# File 'app/models/submission/project_validation.rb', line 55

def save_after_unmarshalling
  @saving_without_validation = true
  save(validate: false)
  @saving_without_validation = false
end

#submittable?Boolean

Returns:

  • (Boolean)

44
45
46
47
48
49
50
51
# File 'app/models/submission/project_validation.rb', line 44

def submittable?
  begin
    @checking_project = true
    valid?
  ensure
    @checking_project = false
  end
end

#validating?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'app/models/submission/project_validation.rb', line 26

def validating?
  project && @checking_project
end