#167 √ resolved
Marcus

opts parameter handling in part invocation insufficient for array values

Reported by Marcus | September 16th, 2008 @ 09:35 PM | in 1.0

Invoking a part like

part MyPart => :doit, :paramarray => [ { :k => value } ]

does not work as expected. :paramarray does not denote an array in the part view.

In parts_mixin, in line 33, the following code is found;

opts = Hash[*(opts.flatten)]

Before, the parameters for a part invocation have been partitioned to extract the part controller and the part parameters.

The line in question is then supposed to recreate a hash for the part parameters. However, if one of the parameter values is an array, it gets flattened, too.

A line such as opts = opts.inject({}) { |memo, subarr| memo[subarr[0]] = subarr[1..-1]; memo } might fix that problem.

Comments and changes to this ticket

  • Michael Klishin (antares)

    Michael Klishin (antares) September 17th, 2008 @ 06:32 AM

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

    I do not want to add injects and unreadable one liners to Merb code. Could you benchmark it with any other simple solution that just uses a for ... in loop?

  • Marcus

    Marcus September 17th, 2008 @ 01:43 PM

    Proposed rewrite of whole method would be for instance:

    def part(opts = {}) klasses = { } params = { } opts.each_pair do |k,v|

        if k.respond_to?(:ancestors) && k.ancestors.include?(Merb::PartController)
          klasses[k] = v
        else
          params[k] = v
        end
    
    

    end

    res = [] klasses.each_pair do |klass, action|

      res << klass.new(self, opts)._dispatch(action)
    
    

    end res.size == 1 ? res[0] : res end

    I still encountered another problem. The local variables I pass on to the view of a controller seem to be partly gone after I call a part! I dont have a fix for that, though.

  • Marcus

    Marcus September 17th, 2008 @ 01:46 PM

    Sorry for the code formatting!

    
      def part(opts = {})
          klasses = { }
          params = { }
          opts.each_pair do |k,v|
            if k.respond_to?(:ancestors) && k.ancestors.include?(Merb::PartController)
              klasses[k] = v
            else
              params[k] = v
            end
          end
    
          res = []
          klasses.each_pair do |klass, action|
            res << klass.new(self, opts)._dispatch(action)
          end
    
          res.size == 1 ? res[0] : res
        end
    
  • Michael Klishin (antares)

    Michael Klishin (antares) October 7th, 2008 @ 11:59 AM

    • → Milestone changed from “0.9.8” to “1.0”
  • Michael Klishin (antares)

    Michael Klishin (antares) October 10th, 2008 @ 04:12 PM

    • → Tag changed from “"merb-parts"” to “merb-parts”
    • → State changed from “open” to “resolved”

    Should be fixed in HEAD.

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 »

Shared Ticket Bins

People watching this ticket

Tags