Skip to content

Commit

Permalink
Fixed block edit with ajax ([edit] method).
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Apr 20, 2011
1 parent c791324 commit f6cda40
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 6 deletions.
33 changes: 28 additions & 5 deletions lib/zena/use/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,11 +191,34 @@ def form_options
if template_url = @context[:template_url]
# Ajax

if edit_or_cancel = descendant('cancel') || descendant('edit')
if cancel_text = edit_or_cancel.params[:cancel] ||
(edit_or_cancel.method == 'cancel' && edit_or_cancel.params[:text])
elsif cancel_text = edit_or_cancel.params[:tcancel] ||
(edit_or_cancel.method == 'cancel' && edit_or_cancel.params[:t])
cancel_text = RubyLess.translate(self, "t(%Q{#{cancel_text}})")
if cancel_text.literal
cancel_text = cancel_text.literal
else
cancel_text_ruby = cancel_text
cancel_text = "<%= #{cancel_text} %>"
end
end
cancel_pre = ''
cancel_post = ''
else
cancel_pre = "<p class='btn_x'>"
cancel_post = "</p>"
end

cancel_text ||= _('btn_x')
cancel_text_ruby ||= cancel_text.inspect

if @context[:in_add]
# Inline form used to create new elements: set values to '' and 'parent_id' from context
opts[:id] = "#{node.dom_prefix}_form"
opts[:form_tag] = "<% remote_form_for(:#{node.form_name}, #{node}, :url => #{node.form_name.pluralize}_path, :html => {:id => \"#{dom_name}_form_t\"}) do |f| %>"
opts[:form_cancel] = "<p class='btn_x'><a href='#' onclick='[\"#{dom_name}_add\", \"#{dom_name}_form\"].each(Element.toggle);return false;'>#{_('btn_x')}</a></p>\n"
opts[:form_cancel] = "#{cancel_pre}<a href='#' onclick='[\"#{dom_name}_add\", \"#{dom_name}_form\"].each(Element.toggle);return false;'>#{cancel_text}</a>#{cancel_post}\n"
else
# Saved form
opts[:id] = "<%= ndom_id(#{node}) %>"
Expand All @@ -206,9 +229,9 @@ def form_options

opts[:form_cancel] = %Q{
<% if #{node}.new_record? %>
<p class='btn_x'><a href='#' onclick='[\"<%= params[:dom_id] %>_add\", \"<%= params[:dom_id] %>_form\"].each(Element.toggle);return false;'>#{_('btn_x')}</a></p>
#{cancel_pre}<a href='#' onclick='[\"<%= params[:dom_id] %>_add\", \"<%= params[:dom_id] %>_form\"].each(Element.toggle);return false;'>#{cancel_text}</a>#{cancel_post}
<% else %>
<p class='btn_x'><%= link_to_remote(#{_('btn_x').inspect}, :url => #{node.form_name}_path(#{node}.zip) + \"/zafu?t_url=#{CGI.escape(template_url)}&dom_id=\#{params[:dom_id]}#{@context[:has_link_id] ? "&link_id=\#{#{node}.link_id}" : ''}\", :method => :get) %></p>
#{cancel_pre}<%= link_to_remote(#{cancel_text_ruby}, :url => #{node.form_name}_path(#{node}.zip) + \"/zafu?t_url=#{CGI.escape(template_url)}&dom_id=\#{params[:dom_id]}#{@context[:has_link_id] ? "&link_id=\#{#{node}.link_id}" : ''}\", :method => :get) %>#{cancel_post}
<% end %>
}
end
Expand Down Expand Up @@ -345,7 +368,7 @@ def form_hidden_fields(opts)
hidden_fields['mode'] = @params[:mode]
end

hidden_fields['node[v_status]'] = Zena::Status[:pub].to_s if add_params[:publish] || auto_publish_param
hidden_fields['node[v_status]'] = Zena::Status[:pub].to_s if add_params[:publish] || auto_publish_param || @context[:publish_after_save]

# All default values set in the <r:new> field should at least appear as hidden fields
if new_keys = node.opts[:new_keys]
Expand Down Expand Up @@ -679,7 +702,7 @@ def node_attribute(attribute)
end
end

# Set v_status parameter value
# Set auto publish parameter value
def auto_publish_param(in_string = false)
if in_string
%w{true force}.include?(@params[:publish]) ? "&publish=#{@params[:publish]}" : ''
Expand Down
29 changes: 28 additions & 1 deletion test/integration/zafu_compiler/ajax.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ edit_not_each:
src: "<li class='blah'>this is a post <r:edit>edit post</r:edit></li>"
res: "/<li class='blah'>this is a post <a href='/nodes/22/edit'>edit</a></li>/"

edit_in_block:
src: "<li do='block'><r:title/> <r:edit/></li>"
res: "/Ajax.Request\(\"/nodes/22/zafu\?dom_id=list1&"
'ajax/edit/in/block/en/list1_form.erb': "/p class='btn_x'.*f.text_field :title/"

edit_in_block_publish:
src: "<li do='block'><r:title/> <r:edit publish='true'/></li>"
res: "/Ajax.Request\(\"/nodes/22/zafu\?dom_id=list1/"
'ajax/edit/in/block/publish/en/list1_form.erb': "/name='node\[v_status\]' value='50'/"

edit_in_block_cancel:
src: "<li do='block'><r:title/> <r:edit tcancel='en'/></li>"
res: "/Ajax.Request\(\"/nodes/22/zafu\?dom_id=list1/"
'ajax/edit/in/block/cancel/en/list1_form.erb': "/link_to_remote\(\"english\"/"

form_in_block:
src: "<li do='block'><r:form>XYZ</r:form><r:title/> <r:edit/></li>"
res: "/XYZ/"
'ajax/form/in/block/en/list1_form.erb': "/XYZ/"
'ajax/form/in/block/en/list1.erb': "/XYZ/"

form_in_block_edit_only:
src: "<li do='block'><r:form on='edit'>XYZ</r:form><r:title/> <r:edit/></li>"
res: "!/XYZ/"
'ajax/form/in/block/edit/only/en/list1_form.erb': "/XYZ/"
'ajax/form/in/block/edit/only/en/list1.erb': "!/XYZ/"

add_each_no_form:
src: "<ul do='children' id='things'><li do='each'>I <p do='title'>blah</p></li><li do='add'/></ul>"
tem: "!/</ul><ul></ul>/" # bug in closing group with [add].
Expand Down Expand Up @@ -76,7 +103,7 @@ each_edit_with_form:
context:
node: 'wiki'
src: "<r:children><li do='each'><r:show attr='title'>blah</r:show> <r:edit>edit</r:edit></li>\n<r:form><li><form>this is the form</form></li></r:form></r:children>"
res: "/<li id='list1_30'>bird <a href='/nodes/30/edit' onclick='new Ajax.Request\(\"/nodes/30/edit\?.*t_url=ajax%2Feach%2Fedit%2Fwith%2Fform%2Flist1_form/"
res: "/<li id='list1_30'>bird <a href='/nodes/30/edit' onclick='new Ajax.Request\(\"/nodes/30/zafu\?.*t_url=ajax%2Feach%2Fedit%2Fwith%2Fform%2Flist1_form/"

form_with_dictionary:
context:
Expand Down

0 comments on commit f6cda40

Please sign in to comment.