Module: NitroRails::NitroCollectionHelper

Defined in:
app/helpers/nitro_rails/nitro_collection_helper.rb

Instance Method Summary collapse

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

Returns:

  • (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, **options, &block)
  resources = is_active_collection?(resources_or_path.first) ? resources_or_path.first : chain_methods(*resources_or_path)
  NitroCollection.new(resources, view: self, **options).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, **options, &block)
  (: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 }, **options) do 
      resources = parent.send(resource_method)
      resources = resources.order(options.delete(:order)) if options[:order]
      resources = resources.where(options.delete(:where)) if options[:where]
      resources = resources.take(options.delete(:take)) if options[:take]
      

      resources.map do |resource|
        yield resource
      end
    end + 

    (: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, **options, &block)
  nitro_options(options) do |options|
    options.default(:nitro_id, parent.nitro_id(resource_method))
    options.concat(:class, classify(resource_method))
    options.concat(:class, classify(parent.type, resource_method))
  end

  nitro_tag(tag, **options, &block)
end