Module: BeastModeHelper

Defined in:
lib/duffy/beast_mode_helper.rb

Instance Method Summary collapse

Instance Method Details

#beast_mode(options = {}) ⇒ Object

Cache each partial in a collection, then display from cache.

IMPORTANT: This only works if you’ve wrapped your partial in a cache block.

If you don't it will take longer than just calling render()
You also need the Parallel gem installed: https://github.com/grosser/parallel

Examples:

beast_mode(@jobs)
beast_mode(partial: 'jobs/job', collection: @jobs)


13
14
15
16
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
43
44
45
46
47
48
49
# File 'lib/duffy/beast_mode_helper.rb', line 13

def beast_mode(options = {})

  if defined? Parallel

    if options.is_a?(Hash)
      # eg: beast_mode(partial: 'jobs/job', collection: @jobs)

      # Issue ArgumentError if anything is missing
      raise ArgumentError.new("partial: option is required") unless options.has_key?(:partial)
      raise ArgumentError.new("collection: option is required") unless options.has_key?(:collection)

      collection = options[:collection]
      partial = options[:partial]

      # Issue ArgumentError if collection isn't a collection
      raise ArgumentError.new("collection doesn't look like something I can render") unless collection.respond_to?(:each)


      # silently render each partial as quickly as possible.  It's up to you to include a cache block!
      # "in_processes: nil" tells Parallel to use Processes instead of Threads, and self-determine the count.
      Parallel.each(collection, in_processes: nil) do |object|
        render(partial: partial, object: object)
      end
    else
      # eg: beast_mode(@jobs)
      # Generate the Cache fragments for each object in parallel as fast as possible, but don't show them yet
      Parallel.each(options, in_processes: nil) do |object|
        render(object)
      end
    end
  else
    warn "  [WARNING] beast_mode is not executing in parallel.  Please add https://github.com/grosser/parallel"
  end

  # Display the cached fragments.
  render(options)
end