module Futurism::Helpers

Public Instance Methods

futurize(records_or_string = nil, extends:, **options, &block) click to toggle source
# File lib/futurism/helpers.rb, line 3
def futurize(records_or_string = nil, extends:, **options, &block)
  if Rails.env.test? && Futurism.skip_in_test
    if records_or_string.nil?
      return render(**options)
    else
      return render(records_or_string, **options)
    end
  end

  if block_given?
    placeholder = capture(&block)
  else
    options[:eager] = true
  end

  if records_or_string.is_a?(ActiveRecord::Base) || records_or_string.is_a?(ActiveRecord::Relation)
    futurize_active_record(records_or_string, extends: extends, placeholder: placeholder, **options)
  elsif records_or_string.is_a?(String)
    html_options = options.delete(:html_options)
    futurize_with_options(extends: extends, placeholder: placeholder, partial: records_or_string, locals: options, html_options: html_options)
  else
    futurize_with_options(extends: extends, placeholder: placeholder, **options)
  end
end
futurize_active_record(records, extends:, placeholder:, **options) click to toggle source
# File lib/futurism/helpers.rb, line 45
def futurize_active_record(records, extends:, placeholder:, **options)
  Array(records).map { |record|
    WrappingFuturismElement.new(extends: extends, options: options.merge(model: record), placeholder: placeholder).render
  }.join.html_safe
end
futurize_with_options(extends:, placeholder:, **options) click to toggle source
# File lib/futurism/helpers.rb, line 28
def futurize_with_options(extends:, placeholder:, **options)
  collection = options.delete(:collection)
  if collection.nil?
    WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options).render
  else
    collection_class_name = collection.try(:klass).try(:name) || collection.first.class.to_s
    as = options.delete(:as) || collection_class_name.underscore
    broadcast_each = options.delete(:broadcast_each) || false
    collection.each_with_index.map { |record, index|
      WrappingFuturismElement.new(extends: extends, placeholder: placeholder, options: options.deep_merge(
        broadcast_each: broadcast_each,
        locals: {as.to_sym => record, "#{as}_counter".to_sym => index}
      )).render
    }.join.html_safe
  end
end