Sha256: dd907fb8f66713ec4cf9c3a7d0a627e4d4732a9ba9ea390dc4ddc2a380f45e5a

Contents?: true

Size: 1.99 KB

Versions: 3

Compression:

Stored size: 1.99 KB

Contents

# frozen_string_literal: true
module SocialNetworking
  module Concerns
    module ProfilePage
      # Displays feed data for profile page.
      class Feed
        attr_reader :participant_id, :page
        SHARED_ITEM_PAGE_SIZE = 10

        def initialize(participant_id:, page:)
          @participant_id = participant_id
          @page = page.try(:to_i) || 0
        end

        def page_items
          if feed_items.size >= start_index
            sorted_feed_items[
              start_index..(start_index + SHARED_ITEM_PAGE_SIZE - 1)
            ]
          else
            []
          end
        end

        private

        def feed_items
          on_the_mind_statements + nudges + shared_items
        end

        def nudges
          Serializers::NudgeSerializer
            .from_collection(
              Nudge
                .where(initiator_id: participant_id)
                .order(created_at: :desc)
                .limit(SHARED_ITEM_PAGE_SIZE * (page + 1))
                .includes(:comments)
            )
        end

        def on_the_mind_statements
          Serializers::OnTheMindStatementSerializer
            .from_collection(
              OnTheMindStatement
                .where(participant_id: participant_id)
                .order(created_at: :desc)
                .limit(SHARED_ITEM_PAGE_SIZE * (page + 1))
                .includes(:comments, :likes)
            )
        end

        def shared_items
          Serializers::SharedItemSerializer
            .from_collection(
              SharedItem
                .includes(:item, :comments)
                .joins(:participant)
                .where(participant_id: participant_id)
                .order(created_at: :desc)
                .limit(SHARED_ITEM_PAGE_SIZE * (page + 1))
            )
        end

        def sorted_feed_items
          feed_items.sort_by { |item| item[:createdAtRaw] }.reverse!
        end

        def start_index
          page * SHARED_ITEM_PAGE_SIZE
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
social_networking-0.13.3 app/controllers/social_networking/concerns/profile_page/feed.rb
social_networking-0.13.2 app/controllers/social_networking/concerns/profile_page/feed.rb
social_networking-0.13.1 app/controllers/social_networking/concerns/profile_page/feed.rb