Skip to content

Commit

Permalink
Added options to query_parse to ignore unwanted parameters. Added 'hp…
Browse files Browse the repository at this point in the history
…arams' option to only return Hash parameters from params.
  • Loading branch information
gaspard committed Feb 28, 2013
1 parent aff74f1 commit 7774176
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 1 deletion.
2 changes: 2 additions & 0 deletions History.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
* Minor changes
* Support for 'lang_list' when creating vhost file <== TODO: Document
* Support for Range in zafu. <== TODO: Document
* Added options to query_parse to ignore unwanted parameters <== TODO: Document
* Added 'hparams' option to only return Hash parameters from params. <== TODO: Document

== 1.2.2 2012-08-30

Expand Down
6 changes: 5 additions & 1 deletion lib/zena/use/query_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ module QueryBuilder
module ViewMethods
include RubyLess
safe_method [:query_parse, String] => {:class => String, :accept_nil => true}
safe_method [:query_parse, String, Hash] => {:class => String, :accept_nil => true}

safe_method :query_errors => {:class => String, :nil => true, :html_safe => true}

def find_node_by_zip(zip)
Expand Down Expand Up @@ -43,13 +45,15 @@ def query(class_name, node_name, pseudo_sql, opts = {})

# Takes a hash of parameters and builds query arguments for SQLiss
# the query ends up like ' AND param_name = "foo" AND param_name > 35'...
def query_parse(params)
def query_parse(params, opts = {})
ignore_params = (opts[:ignore] || '').split(',').map(&:strip)
params ||= {}
res = []
if params.kind_of?(String)
return params
elsif params.kind_of?(Hash)
params.each do |k,v|
next if ignore_params.include?(k.to_s)
clause = query_build_clause(k,v)
res << clause unless clause.blank?
end
Expand Down
33 changes: 33 additions & 0 deletions lib/zena/use/zafu_safe_definitions.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module Zena
module Use
module ZafuSafeDefinitions
# This is a dummy class to declare safe parameters on params.
class ParamsDictionary
include RubyLess
safe_method ['[]', Symbol] => {:class => String, :nil => true}
Expand Down Expand Up @@ -34,6 +35,32 @@ def transform(v)
end
end
end

# This class is used to access nested Hash params {:key => 'foo'}.
class HParamsDictionary
def initialize(params)
@params = {}
params.each do |k, v|
@params[k.to_sym] = transform(v)
end
end

def [](key)
@params[key.to_sym] || {}
end

include RubyLess
safe_method ['[]', Symbol] => {:class => ParamsDictionary, :nil => false}

private
def transform(v)
if v.kind_of?(Hash)
v
else
{}
end
end
end

module ViewMethods
include RubyLess
Expand Down Expand Up @@ -126,6 +153,8 @@ def self.join_proc

safe_method :params => ParamsDictionary
safe_method :aparams => AParamsDictionary
safe_method :hparams => HParamsDictionary

safe_method :now => {:method => 'Time.now', :class => Time}
safe_method :string_hash => {:method => 'StringHash.new', :class => StringHash}
safe_method [:string_hash, Hash] => {:method => 'StringHash.from_hash', :class => StringHash}
Expand Down Expand Up @@ -204,6 +233,10 @@ def zafu_max(a, b)
def aparams
@aparams ||= AParamsDictionary.new(params)
end

def hparams
@hparams ||= HParamsDictionary.new(params)
end
end # ViewMethods


Expand Down

0 comments on commit 7774176

Please sign in to comment.