Module: Submission::ProjectValidation

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

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
# 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|
      record.errors.add(:base, "Project #{project.name} is not suitable for submission: #{project.errors.full_messages.join('; ')}") unless project.submittable?
    end

    before_create :confirm_validity!
  end
end

Instance Method Details

#checking_project?Boolean


24
25
26
# File 'app/models/submission/project_validation.rb', line 24

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

#complete_buildingObject


19
20
21
22
# File 'app/models/submission/project_validation.rb', line 19

def complete_building
  check_project_details!
  super
end

#save_after_unmarshallingObject

Hack to be able to build order from pulled data


57
58
59
60
61
# File 'app/models/submission/project_validation.rb', line 57

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

#submittable?Boolean


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

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

#validating?Boolean


28
29
30
# File 'app/models/submission/project_validation.rb', line 28

def validating?
  project && @checking_project
end