Custom validation with rails: words with more than 26 characters
def validate if subject.split.any?{|w| w.length > 26} errors.add(:subject, "cannot have words more than 26 consecutive characters") end end
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
def validate if subject.split.any?{|w| w.length > 26} errors.add(:subject, "cannot have words more than 26 consecutive characters") end end
def auto_select_text_field_tag(name, value = nil, options = {}) text_field_tag(name, value, { :onclick => "$(’#{name}’).select()", :readonly => "true" }.merge(options)) end
% export GEM_HOME=/Applications/Locomotive2/Bundles/rmagickRailsMar2007_i386.locobundle/framework/lib/ruby/gems/1.8/ % gem install gbarcode
# $RAILS_ROOT/apps/controllers/barcode_controller.rb class BarcodeController < ApplicationController def get_barcode_image string_to_encode = params[:string] barcode_image = BarcodeGenerator.get_barcode_image(string_to_encode) send_data(barcode_image, :type => 'image/gif', :disposition => 'inline') end end
# $RAILS_ROOT/app/helpers/barcode_generator.rb # # note: this will not work without rmagick (Ruby ImageMagick interface) and gbarcode (GNU barcode) # gems installed. rmagick needs ImageMagick plus dependencies. class BarcodeGenerator # Uses subprocesses because # 1. ImageMagick/RMagick leaks memory, # and doesn't work in a long-running process. The fork makes it safe. # 2. The output from the Gbarcode and ImageMagick is often longer than the pipe buffer, # so we have to empty the buffer from another subprocess def BarcodeGenerator.get_barcode_image(barcode_string) return BarcodeGenerator.get_subprocess_output do barcode_generator = BarcodeGenerator.new $stdout.write(barcode_generator.get_barcode_image(barcode_string)) end end def initialize # we do the imports here to protect long-running processes (like mongrel) from ImageMagick's memory leaks require 'RMagick' require 'gbarcode' end def get_barcode_image(string_to_encode) if string_to_encode.nil? string_to_encode = "No string specified" end string_to_encode = remove_rails_file_extension(string_to_encode) eps_barcode = get_barcode_eps(string_to_encode) gif_barcode = convert_eps_to_gif(eps_barcode) return gif_barcode end def remove_rails_file_extension(string_to_encode) if string_to_encode[-4..-1] == ".png" string_to_encode = string_to_encode[0..-5] end return string_to_encode end def get_barcode_eps(string_to_encode) barcode_object = Gbarcode.barcode_create(string_to_encode) Gbarcode.barcode_encode(barcode_object, Gbarcode::BARCODE_128) return BarcodeGenerator.get_subprocess_output do Gbarcode.barcode_print(barcode_object, $stdout, Gbarcode::BARCODE_OUT_EPS) end end def convert_eps_to_gif(eps_image) base64_eps_image = Base64.encode64(eps_image) im = Magick::Image::read_inline(base64_eps_image).first im.format = "GIF" return BarcodeGenerator.get_subprocess_output do im.write($stdout) end end # execute a block's code in a subprocess, returning any output def BarcodeGenerator.get_subprocess_output() data = "" IO.popen('-', 'r+') do |child_filehandle| if child_filehandle begin data = child_filehandle.read ensure child_filehandle.close_write end else yield end end return data end end
emails = emails.split(/,/).join().split()
class ActiveRecord::Base @@_cached_columns = {} class << self alias :old_columns :columns def columns return @@_cached_columns[table_name] if @@_cached_columns[table_name] @@_cached_columns[table_name] = old_columns end end end
def table(collection, headers, options = {}, &proc) options.reverse_merge!({ :placeholder => 'Nothing to display', :caption => nil, :summary => nil, :footer => '' }) placeholder_unless collection.any?, options[:placeholder] do summary = options[:summary] || "A list of #{collection.first.class.to_s.pluralize}" output = "<table summary=\"#{summary}\">\n" output << content_tag('caption', options[:caption]) if options[:caption] output << "\t<caption>#{options[:caption]}</caption>\n" if options[:caption] output << content_tag('thead', content_tag('tr', headers.collect { |h| "\n\t" + content_tag('th', h) })) output << "<tfoot><tr>" + content_tag('th', options[:footer], :colspan => headers.size) + "</tr></tfoot>\n" if options[:footer] output << "<tbody>\n" concat(output, proc.binding) collection.each do |row| proc.call(row, cycle('odd', 'even')) end concat("</tbody>\n</table>\n", proc.binding) end end
<% table(@posts, %w{ID title}) do |post, klass| -%> <tr class="<%= klass %>"> <td><%= post.id</td> <td><%= post.title </td> </tr> <% end -%>
<table summary="A list of posts"> <thead> <tr> <th>ID</th> <th>Title</th> </tr> </thead> <tfoot><tr><td colspan="2"></td></tr></tfoot> <tbody> <tr> <td>1</td> <td>My first post</td> </tr> </tbody> </table>
<p class="placeholder">Nothing to display</p>
def placeholder(message = 'Nothing to display', options = {}, &proc) # set default options o = { :class => 'placeholder', :tag => 'p' }.merge(options) # wrap the results of the supplied block, or # just print out the message if proc t = o.delete(:tag) concat tag(t, o, true), proc.binding yield concat "</#{t}>", proc.binding else content_tag o.delete(:tag), message, o end end def placeholder_unless(condition, *args, &proc) condition ? proc.call : concat(placeholder(args), proc.binding) end
<%= placeholder :message => 'Nothing found' %> <% placeholder do %> Nothing found. <% end %>
<p class="placeholder">Nothing found</p>
<% placeholder_unless @posts.any?, 'No posts found' do %> <%= render :partial => @post %> <% end %>
<% if @client.service_events.empty? -%> | <%= link_to "delete", :controller => "admin", :action => "destroy_client", :id => @client %><% end -%>
def section_link(name,options) if options[:action] == @current_action and options[:controller] == @current_controller link_to(name, options, :class => 'on') else link_to(name,options) end end
before_filter :instantiate_controller_and_action_names def instantiate_controller_and_action_names @current_action = action_name @current_controller = controller_name end
<%=section_link('Home',:controller => 'articles', :action => 'index')%>
a.on{ border-bottom: solid 2px #000; }