Sha256: 02970835c1c65c06b0039653842ca7a546ab6622f524f15e57b0527d23bde60b

Contents?: true

Size: 1.86 KB

Versions: 10

Compression:

Stored size: 1.86 KB

Contents

basket[:tasks][:empty_trash] = {
  mod: :core,
  irreversible: true,
  execute_policy: -> { Cardio::Utils.empty_trash }
}

def trash?
  trash
end

def delete args={}
  add_to_trash args do |delete_args|
    update delete_args
  end
end

def delete! args={}
  add_to_trash args do |delete_args|
    update! delete_args
  end
end

event :manage_trash, :prepare_to_store, on: :create do
  pull_from_trash!
  self.trash = false
  true
end

event :validate_delete, :validate, on: :delete do
  unless codename.blank?
    errors.add :delete, t(:core_error_system_card, name: name, codename: codename)
  end

  undeletable_all_rules_tags =
    %w[default style layout create read update delete]
  # FIXME: HACK! should be configured in the rule

  if compound? && left&.codename == :all &&
     undeletable_all_rules_tags.member?(right.codename.to_s)
    errors.add :delete, t(:core_error_indestructible, name: name)
  end

  errors.add :delete, t(:core_error_user_edits, name: name) if account && has_edits?
end

event :delete_children, after: :validate_delete, on: :delete do
  return if errors.any?

  each_child do |child|
    child.include_set_modules
    delete_as_subcard child
    # next if child.valid?
    # child.errors.each do |field, message|
    #   errors.add field, "can't delete #{child.name}: #{message}"
    # end
  end
end

def pull_from_trash!
  return unless (id = Card::Lexicon.id key) # name is already known
  return unless (trashed_card = Card.where(id: id).take)&.trash

  # confirm name is actually in trash

  db_attributes["id"] = trashed_card.db_attributes["id"]
  # id_in_database returns existing card id

  @from_trash = true
  @new_record = false
end

def add_to_trash args
  yield args.merge(trash: true) unless new_card?
end

def db_attributes
  send(:mutations_from_database).send :attributes
end

def delete_as_subcard subcard
  subcard.trash = true
  subcard subcard
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
card-1.107.0 mod/core/set/all/trash.rb
card-1.106.0 mod/core/set/all/trash.rb
card-1.105.6 mod/core/set/all/trash.rb
card-1.105.5 mod/core/set/all/trash.rb
card-1.105.4 mod/core/set/all/trash.rb
card-1.105.3 mod/core/set/all/trash.rb
card-1.105.2.pre1 mod/core/set/all/trash.rb
card-1.105.2 mod/core/set/all/trash.rb
card-1.105.1 mod/core/set/all/trash.rb
card-1.105.0 mod/core/set/all/trash.rb