Changeset 1250
- Timestamp:
- 2008-11-07 21:10:19 (2 months ago)
- Files:
-
- trunk/app/models/node.rb (modified) (1 diff)
- trunk/lib/has_relations.rb (modified) (1 diff)
- trunk/lib/node_query.rb (modified) (4 diffs)
- trunk/lib/parser/lib/rules/zena.rb (modified) (1 diff)
- trunk/lib/query_builder/lib/query_builder.rb (modified) (4 diffs)
- trunk/lib/query_builder/test/query_builder/basic.yml (modified) (2 diffs)
- trunk/lib/query_builder/test/query_builder/joins.yml (modified) (1 diff)
- trunk/lib/tasks/zena.rake (modified) (4 diffs)
- trunk/test/helpers/node_query/basic.yml (modified) (3 diffs)
- trunk/test/helpers/node_query/relations.yml (modified) (1 diff)
- trunk/test/helpers/testhelp.rb (modified) (1 diff)
- trunk/test/helpers/zena_parser/basic.yml (modified) (1 diff)
- trunk/test/unit/node_test.rb (modified) (1 diff)
- trunk/vendor/plugins/zena_captcha/patch/application_helper.rb (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/app/models/node.rb
r1246 r1250 1075 1075 return nil if new_record? 1076 1076 return @icon if defined? @icon 1077 query = Node.build_find(:first, ['icon order by l_id desc', 'image'], :node_name => 'self')1077 query = Node.build_find(:first, ['icon group by id,l_id order by l_id desc, position asc, name asc', 'image'], :node_name => 'self') 1078 1078 sql, uses_node_name = query.to_sql, query.uses_node_name 1079 1079 @icon = sql ? do_find(:first, eval("\"#{sql}\""), :ignore_source => !uses_node_name) : nil trunk/lib/has_relations.rb
r1147 r1250 61 61 62 62 def link_id 63 @link ? @link[:id] : self[:link_id]63 @link ? @link[:id] : (self[:link_id] == -1 ? nil : self[:link_id]) # -1 == dummy link 64 64 end 65 65 trunk/lib/node_query.rb
r1201 r1250 9 9 load_custom_queries File.join(File.dirname(__FILE__), 'custom_queries') 10 10 11 # make sure there exists a link with NULL content and id == -1 (used in queries using query order sorting) 12 if l = Link.find_by_id(-1) 13 # do nothing 14 else 15 Link.connection.execute "INSERT INTO #{Link.table_name} (id,target_id,source_id,status,comment) VALUES (-1,NULL,NULL,NULL,NULL)" 16 end 11 17 12 18 def initialize(query, opts = {}) … … 40 46 def after_parse 41 47 @where.unshift "(\#{#{@node_name}.secure_scope('#{table}')})" 42 if @tables.include?('links') && safe_links_attributes?48 if @tables.include?('links') 43 49 @select << "#{table('links')}.id AS link_id, links.status AS l_status, links.comment AS l_comment" 44 50 elsif @errors_unless_safe_links … … 60 66 61 67 private 62 def safe_links_attributes? 63 (@alt_where || []).each do |f| 68 # Make sure all alternate queries include "links.id = -1" (dummy link) 69 def fix_where_list(where_list) 70 return unless @tables.include?('links') 71 where_list.each do |f| 64 72 unless f =~ /links\./ 65 return false73 f << " AND links.id = -1" 66 74 end 67 75 end … … 213 221 when 'l_' 214 222 key, function = parse_sql_function_in_field(field) 215 if key == 'l_status' || key == 'l_comment' || (key == 'l_id' && context == :order)223 if key == 'l_status' || key == 'l_comment' || (key == 'l_id' && [:order, :group].include?(context)) 216 224 @errors_unless_safe_links ||= [] 217 225 @errors_unless_safe_links << "cannot use link field '#{key}' in this query" unless (key == 'l_id' && context == :order) trunk/lib/parser/lib/rules/zena.rb
r1242 r1250 1607 1607 def r_icon 1608 1608 if !@params[:in] && !@params[:where] && !@params[:from] && !@params[:find] 1609 finder, klass = build_finder_for(:first, 'icon', @params.merge(:or => 'image', :order => 'l_id desc '))1609 finder, klass = build_finder_for(:first, 'icon', @params.merge(:or => 'image', :order => 'l_id desc, position asc, name asc', :group => 'id,l_id')) 1610 1610 return unless finder 1611 1611 return parser_error("invalid class (#{klass})") unless klass.ancestors.include?(Node) trunk/lib/query_builder/lib/query_builder.rb
r1197 r1250 313 313 end 314 314 315 def parse_group_clause(field) 316 return @group unless field 317 if fld = map_field(field, table, :group) 318 " GROUP BY #{fld}" 319 else 320 @errors << "invalid field '#{field}'" 321 nil 322 end 315 def parse_group_clause(group) 316 return @group unless group 317 res = [] 318 319 group.split(',').each do |field| 320 if fld = map_field(field, table, :group) 321 res << fld 322 else 323 @errors << "invalid field '#{field}'" 324 end 325 end 326 res == [] ? nil : " GROUP BY #{res.join(', ')}" 323 327 end 324 328 … … 441 445 442 446 if @where.compact == [] 443 where = []444 else 445 where = [@where.compact.reverse.join(' AND ')]447 where_list = [] 448 else 449 where_list = [@where.compact.reverse.join(' AND ')] 446 450 end 447 451 … … 455 459 merge_tables(query) 456 460 @distinct ||= query.distinct 457 where << query.where.reverse.join(' AND ')458 end 459 460 @ alt_where = where461 where_list << query.where.reverse.join(' AND ') 462 end 463 464 @where_list = where_list 461 465 462 466 @tables.uniq! 467 468 fix_where_list(where_list) 463 469 464 470 if counter > 1 465 471 @distinct = @tables.size > 1 466 @where = ["((#{where .join(') OR (')}))"]467 else 468 @where = where 472 @where = ["((#{where_list.join(') OR (')}))"] 473 else 474 @where = where_list 469 475 end 470 476 end … … 708 714 "invalid clause #{clause.inspect} near \"#{res[-2..-1]}#{rest[0..1]}\"" 709 715 end 716 717 # Make sure all clauses are compatible (where_list is a list of strings, not arrays) 718 def fix_where_list(where_list) 719 # do nothing 720 end 710 721 end trunk/lib/query_builder/test/query_builder/basic.yml
r1197 r1250 26 26 res: "SELECT objects.* FROM objects,links WHERE objects.id = links.target_id AND links.relation_id = 4 AND links.source_id = ID ORDER BY objects.name ASC" 27 27 28 order_many_params: 29 src: "objects in project order by name ASC, id DESC" 30 res: "SELECT objects.* FROM objects WHERE objects.project_id = PROJECT_ID ORDER BY objects.name ASC, objects.id DESC" 31 28 32 limit: 29 33 src: "objects in project limit 2" … … 48 52 res: "SELECT objects.* FROM objects,links WHERE ((objects.id = links.target_id AND links.relation_id = 4 AND links.source_id = ID) OR (objects.parent_id = ID)) GROUP BY objects.id" 49 53 54 recipients_or_objects_order_query1: 55 src: 56 - "recipients order by query1" 57 - "objects" 58 res: "SELECT objects.*,(objects.id = links.target_id AND links.relation_id = 4 AND links.source_id = ID) AS query1 FROM objects,links WHERE ((objects.id = links.target_id AND links.relation_id = 4 AND links.source_id = ID) OR (objects.parent_id = ID)) GROUP BY objects.id ORDER BY query1 ASC" 59 50 60 custom_query: 51 61 context: trunk/lib/query_builder/test/query_builder/joins.yml
r1028 r1250 20 20 letters_in_project_from_letters: 21 21 res: "SELECT ob1.* FROM objects,objects AS ob1 WHERE ob1.kpath LIKE 'NNL%' AND ob1.project_id = objects.id AND objects.kpath LIKE 'NNL%' AND objects.parent_id = ID GROUP BY ob1.id" 22 23 letters_in_project_from_letters_group_by: 24 src: "letters in project from letters group by name,id" 25 res: "SELECT ob1.* FROM objects,objects AS ob1 WHERE ob1.kpath LIKE 'NNL%' AND ob1.project_id = objects.id AND objects.kpath LIKE 'NNL%' AND objects.parent_id = ID GROUP BY ob1.name, ob1.id" trunk/lib/tasks/zena.rake
r1231 r1250 81 81 class FoxyParser 82 82 attr_reader :column_names, :table, :elements, :site, :name, :defaults 83 84 # included at start of fixture file 85 def self.prelude 86 "" 87 end 88 83 89 def initialize(table_name, opts={}) 84 90 @table = table_name … … 110 116 out "\n# ========== #{site} ===========" 111 117 content = File.read(fixtures_path) + "\n" 118 112 119 # build simple hash to set/get defaults and other special values 113 120 content.gsub!(/<%.*?%>/m,'') … … 244 251 @file.puts "# Fixtures generated from content of 'sites' folder by FoxyParser (rake zena:build_fixtures)" 245 252 @file.puts "" 253 @file.puts self.class.prelude 246 254 end 247 255 @file.puts res … … 651 659 class FoxyLinkParser < FoxyParser 652 660 attr_reader :nodes 661 662 def self.prelude 663 "dummy:\n id: -1\n" 664 end 665 653 666 def initialize(table_name, opts = {}) 654 667 super trunk/test/helpers/node_query/basic.yml
r1203 r1250 57 57 - 'images in site' 58 58 - 'tags in site' 59 sql: "SELECT nodes.* FROM nodes,links WHERE ((nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags)) OR (nodes.kpath LIKE 'NDI%') OR (nodes.kpath LIKE 'NPT%')) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC"59 sql: "SELECT nodes.*,links.id AS link_id, links.status AS l_status, links.comment AS l_comment FROM nodes,links WHERE ((nodes.id = links.source_id AND links.relation_id = _ID(node_has_tags)) OR (nodes.kpath LIKE 'NDI%' AND links.id = -1) OR (nodes.kpath LIKE 'NPT%' AND links.id = -1)) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC" 60 60 res: 'art, bird, cleanWater, flower, lake, menu, news, opening, tree' 61 61 … … 114 114 - notes 115 115 - news 116 sql: "SELECT nodes.* FROM nodes,links WHERE ((nodes.kpath LIKE 'NN%' AND nodes.parent_id = #{@node.id}) OR (nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = #{@node.id})) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC"116 sql: "SELECT nodes.*,links.id AS link_id, links.status AS l_status, links.comment AS l_comment FROM nodes,links WHERE ((nodes.kpath LIKE 'NN%' AND nodes.parent_id = #{@node.id} AND links.id = -1) OR (nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = #{@node.id})) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC" 117 117 118 118 news_or_notes: … … 120 120 - news 121 121 - notes 122 sql: "SELECT nodes.* FROM nodes,links WHERE ((nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = #{@node.id}) OR (nodes.kpath LIKE 'NN%' AND nodes.parent_id = #{@node.id})) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC"122 sql: "SELECT nodes.*,links.id AS link_id, links.status AS l_status, links.comment AS l_comment FROM nodes,links WHERE ((nodes.id = links.source_id AND links.relation_id = _ID(note_has_calendars) AND links.target_id = #{@node.id}) OR (nodes.kpath LIKE 'NN%' AND nodes.parent_id = #{@node.id} AND links.id = -1)) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC" 123 123 124 124 order_dynattr: trunk/test/helpers/node_query/relations.yml
r1168 r1250 22 22 - 'icons' 23 23 - 'images' 24 sql: "SELECT nodes.* FROM nodes,links WHERE ((nodes.id = links.target_id AND links.relation_id = _ID(node_has_an_icon) AND links.source_id = #{@node.id}) OR (nodes.kpath LIKE 'NDI%' AND nodes.parent_id = #{@node.id})) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC"24 sql: "SELECT nodes.*,links.id AS link_id, links.status AS l_status, links.comment AS l_comment FROM nodes,links WHERE ((nodes.id = links.target_id AND links.relation_id = _ID(node_has_an_icon) AND links.source_id = #{@node.id}) OR (nodes.kpath LIKE 'NDI%' AND nodes.parent_id = #{@node.id} AND links.id = -1)) AND (#{@node.secure_scope('nodes')}) GROUP BY nodes.id ORDER BY nodes.position ASC, nodes.name ASC" 25 25 26 26 projects_from_tags: trunk/test/helpers/testhelp.rb
r1189 r1250 2 2 require File.join(File.dirname(__FILE__), '..','..','lib','yaml_test') 3 3 begin 4 require 'turn'5 Test::Unit::UI::Console::TestRunner.use_progressbar = true4 # require 'turn' 5 # Test::Unit::UI::Console::TestRunner.use_progressbar = true 6 6 rescue MissingSourceFile 7 7 # ignore missing 'turn' testing gem trunk/test/helpers/zena_parser/basic.yml
r1231 r1250 713 713 src: "<r:img alt_src='icon'/>" 714 714 res: "<img src='/en/projects/cleanWater/image24_std.jpg' width='545' height='400' alt='it's a lake' class='std'/>" 715 716 icon: 717 context: 718 node: 'cleanWater' 719 src: "<r:icon do='[name]'/>" 720 res: "lake" 715 721 716 722 img_image: trunk/test/unit/node_test.rb
r1232 r1250 1189 1189 1190 1190 def test_icon_by_first_child 1191 login(: ant)1192 node = secure!(Node) { nodes(:wiki) } # has an'icon' relation1191 login(:tiger) 1192 node = secure!(Node) { nodes(:wiki) } # has no 'icon' relation 1193 1193 icon = node.icon 1194 1194 assert_kind_of Image, icon 1195 assert_equal nodes_id(:bird_jpg), icon[:id] 1195 assert_equal nodes_id(:bird_jpg), icon[:id] # first child 1196 # define flower as icon 1197 assert node.update_attributes(:icon_id => nodes_id(:flower_jpg)) 1198 node = secure!(Node) { nodes(:wiki) } # reload 1199 icon = node.icon 1200 assert_kind_of Image, icon 1201 assert_equal nodes_id(:flower_jpg), icon[:id] # icon 1196 1202 end 1197 1203 trunk/vendor/plugins/zena_captcha/patch/application_helper.rb
r1249 r1250 4 4 5 5 # FIXME: remove when rails 2.0. 6 # not present in our version of rails 6 # not present in our version of rails. This version is not multi-byte. 7 7 def truncate(text, length = 30, truncate_string = "...") 8 8 if text 9 l = length - truncate_string.chars.length 10 chars = text.chars 11 (chars.length > length ? chars[0...l] + truncate_string : text).to_s 9 l = length - truncate_string.length 10 text.length > length ? text[0..l] + truncate_string : text 12 11 end 13 12 end
