Module: Card::Set::All::NameEvents

Extended by:
Card::Set
Defined in:
platypus/tmp/set/gem-card/mod001-core/all/name_events.rb

Overview

STAGE: validate

Constant Summary

Constants included from Helpers

Helpers::SET_PATTERN_TEST_REGEXP

Constants included from Event::Api

Event::Api::OPTIONS

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Card::Set

reset

Methods included from I18nScope

#mod_name, #scope

Methods included from Registrar

#extended, #finalize_load, #process_base_modules, #register_set

Methods included from Helpers

#format_module, #format_modules, #method_missing, #modules, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscored_name

Methods included from Card::Set::AdvancedApi

#assign_type, #attachment, #define_set_from_error, #ensure_set, #setting_opts, #stage_method

Methods included from Format

#before, #format, layout_method_name, #view, view_method_name, view_setting_method_name, wrapper_method_name

Methods included from Inheritance

#include_set, #include_set_formats

Methods included from Trait

#card_accessor, #card_reader, #card_writer, #require_field

Methods included from Event::Api

#event

Class Method Details

.source_locationObject



8
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 8

def self.source_location; "/Users/ethan/dev/decko/gem/card/mod/core/set/all/name_events.rb"; end

Instance Method Details

#event: expire_old_nameObject

STAGE: store



63
64
65
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 63

event :expire_old_name, :store, changed: :name, on: :update do
  Director.expirees << name_before_act
end

#event: prepare_left_and_rightObject



74
75
76
77
78
79
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 74

event :prepare_left_and_right, :store, changed: :name, on: :save do
  return if name.simple?

  prepare_side :left
  prepare_side :right
end

#event: rename_in_trashObject



67
68
69
70
71
72
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 67

event :rename_in_trash, after: :expire_old_name, on: :update do
  existing_card = Card.find_by_key_and_trash name.key, true
  return if !existing_card || existing_card == self

  existing_card.rename_as_trash_obstacle
end

#event: update_lexiconObject

as soon as the name has an id, we have to update the lexicon. (the after_store callbacks are called right after the storage)



83
84
85
86
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 83

event :update_lexicon, :store, changed: :name do
  lexicon_action = @action == :create ? :add : @action
  director.after_store { |card| Lexicon.send lexicon_action, card }
end

#event: validate_keyObject



45
46
47
48
49
50
51
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 45

event :validate_key, after: :validate_name, on: :save, when: :no_autoname? do
  if key.empty?
    errors.add :key, t(:core_error_blank_key) if errors.empty?
  elsif key != name.key
    errors.add :key, t(:core_error_wrong_key, key: key, name: name)
  end
end

#event: validate_legality_of_nameObject

called by validate_name



30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 30

event :validate_legality_of_name do
  if name.length > 255
    errors.add :name, t(:core_error_too_long, length: name.length)
  elsif name.blank?
    errors.add :name, t(:core_error_blank_name)
  elsif name_incomplete?
    errors.add :name, t(:core_is_incomplete)
  elsif !name.valid?
    errors.add :name, t(:core_error_banned_characters,
                        banned: Card::Name.banned_array * " ")
  elsif changing_existing_tag_to_compound?
    errors.add :name, t(:core_error_name_tag, name: name)
  end
end

#event: validate_nameObject



10
11
12
13
14
15
16
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 10

event :validate_name, :validate, on: :save, changed: :name, when: :no_autoname? do
  validate_legality_of_name
  return if errors.any?

  Card.write_to_temp_cache self
  validate_uniqueness_of_name
end

#event: validate_renamingObject



53
54
55
56
57
58
59
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 53

event :validate_renaming, :validate, on: :update, changed: :name, skip: :allowed do
  return if name_before_act&.to_name == name # just changing to new variant

  errors.add :content, t(:core_cannot_change_content) if content_is_changing?
  errors.add :type, t(:core_cannot_change_type) if type_is_changing?
  detect_illegal_compound_names
end

#event: validate_uniqueness_of_nameObject

called by validate_name



19
20
21
22
23
24
25
26
27
# File 'platypus/tmp/set/gem-card/mod001-core/all/name_events.rb', line 19

event :validate_uniqueness_of_name, skip: :allowed do
  return unless (existing_id = Lexicon.id key) && existing_id != id
  # The above is a fast check but cannot detect if card is in trash

  # TODO: perform the following as a remote-only fetch (not yet supported)
  return unless (existing_card = Card.where(id: existing_id, trash: false).take)

  errors.add :name, t(:core_error_name_exists, name: existing_card.name)
end