<?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>Tue, 14 Oct 2008 01:43:07 GMT</pubDate>
    <description>DZone Snippets: custom code</description>
    <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>
  </channel>
</rss>
