Skip to content

Commit

Permalink
Added preview option to [form], using JOIN for all queries (qb 1.2.0)…
Browse files Browse the repository at this point in the history
…. Added 'in_group' for visitor.
  • Loading branch information
gaspard committed Nov 12, 2012
1 parent a5ac562 commit aff74f1
Show file tree
Hide file tree
Showing 25 changed files with 213 additions and 133 deletions.
2 changes: 2 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
* html_escape all properties by default
* Better support for Passenger (default deployment method now)
* Simplified caching (using cachestamp in filename)
* Added preview='dom_id' to [form]. (Use preview_node to show preview content.) <== TODO: Document
* Added "in_group" zafu method on visitor. <== TODO: Document

* Minor changes
* Support for 'lang_list' when creating vhost file <== TODO: Document
Expand Down
4 changes: 2 additions & 2 deletions config/gems.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ syntax: '= 1.0.0'
tzinfo:
uuidtools: '= 2.0.0'
rails: '= 2.3.11'
json: '= 1.7.5'
json: '= 1.5.1'
authlogic: '= 2.1.3'
fast_gettext: '~> 0.4.16'
will_paginate: '~> 2.3.12'
Expand All @@ -18,7 +18,7 @@ gem_plugin: # upload progress
simple_xlsx_writer: # spreadsheet
lib: 'simple_xlsx'

querybuilder: '= 1.1.4'
querybuilder: '= 1.2.0'
yamltest: '= 0.7.0'
rubyless: '= 0.8.8'
property: '= 2.3.2'
Expand Down
4 changes: 2 additions & 2 deletions lib/zafu/process/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def make_form
# Return id, style, form and cancel parts of the form.
def form_options
opts = {}

# Do we need this ?
# opts[:klass] = node.master_class(ActiveRecord::Base).to_s

Expand All @@ -85,7 +85,7 @@ def form_options
opts[:id] = "#{node.dom_prefix}_tbl"
form_id = node.dom_prefix
end

form_id ||= "#{node.dom_prefix}_form_t"
if @context[:template_url]
opts[:form_tag] = "<% remote_form_for(:#{node.form_name}, #{node}, :html => {:id => #{form_id.inspect}}) do |f| %>"
Expand Down
11 changes: 8 additions & 3 deletions lib/zena/deploy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,8 +238,12 @@ def ancestry(path)
# directory setup for log
run "test -e #{sites_root}/#{self[:host]}/log || mkdir #{sites_root}/#{self[:host]}/log"
run "chown www-data:www-data #{sites_root}/#{self[:host]}/log"

run "test -e /etc/apache2/sites-enabled/#{self[:host]} || a2ensite #{self[:host]}" if debian_host

if debian_host
vhost_files.each do |host|
run "test -e /etc/apache2/sites-enabled/#{host} || a2ensite #{host}"
end
end

unless self[:host] =~ /^www/
vhost_www = render("#{templates}/vhost_www.rhtml", :config => self, :vhost_port => (self[:ssl] ? ':80' : ''))
Expand Down Expand Up @@ -312,7 +316,8 @@ def ancestry(path)
run "#{in_current} rake zena:rename_site OLD_HOST='#{self[:old_host]}' HOST='#{self[:host]}' RAILS_ENV='production'"
old_vhosts = ["#{self[:old_host]}",
"stats.#{self[:old_host]}",
"www.#{self[:old_host]}"]
"www.#{self[:old_host]}",
"#{self[:old_host]}.ssl"]
old_vhosts.each do |vhost|
run "test -e /etc/apache2/sites-enabled/#{vhost} && a2dissite #{vhost} || true"
vhost_path = "#{vhost_root}/#{vhost}"
Expand Down
6 changes: 3 additions & 3 deletions lib/zena/deploy/vhost.rhtml
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@
RewriteRule ^.*$ /system/maintenance.html [L]

