#713: Actionize && Parameterize. Thoughts?

1 view
Skip to first unread message

Matt Darby

unread,
Jul 27, 2008, 11:05:29 AM7/27/08
to rubyonra...@googlegroups.com, colum...@googlegroups.com
I'm looking for some thoughts (and +1's!) on a patch (#713) that introduces two new Inflectors: Actionize and Parameterize.


Actionize

Transforms a string in titleize() form to a string suitable for a action name

  "Expense Report".actionize         # => "expense_report"
  "My Totally Cool Action".actionize # => "my_totally_cool_action"
   
  @reports = ['Expense Report', 'Employee Hours']
  @reports.each do |report_name|
    link_to(report_name, "/reports/#{report_name.actionize}")
  end
  # => "<a href = '/reports/expense_report'>Expense Report</a>
        <a href = '/reports/employee_hours'>Employee Hours</a>"

Parameterize

Replaces special characters in a string so that it may be used as part of a 'pretty' URL.

==== Examples

  class Person
    def to_param
      "#{self.id}-#{self.name.parameterize}"
    end
  end

  @person = Person.find(1)
  # => #<Person id: 1, name: "Donald E. Knuth">

  <%= link_to(@person.name, person_path %>
  # => <a href="/person/1-Donald_E_Knuth">Login</a>


Thanks!

Matt Darby, M.S.
Rails | PHP | Linux | MySQL | IT

Skype: matt-darby

Jim Cropcho

unread,
Jul 27, 2008, 1:03:50 PM7/27/08
to Columbus Ruby Brigade
mirrored from
http://rails.lighthouseapp.com/projects/8994-ruby-on-rails/tickets/713-new-inflectors-actionize-and-parameterize#ticket-713-2

#actionize seems useful, if only to have a more readable method for
using existing functionality.

i guess i could say the same about #parameterize, except that its
function is to make a string url-safe, in a very generic way, and
there are already methods (with appropriate naming, this time) which
do that, right?

http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000481

ActiveSupport::CoreExtensions::String::Inflections::underscore()

is the same thing, I take it?

On Jul 27, 11:05 am, Matt Darby <m...@matt-darby.com> wrote:
> I'm looking for some thoughts (and +1's!) on a patch (#713) that  
> introduces two new Inflectors: Actionize and Parameterize.http://rails.lighthouseapp.com/projects/8994/tickets/713-new-inflecto...
> Email: m...@matt-darby.com

Matt Darby

unread,
Jul 27, 2008, 1:49:36 PM7/27/08
to colum...@googlegroups.com
On Jul 27, 2008, at 1:03 PM, Jim Cropcho wrote:

> #actionize seems useful, if only to have a more readable method for
> using existing functionality.
>
> i guess i could say the same about #parameterize, except that its
> function is to make a string url-safe, in a very generic way, and
> there are already methods (with appropriate naming, this time) which
> do that, right?
>
> http://api.rubyonrails.org/classes/ActiveSupport/CoreExtensions/String/Inflections.html#M000481
>
> ActiveSupport::CoreExtensions::String::Inflections::underscore()
>
> is the same thing, I take it?


Also mirrored from Lighthouse:


#underscore and #parameterize differ in how they handle spaces and
special characters.

>> "Donald E. Knuth".underscore
=> "donald e. knuth"
>> "Donald E. Knuth".parameterize
=> "Donald_E_Knuth"

It seems that #underscore expects a camelcased string, not a more
human-readable one.

#actionize is like #tableize, but it replaces spaces with underscores
(by default), and doesn't pluralize the output.

>> "Expense Report".tableize
=> "expense reports"
>> "Expense Report".actionize
=> "expense_report"

Rob Funk

unread,
Jul 27, 2008, 9:11:37 PM7/27/08
to colum...@googlegroups.com
Matt Darby wrote:
> Actionize
>
> Transforms a string in titleize() form to a string suitable for a
> action name
>
> "Expense Report".actionize # => "expense_report"

I thought there was already something that did that, but maybe I just
combined a couple other ones to do it....


> @reports.each do |report_name|
> link_to(report_name, "/reports/#{report_name.actionize}")
> end
> # => "<a href = '/reports/expense_report'>Expense Report</a>
> <a href = '/reports/employee_hours'>Employee Hours</a>"

Is that really the HTML that link_to generates? Is it legal HTML to have
those spaces around the =? (I don't like the single-quotes either, but
I'm pretty sure they're legal.)


> Parameterize
>
> Replaces special characters in a string so that it may be used as part
> of a 'pretty' URL.

> # => <a href="/person/1-Donald_E_Knuth">Login</a>

My understanding is that for SEO purposes, hyphens are actually better
than underscores for separating words, because search engines consider
underscores as part of a word (either thanks to PCRE's \w regex, or a
desire to cater to programmers, I'm not sure which). It seems to be a
point of active (mild) controversy though.


--
==============================| "A microscope locked in on one point
Rob Funk <rf...@funknet.net> |Never sees what kind of room that it's in"
http://www.funknet.net/rfunk | -- Chris Mars, "Stuck in Rewind"

Matt Darby

unread,
Jul 27, 2008, 9:19:42 PM7/27/08
to colum...@googlegroups.com
On Jul 27, 2008, at 9:11 PM, Rob Funk wrote:

>
> Matt Darby wrote:
>> Actionize


> I thought there was already something that did that, but maybe I just
> combined a couple other ones to do it....
>

> Is that really the HTML that link_to generates? Is it legal HTML to
> have
> those spaces around the =? (I don't like the single-quotes either,
> but
> I'm pretty sure they're legal.)
>
>
>> Parameterize

> My understanding is that for SEO purposes, hyphens are actually better
> than underscores for separating words, because search engines consider
> underscores as part of a word (either thanks to PCRE's \w regex, or a
> desire to cater to programmers, I'm not sure which). It seems to be a
> point of active (mild) controversy though.

Hi Rob!

Actionize:
Jim brought up the same thoughts about this in Lighthouse ticket.
#actionize is similar to #tableize, but without the pluralization and
that it also treats spaces differently. I kind of half-assed guessed
what #link_to actually generates, but you get the point ;)

Parameterize:
I think you might be right on the SEO point, I should probably make
that the default separator.


Thanks for your thoughts; I use these (very simple) Inflectors in my
everyday code, I thought they might be helpful to Others.

Reply all
Reply to author
Forward
0 new messages