<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Sean&#039;s Blog &#187; helpers</title>
	<atom:link href="http://seanbehan.com/tag/helpers/feed/" rel="self" type="application/rss+xml" />
	<link>http://seanbehan.com</link>
	<description>Web Programming, Ruby on Rails, Wordpress, PHP from Burlington, Vermont</description>
	<lastBuildDate>Wed, 08 Sep 2010 16:10:05 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Collection Select Helper and OnChange Event in Rails</title>
		<link>http://seanbehan.com/posts/collection-select-helper-and-onchange-event-in-rails/</link>
		<comments>http://seanbehan.com/posts/collection-select-helper-and-onchange-event-in-rails/#comments</comments>
		<pubDate>Tue, 31 Aug 2010 14:38:23 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Posts]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[api]]></category>
		<category><![CDATA[collection]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[select]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=1162</guid>
		<description><![CDATA[Given a collection of Active Record objects, you may use the collection_select helper method to produce a select form field. You need to pass in a number of arguments to the helper function. 1) object &#8211; your model object used in the collection 2) method &#8211; a valid model attribute or method 3) collection &#8211; [...]]]></description>
			<content:encoded><![CDATA[<p>Given a collection of Active Record objects, you may use the collection_select helper method to produce a select form field. You need to pass in a number of arguments to the helper function.</p>
<p>1) object &#8211; your model object used in the collection<br />
2) method &#8211; a valid model attribute or method<br />
3) collection &#8211; a collection of active record model objects<br />
4) option_value &#8211; value being set from the model for the &lt;option value=&#8221;option_value&#8221;&gt; html element<br />
5) option_name &#8211; what is displayed for the user e.g., &lt;option&gt; option_name &lt;<br />
6) option &#8211; general options<br />
7) options for the select html element</p>
<pre># helper and arguments...
# collection_select( model, id, collection, option_value, option_name, options, html_options)

&lt;%= collection_select("states", "state_id",
  State.participating,
  "abbreviation", "name",
   {:selected=&gt; get_current_state_or_nil },
   {:onchange=&gt;"document.location='/states/'+this.value"}
) %&gt;

