Sha256: 5c926c40d2ac0cf36c8e9e75ae7e47a36f6931d37fc42b2a9cea6a7760e51071

Contents?: true

Size: 1.66 KB

Versions: 10

Compression:

Stored size: 1.66 KB

Contents

# frozen_string_literal: true

module Decidim
  class Follow < ApplicationRecord
    include Decidim::DataPortability

    belongs_to :followable, foreign_key: "decidim_followable_id", foreign_type: "decidim_followable_type", polymorphic: true
    belongs_to :user, foreign_key: "decidim_user_id", class_name: "Decidim::User"

    validates :user, uniqueness: { scope: [:followable] }

    after_create :increase_following_counters
    after_create :increase_followers_counter

    after_destroy :decrease_following_counters
    after_destroy :decrease_followers_counter

    def self.user_collection(user)
      where(decidim_user_id: user.id)
    end

    def self.export_serializer
      Decidim::DataPortabilitySerializers::DataPortabilityFollowSerializer
    end

    def self.user_follower_ids_for_participatory_spaces(spaces)
      # IF the space has questions it's a Consultations
      followables = spaces.flat_map { |space| space.try(:questions) || space }
      joins(:user).where(followable: followables).pluck(:decidim_user_id).uniq
    end

    private

    # rubocop:disable Rails/SkipsModelValidations
    def increase_following_counters
      user.increment!(:following_count)
    end

    def increase_followers_counter
      return unless followable.is_a?(Decidim::UserBaseEntity)

      followable.increment!(:followers_count)
    end

    def decrease_following_counters
      return unless user

      user.decrement!(:following_count)
    end

    def decrease_followers_counter
      return unless followable.is_a?(Decidim::UserBaseEntity)
      return unless user

      followable.decrement!(:followers_count)
    end
    # rubocop:enable Rails/SkipsModelValidations
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
decidim-core-0.23.6 app/models/decidim/follow.rb
decidim-core-0.23.5 app/models/decidim/follow.rb
decidim-core-0.24.0.rc2 app/models/decidim/follow.rb
decidim-core-0.23.4 app/models/decidim/follow.rb
decidim-core-0.24.0.rc1 app/models/decidim/follow.rb
decidim-core-0.23.3 app/models/decidim/follow.rb
decidim-core-0.23.2 app/models/decidim/follow.rb
decidim-core-0.23.1 app/models/decidim/follow.rb
decidim-core-0.23.1.rc1 app/models/decidim/follow.rb
decidim-core-0.23.0 app/models/decidim/follow.rb