<% list = (config[:lang_list] || 'en').split(',').map(&:strip)
last = list.last
list.each do |l|
if l != last %>
default = list.first
list.reverse_each do |l|
if l != default %>
# Browser wants "<%= l %>" as favorite language
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{HTTP:Accept-Language} ^<%= l %>
Expand Down
25 changes: 23 additions & 2 deletions lib/zena/use/ajax.rb
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,27 @@ def filter_form(node, dom_id, loading, upd)
else
loading = ''
end
# Disable 'redir' parameter during preview or filter.
js_data << %Q{new Form.Observer('#{dom_id}', 0.3, function(element, value) {#{loading}
new Ajax.Request('#{zafu_node_path(node)}', {asynchronous:true, evalScripts:true, method:'get', parameters:Form.serialize('#{dom_id}')})
var data = Form.serialize('#{dom_id}').gsub(/&redir=/,'&no_redir=')
new Ajax.Request('#{zafu_node_path(node)}', {asynchronous:true, evalScripts:true, method:'get', parameters:data})
});}
end

# Load parameters in node before rendering.
# SECURITY: There may be a security threat here if the node attributes are used for queries or relations.
def preview_node(node)
return nil if !node.kind_of?(Node)
if attrs = params[:node]
# Return a copy
new_node = node.dup
new_node.version = node.version.dup
new_node.attributes = Node.transform_attributes(params[:node], node, true)
return new_node
else
return node
end
end

# Include draggable ids in bottom of page Javascript.
def render_js(in_html = true)
Expand Down Expand Up @@ -284,7 +301,11 @@ def wrap_with_drag(text)
end
end

# Display an input field to filter a remote block
def r_preview_node
expand_if("#{var} = preview_node(#{node})", node.move_to(var, node.klass))
end

# Display an input field to filter a remote block.
def r_filter
if upd = @params[:update]
return unless block = find_target(upd)
Expand Down
3 changes: 2 additions & 1 deletion lib/zena/use/ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ def fullpath_map(path, sym = :node)

module ModelMethods
include RubyLess
safe_context :ancestors => {:class => ['Node'], :method => 'z_ancestors'}
# This is used to defer :class type resolution to compilation time
safe_method :ancestors => Proc.new {|h, r, s| {:method => 'z_ancestors', :class => [VirtualClass['Node']], :nil => true}}
safe_method :fullpath => String, :short_path => [String]

def self.included(base)
Expand Down
9 changes: 8 additions & 1 deletion lib/zena/use/forms.rb
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ def form_options
end