#which will produce something like...
<pre id="line27">&lt;select id="state_id" name="state[id]" onchange="document.location='/states/'+this.value"&gt;
&lt;option value="AL"&gt;Alabama&lt;/option&gt;
&lt;option value="AK"&gt;Alaska&lt;/option&gt;
&lt;option value="AZ"&gt;Arizona&lt;/option&gt;
&lt;option value="AR"&gt;Arkansas&lt;/option&gt;
&lt;/select&gt;</pre>
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/posts/collection-select-helper-and-onchange-event-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Yield a Block within Rails Helper Method with Multiple Content_tags Using Concat</title>
		<link>http://seanbehan.com/programming/yield-a-block-within-rails-helper-method-with-multiple-content_tags-using-concat/</link>
		<comments>http://seanbehan.com/programming/yield-a-block-within-rails-helper-method-with-multiple-content_tags-using-concat/#comments</comments>
		<pubDate>Fri, 09 Apr 2010 14:58:53 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[block]]></category>
		<category><![CDATA[concat]]></category>
		<category><![CDATA[content_tag]]></category>
		<category><![CDATA[dry]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[html]]></category>
		<category><![CDATA[strings]]></category>
		<category><![CDATA[views]]></category>
		<category><![CDATA[wrapper]]></category>
		<category><![CDATA[yield]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=923</guid>
		<description><![CDATA[To clean up some repetitive html coding in views, pass a block of text to a helper function which will wrap it for you the same way, each and every time. For example, I have a &#8216;help&#8217; link which will toggle the display of a block of text if a link is clicked. I use [...]]]></description>
			<content:encoded><![CDATA[<p>To clean up some repetitive html coding in views, pass a block of text to a helper function which will wrap it for you the same way, each and every time. For example, I have a &#8216;help&#8217; link which will toggle the display of a block of text if a link is clicked. I use this &#8220;+/- help&#8221; link throughout my application in various views. I could create a partial but this gets messy. Instead this is what I want to write in my views&#8230;</p>
<pre>
&lt;% help do %&gt;
  here is my help text...
&lt;% end %&gt;
</pre>
<p>which will render HTML similar to this&#8230;</p>
<pre>
&lt;a href='#' 'class='help' id='help_link_123456' onclick='some_func_to_toggle_state'&gt;+/- help&lt;/a&gt;
&lt;div id="help_123456"&gt;
  here is my help text...
&lt;/div&gt;
</pre>
<p>In order to accomplish this you need to create a helper method. Assigning a unique id, just a random number, will help avoid collisions with the state of the toggled div if you use this more than once per page. Placing other HTML helper methods inside the concat() method allows multiple tags as well as rendering the block passed to the function in the appropriate place. It looks a little unwieldy, but works nicely.</p>
<pre>
# app/helpers/application_helper.rb
module ApplicationHelper
  def help(&#038;block)
    uniqid = rand; concat( link_to_function("+/- help") do |page|
      page["help_#{uniqid}"].toggle
      page.visual_effect :highlight, "help_#{uniqid}"
    end + content_tag(:div,:class=>"help",:id=>"help_#{uniqid}", :style=>"display:none") do
      yield
    end )
  end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/programming/yield-a-block-within-rails-helper-method-with-multiple-content_tags-using-concat/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails Helper to Remove Leading Zero in 12 Hour Time Format</title>
		<link>http://seanbehan.com/ruby-on-rails/rails-helper-to-remove-leading-zero-in-12-hour-time-format/</link>
		<comments>http://seanbehan.com/ruby-on-rails/rails-helper-to-remove-leading-zero-in-12-hour-time-format/#comments</comments>
		<pubDate>Mon, 01 Mar 2010 23:39:32 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[12 hour time]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[initializers]]></category>
		<category><![CDATA[leading zero]]></category>
		<category><![CDATA[strftime]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=829</guid>
		<description><![CDATA[I can&#8217;t find a strftime() format that will output the hour without the leading zero. For instance 6:20 will be instead 06:20. This just looks a little sloppy. I created a datetime.rb intializer which will contain custom datetime formats for my application. # RAILS_ROOT/config/initializers/datetime.rb Time::DATE_FORMATS[:date] = "%a %b %d, %Y" Time::DATE_FORMATS[:time] = "%I:%M%p" # ... [...]]]></description>
			<content:encoded><![CDATA[<p>I can&#8217;t find a strftime() format that will output the hour without the leading zero.  For instance 6:20 will be instead 06:20. This just looks a little sloppy. I created a datetime.rb intializer which will contain custom datetime formats for my application.</p>
<pre>
# RAILS_ROOT/config/initializers/datetime.rb
Time::DATE_FORMATS[:date] = "%a %b %d, %Y"
Time::DATE_FORMATS[:time] = "%I:%M%p"
# ...
</pre>
<p>Next I set up a helper function which will string together the :date and :time formats and remove the leading zero in the hour with the .gsub method on the string.</p>
<pre>
  # RAILS_ROOT/app/helpers/application_helper.rb
  def fulltime(created_at)
    created_at.to_s(:date)+" "+created_at.to_s(:time).gsub(/^0/,'').downcase
  end
</pre>
<p>In a view I&#8217;ll just pass the timestamp to the fulltime function</p>
<pre>
fulltime(@post.created_at)
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/rails-helper-to-remove-leading-zero-in-12-hour-time-format/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Date and Time Helpers in Rails</title>
		<link>http://seanbehan.com/ruby-on-rails/date-and-time-helpers-in-rails/</link>
		<comments>http://seanbehan.com/ruby-on-rails/date-and-time-helpers-in-rails/#comments</comments>
		<pubDate>Sat, 03 Oct 2009 18:16:03 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[datetime]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[time]]></category>
		<category><![CDATA[time ago in words]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=545</guid>
		<description><![CDATA[Just for reference http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695 This post was created about ago]]></description>
			<content:encoded><![CDATA[<p>Just for reference http://api.rubyonrails.org/classes/ActionView/Helpers/DateHelper.html#M001695</p>
<pre>
This post was created about <%= time_ago_in_words @post.created_at %> ago
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/date-and-time-helpers-in-rails/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Make Rails Lib Module Methods Available to Views</title>
		<link>http://seanbehan.com/ruby-on-rails/make-rails-lib-module-methods-available-to-views/</link>
		<comments>http://seanbehan.com/ruby-on-rails/make-rails-lib-module-methods-available-to-views/#comments</comments>
		<pubDate>Wed, 22 Jul 2009 17:51:14 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[action view]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[modules]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://seanbehan.com/?p=444</guid>
		<description><![CDATA[If you create a module in the lib/ directory of your Rails application you won&#8217;t have access to those methods in your views. If you don&#8217;t want to put those methods in a helper file, you need to add a method to your module that makes them available for you views like so&#8230; #in lib/my_module_name.rb [...]]]></description>
			<content:encoded><![CDATA[<p>If you create a module in the lib/ directory of your Rails application you won&#8217;t have access to those methods in your views. If you don&#8217;t want to put those methods in a helper file, you need to add a method to your module that makes them available for you views like so&#8230;</p>
<pre>
#in lib/my_module_name.rb
module MyModuleName
  def my_method_for_views
     #logic...
  end
  def self.included(base)
    base.send :helper_method, :my_method_for_views if base.respond_to? :helper_method
  end
end
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/ruby-on-rails/make-rails-lib-module-methods-available-to-views/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Rails Prototype JS and TinyMCE Autosave</title>
		<link>http://seanbehan.com/programming/rails-prototype-js-and-tinymce-autosave/</link>
		<comments>http://seanbehan.com/programming/rails-prototype-js-and-tinymce-autosave/#comments</comments>
		<pubDate>Thu, 30 Apr 2009 19:26:32 +0000</pubDate>
		<dc:creator>bseanvt</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[autosave]]></category>
		<category><![CDATA[helpers]]></category>
		<category><![CDATA[periodical]]></category>
		<category><![CDATA[prototype]]></category>
		<category><![CDATA[Rails]]></category>

		<guid isPermaLink="false">http://bseanvt.wordpress.com/?p=206</guid>
		<description><![CDATA[TinyMCE is a nice little WYSIWYG for text processing online. It uses iFrames and Javascript callbacks to manipulate textarea form fields. Using it with Rails can be somewhat problematic if you want to set up an observer on a field that TinyMCE is managing. The reason is that TinyMCE uses an iFrame and a callback [...]]]></description>
			<content:encoded><![CDATA[<p>TinyMCE is a nice little WYSIWYG for text processing online. It uses iFrames and Javascript callbacks to manipulate textarea form fields. Using it with Rails can be somewhat problematic if you want to set up an observer on a field that TinyMCE is managing. The reason is that TinyMCE uses an iFrame and a callback to update the textarea before the form is submitted.</p>
<p>More detailed information on the TinyMCE specifics can be found at this site &lt;a href=&#8221;http://www.crossedconnections.org/w/?p=88&#8243;&gt;</p>
<p>In Rails, there is a nifty helper function called &#8220;observe_field&#8221; that will generate Javascript which listens for changes on form fields. However, because TinyMCE only updates the field after the form is submitted the contents of the textarea are not updated when you type in it. Therefore, your observe_field function is listening but doesn&#8217;t see anything new. The iFrame is a kind of buffer that will write to the textarea later.</p>
<p>A simple solution to this problem is to call the function that writes to the textarea. This will update the textarea and the observer_field function will notice the changes. You can use prototype &#8220;PeriodicalExecuter&#8221; to trigger a function that will contain the TinyMCE callback function. Below is the code.</p>
<pre>
script type="text/javascript" charset="utf-8"
  function triggerTinyMCE(){
    tinyMCE.triggerSave(true, true);
  }
  new PeriodicalExecuter(triggerTinyMCE, 30);
/script

<%= observe_field( :post_body,
        :frequency => 1,
        :update => :update_status,
        :url => { :action => :update_body } ) %>
</pre>
<p>However, the only problem with this approach is that you&#8217;re kind of running two functions that do the same thing. Instead it would make more sense to get rid of the observe_field function altogether and have the PeriodicalExecuter trigger a function that makes the ajax request at the same time. This would look something like</p>
<pre>
script type="text/javascript" charset="utf-8"
  function triggerTinyMCE(){
    tinyMCE.triggerSave(true, true);
    var content = escape($F('post_body'));
    var url = "/posts/update_body";
    var params = "post_body="+content;

    var aRequest = new Ajax.Request(url,
    {
      method: 'post',
      parameters: params,
      onSuccess: function(data){
        $("update_status").innerHTML = data.responseText;
      }
   });
}
new PeriodicalExecuter(triggerTinyMCE, 10);
/script
</pre>
]]></content:encoded>
			<wfw:commentRss>http://seanbehan.com/programming/rails-prototype-js-and-tinymce-autosave/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
