<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: custom code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Fri, 25 Jul 2008 07:02:09 GMT</pubDate>
    <description>DZone Snippets: custom code</description>
    <item>
      <title>Add a Google custom search facility to your website</title>
      <link>http://snippets.dzone.com/posts/show/4951</link>
      <description>This HTML page displays a basic search page similar to http://www.google.com/ however this search page will only search for results from http://snippets.dzone.com.  &lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"&lt;br /&gt;    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;&lt;br /&gt;&lt;html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"&gt;&lt;head&gt;&lt;br /&gt;&lt;title&gt;abc&lt;/title&gt;&lt;br /&gt;&lt;meta http-equiv="Content-Type" content="text/html; charset: UTF-8" /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/head&gt;&lt;br /&gt;&lt;body&gt;&lt;br /&gt;&lt;br /&gt;    &lt;form action="http://www.google.com/search"&gt;&lt;br /&gt;      &lt;div&gt;&lt;input name="query" type="hidden" value="site:snippets.dzone.com"&gt;&lt;/input&gt;&lt;/div&gt;&lt;br /&gt;      &lt;div&gt;&lt;input maxlength="2048" name="q" size="55" title="Google Search" value="" &gt;&lt;/input&gt;&lt;/div&gt;&lt;br /&gt;      &lt;div&gt;&lt;input name="btnG" type="submit" value="Google Search"&gt;&lt;/input&gt;&lt;/div&gt;&lt;br /&gt;    &lt;/form&gt;&lt;br /&gt;&lt;/body&gt;&lt;br /&gt;&lt;/html&gt;&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;I stumbled across the hidden query field while I was looking for ways to perform custom search without the need for JavaScript. If you are interested in Google's official custom search engine on your site go to http://www.google.com/cse&lt;br /&gt;&lt;br /&gt;* update 8-Jan-08 2:01pm *&lt;br /&gt;Here's a more advanced example from someone's blog titled 'Fine-tuning Custom Google Search' http://snipr.com/1wppb [blogs.salon.com]&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&lt;FORM method=GET action=http://www.google.com/custom&gt;;&lt;br /&gt;&lt;A HREF=http://www.google.com/search&gt;;&lt;br /&gt;&lt;IMG SRC=http://www.google.com/logos/Logo_40wht.gif border=0 ALT=Google align=middle&gt;&lt;/A&gt; &lt;INPUT TYPE=text name=q size="10" maxlength=255 value=""&gt;&lt;br /&gt;&lt;INPUT type=submit name=sa VALUE="Just Goog It!"&gt;&lt;br /&gt;&lt;INPUT type=hidden name=cof VALUE="GIMP:darkblue;LW:380;ALC:red;L:http://blogs.salon.com/0001111/images/rfbanner.jpg;GFNT:lightblue;LC:red;LH:100;AH:left;VLC:gray;S:http://blogs.salon.com/0001111/;GALT:blue;AWFID:ba6ecf243ae3d16f;"&gt;&lt;br /&gt;&lt;br /&gt;&gt;span class="small"&gt;&lt;br /&gt;&lt;input type=radio name=sitesearch value="blogs.salon.com" checked&gt;Search blogs.salon.com&lt;br /&gt;&lt;input type=radio name=sitesearch value=""&gt;Search the Web&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;&lt;/FORM&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Sun, 06 Jan 2008 13:48:50 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/4951</guid>
      <author>jrobertson (James Robertson)</author>
    </item>
    <item>
      <title>Using helpers inside a controller</title>
      <link>http://snippets.dzone.com/posts/show/1799</link>
      <description>Ripped from&lt;br /&gt;http://gabriel.gironda.org/articles/2006/02/08/using-helpers-inside-a-controller&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;This is incredibly straightforward and more of an occasional convenience, but I thought I'd throw it out there anyway. You may find that sometimes, even though the controller obviously isn't the view, that you want to use some of the helper methods available.&lt;br /&gt;&lt;br /&gt;The only case I've come across so far is wanting to use pluralize() in a flash message and not have to do it by hand using the inflector. You could include ActionView::Helpers::TextHelper in the controller, but that fills your namespace with crap.&lt;br /&gt;&lt;br /&gt;Put this in the class ApplicationController instead:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def help&lt;br /&gt;    Helper.instance&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  class Helper&lt;br /&gt;    include Singleton&lt;br /&gt;    include ActionView::Helpers::TextHelper&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Then you can just use it like so:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;  def check_for_max_donkeys&lt;br /&gt;    if Donkey.find_fit_donkeys.size == APP_SETTINGS['max_fit_donkeys']&lt;br /&gt;      flash_error "The maximum of #{help.pluralize(APP_SETTINGS['max_fit_donkeys'], 'donkey')} has been reached."&lt;br /&gt;      redirect_to_index&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;Update: Don't use the method name "helper" because Rails already uses that. Just "help" works fine.</description>
      <pubDate>Wed, 29 Mar 2006 11:16:53 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1799</guid>
      <author>Namor ()</author>
    </item>
    <item>
      <title>Validations for Non-ActiveRecord Objects</title>
      <link>http://snippets.dzone.com/posts/show/1055</link>
      <description>I needed to find a way to use active record validations on non active record items so i went searching and found this blog which is useful for me..&lt;br /&gt;Totally ripped off Peter Donald's blog :P&lt;br /&gt;http://www.realityforge.org/articles/2005/12/02/validations-for-non-activerecord-model-objects&lt;br /&gt;&lt;br /&gt;To get this working grab the active_form.rb file and place it in the app/models directory. You can then make your model objects extend ActiveForm and use them like regular ActiveRecord objects.&lt;br /&gt;&lt;br /&gt;active_form.rb&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# Note ".valid?" method  must occur on object for validates_associated&lt;br /&gt;class ActiveForm&lt;br /&gt;  &lt;br /&gt;  def initialize(attributes = nil)&lt;br /&gt;    if attributes&lt;br /&gt;      attributes.each do |key,value|&lt;br /&gt;        send(key.to_s + '=', value)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;    yield self if block_given?&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def [](key)&lt;br /&gt;    instance_variable_get("@#{key}")&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def method_missing( method_id, *args )&lt;br /&gt;    if md = /_before_type_cast$/.match(method_id.to_s)&lt;br /&gt;      attr_name = md.pre_match&lt;br /&gt;      return self[attr_name] if self.respond_to?(attr_name)&lt;br /&gt;    end&lt;br /&gt;    super&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;protected &lt;br /&gt;  def raise_not_implemented_error(*params)&lt;br /&gt;    ValidatingModel.raise_not_implemented_error(*params)&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def self.human_attribute_name(attribute_key_name)&lt;br /&gt;    attribute_key_name.humanize&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  def new_record?&lt;br /&gt;    true&lt;br /&gt;  end&lt;br /&gt;&lt;br /&gt;  # these methods must be defined before include&lt;br /&gt;  alias save raise_not_implemented_error&lt;br /&gt;  alias update_attribute raise_not_implemented_error&lt;br /&gt;&lt;br /&gt;public&lt;br /&gt;  include ActiveRecord::Validations&lt;br /&gt;&lt;br /&gt;protected &lt;br /&gt;&lt;br /&gt;  # the following methods must be defined after include so that they overide&lt;br /&gt;  # methods previously included&lt;br /&gt;  alias save! raise_not_implemented_error&lt;br /&gt;&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    def raise_not_implemented_error(*params)&lt;br /&gt;      raise NotImplementedError&lt;br /&gt;    end&lt;br /&gt;&lt;br /&gt;    alias validates_uniqueness_of raise_not_implemented_error&lt;br /&gt;    alias create! raise_not_implemented_error&lt;br /&gt;    alias validate_on_create raise_not_implemented_error&lt;br /&gt;    alias validate_on_update raise_not_implemented_error&lt;br /&gt;    alias save_with_validation raise_not_implemented_error    &lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;require 'dispatcher'&lt;br /&gt;class Dispatcher&lt;br /&gt;  class &lt;&lt; self&lt;br /&gt;    if ! method_defined?(:form_original_reset_application!) &lt;br /&gt;      alias :form_original_reset_application! :reset_application!&lt;br /&gt;      def reset_application!&lt;br /&gt;        form_original_reset_application!&lt;br /&gt;        Dependencies.remove_subclasses_for(ActiveForm) if defined?(ActiveForm)&lt;br /&gt;      end&lt;br /&gt;    end&lt;br /&gt;  end&lt;br /&gt;end&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 05 Jan 2006 02:01:56 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1055</guid>
      <author>Namor ()</author>
    </item>
    <item>
      <title>Defining a custom validates in rails</title>
      <link>http://snippets.dzone.com/posts/show/822</link>
      <description>in the model:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class User &lt; ActiveRecord::Base&lt;br /&gt;  require 'validations'&lt;br /&gt;&lt;br /&gt;  validates_positive_or_zero :number&lt;br /&gt;  &lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;&lt;br /&gt;in /lib/validations.rb:&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;def validates_positive_or_zero(*attr_names)&lt;br /&gt;  configuration = { :message =&gt; "Cannot be negative" }&lt;br /&gt;  configuration.update(attr_names.pop) if attr_names.last.is_a?(Hash)&lt;br /&gt;  validates_each attr_names do |m, a, v| m.errors.add(a, configuration[:message]) if v&lt;0 end&lt;br /&gt;end&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 19 Oct 2005 21:47:46 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/822</guid>
      <author>zzzrByte (Tal Ater)</author>
    </item>
  </channel>
</rss>
