Sha256: 3c897ea43402695d0da838ed120162c170b4f1390939cdc707ea4b281cc81971

Contents?: true

Size: 1.53 KB

Versions: 21

Compression:

Stored size: 1.53 KB

Contents

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_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

def item_to_insert
  Env.params["insert_item"]
end

# If a card's type and content are updated in the same action, the new module
# will override the old module's events and functions. But this event is only
# on pointers -- other type cards do not have this event,
# Therefore if something is changed from a pointer and its content is changed
# in the same action, this event will be run and will treat the content like
# it' still pointer content.  The "when" clause helps with that (but is a hack)
event :standardize_items, :prepare_to_validate,
      on: :save, changed: :content, when: :still_pointer? do
  self.content = item_names(context: :raw).map do |name|
    "[[#{name}]]"
  end.join "\n"
end

def still_pointer?
  type_id == Card::PointerID
end

stage_method :changed_item_names do
  dropped_item_names + added_item_names
end

stage_method :dropped_item_names do
  old_items = item_names content: db_content_before_act
  old_items - item_names
end

stage_method :added_item_names do
  old_items = item_names content: db_content_before_act
  item_names - old_items
end

stage_method :changed_item_cards do
  item_cards content: changed_item_names
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
card-1.99.1 mod/pointer/set/abstract/02_pointer/events.rb
card-1.99.0 mod/pointer/set/abstract/02_pointer/events.rb
card-1.98.3 mod/pointer/set/abstract/02_pointer/events.rb
card-1.98.2 mod/pointer/set/abstract/02_pointer/events.rb
card-1.98.1 mod/pointer/set/abstract/02_pointer/events.rb
card-1.98.0 mod/pointer/set/abstract/02_pointer/events.rb
card-1.97.0.1 mod/pointer/set/abstract/02_pointer/events.rb
card-1.97.0 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.8 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.7 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.6 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.5 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.4 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.3 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.2 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.1 mod/pointer/set/abstract/02_pointer/events.rb
card-1.96.0 mod/pointer/set/abstract/02_pointer/events.rb
card-1.95.3 mod/pointer/set/abstract/02_pointer/events.rb
card-1.95.2 mod/pointer/set/abstract/02_pointer/events.rb
card-1.95.1 mod/pointer/set/abstract/02_pointer/events.rb