Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

About this user

« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS 

Sexp rendering partials

Root:
   1  
   2  <h1>Result</h1>
   3  <% if not @sexp.class %>
   4  <h1>OH SHIT</h1>
   5  <% elsif @sexp.class == String %>  
   6    <%= h(@sexp) -%>
   7  <% elsif @sexp.first.class
   8    @sexp.each { |element| %>
   9      <%= render(:partial  => 'sexp', :object  => element) -%>
  10    <% } %>
  11  <% elsif @sexp.first.class == String %>
  12      <%= render(:partial  => 'sexp', :object  => @sexp) -%>
  13  <% end %>

Sexp partial
   1  
   2  <table class="key-value-pairs">
   3    <thead>
   4    <tr>
   5      <th>key</th>
   6      <th>value</th>
   7    </tr>
   8    </thead>
   9    <tbody>
  10        <% sexp.each_with_index {
  11          |element, index| %>
  12          <% if element.class == Array 
  13                element.each { |sublist| %>
  14                  <%= render(:partial  => 'sexp', :collection  => sublist) -%>
  15                <% } %>
  16          <% elsif element.class == String && index.modulo(2).zero?
  17                hash = Hash[element, sexp[index + 1]] %>
  18                <%= render :partial  => 'pair', :locals  => { :pair  => hash } -%>
  19          <% end %>
  20        <% } %>
  21  </tbody>
  22  </table>

   1  
   2  <% pair.each_pair{ |key, value| %>
   3    <tr>
   4      <td><%= key %></td>
   5      <td>
   6        <% if key =~ /uri/ %>
   7          <%= link_to value, :controller => "explorer", :action  => "explore", :uri  => value %>
   8        <% else %>
   9          <%= value %>
  10        <% end %>
  11      </td>
  12    </tr>
  13  <% } %>

Customize ActiveRecord validation display to the extreme

Modify how form helpers such as 'text_field' handle errors in the following way:

* errors are shown after the input fields (not on top of the form)
* each error can have an explanation on top of the form (only when using custom validator methods)

A custom error SPAN with a nice arrow *after* the input tag, and handle multiple errors for one field (in environment.rb)
   1  
   2  ActionView::Base.field_error_proc = Proc.new {|html_tag, instance|
   3    if instance.error_message.kind_of?(Array)
   4      %(#{html_tag}<span class='validation-error'>&larr;#{instance.error_message.join(',')}</span>)
   5    else
   6      %(#{html_tag}<span class='validation-error'>&larr;#{instance.error_message}</span>)
   7    end
   8    }


In your model e.g. order.rb, add a custom validation function (to demonstrate multiple errors on one field)
   1  
   2    private
   3    
   4    validate :not_nicky_and_length
   5    
   6    def not_nicky_and_length
   7      if name =~ /Nicky/
   8        errors.add('name', 'No Nicky, no') # adds the error to the field
   9        errors.add('name-explanation', "we don't need your business when you're the infamous Russian gangster Nicky") # adds a general error explanation
  10      end
  11      if name.length > 2
  12        errors.add('name', 'Too long')
  13      end
  14    end


Add the error notice and explanation before your form
   1  
   2  <h1><%= pluralize(@order.errors.count, "error")%> prohibited this Order from being saved</h1>
   3  <%= error_message_on "order", "name-explanation", "Name: ", nil, "inputError" %>
« Newer Snippets
Older Snippets »
Showing 1-2 of 2 total  RSS