Skip to content

Commit

Permalink
Automatically adding dictionary to ajax partials.
Browse files Browse the repository at this point in the history
  • Loading branch information
gaspard committed Apr 11, 2011
1 parent f7377b9 commit ef2976e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
1 change: 0 additions & 1 deletion lib/zena/use/ajax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ def need_ajax?(each_block)
# unlink
each_block.descendant('unlink')
end

end
end # Ajax
end # Use
Expand Down
47 changes: 20 additions & 27 deletions lib/zena/use/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module I18n
::ENV['LANG'] = 'C'

class TranslationDict
attr_reader :last_error
attr_reader :last_error, :node_id

include Zena::Acts::Secure
include RubyLess
Expand Down Expand Up @@ -288,7 +288,7 @@ def r_load
dict_name = get_var_name('dictionary', 'dict')

# This is to use in RubyLess translations and static translations in Zafu
set_context_var('set_var', 'dictionary', RubyLess::TypedString.new(dict_name, :class => TranslationDict, :literal => dict, :dictionary_id => doc.id))
set_context_var('set_var', 'dictionary', RubyLess::TypedString.new(dict_name, :class => TranslationDict, :literal => dict))

# Lazy loading (loads file on first request)
out "<% #{dict_name} = load_dictionary(#{doc.id}) %>"
Expand Down Expand Up @@ -346,31 +346,6 @@ def translate_list(str)
end
end

def r_block
# when saving template, add loaded dictionary
if @context[:block] == self
if @dict
# pass loaded dictionary to ajax templates

# Save dictionary in template for dynamic uses
dict_name = get_var_name('dictionary', 'dict')

# This is to use in RubyLess translations and static translations in Zafu
set_context_var('set_var', 'dictionary', @dict)

# Lazy loading (loads file on first request)
out "<% #{dict_name} = load_dictionary(#{@dict.opts[:dictionary_id]}) %>"
end
super
else
@dict = get_context_var('set_var', 'dictionary')
res = super
@dict = nil

res
end
end

def r_trans
# _1 ==> insert this param ==> trans(@params[:text])
return nil unless method = get_attribute_or_eval
Expand Down Expand Up @@ -449,6 +424,24 @@ def r_trans
# end
# res.join(opts[:join] || '')
#end
protected

# Overwriten from Zafu to insert dictionary in partial if there is one
def context_for_partial(cont)
cleared_context, prefix = super(cont)
prefix = prefix.to_s
dict = get_context_var('set_var', 'dictionary')
if dict && dict.klass <= TranslationDict
# Lazy loading (loads file on first request)

dict_name = get_var_name('dictionary', 'dict', cleared_context)
set_context_var('set_var', 'dictionary', dict, cleared_context)
prefix += "<% #{dict_name} = load_dictionary(#{dict.literal.node_id}) %>"
return cleared_context, prefix
else
return cleared_context, nil
end
end
end
end # I18n
end # Use
Expand Down
8 changes: 8 additions & 0 deletions test/integration/zafu_compiler/ajax.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ each_edit_with_form:
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/"

form_with_dictionary:
context:
node: 'wiki'
src: "<r:load dictionary='/Default skin/translations'><r:children><li do='each'><span do='t(\"foo#{origin}\")'/> <r:edit>edit</r:edit></li>\n<li do='form'><r:select name='origin' values=',en,fr,de' tshow=',en,fr,de'/></li></r:children></r:load>"
# Partial should lazy load dictionary
'ajax/form/with/dictionary/en/list1_form.erb': "/_zdict = load_dictionary/"
'ajax/form/with/dictionary/en/list1.erb': "/_zdict = load_dictionary.*_zdict.get\(\"foo\#\{@node.prop\['origin'\]\}\"\)/"

make_form:
src: "<ul do='children'><li do='each' do='title'/><li do='add'/></ul>"
tem: "/<li style='display:none;' class='form' id='list1_form'>.*remote_form_for\(:node, var2_new"
Expand Down

0 comments on commit ef2976e

Please sign in to comment.