Module: Card::Set::Abstract::List::Events

Extended by:
Card::Set
Defined in:
platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb

Overview

Set: Abstract (List, Events)

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



7
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 7

def self.source_location; "/Users/ethan/dev/decko/gem/mod/list/set/abstract/01_list/events.rb"; end

Instance Method Details

#added_item_cardsObject



88
89
90
91
92
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 88

def added_item_cards
  return item_cards unless db_content_before_act

  all_item_cards item_names: added_item_names
end

#added_item_namesObject



66
67
68
69
70
71
72
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 66

def added_item_names
  return [] if trash
  return item_names unless (old_content = db_content_before_act)

  old_items = item_names content: old_content
  item_names - old_items
end

#changed_item_cardsObject

TODO: refactor. many of the above could be written more elegantly with improved handling of :content in item_names. If content is nil here, we would expect an empty set of cards, but in fact we get items based on self.content.



78
79
80
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 78

def changed_item_cards
  dropped_item_cards + added_item_cards
end

#changed_item_namesObject



54
55
56
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 54

def changed_item_names
  dropped_item_names + added_item_names
end

#dropped_item_cardsObject



82
83
84
85
86
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 82

def dropped_item_cards
  return [] unless db_content_before_act

  all_item_cards item_names: dropped_item_names
end

#dropped_item_namesObject



58
59
60
61
62
63
64
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 58

def dropped_item_names
  return item_names if trash
  return [] unless (old_content = db_content_before_act)

  old_items = item_names content: old_content
  old_items - item_names
end

#event: add_and_drop_itemsObject



8
9
10
11
12
13
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 8

event :add_and_drop_items, :prepare_to_validate, on: :save do
  adds = Env.params["add_item"]
  drops = Env.params["drop_item"]
  Array.wrap(adds).each { |i| add_item i } if adds
  Array.wrap(drops).each { |i| drop_item i } if drops
end

#event: insert_item_eventObject



15
16
17
18
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 15

event :insert_item_event, :prepare_to_validate, on: :save, when: :item_to_insert do
  index = Env.params["item_index"] || 0
  insert_item index.to_i, item_to_insert
end

#event: validate_item_typeObject



20
21
22
23
24
25
26
27
28
29
30
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 20

event :validate_item_type, :validate, on: :save, when: :validate_item_type? do
  ok_types = Array.wrap(ok_item_types).map(&:codename)
  item_cards.each do |item|
    next if ok_types.include? item.type_code

    errors.add :content, t(:list_invalid_item_type,
                           item: item.name,
                           type: item.type,
                           allowed_types: ok_types.map(&:cardname).to_sentence)
  end
end

#event: validate_item_uniquenessObject

At present this may never generate an error, because the uniqueness is enforced by #standardize_items. It’s possible we may want to separate the policy about whether duplicates are allowed and the policy about whether we autocorrect vs. raise an error?



36
37
38
39
40
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 36

event :validate_item_uniqueness, :validate, on: :save, when: :unique_items? do
  return unless (dupes = duplicate_item_names)&.present?

  errors.add :content, t(:list_duplicate_items_names, duplicates: dupes.to_sentence)
end

#item_to_insertObject



50
51
52
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 50

def item_to_insert
  Env.params["insert_item"]
end

#ok_item_typesObject



42
43
44
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 42

def ok_item_types
  nil
end

#validate_item_type?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'platypus/tmp/set/gem-defaults/mod005-list/abstract/01_list/events.rb', line 46

def validate_item_type?
  ok_item_types.present?
end