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

Extended by:
Card::Set
Defined in:
card/tmpsets/set/mod002-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_modules

Methods included from I18nScope

#mod_name, #scope

Methods included from Loader

#clean_empty_module_from_hash, #clean_empty_modules, #extended, #process_base_modules, #register_set

Methods included from Helpers

#method_missing, #num_set_parts, #pattern_code, #respond_to_missing?, #set_name_parts, #shortname, #underscore

Methods included from Card::Set::AdvancedApi

#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 Basket

#abstract_basket, #add_to_basket, #basket, #unshift_basket

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 'card/tmpsets/set/mod002-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

#autoname?Boolean

Returns:

  • (Boolean)


119
120
121
122
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 119

def autoname?
  name.blank? &&
    (@autoname_rule.nil? ? (@autoname_rule = rule(:autoname).present?) : @autoname_rule)
end

#event: cascade_name_changesObject



86
87
88
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 86

event :cascade_name_changes, :finalize, on: :update, changed: :name do
  each_descendant { |d| d.rename_as_descendant update_referers }
end

#event: expire_old_nameObject

STAGE: store



63
64
65
# File 'card/tmpsets/set/mod002-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



84
85
86
87
88
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 84

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 'card/tmpsets/set/mod002-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: set_autonameObject

STAGE: store



53
54
55
56
57
58
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 53

event :set_autoname, :prepare_to_store, on: :create, when: :autoname? do
  self.name = autoname rule(:autoname)
  rule_card(:autoname).update_column :db_content, name
  pull_from_trash!
  Card.write_to_soft_cache self
end

#event: update_lexiconObject



81
82
83
84
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 81

event :update_lexicon, :finalize, changed: :name, on: :save do
  lexicon_action = @action == :create ? :add : :update
  Card::Lexicon.send lexicon_action, self
end

#event: update_lexicon_on_createObject



64
65
66
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 64

event :update_lexicon_on_create, :finalize, changed: :name, on: :create do
  Card::Lexicon.add self
end

#event: update_lexicon_on_renameObject



68
69
70
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 68

event :update_lexicon_on_rename, :finalize, changed: :name, on: :update do
  Card::Lexicon.update self
end

#event: validate_keyObject



43
44
45
46
47
48
49
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 43

event :validate_key, after: :validate_name, on: :save, when: :no_autoname? do
  if key.empty?
    errors.add :key, tr(:error_blank_key) if errors.empty?
  elsif key != name.key
    errors.add :key, tr(: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
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 30

event :validate_legality_of_name do
  if name.length > 255
    errors.add :name, tr(:error_too_long, length: name.length)
  elsif name.blank?
    errors.add :name, tr(:error_blank_name)
  elsif name_incomplete?
    errors.add :name, tr(:is_incomplete)
  elsif !name.valid?
    errors.add :name, tr(:error_banned_characters, banned: Card::Name.banned_array * " ")
  elsif changing_existing_tag_to_junction?
    errors.add :name, tr(:error_name_tag, name: name)
  end
end

#event: validate_nameObject



10
11
12
13
14
15
# File 'card/tmpsets/set/mod002-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_soft_cache self
  validate_uniqueness_of_name
end

#event: validate_renamingObject



53
54
55
56
57
58
59
# File 'card/tmpsets/set/mod002-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
28
29
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 19

event :validate_uniqueness_of_name, skip: :allowed do
  # validate uniqueness of name

  return unless (existing_id = Card::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, tr(:error_name_exists, name: existing_card.name)
end

#lexObject



72
73
74
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 72

def lex
  simple? ? name : [left_id, right_id]
end

#no_autoname?Boolean

Returns:

  • (Boolean)


115
116
117
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 115

def no_autoname?
  !autoname?
end

#old_lexObject



76
77
78
79
80
81
82
# File 'card/tmpsets/set/mod002-core/all/name_events.rb', line 76

def old_lex
  if (old_left_id = left_id_before_act)
    [old_left_id, right_id_before_act]
  else
    name_before_act
  end
end