Module: Organizations::MetadataFlags

Included in:
Membership, Organization
Defined in:
lib/organizations/metadata_flags.rb

Overview

Typed boolean accessors over a json(b) metadata bag, with a DEFAULT for missing keys — the pattern every host grows around the gem's metadata channel ("show this membership on the profile?", "receive digests?").

Why not store_accessor: store_accessor gives raw reads — a nil (never set) is indistinguishable from false, so hosts hand-roll the same value.nil? ? default : Boolean.cast(value) predicate for every flag (one production host had three identical copies). metadata_flag bakes the default + cast in.

Organizations::Organization and Organizations::Membership are already extended with this — call the macro from your extension concern:

ActiveSupport.on_load(:organizations_membership) do
 :show_on_profile, default: true
 :show_on_leaderboard, default: true
end

membership.show_on_profile?          # => true (unset ⇒ the default)
membership.show_on_profile = false   # writes into metadata
membership.toggle_show_on_profile!   # flip + save!

⚠️ Don't ALSO declare the same key via store_accessor — you'd get two writers with different semantics for one key. Pick one mechanism per key.

Examples:

A flag over a different bag column

 :beta_features, default: false, column: :settings

Instance Method Summary collapse

Instance Method Details

#metadata_flag(name, default:, column: :metadata) ⇒ Object

Parameters:

  • name (Symbol)

    flag name (becomes name?, name=, toggle_name!)

  • default (Boolean)

    value when the key is absent from the bag

  • column (Symbol) (defaults to: :metadata)

    the json(b) attribute holding the bag



35
36
37
38
39
# File 'lib/organizations/metadata_flags.rb', line 35

def (name, default:, column: :metadata)
  (name, default, column)
  (name, column)
  (name)
end