| 1 |
require File.join(File.dirname(__FILE__) , 'query_builder', 'lib', 'query_builder') |
|---|
| 2 |
|
|---|
| 3 |
class CommentQuery < QueryBuilder |
|---|
| 4 |
attr_reader :uses_node_name, :node_name |
|---|
| 5 |
set_main_table 'comments' |
|---|
| 6 |
set_main_class 'Comment' |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
def default_order_clause |
|---|
| 10 |
"created_at ASC" |
|---|
| 11 |
end |
|---|
| 12 |
|
|---|
| 13 |
def default_context_filter |
|---|
| 14 |
|
|---|
| 15 |
raise Exception.new("CommentQuery should only be called from within NodeQuery") |
|---|
| 16 |
end |
|---|
| 17 |
|
|---|
| 18 |
def parse_change_class(rel, is_last) |
|---|
| 19 |
case rel |
|---|
| 20 |
when 'author' |
|---|
| 21 |
add_table('users') |
|---|
| 22 |
@where << "#{table('users')}.id = #{field_or_attr('author_id')}" |
|---|
| 23 |
|
|---|
| 24 |
when 'node', 'nodes' |
|---|
| 25 |
add_table('discussions') |
|---|
| 26 |
add_table('nodes') |
|---|
| 27 |
@where << "#{table('discussions')}.id = #{table('comments')}.discussion_id" |
|---|
| 28 |
@where << "#{table('nodes')}.id = #{table('discussions')}.node_id" |
|---|
| 29 |
return NodeQuery |
|---|
| 30 |
else |
|---|
| 31 |
return nil |
|---|
| 32 |
end |
|---|
| 33 |
end |
|---|
| 34 |
|
|---|
| 35 |
|
|---|
| 36 |
def map_literal(value) |
|---|
| 37 |
if value =~ /(.*?)\[(node|visitor|param):(\w+)\](.*)/ |
|---|
| 38 |
val_start = $1 == '' ? '' : "#{$1.inspect} +" |
|---|
| 39 |
val_end = $4 == '' ? '' : "+ #{$4.inspect}" |
|---|
| 40 |
case $2 |
|---|
| 41 |
when 'visitor' |
|---|
| 42 |
if $3 == 'user_id' |
|---|
| 43 |
value = "visitor.id" |
|---|
| 44 |
else |
|---|
| 45 |
value = "Node.zafu_attribute(visitor.contact, #{$3.inspect})" |
|---|
| 46 |
end |
|---|
| 47 |
when 'node' |
|---|
| 48 |
@uses_node_name = true |
|---|
| 49 |
if $3 == 'user_id' |
|---|
| 50 |
value = "#{@node_name}.user_id" |
|---|
| 51 |
else |
|---|
| 52 |
value = "Node.zafu_attribute(#{@node_name}, #{$3.inspect})" |
|---|
| 53 |
end |
|---|
| 54 |
when 'param' |
|---|
| 55 |
return "\#{Node.connection.quote(#{val_start}params[:#{$3}].to_s#{val_end})}" |
|---|
| 56 |
end |
|---|
| 57 |
|
|---|
| 58 |
if !val_start.blank? || !val_end.blank? |
|---|
| 59 |
"\#{Node.connection.quote(#{val_start}#{value}#{val_end})}" |
|---|
| 60 |
else |
|---|
| 61 |
"\#{#{value}}" |
|---|
| 62 |
end |
|---|
| 63 |
else |
|---|
| 64 |
value = Node.connection.quote(value) |
|---|
| 65 |
end |
|---|
| 66 |
end |
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
def map_field(fld, table_name, context = nil) |
|---|
| 70 |
if ['status', 'updated_at', 'author_name', 'created_at', 'title', 'text', 'author_id'].include?(fld) |
|---|
| 71 |
"#{table_name}.#{fld}" |
|---|
| 72 |
else |
|---|
| 73 |
|
|---|
| 74 |
end |
|---|
| 75 |
end |
|---|
| 76 |
|
|---|
| 77 |
def map_attr(fld) |
|---|
| 78 |
|
|---|
| 79 |
nil |
|---|
| 80 |
end |
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
def finder(count) |
|---|
| 84 |
return 'nil' unless valid? |
|---|
| 85 |
case count |
|---|
| 86 |
when :count |
|---|
| 87 |
"#{node_name}.do_find(:count, \"#{count_sql}\", #{!uses_node_name}, #{main_class})" |
|---|
| 88 |
else |
|---|
| 89 |
"#{node_name}.do_find(#{count.inspect}, \"#{to_sql}\", #{!uses_node_name}, #{main_class})" |
|---|
| 90 |
end |
|---|
| 91 |
end |
|---|
| 92 |
end |
|---|