#389 √ invalid
Matthew Windwer

Passing params into filter arguments

Reported by Matthew Windwer | July 3rd, 2008 @ 08:56 PM | in 0.9.5

It would be nice if you can do this:

before :ensure_user_can_see_group :with => params[:group_id]

or even better (using merb-action-args), this:

before :ensure_user_can_see_group, :with => group_id

Right now, I patched _call_filters to be able to do this, which works fine, but it's kind of ugly:

before :ensure_user_can_see_group, :with => Proc.new { |c| c.params[:group_id] }

_call_filters:

  def _call_filters(filter_set)
    (filter_set || []).each do |filter, rule|
      if _call_filter_for_action?(rule, action_name) && _filter_condition_met?(rule)
        case filter
        when Symbol, String
          if rule.key?(:with)
            args = rule[:with]
            # allow procs that are called in the context of the controller
            args = [args].flatten.map { |a| a.call(self) if a.is_a?(Proc) }
            send(filter, *args)
          else
            send(filter)
          end
        when Proc           then self.instance_eval(&filter)
        end
      end
    end
    return :filter_chain_completed
  end

What do you think?

Comments and changes to this ticket

  • Michael Klishin (antares)

    Michael Klishin (antares) July 5th, 2008 @ 11:42 PM

    I think Proc approach is good enough, for method operating on a class level like filter methods it's impossible to know anything about params hash upfront.

  • Michael Klishin (antares)

    Michael Klishin (antares) August 2nd, 2008 @ 06:33 PM

    • → Milestone changed from “” to “0.9.5”
    • → State changed from “new” to “open”
  • Michael Klishin (antares)

    Michael Klishin (antares) August 7th, 2008 @ 10:26 AM

    • → Assigned user changed from “” to “Michael Klishin (antares)”
  • Michael Klishin (antares)

    Michael Klishin (antares) August 9th, 2008 @ 05:10 AM

    Matthew,

    Could you make a patch from your version? Proc that takes controller instance is probably the best way we can go with this. I'd apply it if you submit a patch.

  • Michael Klishin (antares)

    Michael Klishin (antares) August 17th, 2008 @ 05:34 PM

    Any updates on this?

    By the way, I usually just use :with => :parent_id or something to pass in a key to be taken from params hash. It's as useful as passing some magic token to get params[key] in the end.

    What you think about just using this technique/convention and not adding anything to the framework itself? You can actually implement a controller plugin that uses filters under the covers. This lets you do pretty impressive things that may be very app specific: from fetching objects in resources chain to doing authorization using a variety of data sources like DB or LDAP or anything you can reach from Ruby code.

  • Michael Klishin (antares)

    Michael Klishin (antares) August 18th, 2008 @ 04:07 PM

    • → State changed from “open” to “invalid”

    Closing this for now because existing filters functionality is flexible and lets you build on top of. I think you can build a plugin to get exactly what you want: we try to keep -core as small and simple as possible.

    If you have suggestions on how we can make -core more open for extension by plugin developers, I am all ears.

Please Login or create a free account to add a new comment.

You can update this ticket by sending an email to from your email client. (help)

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

People watching this ticket