Module: NitroRails::NitroCollectionHelper
- Defined in:
- app/helpers/nitro_rails/nitro_collection_helper.rb
Instance Method Summary collapse
- #chain_methods(*array) ⇒ Object
- #is_active_collection?(collection) ⇒ Boolean
- #new_nitro_collection(*resources_or_path, **options, &block) ⇒ Object
- #nitro_collection(parent, resource_method, **options, &block) ⇒ Object
- #nitro_collection_tag(tag = :ul, parent, resource_method, **options, &block) ⇒ Object
Instance Method Details
#chain_methods(*array) ⇒ Object
33 34 35 |
# File 'app/helpers/nitro_rails/nitro_collection_helper.rb', line 33 def chain_methods(*array) array.inject { |obj, method| obj.public_send(method) } end |
#is_active_collection?(collection) ⇒ Boolean
42 43 44 |
# File 'app/helpers/nitro_rails/nitro_collection_helper.rb', line 42 def is_active_collection?(collection) collection.is_a?(ActiveRecord::Associations::CollectionProxy || ActiveRecord::Relation) end |
#new_nitro_collection(*resources_or_path, **options, &block) ⇒ Object
37 38 39 40 |
# File 'app/helpers/nitro_rails/nitro_collection_helper.rb', line 37 def new_nitro_collection(*resources_or_path, **, &block) resources = is_active_collection?(resources_or_path.first) ? resources_or_path.first : chain_methods(*resources_or_path) NitroCollection.new(resources, view: self, **).render_content(&block) end |
#nitro_collection(parent, resource_method, **options, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'app/helpers/nitro_rails/nitro_collection_helper.rb', line 13 def nitro_collection(parent, resource_method, **, &block) content_tag(:div, data: { controller: "nitro-collection", nitro_collection_target: "container", nitro_collection_id: "#{parent.nitro_id}:#{resource_method}" }) do nitro_collection_tag(:ul, parent, resource_method, data: { nitro_collection_target: :collection }, **) do resources = parent.send(resource_method) resources = resources.order(.delete(:order)) if [:order] resources = resources.where(.delete(:where)) if [:where] resources = resources.take(.delete(:take)) if [:take] resources.map do |resource| yield resource end end + content_tag(:template, class: "nitro-collection-template", style: "display: none;", data: { nitro_collection_target: :template }) do yield parent.send(resource_method).template end end end |
#nitro_collection_tag(tag = :ul, parent, resource_method, **options, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 |
# File 'app/helpers/nitro_rails/nitro_collection_helper.rb', line 3 def nitro_collection_tag(tag = :ul, parent, resource_method, **, &block) () do || .default(:nitro_id, parent.nitro_id(resource_method)) .concat(:class, classify(resource_method)) .concat(:class, classify(parent.type, resource_method)) end nitro_tag(tag, **, &block) end |