Class: NitroRails::ResourceCollection
- Inherits:
-
Object
- Object
- NitroRails::ResourceCollection
- Defined in:
- app/lib/nitro_rails/resource_collection.rb
Instance Method Summary collapse
- #active_organizer(**opts) ⇒ Object
-
#collection_classes(classes) ⇒ Object
Actually maybe split this so list classes are separate from container classes.
- #container_tag(&block) ⇒ Object
- #create_resource_button(text = nil, **opts, &block) ⇒ Object
- #current_path_with_filter_params(**opts) ⇒ Object
- #current_path_with_new_order_params(**opts) ⇒ Object
- #current_path_without_order_params(**opts) ⇒ Object
- #filter(text, **opts) ⇒ Object
-
#initialize(resources, view:, create_path:, parent: nil, resource_type: nil, id: nil, signature: nil) ⇒ ResourceCollection
constructor
Should completely refigure what’s passed in here.
- #new_resource_form(&block) ⇒ Object
- #nitro_id ⇒ Object
-
#render_content(&block) ⇒ Object
RENDERING METHODS.
- #render_item(resource, partial, &block) ⇒ Object
- #render_items(partial = nil, **opts, &block) ⇒ Object
-
#render_list_item(resource, &block) ⇒ Object
Do we need separate functions for item and list item?.
-
#sorter(text, active_text = text, **opts) ⇒ Object
ORDERING AND FILTERING METHODS Probably need to split these out into a separate module.
Constructor Details
#initialize(resources, view:, create_path:, parent: nil, resource_type: nil, id: nil, signature: nil) ⇒ ResourceCollection
Should completely refigure what’s passed in here
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 5 def initialize(resources, view:, create_path:, parent: nil, resource_type: nil, id: nil, signature: nil) @resources = resources @resource_path = create_path @view = view @parent = parent @resource_type = resource_type if resource_type @collection_tag = resource_type.pluralize if resource_type # Little weird to override create path here @create_path = (defined?(User) && @parent.is_a?(User)) ? @collection_tag : create_path @id = id @params = @view.params if resources.first @collection_tag ||= resources.first.collection_tag # For list @resource_type ||= resources.first.type # For list elements end end |
Instance Method Details
#active_organizer(**opts) ⇒ Object
118 119 120 121 122 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 118 def active_organizer(**opts) list_params && list_params[:order] && opts.all? do |key, val| list_params[:order][key].to_s == val.to_s end end |
#collection_classes(classes) ⇒ Object
Actually maybe split this so list classes are separate from container classes
94 95 96 97 98 99 100 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 94 def collection_classes(classes) if @parent @collection_classes ||= "#{@parent.type}-#{@collection_tag} #{@parent.type}-#{@parent.id}-#{@collection_tag} #{@parent.type}-#{@collection_tag}-list #{@collection_tag} #{@collection_tag}-list list list--arrow-detail list--basic-link #{classes}" else @collection_classes ||= "#{@collection_tag} #{@collection_tag}-list list list--arrow-detail list--basic-link #{classes}" end end |
#container_tag(&block) ⇒ Object
33 34 35 36 37 38 39 40 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 33 def container_tag(&block) content_tag(:div, class: "resource-collection", data: { controller: "resource-collection", action: "keydown->resource-collection#handleKeyDown" }, &block) end |
#create_resource_button(text = nil, **opts, &block) ⇒ Object
84 85 86 87 88 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 84 def (text = nil, **opts, &block) new_resource_form() do block_given? ? concat(content_tag(:button, class: "btn btn-create", type: "submit", data: { turbo: opts[:turbo] || false, "keyboard-target": "enter" }, &block)) : concat(submit_tag(text || "Create", class: "btn-new")) # This can be cleaned up end end |
#current_path_with_filter_params(**opts) ⇒ Object
139 140 141 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 139 def current_path_with_filter_params(**opts) url_for("#{@signature}" => { filters: opts }) end |
#current_path_with_new_order_params(**opts) ⇒ Object
128 129 130 131 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 128 def current_path_with_new_order_params(**opts) # current_params = request.query_parameters # url_for(current_params.merge("#{@signature}" => { order: opts })) end |
#current_path_without_order_params(**opts) ⇒ Object
133 134 135 136 137 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 133 def current_path_without_order_params(**opts) # current_params = request.query_parameters.deep_dup # opts.each { |key, val| current_params["#{@signature}"][:order].delete("#{key}") } # url_for(current_params) end |
#filter(text, **opts) ⇒ Object
124 125 126 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 124 def filter(text, **opts) link_to(text, current_path_with_filter_params(**opts)) end |
#new_resource_form(&block) ⇒ Object
75 76 77 78 79 80 81 82 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 75 def new_resource_form(&block) @form_rendered = true form_tag(@create_path || "/#{@view.url_for(@collection_tag)}", method: :post, data: { action: "ajax:success->resource-collection#handleSuccess" }) do # I hate the way we're setting the path here concat(hidden_field_tag("#{@resource_type.singularize}[#{@parent.type.underscore}_id]", @parent.id)) # This definitely shouldn't be specific to projects concat(capture(self, &block)) if block_given? end end |
#nitro_id ⇒ Object
102 103 104 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 102 def nitro_id @nitro_id ||= @parent ? "#{@parent.resource_id}:#{@collection_tag}" : "#{@collection_tag}" end |
#render_content(&block) ⇒ Object
RENDERING METHODS
25 26 27 28 29 30 31 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 25 def render_content(&block) # MAYBE - PASSED BLOCKS JUST DO A REFRESH OF THE COLLECTION, WHERE AS RENDERED PARTIALS DOES APPEND/REMOVE BROADCASTS (nitro_id: RESOURCES_ID_REFRESH, RESOURCE_ID_BROADCAST) container_tag do concat(capture(self, &block)) if block_given? concat(render_items) unless @resources_rendered end end |
#render_item(resource, partial, &block) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 62 def render_item(resource, partial, &block) if block_given? # set_resource_collection_cache(&block) capture(resource, &block) else if partial.present? render(partial, "#{resource.type}": resource) else render resource end end end |
#render_items(partial = nil, **opts, &block) ⇒ Object
42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 42 def render_items(partial = nil, **opts, &block) @resources_rendered = true # set_order_info(opts[:order]) content_tag(:ul, id: @collection_tag, class: collection_classes(opts[:class]), data: { nitro_id: "#{nitro_id}" }) do @resources.order(opts[:order]).each do |resource| concat(render_list_item(resource) do render_item(resource, partial, &block) end) end end end |
#render_list_item(resource, &block) ⇒ Object
Do we need separate functions for item and list item?
56 57 58 59 60 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 56 def render_list_item(resource, &block) content_tag(:li, class: "resource-collection-item list-item #{resource.type}", data: { id: resource.id, nitro_id: "#{resource.nitro_id}" }) do concat(capture(resource, &block)) end end |
#sorter(text, active_text = text, **opts) ⇒ Object
ORDERING AND FILTERING METHODS Probably need to split these out into a separate module
110 111 112 113 114 115 116 |
# File 'app/lib/nitro_rails/resource_collection.rb', line 110 def sorter(text, active_text=text, **opts) if active_organizer(**opts) link_to(active_text, current_path_without_order_params(**opts), class: "active", data: { turbo_action: :replace }) else link_to(text, current_path_with_new_order_params(**opts), class: "inactive", data: { turbo_action: :replace }) end end |