Changeset 370

Show
Ignore:
Timestamp:
2007-03-23 14:33:07 (2 years ago)
Author:
gaspard
Message:

[test] Tests for helpers pass
[zafu] Respect text indentation when looping (each)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/app/controllers/application.rb

    r361 r370  
    192192 
    193193  # TODO: implement 
    194   def template_url_for_asset(type,url) 
     194  def template_url_for_asset(opts) 
     195     
    195196    # 1. find in current skin ? 
    196     # 2. find in corresponding public directory ? 
    197     case type 
    198     when :stylesheet 
    199     when :link 
    200     when :script 
    201     end 
    202     # 3. find in site ? 
     197    url = opts[:current_template][1..-1].split('/') + opts[:src].split('/') 
     198    url.compact! 
     199    skin_name = url.shift 
     200    if @skin_obj && @skin_obj[:name] == skin_name 
     201      skin = @skin_obj 
     202    end 
     203    skin ||= secure(Skin) { Skin.find_by_name(skin_name) } 
     204    asset = skin.asset_for_path(url.join('/'), Document) 
     205    asset ? url_for(data_url(asset).merge(:only_path=>true)) : nil 
     206  rescue ActiveRecord::RecordNotFound 
     207    return nil 
    203208  end 
    204209   
     
    231236    new_lang = nil 
    232237    if params[:lang] 
    233       if visitor.site[:languages].include?(params[:lang]) 
     238      if visitor.site.lang_list.include?(params[:lang]) 
    234239        new_lang = params[:lang] 
    235240      else 
     
    237242      end 
    238243    elsif params[:prefix] && params[:prefix] != AUTHENTICATED_PREFIX 
    239       if visitor.site.languages.split(',').include?(params[:prefix]) 
     244      if visitor.site.lang_list.include?(params[:prefix]) 
    240245        session[:lang] = params[:prefix] 
    241246      else 
  • trunk/app/helpers/application_helper.rb

    r361 r370  
    718718        actions << version_form_action('propose',version[:id]) 
    719719      when Zena::Status[:del] 
    720         if (version[:user_id] == session[:user][:id]) 
     720        if (version[:user_id] == visitor[:id]) 
    721721          actions << version_form_action('edit',version[:id]) if @node.can_edit_lang?(version.lang) 
    722722        end 
     
    797797   
    798798  def change_lang(new_lang) 
    799     if session[:user] 
     799    if visitor.is_anon? 
     800      {:overwrite_params => { :prefix => new_lang }} 
     801    else 
    800802      {:overwrite_params => { :lang => new_lang }} 
    801     else 
    802       {:overwrite_params => { :prefix => new_lang }} 
    803803    end 
    804804  end 
     
    856856      [show_link(:home), show_link(:preferences), show_link(:comments), show_link(:users), show_link(:groups), show_link(:translation)].reject {|l| l==''} 
    857857    when :home 
    858       return '' unless session[:user] 
     858      return '' if visitor.is_anon? 
    859859      tlink_to_with_state('my home', :controller=>'user', :action=>'home') 
    860860    when :preferences 
    861       return '' unless session[:user] 
     861      return '' if visitor.is_anon? 
    862862      tlink_to_with_state('preferences', :controller=>'preferences', :action=>'list') 
    863863    when :translation 
     
    911911    else 
    912912      res = [] 
    913       visitor.site[:languages].sort.each do |l| 
     913      visitor.site.lang_list.sort.each do |l| 
    914914        if l == lang 
    915915          res << "<b>#{l}</b>" 
    916916        else 
    917           if session[:user] 
     917          if visitor.is_anon? 
     918            res << link_to(l, request.parameters.merge(:prefix=>l)) 
     919          else 
    918920            res << link_to(l, request.parameters.merge(:lang=>l)) 
    919           else 
    920             res << link_to(l, request.parameters.merge(:prefix=>l)) 
    921921          end 
    922922        end 
  • trunk/app/models/contact.rb

    r245 r370  
    11class Contact < Page 
    22  link :contact_for, :class_name=>'Project', :as=>'contact', :as_unique=>true 
     3  link :favorites,   :class_name=>'Node' 
    34   
    45  private 
  • trunk/app/models/site.rb

    r366 r370  
    191191    [anon_id, su_id] 
    192192  end 
     193   
     194  # Return an array with the languages for the site. 
     195  def lang_list 
     196    (self[:languages] || "").split(',').map(&:strip) 
     197  end 
    193198end 
  • trunk/app/models/skin.rb

    r352 r370  
    2424   
    2525  def template_for_path(path) 
     26    asset_for_path(path, Template) 
     27  end 
     28   
     29   
     30  def asset_for_path(path, klass=Node) 
    2631    current = self 
    2732    if path == 'any' 
     
    3136      while path != [] 
    3237        template_name = path.shift 
    33         current = secure(Template) { Template.find(:first, :conditions=>["parent_id = ? AND name = ?", current[:id], template_name])} 
     38        current = secure(klass) { klass.find(:first, :conditions=>["parent_id = ? AND name = ?", current[:id], template_name])} 
    3439      end 
    3540      current 
     
    3742  rescue ActiveRecord::RecordNotFound 
    3843    nil 
    39   end    
     44  end 
    4045end 
  • trunk/app/models/user.rb

    r366 r370  
    8080  end 
    8181   
     82  def contact 
     83    secure(Contact) { Contact.find(self[:contact_id]) } 
     84  rescue ActiveRecord::RecordNotFound 
     85    nil 
     86  end 
     87   
    8288  # Each time a node is found using secure (Zena::Acts::Secure or Zena::Acts::SecureNode), this method is 
    8389  # called to set the visitor in the found object. This is also used to keep track of the opened nodes 
     
    321327      end 
    322328       
    323       added_site_ids = @defined_site_ids.reject do |id| 
    324         site_ids.include?(id) 
    325       end 
     329      added_site_ids = @defined_site_ids - site_ids 
    326330       
    327       removed_site_ids = site_ids.reject do |id| 
    328         @defined_site_ids.include?(id) 
    329       end 
     331      removed_site_ids = site_ids - @defined_site_ids 
    330332       
    331333      # make sure visitor is an admin in all the modified site ids: 
  • trunk/db/init/images.yml

    r355 r370  
    179179    lang:      en 
    180180    value:     "<img src='/images/layers.png' alt='diff'/>" 
     181 
     182  - phrase_id:    102 
     183    lang:      en 
     184    value:     "<img src='/images/heart.png' alt='favorite'/>" 
  • trunk/db/init/trans_phrases.yml

    r355 r370  
    302302  - id:   101 
    303303    phrase:  'btn_diff' 
     304 
     305  - id:   102 
     306    phrase:  'favorite' 
  • trunk/lib/parser/lib/parser.rb

    r340 r370  
    1919    end 
    2020 
    21     def template_url_for_asset(type,url
    22       "/test_#{type}/#{url}" 
     21    def template_url_for_asset(opts
     22      "/test_#{opts[:type]}/#{opts[:src]}" 
    2323    end 
    2424 
     
    4343 
    4444class Parser 
    45   attr_accessor :text, :method 
    46   attr_accessor :pass 
     45  attr_accessor :text, :method, :pass, :options 
    4746     
    4847  class << self 
     
    311310    # puts "MADE #{new_obj.inspect}" 
    312311    # puts "TEXT #{@text.inspect}" 
     312    new_obj 
    313313  end 
    314314   
  • trunk/lib/parser/lib/rules/zafu.rb

    r315 r370  
    2020      res = super 
    2121      if (@context[:parts] || {})[@context[:name]] 
    22         res 
     22        (@space_before || '') + res + (@space_after || '') 
    2323      else 
    2424        render_html_tag(res) 
     
    4949     
    5050    def render_html_tag(text) 
    51       return text unless @html_tag && !@html_tag_done 
    52       res = "<#{@html_tag}#{params_to_html(@html_tag_params || {})}" 
    53       if text != '' 
    54         res << ">#{text}</#{@html_tag}>" 
    55       else 
    56         res << "/>" 
     51      return text if @html_tag_done 
     52      if @html_tag 
     53        res = "<#{@html_tag}#{params_to_html(@html_tag_params || {})}" 
     54        if text != '' 
     55          res << ">#{text}</#{@html_tag}>" 
     56        else 
     57          res << "/>" 
     58        end 
     59      else 
     60        res = text 
    5761      end 
    5862      @html_tag_done = true 
    59       res 
     63      (@space_before || '') + res + (@space_after || '') 
    6064    end 
    6165     
     
    6367      return expand_with unless @html_tag 
    6468      unless @params[:src][0..0] == '/' 
     69        opts = {:src => @params[:src]} 
    6570        case @html_tag 
    6671        when 'link' 
    6772          if @params[:rel].downcase == 'stylesheet' 
    68             @params[:src] = @options[:helper].send(:template_url_for_asset, :stylesheet , @params[:src]) 
     73            opts[:type] = :stylesheet 
    6974          else 
    70             @params[:src] = @options[:helper].send(:template_url_for_asset, :link, @params[:src]) 
    71           end 
    72         else 
    73           @params[:src] = @options[:helper].send(:template_url_for_asset, @html_tag.to_sym , @params[:src]) 
    74         end 
     75            opts[:type] = :link 
     76          end 
     77        else 
     78          opts[:type] = @html_tag.to_sym 
     79        end 
     80        opts[:current_template] = @options[:included_history].last 
     81        @params[:src] = @options[:helper].send(:template_url_for_asset, opts) 
    7582      end 
    7683      res   = "<#{@html_tag}#{params_to_html(@params)}" 
     
    100107      @end_tag_count  = 1 
    101108       
     109      # code indentation 
     110      @space_before = @options[:space_before] 
     111      @options.delete(:space_before) 
     112       
     113      # puts "[#{@space_before}(#{@method})#{@space_after}]" 
    102114      if @params =~ /\A([^>]*?)do\s*=('|")([^\2]*?[^\\])\2([^>]*)\Z/   
    103115        # we have a sub 'do' 
     
    108120        opts[:end_do] = @end_tag 
    109121         
    110         make(:void, opts) 
     122        sub = make(:void, opts) 
     123        @space_after = sub.instance_variable_get(:@space_after) 
     124        sub.instance_variable_set(:@space_after,"") 
    111125        if @method == 'include' 
    112126          include_template 
     
    122136        end 
    123137      end 
    124        
    125138      if !@html_tag && (@html_tag = @params[:tag]) 
    126139        @params.delete(:tag) 
     
    142155    def scan 
    143156      # puts "SCAN(#{@method}): [#{@text}]" 
    144       if @text =~ /\A([^<]*)</ 
     157      if @text =~ /\A([^<]*?)(^ *|)</m 
    145158        flush $1 
     159        eat $2 
    146160        if @text[1..1] == '/' 
     161          store $2 
    147162          scan_close_tag 
    148163        elsif @text[0..3] == '<!--' 
    149           scan_html_comment 
    150         else 
    151           scan_tag 
     164          scan_html_comment(:space_before=> $2) 
     165        else 
     166          scan_tag(:space_before=> $2) 
    152167        end 
    153168      else 
     
    158173   
    159174    def scan_close_tag 
    160       if @text =~ /\A<\/([^>]+)>/ 
     175      if @text =~ /\A<\/([^>]+)>( *\n+|)/m 
    161176        # puts "CLOSE:[#{$&}]}" # ztag 
    162177        # closing tag 
     
    165180          if @end_tag_count == 0 
    166181            eat $& 
     182            @space_after = $2 
    167183            leave 
    168184          else   
     
    188204    end 
    189205 
    190     def scan_html_comment 
     206    def scan_html_comment(opts={}) 
    191207      if @text =~ /<!--\|(.*?)-->/m 
    192208        # zafu html escaped 
    193209        eat $& 
    194         @text = $1 + @text 
     210        @text = opts[:space_before] + $1 + @text 
    195211      elsif @text =~ /<!--.*?-->/m 
    196212        # html comment 
     
    202218    end 
    203219   
    204     def scan_tag 
     220    def scan_tag(opts={}) 
    205221      # puts "TAG(#{@method}): [#{@text}]" 
    206222      if @text =~ /\A<z:(\w+)([^>]*?)(\/?)>/ 
    207223        # puts "ZTAG:[#{$&}]}" # ztag 
    208224        eat $& 
    209         opts = {:method=>$1, :params=>$2} 
     225        opts.merge!(:method=>$1, :params=>$2) 
    210226        opts.merge!(:text=>'') if $3 != '' 
    211227        make(:void, opts) 
     
    213229        # puts "DO:[#{$&}]}" # do tag 
    214230        eat $& 
    215         opts = {:method=>$4, :html_tag=>$1, :html_tag_params=>$2, :params=>$5} 
     231        opts.merge!(:method=>$4, :html_tag=>$1, :html_tag_params=>$2, :params=>$5) 
    216232        opts.merge!(:text=>'') if $6 != '' 
    217233        make(:void, opts) 
  • trunk/lib/parser/lib/rules/zena.rb

    r326 r370  
    5656      elsif @params[:attr] 
    5757        "<%= #{node}#{get_attribute(attribute)} %>" 
     58      elsif @params[:date] 
     59        # date can be any attribute v_created_at or updated_at etc. 
     60        # TODO format with @params[:format] and @params[:tformat] << translated format 
     61      else 
     62        # error 
     63      end 
     64    end 
     65     
     66    def r_zazen 
     67      attribute = @params[:attr] || @params[:tattr] 
     68      if @context[:trans] 
     69        # TODO: what do we do here with dates ? 
     70        "#{node}#{get_attribute(attribute)}" 
     71      elsif @params[:tattr] 
     72        "<%= zazen(trans(#{node}#{get_attribute(attribute)})) %>" 
     73      elsif @params[:attr] 
     74        "<%= zazen(#{node}#{get_attribute(attribute)}) %>" 
    5875      elsif @params[:date] 
    5976        # date can be any attribute v_created_at or updated_at etc. 
     
    432449          "<span class='parser_error'>No stored nodes in the current context</span>" 
    433450        end 
     451      elsif select == 'visitor' 
     452        do_var("visitor.contact") 
    434453      elsif select =~ /^\d+$/ 
    435454        do_var("secure(Node) { Node.find_by_id(#{select.inspect})} rescue nil") 
     
    782801    end 
    783802     
     803     
    784804    def render_html_tag(text) 
     805      return text if @html_tag_done 
     806      if @html_tag 
     807        res = "<#{@html_tag}#{params_to_html(@html_tag_params || {})}" 
     808        if text != '' 
     809          res << ">#{text}</#{@html_tag}>" 
     810        else 
     811          res << "/>" 
     812        end 
     813      else 
     814        res = text 
     815      end 
     816      @html_tag_done = true 
     817      (@options[:space_before] || '') + res + (@options[:space_after] || '') 
     818    end 
     819     
     820    def render_html_tag(text) 
     821      return text if @html_tag_done 
    785822      set_params = {} 
    786823      @params.each do |k,v| 
     
    790827      @html_tag = 'div' if !@html_tag && set_params != {} 
    791828       
    792       return text unless @html_tag && !@html_tag_done 
    793829      @html_tag_params ||= {} 
    794830      bak = @html_tag_params.dup 
  • trunk/lib/parser/test/parser_test.rb

    r359 r370  
    3333  testfile :zafu, :zafu_asset, :zafu_insight, :zazen 
    3434  def test_single 
    35     do_test('zazen', 'wiki_link_partial_url') 
     35    do_test('zafu', 'tada') 
    3636  end 
    3737  def test_zazen_image_no_image 
  • trunk/test/fixtures/links.yml

    r307 r370  
    3434  target_id:      12 
    3535  role:           hot 
     36   
     37nature_favorite_for_ant: 
     38  id:             7 
     39  source_id:      3 
     40  target_id:      29 
     41  role:           favorite 
  • trunk/test/fixtures/trans_phrases.yml

    r350 r370  
    402402  id:   101 
    403403  phrase:  'btn_diff' 
     404   
     405102: 
     406  id:   102 
     407  phrase:  'favorite' 
  • trunk/test/fixtures/trans_values.yml

    r350 r370  
    934934  lang:      en 
    935935  value:     "<img src='/images/layers.png' alt='diff'/>" 
     936 
     937174: 
     938  id:        174 
     939  phrase_id: 102 
     940  lang:      en 
     941  value:     "<img src='/images/heart.png' alt='favorite'/>" 
  • trunk/test/fixtures/versions.yml

    r360 r370  
    409409  title:          parc opening 
    410410  summary:        no summary yet 
    411   text:           nothing written yet 
     411  text:           | 
     412    We are glad to announce the opening of our new project ! 
     413     
     414    You are all invited to *Romanel* for a great party this weekend. 
    412415  lang:           en 
    413416  number:         2 
  • trunk/test/helpers/basic.yml

    r326 r370  
    3030  tem: "include: this &lt;% puts \"could be\" %&gt; nasty" 
    3131 
    32 rename_asset
    33   src: "<link rel='stylesheet' src='water' type='text/css'/>" 
    34   tem: "<link rel='stylesheet' src='/data/pdf/15/water.pdf' type='text/css'/>" 
     32wiki
     33  src: "<img src='bird' alt='bird'/>" 
     34  tem: "<img alt='bird' src='/data/jpg/20/bird.jpg'/>" 
    3535 
    3636show_title: 
     
    180180  src: "<div id='lang' do='lang_links'><a>en</a> | <b>fr</b></div>" 
    181181  tem: "<div id='lang'><%= lang_links(:node=>@node) %></div>" 
    182   res: "/<div id='lang'><b>en.*test_render.*lang=es.*\|.*test_render.*=en.*fr/" 
     182  res: "/<div id='lang'><b>en.*\|.*test_render.*=fr/" 
    183183 
    184184show_path: 
     
    365365  src: "<z:show_comments/>" 
    366366  tem: "<%= show_comments(:node=>@node) %>" 
    367   res: "/<div class='comment' id='comments_list'>.*reply.*add/" 
     367  res: "/<div class='comments' id='comments'>.*reply.*add/" 
    368368 
    369369content_for_layout: 
     
    376376  tem: "<% if @title_for_layout -%><%= @title_for_layout %><% else -%>blah blih<% end -%>" 
    377377  res: "blah blih" 
     378 
     379tada: 
     380  context: 
     381    node: 'opening' 
     382  src: | 
     383    <h1 id='main_title' do='show' attr='v_title'>Super project</h1> 
     384 
     385    <ul do='tags'> 
     386      <li do='each' do='link'>art</li> 
     387      <li do='ignore'>life</li> 
     388      <li do='ignore'>ruby</li> 
     389    </ul> 
     390 
     391    <div id='text' do='zazen' attr='v_text'> 
     392      <p>Lorem ipsum dolor sit amet, consectetuer elit....</p> 
     393      <h3>some title</h3> 
     394    </div> 
     395  res: | 
     396    <h1 id='main_title'>parc opening</h1> 
     397     
     398    <ul> 
     399      <li><a href="/oo/tag23.html">Art</a></li> 
     400      <li><a href="/oo/tag24.html">News list</a></li> 
     401    </ul> 
     402     
     403    <div id='text'><p>We are glad to announce the opening of our new project !</p> 
     404     
     405     
     406        <p>You are all invited to <strong>Romanel</strong> for a great party this weekend.</p></div> 
  • trunk/test/helpers/relations.yml

    r326 r370  
    187187 
    188188menu_with_favorites: 
    189   src: "<ul><z:root do='pages' only='public' cache='true' name='menu'><li do='each'><z:link/><ul do='pages'><li do='each' do='link'/></ul></li></z:root><z:node select='visitor' do='favorites'><li do='trans'>favorites<ul><li do='each' do='link'/></ul></li></ul>" 
    190   res: "" 
     189  src: "<ul><z:root do='pages' only='public' cache='true' name='menu'><li do='each'><z:link/><ul do='pages'><li do='each' do='link'/></ul></li></z:root><z:node select='visitor' do='favorites'><li class='favorites'><z:trans>favorites</z:trans><ul><li do='each' do='link'/></ul></li></ul>" 
     190  res: "/.*tag23.*tag25.*Clean Water.*favorites.*Nature/" 
     191   
     192visitor_favorites: 
     193  src: "<z:node select='visitor'><ul do='favorites'><li do='each' do='link'/></ul></z:node>" 
     194  res: "<ul><li><a href=\"/oo/page29.html\">Nature</a></li></ul>" 
  • trunk/test/helpers/test_all.rb

    r352 r370  
    44  testfile :relations, :basic 
    55  Project # make sure we load Project links before trying relations 
     6   
    67  def test_single 
    7     do_test('basic', 'link_version') 
     8    do_test('basic', 'show_title') 
     9  end 
     10   
     11  # test rename asset (has to be called wiki to find the proper skin) 
     12  def test_basic_wiki 
     13    Node.connection.execute "UPDATE nodes SET parent_id = 33 WHERE id = 20;" # status, art 
     14    do_test('basic', 'wiki') 
    815  end 
    916   
  • trunk/test/helpers/testhelp.rb

    r352 r370  
    3737 
    3838  def set_context 
    39     session[:user] = User.find(@params[:user_id])[:id] # make sure the user exists 
    40     session[:lang] = visitor.lang = @params[:prefix] 
     39    @visitor = User.find(@params[:user_id]) 
     40    @visitor.lang = @params[:prefix] 
     41    @visitor.site = Site.find(:first, :select=>"sites.*", :from=>"sites, sites_users", 
     42                              :conditions=>["sites_users.site_id = sites.id AND sites_users.user_id = ?", visitor[:id]]) 
     43    @visitor.visit(@visitor) 
    4144    @node = secure(Node) { Node.find(@params[:node_id])} 
    4245    @text = @params[:text] 
    4346    @test_url  = @params[:url] 
    4447    @params.delete(:user_id) 
     48    @params.delete(:prefix) 
    4549    @params.delete(:node_id) 
    4650    @params.delete(:text) 
  • trunk/test/unit/site_test.rb

    r363 r370  
    116116    assert_equal 'test.host', site[:host] 
    117117  end 
     118   
     119  def test_lang_list 
     120    site = sites(:zena) 
     121    site.languages = "en,fr" 
     122    assert_equal ['en', 'fr'], site.lang_list 
     123    site.languages = "en,fr, ru , es" 
     124    assert_equal ['en', 'fr', 'ru', 'es'], site.lang_list 
     125  end 
    118126end 
  • trunk/test/zena_test.rb

    r362 r370  
    170170        @visitor.site = Site.find(:first, :select=>"sites.*", :from=>"sites, sites_users", 
    171171                                  :conditions=>["sites_users.site_id = sites.id AND sites_users.user_id = ?", @visitor[:id]]) 
     172        @visitor.visit(@visitor) 
    172173        @lang = @visitor.lang 
    173174      end