Class: Primer::ViewComponents::FilterableTreeViewItemsController
- Inherits:
-
ApplicationController
- Object
- ActionController::Base
- ApplicationController
- Primer::ViewComponents::FilterableTreeViewItemsController
- Defined in:
- app/controllers/primer/view_components/filterable_tree_view_items_controller.rb
Overview
:nodoc: :nocov:
Defined Under Namespace
Classes: TreeNode
Constant Summary collapse
- TREE =
[ branch( id: "hogwarts", label: "Hogwarts School of Witchcraft and Wizardry", children: [ branch( id: "hogwarts-students", label: "Students", children: [ branch( id: "gryffindor", label: "Gryffindor", children: [ leaf(id: "harry-potter", label: "Harry Potter"), leaf(id: "ron-weasley", label: "Ron Weasley"), leaf(id: "hermione-granger", label: "Hermione Granger"), leaf(id: "neville-longbottom", label: "Neville Longbottom"), leaf(id: "ginny-weasley", label: "Ginny Weasley") ] ), branch( id: "slytherin", label: "Slytherin", children: [ leaf(id: "draco-malfoy", label: "Draco Malfoy"), leaf(id: "pansy-parkinson", label: "Pansy Parkinson"), leaf(id: "blaise-zabini", label: "Blaise Zabini") ] ), branch( id: "ravenclaw", label: "Ravenclaw", children: [ leaf(id: "luna-lovegood", label: "Luna Lovegood"), leaf(id: "cho-chang", label: "Cho Chang"), leaf(id: "terry-boot", label: "Terry Boot") ] ), branch( id: "hufflepuff", label: "Hufflepuff", children: [ leaf(id: "cedric-diggory", label: "Cedric Diggory"), leaf(id: "susan-bones", label: "Susan Bones"), leaf(id: "hannah-abbott", label: "Hannah Abbott") ] ) ] ), branch( id: "hogwarts-teachers", label: "Teachers", children: [ leaf(id: "albus-dumbledore", label: "Albus Dumbledore"), leaf(id: "minerva-mcgonagall", label: "Minerva McGonagall"), leaf(id: "severus-snape", label: "Severus Snape"), leaf(id: "rubeus-hagrid", label: "Rubeus Hagrid"), leaf(id: "remus-lupin", label: "Remus Lupin"), leaf(id: "sybill-trelawney", label: "Sybill Trelawney") ] ) ] ), branch( id: "beauxbatons", label: "Beauxbatons Academy of Magic", children: [ branch( id: "beauxbatons-students", label: "Students", children: [ leaf(id: "fleur-delacour", label: "Fleur Delacour"), leaf(id: "gabrielle-delacour", label: "Gabrielle Delacour"), leaf(id: "cecile-fontaine", label: "Cécile Fontaine") ] ), branch( id: "beauxbatons-teachers", label: "Teachers", children: [ leaf(id: "olympe-maxime", label: "Olympe Maxime"), leaf(id: "sylvie-beaumont", label: "Sylvie Beaumont") ] ) ] ), branch( id: "durmstrang", label: "Durmstrang Institute", children: [ branch( id: "durmstrang-students", label: "Students", children: [ leaf(id: "viktor-krum", label: "Viktor Krum"), leaf(id: "dmitri-poliakoff", label: "Dmitri Poliakoff"), leaf(id: "gregor-tarasov", label: "Gregor Tarasov") ] ), branch( id: "durmstrang-teachers", label: "Teachers", children: [ leaf(id: "igor-karkaroff", label: "Igor Karkaroff"), leaf(id: "fyodor-dolohov", label: "Fyodor Dolohov") ] ) ] ) ].freeze
Class Method Summary collapse
-
.branch(id:, label:, children:) ⇒ Object
Build a branch node, computing all_descendant_ids automatically.
-
.leaf(id:, label:) ⇒ Object
Build a leaf node.
Instance Method Summary collapse
Class Method Details
.branch(id:, label:, children:) ⇒ Object
Build a branch node, computing all_descendant_ids automatically
49 50 51 52 |
# File 'app/controllers/primer/view_components/filterable_tree_view_items_controller.rb', line 49 def self.branch(id:, label:, children:) all_ids = children.flat_map { |c| [c.id] + c.all_descendant_ids } TreeNode.new(id: id, label: label, children: children, all_descendant_ids: all_ids) end |
.leaf(id:, label:) ⇒ Object
Build a leaf node
44 45 46 |
# File 'app/controllers/primer/view_components/filterable_tree_view_items_controller.rb', line 44 def self.leaf(id:, label:) TreeNode.new(id: id, label: label, children: [], all_descendant_ids: []) end |
Instance Method Details
#async_form_tree ⇒ Object
177 178 179 180 181 182 183 184 185 186 187 188 189 |
# File 'app/controllers/primer/view_components/filterable_tree_view_items_controller.rb', line 177 def async_form_tree query = params[:query].to_s.strip name = params[:name].to_s.presence || "characters" nodes = TREE.filter_map { |node| node.filter(query) } builder = ActionView::Helpers::FormBuilder.new("", nil, view_context, {}) render locals: { nodes: nodes, query: query, name: name, builder: builder } end |
#index ⇒ Object
165 166 167 168 169 170 171 172 173 174 175 |
# File 'app/controllers/primer/view_components/filterable_tree_view_items_controller.rb', line 165 def index query = params[:query].to_s.strip select_variant = (params[:select_variant].presence || "multiple").to_sym nodes = TREE.filter_map { |node| node.filter(query) } render locals: { nodes: nodes, query: query, select_variant: select_variant } end |