Module: Listpress::Helper

Defined in:
lib/listpress/helper.rb

Instance Method Summary collapse

Instance Method Details

#list_only(name, control) ⇒ Object

Used in controller to limit listing rendering only for name listing and pass control options control



79
80
81
82
# File 'lib/listpress/helper.rb', line 79

def list_only(name, control)
  @_listing_only = name
  @_listing_control = control
end

#listing(collection, options = {}, &block) ⇒ Object

Used to define and render Listing.

Usage: <%= listing collection, options do |l| %>

<% l.column :id %>
...

<% end %>

Options:

name: Identifies listing within a page - required for multiple listings on one page, (default: :list)
params: Use these params instead of parsing them from request

See Listing.new for other options



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/listpress/helper.rb', line 17

def listing(collection, options = {}, &block)
  options = options.dup

  name = options.delete(:name)&.to_sym || :list
  pars = options.delete(:params) || params[name] || {}

  if @_listing_only.nil? || @_listing_only == name
    pars = pars.merge(@_listing_control) if @_listing_control

    list = Listing.new(name, collection, options, pars)
    yield list

    output = render list.view, list: list

    if controller.respond_to?(:listing_content)
      controller.listing_content[name] = list.captures
    end
    if controller.respond_to?(:listing_instances)
      controller.listing_instances[name] = list
    end

    output
  else
    ""
  end
end

#listing_contentObject

Used in controller to store rendered listing content for JSON response



69
70
71
# File 'lib/listpress/helper.rb', line 69

def listing_content
  @_listing_content ||= {}
end

#listing_input(form, col) ⇒ Object

View helper to render input for using SimpleForm builder form and column col



85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/listpress/helper.rb', line 85

def listing_input(form, col)
  options = col[:edit].is_a?(Hash) ? col[:edit].dup : {}
  options = options.reverse_merge(label: false, grid_wrapper_html: {class: "col-sm-12 ms-0"})
  options[:input_html] ||= {}
  options[:input_html][:form] = form.id

  if options.delete(:association)
    form.association col[:attr], options
  else
    form.input col[:attr], options
  end
end

#listing_instancesObject

Used in controller to retrieve rendered listing instance



74
75
76
# File 'lib/listpress/helper.rb', line 74

def listing_instances
  @_listing_instances ||= {}
end

#render_listing(action = :index, name = nil, control = {}) ⇒ Object



63
64
65
66
# File 'lib/listpress/helper.rb', line 63

def render_listing(action = :index, name = nil, control = {})
  list_only(name, control)
  render_to_string action: action, formats: [:html], layout: nil
end

#respond_with_listing(action = :index) ⇒ Object

Use in controller’s index action to automatically handle AJAX response for listing

Responds as usually for HTML requests, renders only the selected listing for AJAX.



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/listpress/helper.rb', line 47

def respond_with_listing(action = :index)
  respond_to do |format|
    format.html { render action }
    format.json {
      name = params[:listing].presence&.to_sym || :list
      control = {
          item_id: params[:listing_item_id].presence,
          edit: params[:listing_edit].to_i == 1,
      }

      render_listing(action, name, control)
      render json: listing_content[name].merge(params: Listing.flatten_params(request.GET.except(:listing, :listing_item_id, :listing_edit)))
    }
  end
end