class Organization < ApplicationRecord

  include Auditable
  include Concealable
  extend FriendlyId
  friendly_id :name, use: :slugged
  strip_attributes collapse_spaces: true
  has_many :event_groups, dependent: :destroy
  has_many :stewardships, dependent: :destroy
  has_many :stewards, through: :stewardships, source: :user
  has_many :events, through: :event_groups

  scope :with_visible_event_count, -> do
    left_joins(event_groups: :events).select('organizations.*, COUNT(DISTINCT events) AS event_count')
        .where(event_groups: {concealed: false}).group('organizations.id, event_groups.organization_id')
  end

  validates_presence_of :name
  validates_uniqueness_of :name, case_sensitive: false

  def to_s
    slug
  end

  def add_stewardship(user)
    stewards << user
  end

  def remove_stewardship(user)
    stewards.delete(user)
  end

  def should_be_concealed?
    !event_groups.visible.present?
  end
end

class Organization < ApplicationRecord

  include Auditable
  include Concealable
  extend FriendlyId
  friendly_id :name, use: :slugged
  strip_attributes collapse_spaces: true
  has_many :event_groups, dependent: :destroy
  has_many :stewardships, dependent: :destroy
  has_many :stewards, through: :stewardships, source: :user

  scope :with_visible_event_count, -> do
    left_joins(event_groups: :events).select('organizations.*, COUNT(DISTINCT events) AS event_count')
        .where(event_groups: {concealed: false}).group('organizations.id, event_groups.organization_id')
  end

  validates_presence_of :name
  validates_uniqueness_of :name, case_sensitive: false

  def to_s
    slug
  end

  def events
    Event.where(event_group_id: event_groups.ids)
  end

  def add_stewardship(user)
    stewards << user
  end

  def remove_stewardship(user)
    stewards.delete(user)
  end

  def should_be_concealed?
    !event_groups.visible.present?
  end
end

Revert Organization / Event relationship.
Revert Organization / Event relationship.
