Module: Zena::Use::QueryBuilder::ViewMethods

Includes:
RubyLess
Defined in:
lib/zena/use/query_builder.rb

Instance Method Summary collapse

Instance Method Details

#find_node_by_zip(zip) ⇒ Object



14
15
16
17
# File 'lib/zena/use/query_builder.rb', line 14

def find_node_by_zip(zip)
  return nil unless zip
  secure(Node) { Node.find_by_zip(zip) }
end

#query(class_name, node_name, pseudo_sql, opts = {}) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/zena/use/query_builder.rb', line 23

def query(class_name, node_name, pseudo_sql, opts = {})
  type = opts[:type] || (opts[:find] == :count ? :count : :find)
  @query_errors = nil
  if klass = VirtualClass[class_name]
    begin
      query = klass.build_query(:all, pseudo_sql,
        :node_name       => node_name,
        :main_class      => klass,
        # We use 'zafu_helper' (which is slower) instead of 'self' because our helper needs to have helper modules
        # mixed in and strangely RubyLess cannot access the helpers from 'self'.
        :rubyless_helper => zafu_helper.helpers
      )
      
      # Node.logger.warn eval(query.to_s(type == :count ? :count : :find), opts[:binding] || binding)
      
      if type == :count
        return klass.do_find(:count, eval(query.to_s(:count), opts[:binding] || binding))
      else
        return klass.do_find(opts[:find] || :all, eval(query.to_s, opts[:binding] || binding))
      end
    rescue ::QueryBuilder::Error => err
      @query_errors = "<span class='query'>#{::ERB::Util.html_escape(pseudo_sql)}</span> <span class='error'>#{::ERB::Util.html_escape(err)}</span>"
    end
  end
  # error
  type == :count ? 0 : nil
end

#query_errorsObject



19
20
21
# File 'lib/zena/use/query_builder.rb', line 19

def query_errors
  @query_errors
end

#query_parse(params, opts = {}) ⇒ Object

Takes a hash of parameters and builds query arguments for SQLiss the query ends up like ‘ AND param_name = “foo” AND param_name > 35’…



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/zena/use/query_builder.rb', line 53

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
  end
  res.empty? ? '1=1' : res.join(' and ')
end