Module: ActiveScaffold::UnsavedRecord

Defined in:
lib/active_scaffold/extensions/unsaved_record.rb

Overview

a simple (manual) unsaved? flag and method. at least it automatically reverts after a save!

Instance Method Summary collapse

Instance Method Details

#keeping_errorsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_scaffold/extensions/unsaved_record.rb', line 18

def keeping_errors
  old_errors = errors.dup if errors.present?
  result = yield
  old_errors&.each do |e|
    if e.is_a?(String) || e.is_a?(Symbol)
      # Rails <6.1 errors API.
      old_errors[e].each { |msg| errors.add(e, msg) unless errors.added?(e, msg) }
    else
      # Rails >=6.1 errors API (https://code.lulalala.com/2020/0531-1013.html).
      errors.add(e.attribute, e.message) unless errors.added?(e.attribute, e.message)
    end
  end
  result && old_errors.blank?
end

#saveObject

automatically unsets the unsaved flag



14
15
16
# File 'lib/active_scaffold/extensions/unsaved_record.rb', line 14

def save(**)
  super.tap { self.unsaved = false }
end

#unsaved=(val) ⇒ Object

acts like a dirty? flag, manually thrown during update_record_from_params.



4
5
6
# File 'lib/active_scaffold/extensions/unsaved_record.rb', line 4

def unsaved=(val)
  @unsaved = val ? true : false
end

#unsaved?Boolean

whether the unsaved? flag has been thrown

Returns:

  • (Boolean)


9
10
11
# File 'lib/active_scaffold/extensions/unsaved_record.rb', line 9

def unsaved?
  @unsaved
end