Return to Snippet

Revision: 3374
at July 17, 2007 15:03 by vanne


Initial Code
class Object
  
  # provides a params hash which will can useful for testing
  # USAGE : Model.to_params :name_of_params_hash # => :name_of_params_hash defaults to :params
  #         to override the params hash, just pass a hash list of attributes :
  #         Model.to_params.merge(:my_options => "my_value")
  def to_params(params_name="params")
    @@params_var = params_name.to_sym
    attributes = self.new.attributes.keys.map {|c| c.to_sym}
    p_hsh = {}
    attributes.map {|a| p_hsh[a]=nil}
    params = {@@params_var => p_hsh}  
    
    params.instance_eval do
      #overrides Hash#merge method by merging just the sub hash and not the main one
      def merge(options={})
        self[@@params_var].update(options)
        return self
      end
    end
    return params
  end
  
end

Initial URL


Initial Description


Initial Title
Generate params hash for Model

Initial Tags
rails, ruby

Initial Language
Other