Sha256: cce8bb413395df4b80c79533a1f255a31fcced8f765abf56dc96fb7603434bce

Contents?: true

Size: 1.79 KB

Versions: 12

Compression:

Stored size: 1.79 KB

Contents

# frozen_string_literal: true

require "cell/partial"

module Decidim
  module Proposals
    # This cell renders a proposals picker.
    class ProposalsPickerCell < Decidim::ViewModel
      MAX_PROPOSALS = 1000

      def show
        if filtered?
          render :proposals
        else
          render
        end
      end

      alias component model

      def filtered?
        !search_text.nil?
      end

      def picker_path
        request.path
      end

      def search_text
        params[:q]
      end

      def more_proposals?
        @more_proposals ||= more_proposals_count.positive?
      end

      def more_proposals_count
        @more_proposals_count ||= proposals_count - MAX_PROPOSALS
      end

      def proposals_count
        @proposals_count ||= filtered_proposals.count
      end

      def decorated_proposals
        filtered_proposals.limit(MAX_PROPOSALS).each do |proposal|
          yield Decidim::Proposals::ProposalPresenter.new(proposal)
        end
      end

      def filtered_proposals
        @filtered_proposals ||= if filtered?
                                  proposals.where("title ILIKE ?", "%#{search_text}%")
                                           .or(proposals.where("reference ILIKE ?", "%#{search_text}%"))
                                           .or(proposals.where("id::text ILIKE ?", "%#{search_text}%"))
                                else
                                  proposals
                                end
      end

      def proposals
        @proposals ||= Decidim.find_resource_manifest(:proposals).try(:resource_scope, component)
                       &.published
                       &.order(id: :asc)
      end

      def proposals_collection_name
        Decidim::Proposals::Proposal.model_name.human(count: 2)
      end
    end
  end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
decidim-proposals-0.23.5 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.24.1 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.24.0 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.24.0.rc2 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.23.4 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.24.0.rc1 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.23.3 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.23.2 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.23.1 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.23.1.rc1 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.23.0 app/cells/decidim/proposals/proposals_picker_cell.rb
decidim-proposals-0.22.0 app/cells/decidim/proposals/proposals_picker_cell.rb