Method: Zafu::Process::Ajax#expand_with_finder

Defined in:
lib/zafu/process/ajax.rb

#expand_with_finder(finder) ⇒ Object

This method process a list and handles building the necessary templates for ajax ‘add’.



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/zafu/process/ajax.rb', line 10

def expand_with_finder(finder)
  @context.delete(:make_form) # Do not propagate.
  return super unless finder[:class].kind_of?(Array)

  # reset scope
  @context[:saved_template] = nil

  # Get the block responsible for rendering each elements in the list
  each_block = descendant('each')
  add_block  = descendant('add')
  form_block = descendant('form') || each_block
  edit_block = descendant('edit')


  # Should 'edit' and 'add' auto-publish ?
  publish_after_save = (form_block && form_block.params[:publish]) ||
                       (edit_block && edit_block.params[:publish])

  # class name for create form
  if class_name = (add_block  &&  add_block.params[:klass]) ||
                  (form_block && form_block.params[:klass])
    unless klass = get_class(class_name)
      return parser_error("Invalid class '#{class_name}'")
    end
  else
    klass = finder[:class].first
  end

  if need_ajax?(each_block)
    #node.dom_prefix = dom_name
    # We need to build the templates for ajax rendering.
    # HACK to avoid changing dom_prefix in drag&drop

    # The bug with wrong prefix between drop inline and ajax result comes from
    # here (see selenium test drop1 and drop2).
    node.dom_prefix = dom_name

    # 1. Render inline
    #                                                                                                                 assign [] to var
    out "<% if (#{var} = #{finder[:method]}) || (#{node}.#{finder[:class].first <= Comment ? "can_comment?" : "can_write?"} && #{var}=[]) %>"
    # The list is not empty or we have enough rights to add new elements.

    # New node context.
    open_node_context(finder, :node => self.node.move_to(var, finder[:class])) do
      # Pagination count and other contextual variables exist here.

      tmplt_node = self.node.move_to(var, finder[:class])
      tmplt_node.dom_prefix = node.dom_prefix

      # INLINE ==========
      out wrap(
        expand_with(
          :in_if              => false,
          # 'r_add' needs the form when rendering. Send with :form.
          :form               => form_block,
          :publish_after_save => publish_after_save,
          # Do not render the form block directly: let [add] do this.
          :ignore             => ['form'],
          :klass              => klass
        )
      )

      # Render 'else' clauses
      else_clauses = expand_with(
        :in_if => true,
        :only  => ['elsif', 'else'],
        :markup => @markup
      )

      # 2. Save 'each' template
      store_block(each_block, :node => tmplt_node)

      # 3. Save 'form' template
      cont = {
        :saved_template     => form_url(tmplt_node.dom_prefix),
        :klass              => klass,
        :make_form          => each_block == form_block,
        # Used to get parameters like 'publish', 'done', 'after'
        :add                => add_block,
        :publish_after_save => publish_after_save,
        :new_keys           => {:parent_id => '@node.parent_zip'}
      }

      store_block(form_block, cont)
    end
    out "<% end %>"
  else
    super
  end

  # out wrap(expand_with(:node => node.move_to(var, finder[:class]), :in_if => true))


  #query = opts[:query]
  #
  #
  #if need_ajax?
  #  new_dom_scope
  #  # ajax, build template. We could merge the following code with 'r_block'.
  #
  #  # FORM ============
  #  if each_block != form_block
  #    form = expand_block(form_block, :klass => klass, :add=>add_block, :publish_after_save => publish_after_save, :saved_template => true)
  #  else
  #    form = expand_block(form_block, :klass => klass, :add=>add_block, :make_form=>true, :publish_after_save => publish_after_save, :saved_template => true)
  #  end
  #  out helper.save_erb_to_url(form, form_url)
  #else
  #  # no form, render, edit and add are not ajax
  #  if descendant('add') || descendant('add_document')
  #    out "<% if (#{list_var} = #{list_finder}) || (#{node}.#{node.will_be?(Comment) ? "can_comment?" : "can_write?"} && #{list_var}=[]) %>"
  #  elsif list_finder != 'nil'
  #    out "<% if #{list_var} = #{list_finder} %>"
  #  else
  #    out "<% if nil %>"
  #  end
  #
  #
  #  out render_html_tag(expand_with(:list=>list_var, :in_if => false))
  #  out expand_with(:in_if=>true, :only=>['elsif', 'else'], :html_tag => @html_tag, :html_tag_params => @html_tag_params)
  #  out "<% end %>"
  #end
end