Class: Decidim::EnhancedTextwork::ParagraphsPickerCell

Inherits:
ViewModel
  • Object
show all
Defined in:
app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb

Overview

This cell renders a paragraphs picker.

Constant Summary collapse

MAX_PARAGRAPHS =
1000

Instance Method Summary collapse

Instance Method Details

#decorated_paragraphsObject



45
46
47
48
49
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 45

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

#filtered?Boolean

Returns:

  • (Boolean)


21
22
23
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 21

def filtered?
  !search_text.nil?
end

#filtered_paragraphsObject



51
52
53
54
55
56
57
58
59
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 51

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

#more_paragraphs?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 33

def more_paragraphs?
  @more_paragraphs ||= more_paragraphs_count.positive?
end

#more_paragraphs_countObject



37
38
39
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 37

def more_paragraphs_count
  @more_paragraphs_count ||= paragraphs_count - MAX_PARAGRAPHS
end

#paragraphsObject



61
62
63
64
65
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 61

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

#paragraphs_collection_nameObject



67
68
69
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 67

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

#paragraphs_countObject



41
42
43
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 41

def paragraphs_count
  @paragraphs_count ||= filtered_paragraphs.count
end

#picker_pathObject



25
26
27
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 25

def picker_path
  request.path
end

#search_textObject



29
30
31
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 29

def search_text
  params[:q]
end

#showObject



11
12
13
14
15
16
17
# File 'app/cells/decidim/enhanced_textwork/paragraphs_picker_cell.rb', line 11

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