class Card::Set::RequiredField

Attributes

field[R]
options[R]
parent_set[R]

Public Class Methods

new(parent_set, field, options={}) click to toggle source
# File lib/card/set/required_field.rb, line 6
def initialize parent_set, field, options={}
  @parent_set = parent_set
  @field = field
  @options = options
end

Public Instance Methods

add() click to toggle source
# File lib/card/set/required_field.rb, line 12
def add
  create_parent_event
  return unless field_events?

  define_field_test
  create_field_events
end
field_event_name(action) click to toggle source
# File lib/card/set/required_field.rb, line 24
def field_event_name action
  [field, "required_by", parent_set.underscored_name, "on", action]
    .join("__").to_sym
end
parent_event_name() click to toggle source
# File lib/card/set/required_field.rb, line 20
def parent_event_name
  [parent_set.underscored_name, "requires_field", field].join("__").to_sym
end

Private Instance Methods

create_field_event(action, action_verb, allow_test, extra_options={}) click to toggle source
# File lib/card/set/required_field.rb, line 76
def create_field_event action, action_verb, allow_test, extra_options={}
  event_name = field_event_name action
  event_options = field_event_options action, extra_options
  field_set.class_exec(self) do |required|
    event event_name, :validate, event_options  do
      return if send allow_test

      errors.add required.field, "can't be #{action_verb}; required field"
    end
  end
end
create_field_events() click to toggle source
# File lib/card/set/required_field.rb, line 65
def create_field_events
  create_field_event :delete, "deleted", :trashed_left?
  create_field_event :update, "renamed", :same_field?, changing: :name
end
create_parent_event() click to toggle source
# File lib/card/set/required_field.rb, line 88
def create_parent_event
  parent_set.class_exec(self) do |required|
    event required.parent_event_name, :validate,
          required.options.merge(on: :create) do
      return if field?(required.field) || left&.type_id == Card::CardtypeID

      # Without the Cardtype exemption, we can get errors on type plus right sets
      # eg, if right/account has require_field :email, then when we're trying
      # to create User+*account+*type_plus right rules, it fails, because
      # User+*account doesn't have an +email field.
      #
      # Need a better solution so we can require fields on cardtype+X cards, too.

      errors.add required.field, "required" # LOCALIZE
    end
  end
end
define_field_test() click to toggle source
# File lib/card/set/required_field.rb, line 31
def define_field_test
  return unless (test = event_test)

  method_name = field_test_name
  field_set.class_exec do
    define_method method_name do
      left.send test
    end
  end
end
ensure_field_set(parent_set, field) click to toggle source
# File lib/card/set/required_field.rb, line 106
def ensure_field_set parent_set, field
  field_set = parent_set.ensure_set { field_set_name parent_set, field }
  Card::Set.register_set field_set
  field_set
end
event_test() click to toggle source
# File lib/card/set/required_field.rb, line 48
def event_test
  return @event_test unless @event_test.nil?

  test = options[:when]
  @event_test = test.is_a?(Symbol) ? test : false
end
field_event_options(action, extra_options) click to toggle source
# File lib/card/set/required_field.rb, line 70
def field_event_options action, extra_options
  options = { on: action }.merge extra_options
  options[:when] = field_test_name if event_test
  options
end
field_events?() click to toggle source

for now, we only support field events on type sets. That’s because only type sets have fields that are set-addressable (via type plus right sets)

# File lib/card/set/required_field.rb, line 61
def field_events?
  parent_set.type_set?
end
field_set() click to toggle source
# File lib/card/set/required_field.rb, line 55
def field_set
  @field_set ||= ensure_field_set parent_set, field
end
field_set_name(parent_set, field) click to toggle source
# File lib/card/set/required_field.rb, line 112
def field_set_name parent_set, field
  "TypePlusRight::#{parent_set.set_name_parts.last}::#{field.to_s.capitalize}"
end
field_test_name() click to toggle source
# File lib/card/set/required_field.rb, line 42
def field_test_name
  return unless event_test

  "_when_left_#{event_test}".to_sym
end