On Fri, Apr 9, 2010 at 4:31 PM, Joao Carlos <[email protected]> wrote:
> I'll admit mine was a bad example. However, the most common (only?)
> use case I could see for it is in fact with #send.
>
> Your idea for filtering is great, by the way.
Instead of send you can use instance_eval. Where you'd do something like
obj.send(x? ? :self : :other) you can do obj.instance_eval { x? ? self : other }
Cheers
-foca
>
> On Apr 9, 9:19 pm, Mislav Marohnić <[email protected]> wrote:
>> On Fri, Apr 9, 2010 at 13:26, Joao Carlos <[email protected]> wrote:
>>
>> > filter = %w(with_votes without_votes).include?(params[:filter]) ?
>> > params[:filter].to_sym : :self
>> > Idea.published.send(filter).all
>>
>> I wouldn't call this use case where it would be "extremely handy". Here is
>> better code for it:
>>
>> filters = Idea.scopes.keys & Array.wrap(params[:filter]).map(&:to_sym)
>>
>> records = filters.inject(Idea.published) { |model, name|
>> model.send(name) }.all
>>
>> First bonus feature is that it knows about all your named scopes, you don't
>> have to specify them in the controller (business logic in your controller =
>> wrong).
>>
>> Another bonus feature — multiple filters can be given:
>>
>> GET /resource?filter[]=foo&filter[]=bar
>>
>> Third bonus feature is that we didn't need to extend the language with a
>> method that doesn't do absolutely anything.
>> (Even `tap` does something and is incredibly useful.)
>
> --
> You received this message because you are subscribed to the Google Groups
> "Ruby on Rails: Core" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/rubyonrails-core?hl=en.
>
>
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Core" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-core?hl=en.