Module: Partializer::ViewHelper

Defined in:
lib/partializer/view_helper.rb

Instance Method Summary collapse

Instance Method Details

#partialize(*args, &block) ⇒ Object



3
4
5
6
7
8
9
10
11
12
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
# File 'lib/partializer/view_helper.rb', line 3

def partialize *args, &block
  case args.size
  when 2
    subject = args.first
    path = args.last
  when 1
    path = args.first
    subject = partializer
  else
    raise ArgumentError, "partialize takes 1 or 2 args, was: #{args}"
  end

  parts = case subject
  when String
    subject.split('#')
  when Partializer::Collection
    [subject.ns, subject.action]
  end

  name = parts.first
  action = parts.last
  try_clazzes = [name.to_s.camelize, "Partializers::#{name.to_s.camelize}"]

  clazz = try_clazzes.select do |clazz|
    begin
      clazz.constantize
    rescue NameError
      nil
    end
  end.first

  unless clazz
    raise ArgumentError, "No Partializer could be found for: #{subject} in: #{try_clazzes}"
  end
  
  instance = clazz.constantize.new
  instance = instance.send(action)

  path.split('.').each do |meth|
    instance = instance.send(meth)
  end
  instance.set_context *parts
  instance
end

#render_partials(subject, options = {}) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/partializer/view_helper.rb', line 48

def render_partials subject, options = {}
  partials = case subject
  when Partializer::Collection
    subject.partials
  else
    subject
  end

  unless partials.kind_of? Partializer::Partials
    raise ArgumentError, "Must be a Partializer::Collection or Partializer::Partials, was: #{collection}"
  end

  # make partializer available as object in partial called
  locals_opts = {locals: {:partializer => subject}}.merge(options[:locals] || {})
  options.merge! locals_opts

  # render options.merge(:partial => partials)
  partials.list.inject("") do |res, partial|
    opts = {:partial => partial.to_partial_path}.merge(options)
    res += raw(render opts)
    # res += "#{opts.inspect} ------- "
  end.html_safe
end