Module: Parse::Core::FieldGuards::ClassMethods
- Defined in:
- lib/parse/model/core/field_guards.rb
Instance Method Summary collapse
-
#guard(*fields, mode: nil) ⇒ Object
Declare one or more guarded fields.
Instance Method Details
#guard(*fields, mode: nil) ⇒ Object
Declare one or more guarded fields. Two call shapes are accepted:
guard :slug, :immutable # positional mode (must be the last arg)
guard :owner, :tags, :master_only # multiple fields, positional mode
guard :slug, mode: :immutable # keyword mode (less ambiguous)
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/parse/model/core/field_guards.rb', line 69 def guard(*fields, mode: nil) # Support `guard :field, :master_only` by treating a trailing # symbol that matches a known mode as the positional mode arg. # Anything else (unknown symbol, no trailing symbol, etc.) falls # through to the validation below with a clear error message. if mode.nil? && fields.last.is_a?(Symbol) && GUARD_MODES.include?(fields.last) mode = fields.pop end raise ArgumentError, "guard requires at least one field name" if fields.empty? unless GUARD_MODES.include?(mode) raise ArgumentError, "guard mode missing or invalid: #{mode.inspect}. " \ "Allowed: #{GUARD_MODES.inspect}. Call as " \ "`guard :field, :master_only` or `guard :field, mode: :master_only`." end new_guards = field_guards.dup fields.each { |f| new_guards[f.to_sym] = mode } self.field_guards = new_guards.freeze # Ensure Parse Server is configured to call our webhook for this # class. Without a before_save route, the webhook is never invoked # and the guard is silently a no-op (a credible misconfiguration # footgun). Register a stub only if no handler exists yet; if the # user later declares `webhook :before_save`, it replaces this stub. ensure_field_guards_webhook! end |