Sha256: 5d256ea5984c0c76d45b6620baecac984ae33fa033bd5176df2f8472c2ebfbac

Contents?: true

Size: 1.53 KB

Versions: 6

Compression:

Stored size: 1.53 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)
      joins(:user).where(followable: spaces).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

6 entries across 6 versions & 1 rubygems

Version Path
decidim-core-0.22.0 app/models/decidim/follow.rb
decidim-core-0.21.0 app/models/decidim/follow.rb
decidim-core-0.20.1 app/models/decidim/follow.rb
decidim-core-0.20.0 app/models/decidim/follow.rb
decidim-core-0.19.1 app/models/decidim/follow.rb
decidim-core-0.19.0 app/models/decidim/follow.rb