opts[:form_tag] = %Q{
<% form_for(:#{node.form_name}, #{node}, :url => #{node}.new_record? ? #{node.form_name.pluralize}_path : #{node.form_name}_path(#{node}.zip), :html => {:method => #{node}.new_record? ? :post : :put, :id => \"\#{ndom_id(#{node})}_form_t\"}) do |f| %>
<% form_for(:#{node.form_name}, #{node}, :url => #{node}.new_record? ? #{node.form_name.pluralize}_path : #{node.form_name}_path(#{node}.zip), :html => {:method => #{node}.new_record? ? :post : :put, :id => #{"#{node.dom_prefix}_form_t".inspect}}) do |f| %>
#{error_messages}}
end

Expand Down Expand Up @@ -340,6 +340,13 @@ def form_hidden_fields(opts)
end
hidden_fields['done'] = done
end
elsif upd = @params.delete(:preview)
hidden_fields['t_url'] = template_url(upd)
hidden_fields['dom_id'] = upd
hidden_fields['s'] = "<%= start_node_zip %>"
loading = @params[:loading]
loading = 'Zena.loading' if loading == 'true'
out "<% filter_form(#{node}, \"#{dom_name}_form_t\", #{loading.inspect}, '#{upd}') %>"
else
# no ajax
cancel = "" # link to normal node ?
Expand Down
24 changes: 14 additions & 10 deletions lib/zena/use/query_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def get_scope_index_field(field_name)
index_model = Zena.resolve_const(index_model) rescue NilClass
if index_model < Zena::Use::ScopeIndex::IndexMethods && index_model.column_names.include?(field_name)
table_to_use = add_key_value_table('scope_index', index_model.table_name) do |tbl_name|
# This block is only executed once
add_filter "#{table('nodes')}.id = #{tbl_name}.node_id"
# This block is only executed once (ON clause)
"#{table('nodes')}.id = #{tbl_name}.node_id"
end
"#{table_to_use}.#{field_name}"
else
Expand Down Expand Up @@ -219,7 +219,7 @@ def process_field(field_name)
use_name, source, target, filter = table_def
table_to_use = add_key_value_table(use_name, target, map_def[:key]) do |tbl_name|
# This block is only executed once
add_filter filter.gsub(
filter.gsub(
'TABLE1', table(source)
).gsub(
'TABLE2', tbl_name
Expand Down Expand Up @@ -264,12 +264,14 @@ def process_field(field_name)

tbl = add_key_value_table(group_name, index_table, field_name) do |tbl_name|
# This block is only executed once
add_filter "#{tbl_name}.node_id = #{table}.id"
add_filter "#{tbl_name}.key = #{quote(field_name)}"
on_clause = "#{tbl_name}.node_id = #{table}.id AND #{tbl_name}.key = #{quote(field_name)}"
if group_name.to_s =~ /^ml_/
add_filter "#{tbl_name}.lang = #{quote(visitor.lang)}"
on_clause << " AND #{tbl_name}.lang = #{quote(visitor.lang)}"
end
# no need for distinct, the new table makes a 1-1 relation
# ON CLAUSE
on_clause

end

"#{tbl}.value"
Expand Down Expand Up @@ -358,11 +360,13 @@ def resolve_scope_idx_fields(arg1, arg2)
if rel = RelationProxy.find_by_role(rel.singularize, source_kpath)
table_to_use = add_key_value_table('jnode', 'nodes', class_name) do |tbl_name|
# This block is only executed once per relation name (once for 'hot', once for 'hot2')
# TODO: Can we remove this ?
distinct!
add_table('links', nil, :left)
add_filter "#{table('links')}.#{rel.link_side} = #{table(main_table)}.id"
add_filter "#{table('links')}.relation_id = #{rel.id}"
add_filter "#{table('jnode')}.id = #{table('links')}.#{rel.other_side}"
lnk = add_key_value_table('links', 'links', class_name) do |lt|
"#{lt}.#{rel.link_side} = #{table(main_table)}.id AND #{lt}.relation_id = #{rel.id}"
end

%Q{#{tbl_name}.id = #{lnk}.#{rel.other_side}}
end

# Temporarily move to the remote class
Expand Down
21 changes: 19 additions & 2 deletions test/fixtures/files/TestNode.zafu
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<r:void name='stylesheets'>
<r:stylesheets media='all' list='reset,zena,code,calendar,grid'/>
</r:void>
<link href="style.css" rel="Stylesheet" type="text/css"/>
<link href="img/style.css" rel="Stylesheet" type="text/css"/>
<style>
.test { float:left; width:200px; padding:10px; background:#eee; border:1px solid #333; margin:0 10px 10px 0}
.test h3 {background:#333; color:white; text-align:center; margin:-10px -10px 10px -10px;}
Expand Down Expand Up @@ -229,6 +229,23 @@
</ul>
</div>

<div class='test' id='filter3' do='selenium'>
<h3>filter3</h3>
<p>From with live preview.</p>
<r:form redir='url(this, :test => "filter3")' node[v_status]='50' preview='filter3_preview'>
<r:input label='t' name='title'/>
<r:textarea label='t' name='text'/>
</r:form>

<div id='filter3_preview' do='block' do='preview_node'>
<r:ajax?>
<span id='filter3_msg' do='if' test='title == "Foobar"'>PREVIEW</span>
</r:ajax?>
<h3 id='filter3_title' do='title'/>
<div id='filter3_zazen' do='zazen' attr='text'/>
</div>
</div>

//! form1
<div class='test' id='form1' do='selenium'>
<h3>form1</h3>
Expand Down Expand Up @@ -365,7 +382,7 @@
<div class='test' id='grid1' do='selenium'>
<h3>grid editing</h3>
<p>Attribute based grid.</p>
<r:form redir='url(this, :test =&gt;"grid1")'>
<r:form redir='url(this, :test => "grid1")'>
<r:grid attr='origin'/>
<r:input type='submit'/>
</r:form>
Expand Down
14 changes: 7 additions & 7 deletions test/integration/query_node/basic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pages_or_letters:
context:
node: zena
src: pages or letters
sql: "[%Q{SELECT nodes.* FROM nodes WHERE #{secure_scope('nodes')} AND ((nodes.kpath LIKE 'NP%' AND nodes.parent_id = ?) OR (nodes.kpath LIKE 'NNL%' AND nodes.parent_id = ?)) GROUP BY id ORDER BY nodes.zip ASC}, @node.id, @node.id]"
sql: "[%Q{SELECT nodes.* FROM nodes WHERE #{secure_scope('nodes')} AND ((nodes.kpath LIKE 'NP%' AND nodes.parent_id = ?) OR (nodes.kpath LIKE 'NNL%' AND nodes.parent_id = ?)) GROUP BY nodes.id ORDER BY nodes.zip ASC}, @node.id, @node.id]"
res: "people, projects list, Collections, Kill the bugs, Nature, zena enhancements, Skins (layout themes)"

project:
Expand All @@ -62,19 +62,19 @@ min:

many_alternatives:
src: tagged in site or images in site or tags in site
sql: "%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links,nodes WHERE #{secure_scope('nodes')} AND ((nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags)) OR (nodes.kpath LIKE 'NDI%' AND links.id = 0) OR (nodes.kpath LIKE 'NPT%' AND links.id = 0)) GROUP BY nodes.id ORDER BY nodes.zip ASC}"
sql: "%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links JOIN nodes WHERE #{secure_scope('nodes')} AND ((nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags)) OR (nodes.kpath LIKE 'NDI%' AND links.id = 0) OR (nodes.kpath LIKE 'NPT%' AND links.id = 0)) GROUP BY nodes.id ORDER BY nodes.zip ASC}"
# res: "art, bird, Clean Water project, flower, it's a lake, menu, news, opening, tree"

alternatives_same_join:
src: tagged in site or icons
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links,nodes WHERE #{secure_scope('nodes')} AND ((nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags)) OR (nodes.id = links.target_id AND links.relation_id = _ID(node_has_an_icon) AND links.source_id = ?)) GROUP BY nodes.id ORDER BY nodes.zip ASC}, @node.id]"
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links JOIN nodes WHERE #{secure_scope('nodes')} AND ((nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags)) OR (nodes.id = links.target_id AND links.relation_id = _ID(node_has_an_icon) AND links.source_id = ?)) GROUP BY nodes.id ORDER BY nodes.zip ASC}, @node.id]"
res: "Clean Water project, it's a lake, super ouverture"

same_name_as_class:
context:
node: 'opening'
src: "set_tags"
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links,nodes WHERE #{secure_scope('nodes')} AND nodes.id = links.target_id AND links.relation_id = _ID(node_has_tags) AND links.source_id = ? ORDER BY nodes.zip ASC}, @node.id]"
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links JOIN nodes WHERE #{secure_scope('nodes')} AND nodes.id = links.target_id AND links.relation_id = _ID(node_has_tags) AND links.source_id = ? ORDER BY nodes.zip ASC}, @node.id]"
res: "Art, News list"


Expand Down Expand Up @@ -109,19 +109,19 @@ children_with_order_clause:
notes_or_news:
context:
class: Project
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links,nodes WHERE #{secure_scope('nodes')} AND ((nodes.kpath LIKE 'NN%' AND nodes.parent_id = ? AND links.id = 0) OR (nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = ?)) GROUP BY nodes.id ORDER BY nodes.zip ASC}, @node.id, @node.id]"
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links JOIN nodes WHERE #{secure_scope('nodes')} AND ((nodes.kpath LIKE 'NN%' AND nodes.parent_id = ? AND links.id = 0) OR (nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = ?)) GROUP BY nodes.id ORDER BY nodes.zip ASC}, @node.id, @node.id]"

news_or_notes:
context:
class: Project
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links,nodes WHERE #{secure_scope('nodes')} AND ((nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = ?) OR (nodes.kpath LIKE 'NN%' AND nodes.parent_id = ? AND links.id = 0)) GROUP BY nodes.id ORDER BY nodes.zip ASC}, @node.id, @node.id]"
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links JOIN nodes WHERE #{secure_scope('nodes')} AND ((nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = ?) OR (nodes.kpath LIKE 'NN%' AND nodes.parent_id = ? AND links.id = 0)) GROUP BY nodes.id ORDER BY nodes.zip ASC}, @node.id, @node.id]"

order_l_status:
context:
node: art
class: Tag
src: "tagged order by l_status ASC, zip ASC"
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links,nodes WHERE #{secure_scope('nodes')} AND nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags) AND links.target_id = ? ORDER BY links.status ASC, nodes.zip ASC}, @node.id]"
sql: "[%Q{SELECT nodes.*,links.id AS `link_id`,links.status AS `l_status`,links.comment AS `l_comment`,links.date AS `l_date` FROM links JOIN nodes WHERE #{secure_scope('nodes')} AND nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags) AND links.target_id = ? ORDER BY links.status ASC, nodes.zip ASC}, @node.id]"
res: "super ouverture, Clean Water project"

order_random:
Expand Down
2 changes: 1 addition & 1 deletion test/integration/query_node/comments.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ default:
comments_from_nodes_in_site:
context:
visitor: lion
sql: "Comment: %Q{SELECT comments.* FROM comments,discussions,nodes AS no1 WHERE comments.discussion_id = discussions.id AND discussions.node_id = no1.id AND #{secure_scope('no1')} GROUP BY comments.id ORDER BY comments.created_at ASC}"
sql: "Comment: %Q{SELECT comments.* FROM comments JOIN discussions JOIN nodes AS no1 WHERE comments.discussion_id = discussions.id AND discussions.node_id = no1.id AND #{secure_scope('no1')} GROUP BY comments.id ORDER BY comments.created_at ASC}"
res: "Comment: I agree, I think this is bad, OK for me, Why ?, Travailleurs, unissez-vous !, Nice site, What about rivers ?, re: What about rivers ?, I want to become queen"

does_not_see_secret:
Expand Down
2 changes: 1 addition & 1 deletion test/integration/query_node/complex.yml
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ all_courses_for_mike:
class: Employee
src: assigned_courses from pages in project from assigned_jobs
# TODO: optimization
#sql: "[%Q{SELECT no1.*,li1.id AS link_id,links.status AS l_status,links.comment AS l_comment,links.date AS l_date FROM links,nodes,nodes AS no1,links AS li1 WHERE no1.id = li1.target_id AND li1.relation_id = 512923747 AND li1.source_id = nodes.id AND nodes.kpath LIKE 'NP%' AND nodes.project_id = links.target_id AND links.relation_id = 813976280 AND links.source_id = \#{@node.id} AND (\#{@node.secure_scope('no1')}) GROUP BY no1.id ORDER BY no1.position ASC, no1.node_name ASC}]"
#sql: "[%Q{SELECT no1.*,li1.id AS link_id,links.status AS l_status,links.comment AS l_comment,links.date AS l_date FROM links JOIN nodes JOIN nodes AS no1,links AS li1 WHERE no1.id = li1.target_id AND li1.relation_id = 512923747 AND li1.source_id = nodes.id AND nodes.kpath LIKE 'NP%' AND nodes.project_id = links.target_id AND links.relation_id = 813976280 AND links.source_id = \#{@node.id} AND (\#{@node.secure_scope('no1')}) GROUP BY no1.id ORDER BY no1.position ASC, no1.node_name ASC}]"
res: "problem formulation, information transmission, secure a site, how to use the winch, how to use the radio, fiber junction, responsabilities management, motivate your team, our vision"

all_achieved_formations_for_roger:
Expand Down

0 comments on commit aff74f1

Please sign in to comment.