Changeset 47

Show
Ignore:
Timestamp:
03/29/06 11:13:26 (7 years ago)
Author:
dema
Message:

Enhanced the attribute helpers to behave better on error conditions.

Location:
trunk/app/helpers
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/app/helpers/hyperde/node.rb

    r39 r47  
    99     
    1010  def attr(attr_name, template = nil, &block) 
    11     value = @node.send(attr_name) rescue nil 
    12     if value.nil? 
    13       "" 
    14     else 
    15       attr_value(value, attr_name, template, &block)       
     11    value = "" 
     12    begin 
     13        value = @node.send(attr_name) 
     14        if value.nil? 
     15          "" 
     16        else 
     17          attr_value(value, attr_name, template, &block)       
     18      end 
     19    rescue => ex 
     20        value = ex.to_s 
    1621    end 
     22    value 
    1723  end 
    1824 
  • trunk/app/helpers/navigation_helper.rb

    r39 r47  
    1111  end 
    1212   
    13         def render_node_attribute(attr, node) 
    14           buffer = "" 
    15           buffer_label = "" 
    16           value = node.send(attr.name) 
    17                 if value && value.to_s.strip.length > 0 
    18                         case attr 
    19                         when IndexNavAttribute 
    20                           buffer_label << "<hr/>" 
    21                                 buffer << render_index(value) if value.entries.length > 0 
    22                         when AnchorNavAttribute 
    23                           buffer << render_node_anchor(value) 
    24                         else 
    25                           case attr.data_type.strip.downcase 
    26                                 when "image" 
    27                                         buffer << "<img src=\"#{value.to_s}\"/>" 
    28                                 when "url", "uri" 
    29                                         buffer << "<a href=\"#{value.to_s}\" target=\"_blank\">#{value.to_s}</a>" 
    30                                 when "email" 
    31                                         buffer << "<a href=\"mailto:#{value.to_s}\" target=\"_blank\">#{value.to_s}</a>" 
    32                                 else 
    33                                   buffer << value.to_s 
    34                                 end 
    35                         end 
    36                 end 
    37                 if buffer.length > 0 
    38                   buffer_label << "<span class=node_attribute_label>" 
    39                   buffer_label << NavAttribute.human_attribute_name(attr.name) 
    40                   buffer_label << ":&nbsp;</span><span class=node_attribute_value>" 
    41                   buffer = buffer_label + buffer 
    42                   buffer << "</span>" 
    43                 end 
    44                 buffer 
    45         end 
    46          
    47         def render_node_attributes(node) 
    48           buffer = "" 
    49           buffer << "<div class=node_attribute>" 
    50           buffer << "<span class=node_attribute_label>Id:</span>&nbsp;" 
    51           buffer << "<span class=node_attribute_value>#{node.id}</span>" 
    52           buffer << "</div>" 
    53           attrs = node.nav_attributes.values 
    54           attrs.sort! { |a,b| ((a.order * 10) + (a.position ? a.position : 0) ) <=> ((b.order * 10) + (b.position ? b.position : 0) ) }  
    55                 for attr in attrs 
    56                   buf_value = render_node_attribute(attr, node) 
    57                   buffer << "<div class=node_attribute>" << buf_value << "</div>" if buf_value.length > 0 
    58                 end 
    59                 buffer 
    60         end 
    61  
    62         def action_for_anchor(anc) 
    63           case anc 
    64           when ContextAnchorValue 
    65                         "context" 
    66                 when IndexAnchorValue 
    67                   "show_index" 
    68                 else 
    69                   nil 
    70                 end 
    71         end 
    72                  
     13  def render_node_attribute(attr, node) 
     14    buffer = "" 
     15    buffer_label = "" 
     16    begin 
     17      value = node.send(attr.name) 
     18      if value && value.to_s.strip.length > 0 
     19        case attr 
     20        when IndexNavAttribute 
     21          buffer_label << "<hr/>" 
     22          buffer << render_index(value) if value.entries.length > 0 
     23        when AnchorNavAttribute 
     24          buffer << render_node_anchor(value) 
     25        else 
     26          case attr.data_type.strip.downcase 
     27          when "image" 
     28            buffer << "<img src=\"#{value.to_s}\"/>" 
     29          when "url", "uri" 
     30            buffer << "<a href=\"#{value.to_s}\" target=\"_blank\">#{value.to_s}</a>" 
     31          when "email" 
     32            buffer << "<a href=\"mailto:#{value.to_s}\" target=\"_blank\">#{value.to_s}</a>" 
     33          else 
     34            buffer << value.to_s 
     35          end 
     36        end 
     37      end 
     38    rescue => ex 
     39      buffer = ex.to_s 
     40    end 
     41    if buffer.length > 0 
     42      buffer_label << "<span class=node_attribute_label>" 
     43      buffer_label << NavAttribute.human_attribute_name(attr.name) 
     44      buffer_label << ":&nbsp;</span><span class=node_attribute_value>" 
     45      buffer = buffer_label + buffer 
     46      buffer << "</span>" 
     47    end 
     48    buffer 
     49  end 
     50   
     51  def render_node_attributes(node) 
     52    buffer = "" 
     53    buffer << "<div class=node_attribute>" 
     54    buffer << "<span class=node_attribute_label>Id:</span>&nbsp;" 
     55    buffer << "<span class=node_attribute_value>#{node.id}</span>" 
     56    buffer << "</div>" 
     57    attrs = node.nav_attributes.values 
     58    attrs.sort! { |a,b| ((a.order * 10) + (a.position ? a.position : 0) ) <=> ((b.order * 10) + (b.position ? b.position : 0) ) }  
     59    for attr in attrs 
     60      buf_value = render_node_attribute(attr, node) 
     61      buffer << "<div class=node_attribute>" << buf_value << "</div>" if buf_value.length > 0 
     62    end 
     63    buffer 
     64  end 
     65 
     66  def action_for_anchor(anc) 
     67    case anc 
     68    when ContextAnchorValue 
     69      "context" 
     70    when IndexAnchorValue 
     71      "show_index" 
     72    else 
     73      nil 
     74    end 
     75  end 
     76       
    7377  def render_node_anchor(anc) 
    74                 action = action_for_anchor(anc)      
    75                 anchor_url = link_to(anc.label_value, :action => action, :id => anc.target_id, :params => params_for_anchor(anc.params_values)) if anc.target_id 
    76                 buffer = "" 
    77                 buffer << "<span class=node_anchor_link>" << anchor_url << "<span>" if anchor_url 
    78                 buffer 
    79         end 
    80  
    81         def action_for_index_entry_attribute(attr) 
    82                 case attr 
    83                 when ContextAnchorIndexEntryAttribute 
    84                         "context" 
    85                 when IndexAnchorIndexEntryAttribute 
    86                         "show_index" 
    87                 else 
    88                         nil 
    89                 end 
    90         end 
    91  
    92         def params_for_anchor(*params) 
    93           ret = [] 
    94           params.each { |p| ret << p if p } if params 
    95           { :p => ret.length == 0 ? nil : ret.join(',') } 
    96         end 
    97          
    98         def render_index_entry(entry, params = nil, truncate_by = nil) 
    99           buffer = "" 
    100           for attr in entry.index_attributes 
    101             action = action_for_index_entry_attribute(attr) 
    102                         buffer << "<td class=index_entry_attribute>" 
    103                         if action 
    104                           buffer << link_to(truncate_by ? truncate(attr.label_value, truncate_by) : attr.label_value, \ 
    105                                             :controller => "navigation", \ 
    106                                                                                                   :action => action, :id => attr.target_id, \ 
    107                                                                                                   :params => params_for_anchor(attr.params_values, params) ) 
    108                         else 
    109                                 buffer << attr.label_value.to_s 
    110                         end 
    111                         buffer << "</td>" 
    112                 end 
    113                 buffer 
    114         end 
    115                                                                                  
    116         def render_index(idx, params = nil, selected = nil, limit = nil, truncate_by = nil) 
    117           buffer = "" 
    118           buffer << "<table class=index_table><th class=index_header><thead><tr>" 
    119           for attr in idx.index_attributes 
    120             buffer << "<th>#{IndexAttribute.human_attribute_name(attr.name)}</th>"  
    121                 end 
    122                 buffer << "</tr></thead><tbody>" 
    123                 entries = idx.entries 
    124                 if selected and limit 
    125                   lbound = selected - limit 
    126                   ubound = selected + limit  
    127                   if lbound < 0 
    128                     ubound -= lbound 
    129                     lbound = 0 
    130                         elsif ubound > entries.length-1 
    131                           lbound -= (ubound - entries.length-1) 
    132                           ubound = entries.length-1 
    133                         end 
    134                         lbound = 0 if lbound < 0 
    135                   ubound = entries.length-1 if ubound > entries.length-1 
    136                 else 
    137                   lbound = 0 
    138                   ubound = entries.length-1 
    139                 end 
    140                  
    141                 if lbound > 0 
    142                   buffer << "<tr class=index_entry><td>...</td></tr>" 
    143                 end 
    144                  
    145                 i=lbound 
    146                 for entry in entries[lbound..ubound] 
    147                         buffer << "<tr class=index_entry" 
    148                         buffer << (i == selected ? "_selected>" : ">") 
    149                         buffer << render_index_entry(entry, params, truncate_by) 
    150                         buffer << "</tr>" 
    151                         i += 1 
    152                 end 
    153                  
    154                 if ubound < entries.length-1 
    155                   buffer << "<tr class=index_entry><td>...</td></tr>" 
    156                 end 
    157                  
    158                 buffer << "</tbody></table>" 
    159           buffer 
    160         end 
    161  
    162         def render_context_index(context) 
    163           buffer = "" 
     78    action = action_for_anchor(anc)          
     79    anchor_url = link_to(anc.label_value, :action => action, :id => anc.target_id, :params => params_for_anchor(anc.params_values)) if anc.target_id 
     80    buffer = "" 
     81    buffer << "<span class=node_anchor_link>" << anchor_url << "<span>" if anchor_url 
     82    buffer 
     83  end 
     84 
     85  def action_for_index_entry_attribute(attr) 
     86    case attr 
     87    when ContextAnchorIndexEntryAttribute 
     88      "context" 
     89    when IndexAnchorIndexEntryAttribute 
     90      "show_index" 
     91    else 
     92      nil 
     93    end 
     94  end 
     95 
     96  def params_for_anchor(*params) 
     97    ret = [] 
     98    params.each { |p| ret << p if p } if params 
     99    { :p => ret.length == 0 ? nil : ret.join(',') } 
     100  end 
     101   
     102  def render_index_entry(entry, params = nil, truncate_by = nil) 
     103    buffer = "" 
     104    for attr in entry.index_attributes 
     105      action = action_for_index_entry_attribute(attr) 
     106      buffer << "<td class=index_entry_attribute>" 
     107      if action 
     108        buffer << link_to(truncate_by ? truncate(attr.label_value, truncate_by) : attr.label_value, \ 
     109                          :controller => "navigation", \ 
     110                          :action => action, :id => attr.target_id, \ 
     111                          :params => params_for_anchor(attr.params_values, params) ) 
     112      else 
     113        buffer << attr.label_value.to_s 
     114      end 
     115      buffer << "</td>" 
     116    end 
     117    buffer 
     118  end 
     119                     
     120  def render_index(idx, params = nil, selected = nil, limit = nil, truncate_by = nil) 
     121    buffer = "" 
     122    buffer << "<table class=index_table><th class=index_header><thead><tr>" 
     123    for attr in idx.index_attributes 
     124      buffer << "<th>#{IndexAttribute.human_attribute_name(attr.name)}</th>"  
     125    end 
     126    buffer << "</tr></thead><tbody>" 
     127    entries = idx.entries 
     128    if selected and limit 
     129      lbound = selected - limit 
     130      ubound = selected + limit  
     131      if lbound < 0 
     132        ubound -= lbound 
     133        lbound = 0 
     134      elsif ubound > entries.length-1 
     135        lbound -= (ubound - entries.length-1) 
     136        ubound = entries.length-1 
     137      end 
     138      lbound = 0 if lbound < 0 
     139      ubound = entries.length-1 if ubound > entries.length-1 
     140    else 
     141      lbound = 0 
     142      ubound = entries.length-1 
     143    end 
     144     
     145    if lbound > 0 
     146      buffer << "<tr class=index_entry><td>...</td></tr>" 
     147    end 
     148     
     149    i=lbound 
     150    for entry in entries[lbound..ubound] 
     151      buffer << "<tr class=index_entry" 
     152      buffer << (i == selected ? "_selected>" : ">") 
     153      buffer << render_index_entry(entry, params, truncate_by) 
     154      buffer << "</tr>" 
     155      i += 1 
     156    end 
     157     
     158    if ubound < entries.length-1 
     159      buffer << "<tr class=index_entry><td>...</td></tr>" 
     160    end 
     161     
     162    buffer << "</tbody></table>" 
     163    buffer 
     164  end 
     165 
     166  def render_context_index(context) 
     167    buffer = "" 
    164168    buffer << render_index(context.index, @params[:p], context.position, 9, 20) 
    165                 buffer 
    166         end 
    167  
    168         def render_params(params) 
    169           buffer = "" 
    170           for param in params 
    171             buffer << ", " if buffer.length > 0 
    172             buffer << "#{param[:name]}=#{param[:value]}" 
    173                 end 
    174                 buffer = "(" + buffer + ")" if buffer.length > 0 
    175                 buffer 
    176         end 
    177          
    178         def render_context_navigation(context) 
    179                 buffer = render_context_previous_anchor(context) 
    180                 buffer << render_context_next_anchor(context) 
    181                 buffer 
    182         end 
    183          
    184         def render_context_previous_anchor(context) 
    185           buffer = "<span id=previous_link>" 
    186           if !context.on_first? 
    187             buffer << link_to("<< #{truncate(context.previous.label)}",  
    188                               :controller => "navigation", :action => "context",  
    189                               :id => "#{Context.full_id(context,context.position-1)}",  
    190                               :params => params_for_anchor(@params[:p]) ) 
    191                 else 
    192                   buffer << "&nbsp;" 
    193                 end 
    194                 buffer << "</span>" 
    195                 buffer 
    196         end 
    197          
    198         def render_context_next_anchor(context) 
    199                 buffer = "<span id=next_link>" 
    200           if !context.on_last? 
    201                 buffer << link_to("#{truncate(context.next.label)} >>", :controller => "navigation", :action => "context", :id => "#{Context.full_id(context,context.position+1)}", :params => params_for_anchor(@params[:p]) ) 
    202     else 
    203            buffer << "&nbsp;" 
    204           end 
    205                 buffer << "</span>" 
    206                 buffer 
    207         end 
    208  
    209         def render_landmark(lm) 
    210           buffer = "[<span class=landmark>" 
    211           action = action_for_landmark(lm) 
    212                 buffer << link_to(lm.label_value, :action => action, :id => lm.target_id, :params => params_for_anchor(lm.params_values)) 
    213                 buffer << "</span>]&nbsp;" 
    214         end 
    215          
    216         def render_landmarks(landmarks) 
    217                 buffer = "" 
    218                 for lm in landmarks 
    219                         buffer << render_landmark(lm) 
    220                 end 
    221                 buffer 
    222         end 
    223  
    224         def render_breadcrumb(separator = nil) 
    225           return "" if @breadcrumb.nil? 
    226           buffer = "" 
    227           @breadcrumb.entries.each do |bc| 
    228             buffer << separator if separator 
    229             buffer << "<span class=breadcrumb_item>" 
    230             buffer << link_to(truncate(bc["title"]), :action => bc["action"], :id => bc["id"], :params => params_for_anchor(bc[:p])) 
    231             buffer << "</span>" 
    232                 end      
    233                 buffer                       
    234         end 
    235  
    236         def render_actions 
    237           buffer = "" 
    238           class_name = node_id = nil 
    239  
    240           if @params["action"] == "context" 
    241             class_name = @context.current.class.name 
    242             node_id = @context.current.id 
    243                 end 
    244                                  
    245           context_actions = [ { :title => "Edit this #{class_name}", :action => "edit" }, \ 
    246                                { :title => "Delete this #{class_name}", :action => "delete"}, \ 
    247                                { :title => "Add New #{class_name}", :action => "new" } ] 
    248           global_actions = [{ :title => "Add New Node (Any Class)", :action => "select_class" }, \ 
    249                              { :title => "Go to Metamodel", :controller => "nav_class", :action => "list" } ] 
    250  
    251                 @params["action"] == "context" ? actions = context_actions + global_actions : actions = global_actions 
    252                       
    253           for action in actions 
    254                         node_id = nil if action[:action] == "select_class"           
    255             buffer << "&nbsp;<span class=action>[" 
    256             buffer << link_to(action[:title], :controller => action[:controller] ? action[:controller] : "node", \ 
    257                               :action => action[:action], :id => node_id, \ 
    258                               :params => { "return_to" => url_for( :controller => @params["controller"], \ 
    259                                                           :action => @controller.action_name, \ 
    260                                                           :id => @params["id"], \ 
    261                                                           :params => { :p => @params[:p] } ) } ) 
    262             buffer << "]</span>" 
    263                 end 
    264                 buffer 
    265         end 
     169    buffer 
     170  end 
     171 
     172  def render_params(params) 
     173    buffer = "" 
     174    for param in params 
     175      buffer << ", " if buffer.length > 0 
     176      buffer << "#{param[:name]}=#{param[:value]}" 
     177    end 
     178    buffer = "(" + buffer + ")" if buffer.length > 0 
     179    buffer 
     180  end 
     181   
     182  def render_context_navigation(context) 
     183    buffer = render_context_previous_anchor(context) 
     184    buffer << render_context_next_anchor(context) 
     185    buffer 
     186  end 
     187   
     188  def render_context_previous_anchor(context) 
     189    buffer = "<span id=previous_link>" 
     190    if !context.on_first? 
     191      buffer << link_to("<< #{truncate(context.previous.label)}",  
     192                        :controller => "navigation", :action => "context",  
     193                        :id => "#{Context.full_id(context,context.position-1)}",  
     194                        :params => params_for_anchor(@params[:p]) ) 
     195    else 
     196      buffer << "&nbsp;" 
     197    end 
     198    buffer << "</span>" 
     199    buffer 
     200  end 
     201   
     202  def render_context_next_anchor(context) 
     203    buffer = "<span id=next_link>" 
     204    if !context.on_last? 
     205      buffer << link_to("#{truncate(context.next.label)} >>", :controller => "navigation", :action => "context", :id => "#{Context.full_id(context,context.position+1)}", :params => params_for_anchor(@params[:p]) ) 
     206    else 
     207     buffer << "&nbsp;" 
     208    end 
     209    buffer << "</span>" 
     210    buffer 
     211  end 
     212 
     213  def render_landmark(lm) 
     214    buffer = "[<span class=landmark>" 
     215    action = action_for_landmark(lm) 
     216    buffer << link_to(lm.label_value, :action => action, :id => lm.target_id, :params => params_for_anchor(lm.params_values)) 
     217    buffer << "</span>]&nbsp;" 
     218  end 
     219   
     220  def render_landmarks(landmarks) 
     221    buffer = "" 
     222    for lm in landmarks 
     223      buffer << render_landmark(lm) 
     224    end 
     225    buffer 
     226  end 
     227 
     228  def render_breadcrumb(separator = nil) 
     229    return "" if @breadcrumb.nil? 
     230    buffer = "" 
     231    @breadcrumb.entries.each do |bc| 
     232      buffer << separator if separator 
     233      buffer << "<span class=breadcrumb_item>" 
     234      buffer << link_to(truncate(bc["title"]), :action => bc["action"], :id => bc["id"], :params => params_for_anchor(bc[:p])) 
     235      buffer << "</span>" 
     236    end  
     237    buffer                           
     238  end 
     239 
     240  def render_actions 
     241    buffer = "" 
     242    class_name = node_id = nil 
     243 
     244    if @params["action"] == "context" 
     245      class_name = @context.current.class.name 
     246      node_id = @context.current.id 
     247    end 
     248         
     249    context_actions = [ { :title => "Edit this #{class_name}", :action => "edit" }, \ 
     250                         { :title => "Delete this #{class_name}", :action => "delete"}, \ 
     251                         { :title => "Add New #{class_name}", :action => "new" } ] 
     252    global_actions = [{ :title => "Add New Node (Any Class)", :action => "select_class" }, \ 
     253                       { :title => "Go to Metamodel", :controller => "nav_class", :action => "list" } ] 
     254 
     255    @params["action"] == "context" ? actions = context_actions + global_actions : actions = global_actions 
     256          
     257    for action in actions 
     258      node_id = nil if action[:action] == "select_class"             
     259      buffer << "&nbsp;<span class=action>[" 
     260      buffer << link_to(action[:title], :controller => action[:controller] ? action[:controller] : "node", \ 
     261                        :action => action[:action], :id => node_id, \ 
     262                        :params => { "return_to" => url_for( :controller => @params["controller"], \ 
     263                                                    :action => @controller.action_name, \ 
     264                                                    :id => @params["id"], \ 
     265                                                    :params => { :p => @params[:p] } ) } ) 
     266      buffer << "]</span>" 
     267    end 
     268    buffer 
     269  end 
    266270end 
    267271