Changeset 1250

Show
Ignore:
Timestamp:
2008-11-07 21:10:19 (2 months ago)
Author:
gaspard
Message:

commit 130da0eddee11ad91547e51f901da87492afd20e
Author: Gaspard Bucher <gaspard@teti.ch>

Fixed bug where 'icon' would not give priority to the node's icon compared to it's children images. With this fix, it's also possible to mix linked elements with elements that are not linked and still use 'l_status', 'l_comment' and the like.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/models/node.rb

    r1246 r1250  
    10751075    return nil if new_record? 
    10761076    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') 
    10781078    sql, uses_node_name = query.to_sql, query.uses_node_name 
    10791079    @icon = sql ? do_find(:first, eval("\"#{sql}\""), :ignore_source => !uses_node_name) : nil 
  • trunk/lib/has_relations.rb

    r1147 r1250  
    6161       
    6262      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 
    6464      end 
    6565       
  • trunk/lib/node_query.rb

    r1201 r1250  
    99  load_custom_queries File.join(File.dirname(__FILE__), 'custom_queries') 
    1010 
     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 
    1117   
    1218  def initialize(query, opts = {}) 
     
    4046  def after_parse 
    4147    @where.unshift "(\#{#{@node_name}.secure_scope('#{table}')})" 
    42     if @tables.include?('links') && safe_links_attributes? 
     48    if @tables.include?('links') 
    4349      @select << "#{table('links')}.id AS link_id, links.status AS l_status, links.comment AS l_comment" 
    4450    elsif @errors_unless_safe_links 
     
    6066   
    6167  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| 
    6472        unless f =~ /links\./ 
    65           return false 
     73          f << " AND links.id = -1" 
    6674        end 
    6775      end 
     
    213221      when 'l_'   
    214222        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)
    216224          @errors_unless_safe_links ||= [] 
    217225          @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  
    16071607    def r_icon 
    16081608      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')) 
    16101610        return unless finder 
    16111611        return parser_error("invalid class (#{klass})") unless klass.ancestors.include?(Node) 
  • trunk/lib/query_builder/lib/query_builder.rb

    r1197 r1250  
    313313    end 
    314314     
    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(', ')}" 
    323327    end 
    324328     
     
    441445       
    442446      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 ')] 
    446450      end 
    447451       
     
    455459        merge_tables(query) 
    456460        @distinct ||= query.distinct 
    457         where << query.where.reverse.join(' AND ') 
    458       end 
    459        
    460       @alt_where = where 
     461        where_list << query.where.reverse.join(' AND ') 
     462      end 
     463       
     464      @where_list = where_list 
    461465       
    462466      @tables.uniq! 
     467       
     468      fix_where_list(where_list) 
    463469       
    464470      if counter > 1 
    465471        @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 
    469475      end 
    470476    end 
     
    708714      "invalid clause #{clause.inspect} near \"#{res[-2..-1]}#{rest[0..1]}\"" 
    709715    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 
    710721end 
  • trunk/lib/query_builder/test/query_builder/basic.yml

    r1197 r1250  
    2626  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" 
    2727 
     28order_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 
    2832limit: 
    2933  src: "objects in project limit 2" 
     
    4852  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" 
    4953 
     54recipients_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 
    5060custom_query: 
    5161  context: 
  • trunk/lib/query_builder/test/query_builder/joins.yml

    r1028 r1250  
    2020letters_in_project_from_letters: 
    2121  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 
     23letters_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  
    8181  class FoxyParser 
    8282    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     
    8389    def initialize(table_name, opts={}) 
    8490      @table = table_name 
     
    110116        out "\n# ========== #{site} ===========" 
    111117        content = File.read(fixtures_path) + "\n" 
     118         
    112119        # build simple hash to set/get defaults and other special values 
    113120        content.gsub!(/<%.*?%>/m,'') 
     
    244251          @file.puts "# Fixtures generated from content of 'sites' folder by FoxyParser (rake zena:build_fixtures)" 
    245252          @file.puts "" 
     253          @file.puts self.class.prelude 
    246254        end 
    247255        @file.puts res 
     
    651659  class FoxyLinkParser < FoxyParser 
    652660    attr_reader :nodes 
     661     
     662    def self.prelude 
     663      "dummy:\n  id: -1\n" 
     664    end 
     665     
    653666    def initialize(table_name, opts = {}) 
    654667      super 
  • trunk/test/helpers/node_query/basic.yml

    r1203 r1250  
    5757    - 'images in site' 
    5858    - '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" 
    6060  res: 'art, bird, cleanWater, flower, lake, menu, news, opening, tree' 
    6161 
     
    114114    - notes 
    115115    - 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" 
    117117 
    118118news_or_notes: 
     
    120120    - news 
    121121    - 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" 
    123123 
    124124order_dynattr: 
  • trunk/test/helpers/node_query/relations.yml

    r1168 r1250  
    2222    - 'icons' 
    2323    - '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" 
    2525 
    2626projects_from_tags: 
  • trunk/test/helpers/testhelp.rb

    r1189 r1250  
    22require File.join(File.dirname(__FILE__), '..','..','lib','yaml_test') 
    33begin 
    4   require 'turn' 
    5   Test::Unit::UI::Console::TestRunner.use_progressbar = true 
     4#  require 'turn' 
     5#  Test::Unit::UI::Console::TestRunner.use_progressbar = true 
    66rescue MissingSourceFile 
    77  # ignore missing 'turn' testing gem 
  • trunk/test/helpers/zena_parser/basic.yml

    r1231 r1250  
    713713  src: "<r:img alt_src='icon'/>" 
    714714  res: "<img src='/en/projects/cleanWater/image24_std.jpg' width='545' height='400' alt='it&apos;s a lake' class='std'/>" 
     715 
     716icon: 
     717  context: 
     718    node: 'cleanWater' 
     719  src: "<r:icon do='[name]'/>" 
     720  res: "lake" 
    715721 
    716722img_image: 
  • trunk/test/unit/node_test.rb

    r1232 r1250  
    11891189   
    11901190  def test_icon_by_first_child 
    1191     login(:ant
    1192     node = secure!(Node) { nodes(:wiki) } # has an 'icon' relation 
     1191    login(:tiger
     1192    node = secure!(Node) { nodes(:wiki) } # has no 'icon' relation 
    11931193    icon = node.icon 
    11941194    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 
    11961202  end 
    11971203   
  • trunk/vendor/plugins/zena_captcha/patch/application_helper.rb

    r1249 r1250  
    44   
    55  # 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. 
    77  def truncate(text, length = 30, truncate_string = "...") 
    88    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 
    1211    end 
    1312  end