Skip to content

Commit

Permalink
Fixed special 'copy' attribute used to copy node content, added some
Browse files Browse the repository at this point in the history
console methods, fixed index rebuilding when adding languages to a site.
  • Loading branch information
gaspard committed Nov 1, 2011
1 parent c023df6 commit eaac9fa
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 84 deletions.
6 changes: 3 additions & 3 deletions app/controllers/nodes_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,13 +132,13 @@ def drop
# swap (a way to preview content by drag & drop)
@node = other
elsif params[:change] == 'receiver'
attributes[:copy] = other
attributes[:_copy] = other
@node.update_attributes_with_transformation(attributes)
if !@node.errors.empty?
@errors = @node.errors
end
else
attributes[:copy] = @node
attributes[:_copy] = @node
other.update_attributes_with_transformation(attributes)
if !other.errors.empty?
@errors = other.errors
Expand Down Expand Up @@ -597,7 +597,7 @@ def check_path

case params[:action]
when 'index'
# TODO: this should live in I18n (and maybe it is not needed anymore)
# We need this redirection here to enable document caching in another lang
# bad prefix '/so', '/rx' or '/en?lang=fr'
if params[:prefix] != prefix
set_visitor_lang(params[:prefix])
Expand Down
5 changes: 3 additions & 2 deletions app/models/node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -846,10 +846,10 @@ def transform_attributes(new_attributes, base_node = nil, change_timezone = true
res = {}
res['parent_id'] = new_attributes[:_parent_id] if new_attributes[:_parent_id] # real id set inside zena.

copy_node = new_attributes.delete(:_copy)
attributes = new_attributes.stringify_keys

if attributes['copy'] || attributes['copy_id']
copy_node = attributes.delete('copy')
if copy_node || attributes['copy_id']
copy_node ||= Node.find_by_zip(attributes.delete('copy_id'))
attributes = copy_node.replace_attributes_in_values(attributes)
end
Expand Down Expand Up @@ -1009,6 +1009,7 @@ def parse_assets(text, helper, key)
end

# Parse text and replace ids '!30!' by their pseudo path '!(img/bird)!'
# key is used in TextDocument overloaded method.
def unparse_assets(text, helper, key)
ZazenParser.new(text,:helper=>helper).render(:translate_ids => :relative_path, :node=>self)
end
Expand Down
3 changes: 3 additions & 0 deletions app/models/site.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ class Site < ActiveRecord::Base
# Should be the same serialization as in Node
include Property::Serialization::JSON

# TODO: can we just use MLIndex in app.rb ?
include Zena::Use::MLIndex::SiteMethods

@@attributes_for_form = {
:bool => %w{authentication http_auth auto_publish},
:text => %w{name languages default_lang},
Expand Down
1 change: 1 addition & 0 deletions bricks/worker/zena/worker
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Daemons.run_proc('worker', daemon_options) do

require 'delayed/worker'
begin
require 'thinking_sphinx'
require 'thinking_sphinx/deltas/delayed_delta'
rescue LoadError
# Ignore
Expand Down
4 changes: 3 additions & 1 deletion config/gems.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,12 @@ differ: '= 0.1.2'
shoulda: '= 2.10.3'
httparty: '= 0.7.8'
open4:
daemons: # upload progress
gem_plugin: # upload progress

querybuilder: '= 1.0.1'
yamltest: '= 0.7.0'
rubyless: '= 0.8.5'
rubyless: '= 0.8.6'
property: '= 2.2.0'
versions: '= 0.3.1'
zafu: '= 0.8.4'
Expand Down
24 changes: 24 additions & 0 deletions lib/zena/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,30 @@ def set_prop(list, key, value)
end
end

# Transform every value of a given property by using a block with |node, old_value| and
# returning the new value.
def change_prop(pseudo_sql, key)
unless block_given?
puts "You need to provide a block |node, old_value| and return the new value"
return
end
foreach(pseudo_sql) do |node|
node.versions.each do |v|
prop = v.prop
val = prop[key]
new_val = yield(node, val)
unless new_val == val
if new_val
prop[key] = new_val
else
prop.delete(key)
end
Zena::Db.execute "UPDATE #{v.class.table_name} SET properties=#{Zena::Db.quote(v.class.encode_properties(prop))} WHERE id=#{v[:id]}"
end
end
end
end

def login(name, host = nil)
finder = {}
finder[:conditions] = cond = [[]]
Expand Down
8 changes: 6 additions & 2 deletions lib/zena/use/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,12 @@ def r_input(skip_col = false)
r_select
when 'date_box', 'date'
return parser_error("date_box without name") unless attribute
code = ::RubyLess.translate(self, "this.#{attribute}")
value = @context[:in_add] ? "''" : code
if value = @params[:value]
code = ::RubyLess.translate(self, value)
else
code = ::RubyLess.translate(self, "this.#{attribute}")
end
value = code # @context[:in_add] ? "''" : code
html_params = [':size => 15']
[:style, :class, :onclick, :size, :time].each do |key|
html_params << ":#{key} => #{@params[key].inspect}" if @params[key]
Expand Down
78 changes: 13 additions & 65 deletions lib/zena/use/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,17 @@ def self.included(base)

# Choose best language to display content.
# 1. 'test.host/oo?lang=en' use 'lang', redirect without lang
# 2. 'test.host/nodes/12?node[v_lang]=es
# 3. 'test.host/oo' use visitor[:lang]
# 4. 'test.host/' use session[:lang]
# 5. 'test.host/oo' use visitor lang
# 6. 'test.host/' use HTTP_ACCEPT_LANGUAGE
# 7. 'test.host/' use default language
# 4. 'test.host/es' use prefix ONLY ENABLED FOR html format (see below)
# 5. 'test.host/' use session[:lang]
# 6. 'test.host/oo' use visitor lang
# 7. 'test.host/' use HTTP_ACCEPT_LANGUAGE
# 8. 'test.host/' use default language
#
# 8. 'test.host/fr' the redirect for this rule is called once we are sure the request is not for document data (lang in this case can be different from what the visitor is visiting due to caching optimization)
# 4. 'test.host/es' the redirect for this rule is called once we are sure the request is
# not for document data (lang in this case can be different from what the visitor is
# visiting due to caching optimization).
def set_lang
if params[:prefix] =~ /^\d+$/
# this has nothing to do with set_lang...
Expand All @@ -132,7 +136,11 @@ def set_lang
chosen_lang = nil
[
params[:lang],
# FIXME: This is good to protect templates and other documents but is *NOT* nice when translating a website !!
# What should we do ?
params[:node] ? params[:node][:v_lang] : nil,
# Avoid redirects for static assets (cached documents).
request.format == Mime::HTML ? params[:prefix] : nil,
visitor.is_anon? ? session[:lang] : visitor.lang,
(request.headers['HTTP_ACCEPT_LANGUAGE'] || '').split(',').sort {|a,b| (b.split(';q=')[1] || 1.0).to_f <=> (a.split(';q=')[1] || 1.0).to_f }.map {|l| l.split(';')[0].split('-')[0] },
(visitor.is_anon? ? visitor.lang : nil), # anonymous user's lang comes last
Expand All @@ -142,7 +150,6 @@ def set_lang
break
end
end

set_visitor_lang(chosen_lang || current_site[:default_lang])
true
end
Expand Down Expand Up @@ -374,66 +381,7 @@ def r_trans
end

alias r_t r_trans
#def r_trans
# static = true
# if @params[:text]
# text = @params[:text]
# elsif @params[:attr]
# text = "#{node_attribute(@params[:attr])}"
# static = false
# else
# res = []
# text = ""
# @blocks.each do |b|
# if b.kind_of?(String)
# res << b.inspect
# text << b
# elsif ['show', 'current_date'].include?(b.method)
# res << expand_block(b, :trans=>true)
# static = false
# else
# # ignore
# end
# end
# unless static
# text = res.join(' + ')
# end
# end
# if static
# _(text)
# else
# "<%= _(#{text}) %>"
# end
#end


# show language selector
#def r_lang_links
# if wrap_tag = @params[:wrap]
# wrap_tag = Zafu::Markup.new(wrap_tag)
# tag_in = "<#{opts[:wrap]}>"
# tag_out = "</#{opts[:wrap]}>"
# else
# tag_in = tag_out = ''
# end
# res = []
# visitor.site.lang_list.each do |l|
# if l == visitor.lang
# if opts[:wrap]
# res << "<#{opts[:wrap]} class='on'>#{l}" + tag_out
# else
# res << "<em>#{l}</em>"
# end
# else
# if visitor.is_anon? && params[:prefix]
# res << tag_in + link_to(l, params.merge(:prefix => l)) + tag_out
# else
# res << tag_in + link_to(l, params.merge(:lang => l)) + tag_out
# end
# end
# end
# res.join(opts[:join] || '')
#end
protected

# Overwriten from Zafu to insert dictionary in partial if there is one
Expand Down
15 changes: 15 additions & 0 deletions lib/zena/use/ml_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,21 @@ def index_langs
end
end
end # ModelMethods

module SiteMethods
def self.included(base)
base.before_save :rebuild_index_on_lang_list_change
end

protected
def rebuild_index_on_lang_list_change
if languages_changed?
# delete all ml entries for this site and rebuild
Zena::Db.execute "DELETE idx FROM idx_nodes_ml_strings idx INNER JOIN nodes ON idx.node_id = nodes.id WHERE nodes.site_id = #{self[:id]}"
rebuild_index
end
end
end # SiteMethods
end # MLIndex
end # Use
end # Zena
10 changes: 6 additions & 4 deletions lib/zena/use/query_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,15 @@ def get_scope_index_field(field_name)
end
end

def process_idx_field(scope_field)
scope_field
end

# Overwrite this and take care to check for valid fields.
def process_field(field_name)
if fld = @query.attributes_alias[field_name]
# use custom query alias value defined in select clause: 'custom_a AS validation'
return processing_filter? ? "(#{fld})" : fld
elsif processing_filter? && scope_field = get_scope_index_field(field_name)
scope_field
elsif processing_filter? && map_def = self.class.filter_fields[field_name]
# Special filter fields such as 'role', 'tag' or 'class'
if map_def.kind_of?(String)
Expand Down Expand Up @@ -353,8 +355,8 @@ def resolve_scope_idx_fields(arg1, arg2)
end

scope_idx_field = "#{class_name}_#{field_name}"
if get_scope_index_field(scope_idx_field)
return [[:field, scope_idx_field], function]
if fld = get_scope_index_field(scope_idx_field)
return [[:idx_field, fld], function]
else
# not a scope index field
return [arg1, arg2]
Expand Down
5 changes: 5 additions & 0 deletions lib/zena/use/rendering.rb
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,11 @@ def r_headers
out "<% set_headers(#{headers.join(', ')}) %>"
end
end

def r_style
@markup.tag = 'style'
expand_with
end

def r_not_found
out "<% raise ActiveRecord::RecordNotFound %>"
Expand Down
7 changes: 5 additions & 2 deletions lib/zena/use/upload.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,9 @@ def render_upload_progress
end # ControllerMethods

module ViewMethods
include RubyLess
safe_method [:upload_field, {:type => String}] => String

UPLOAD_KEY = defined?(Mongrel) ? 'upload_id' : "X-Progress-ID"
def upload_form_tag(url_opts, html_opts = {})
@uuid = UUIDTools::UUID.random_create.to_s.gsub('-','')
Expand All @@ -162,8 +165,8 @@ def upload_form_tag(url_opts, html_opts = {})
end

def upload_field(opts = {})
case opts[:type]
when :onclick
case opts[:type].to_s
when 'onclick'
link = link_to_remote(_("change"), :update=>'upload_field', :url => get_uf_documents_path(:uuid => @uuid), :method => :get, :complete=>"['file', 'upload_field'].each(Element.toggle);")
<<-TXT
<label for='attachment'>#{_('file')}</label>
Expand Down
2 changes: 2 additions & 0 deletions lib/zena/use/version_hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ module Use
#
# Technically, the vhash field contains two dictionaries "readonly" and "write". Each of these dictionaries
# provide mapping from languages to version id.
#
# {'r' => {'en' => 1234, 'fr' => 3456}, 'w' => {'en' => 5436, 'fr' => 4526}}
module VersionHash
def self.cached_values_from_records(records)
r_hash, w_hash = {}, {}
Expand Down
26 changes: 25 additions & 1 deletion test/integration/navigation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,15 @@ def test_out_of_oo_custom_base_set_lang
assert_redirected_to 'http://test.host/fr'
end
end # without clues

context 'with lang on bad url' do
should 'set lang from url' do
get 'http://test.host/de/foobar'
assert_response :not_found
assert_equal 'de', session[:lang]
assert_equal 'de', visitor.lang
end
end
end # Selecting lang

context 'In an intranet' do
Expand Down Expand Up @@ -292,6 +301,21 @@ def test_set_lang_with_login
assert_equal 'fr', session[:lang]
end

context 'With new languages defined' do
setup do
login(:lion)
assert visitor.site.update_attributes(:languages => 'en,fr,cn')
end

should 'compile templates' do
post 'http://test.host/session', :login=>'lion', :password=>'lion'
get 'http://test.host/oo?lang=cn'
assert_redirected_to 'http://test.host/oo'
follow_redirect!
assert_response :success
end
end

context 'On a page with custom base' do
setup do
login(:lion)
Expand Down Expand Up @@ -332,7 +356,7 @@ def test_set_lang_with_login
login(:lion)
secure(Template) { Template.create(:parent_id => nodes_id(:default), :title => 'Project-changes.zafu', :v_status => Zena::Status::Pub, :text => 'nothing ever changes in "<r:title/>"') }
end

subject do
'http://test.host/en/projects-list/Clean-Water-project_changes'
end
Expand Down
10 changes: 10 additions & 0 deletions test/integration/query_node/idx_scope.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,14 @@ or_keys:
id_key:
src: "blogs where id = 29 in site"
sql: '!/idx_projects/'
res: 'a wiki with Zena'

sort_by_scoped_value:
src: "blogs in site order by contact.name asc"
sql: "%Q{SELECT nodes.* FROM idx_projects AS sc1,nodes WHERE #{secure_scope('nodes')} AND nodes.id = sc1.node_id AND nodes.kpath LIKE 'NPPB%' ORDER BY sc1.contact_name ASC}"
res: 'a wiki with Zena'

should_not_mess_with_select:
src: "blogs select title as blog_title in site order by contact.name asc"
sql: "%Q{SELECT nodes.*,ml1.value AS `blog_title` FROM idx_nodes_ml_strings AS ml1,idx_projects AS sc1,nodes WHERE #{secure_scope('nodes')} AND nodes.id = sc1.node_id AND ml1.lang = 'fr' AND ml1.key = 'title' AND ml1.node_id = nodes.id AND nodes.kpath LIKE 'NPPB%' ORDER BY sc1.contact_name ASC}"
res: 'a wiki with Zena'
4 changes: 4 additions & 0 deletions test/integration/zafu_compiler/display.yml
Original file line number Diff line number Diff line change
Expand Up @@ -391,3 +391,7 @@ insert_slash_slash_exclam:
array_literal:
src: "<p do='%w{one two}'><span do='each' join=', ' do='this'/></p>"
res: "<p><span>one</span>, <span>two</span></p>"

style_tag:
src: "<r:style>.foo <r:title/></r:style>"
res: "<style>.foo status title</style>"

0 comments on commit eaac9fa

Please sign in to comment.