Sha256: 8487d31bfc1efbde05a87996e39b56e31f7329d3d6490587ed65df561d839c1c

Contents?: true

Size: 1.85 KB

Versions: 6

Compression:

Stored size: 1.85 KB

Contents

# frozen_string_literal: true

require "cell/partial"

module Decidim
  module EnhancedTextwork
    # This cell renders a paragraphs picker.
    class ParagraphsPickerCell < Decidim::ViewModel
      MAX_PARAGRAPHS = 1000

      def show
        if filtered?
          render :paragraphs
        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_paragraphs?
        @more_paragraphs ||= more_paragraphs_count.positive?
      end

      def more_paragraphs_count
        @more_paragraphs_count ||= paragraphs_count - MAX_PARAGRAPHS
      end

      def paragraphs_count
        @paragraphs_count ||= filtered_paragraphs.count
      end

      def decorated_paragraphs
        filtered_paragraphs.limit(MAX_PARAGRAPHS).each do |paragraph|
          yield Decidim::EnhancedTextwork::ParagraphPresenter.new(paragraph)
        end
      end

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

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

      def paragraphs_collection_name
        Decidim::EnhancedTextwork::Paragraph.model_name.human(count: 2)
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
decidim-enhanced_textwork-1.0.5 app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb
decidim-enhanced_textwork-1.0.4 app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb
decidim-enhanced_textwork-1.0.3 app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb
decidim-enhanced_textwork-1.0.2 app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb
decidim-enhanced_textwork-1.0.1 app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb
decidim-enhanced_textwork-1.0.0 app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb