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

rubocop:todo Metrics/AbcSize, Metrics/MethodLength



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

def self.included(base) # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
  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)


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

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

#save_after_unmarshallingObject

Hack to be able to build order from pulled data



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

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

#submittable?Boolean

Returns:

  • (Boolean)


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

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

#validating?Boolean

Returns:

  • (Boolean)


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

def validating?
  project && @checking_project
end