Class: Lcms::Engine::Filterbar

Inherits:
Object
  • Object
show all
Defined in:
app/entities/lcms/engine/filterbar.rb

Constant Summary collapse

GRADES =
%w(pk k 1 2 3 4 5 6 7 8 9 10 11 12).freeze
FACETS =
%w(grade module unit lesson multimedia other).freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Filterbar

Returns a new instance of Filterbar.



11
12
13
# File 'app/entities/lcms/engine/filterbar.rb', line 11

def initialize(params)
  @params = params
end

Instance Attribute Details

#paramsObject (readonly)

Returns the value of attribute params.



6
7
8
# File 'app/entities/lcms/engine/filterbar.rb', line 6

def params
  @params
end

Instance Method Details

#facetsObject



39
40
41
# File 'app/entities/lcms/engine/filterbar.rb', line 39

def facets
  @facets ||= split(params[:facets]) & FACETS
end

#gradesObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'app/entities/lcms/engine/filterbar.rb', line 15

def grades
  @grades ||= begin
    names = untranslated_grades

    names.map! do |name|
      name = name.to_s
      if name.casecmp('K').zero?
        name = 'kindergarten'
      elsif name.casecmp('PK').zero?
        name = 'prekindergarten'
      elsif !name.start_with?('grade')
        name = "grade #{name}"
      end
      name
    end

    names
  end
end

#propsObject

props used for building the Filterbar React component



66
67
68
69
70
71
72
73
74
75
# File 'app/entities/lcms/engine/filterbar.rb', line 66

def props
  {
    filterbar: {
      subjects: subjects,
      grades: untranslated_grades,
      facets: facets,
      search_term: search_term
    }
  }
end

#search_facetsObject



58
59
60
61
62
63
# File 'app/entities/lcms/engine/filterbar.rb', line 58

def search_facets
  search_facets = facets
  search_facets.push('video', 'podcast') if search_facets.delete('multimedia')
  search_facets.push('text_set', 'quick_reference_guide') if search_facets.delete('other')
  search_facets
end

#search_paramsObject



47
48
49
50
51
52
53
54
55
56
# File 'app/entities/lcms/engine/filterbar.rb', line 47

def search_params
  options = pagination.params(strict: true)

  # handle filters
  options[:doc_type] = search_facets if facets.present?
  options[:subject] = subjects.first if subjects.present?
  options[:grade] = grades if grades.present?

  options
end

#search_termObject



43
44
45
# File 'app/entities/lcms/engine/filterbar.rb', line 43

def search_term
  @search_term ||= params[:search_term].presence
end

#subjectsObject



35
36
37
# File 'app/entities/lcms/engine/filterbar.rb', line 35

def subjects
  @subjects ||= split(params[:subjects]) & Resource::SUBJECTS
end