Skip to content

Commit

Permalink
inspect on Ransack::Search objects now will not load entire object graph
Browse files Browse the repository at this point in the history
This means that:

1) inspect calls will be faster
2) inspect calls will be easier to understand
3) queries won't be executed until #result is called on the Search object

Fixes #115 with #121
  • Loading branch information
glebm authored and radar committed Aug 10, 2012
1 parent 2a6f9cc commit 953ec1b
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 0 deletions.
4 changes: 4 additions & 0 deletions lib/ransack/nodes/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ def persisted?
false
end

def inspect
"Attribute <#{name}>"
end

end
end
end
7 changes: 7 additions & 0 deletions lib/ransack/nodes/condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,13 @@ def default_type
predicate.type || (attributes.first && attributes.first.type)
end

def inspect
data =[['attributes', a.try(:map, &:name)], ['predicate', p], ['combinator', m], ['values', v.try(:map, &:value)]].reject { |e|
e[1].blank?
}.map { |v| "#{v[0]}: #{v[1]}" }.join(', ')
"Condition <#{data}>"
end

private

def valid_combinator?
Expand Down
7 changes: 7 additions & 0 deletions lib/ransack/nodes/grouping.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,13 @@ def build(params)
self
end

def inspect
data =[['conditions', conditions], ['combinator', combinator]].reject { |e|
e[1].blank?
}.map { |v| "#{v[0]}: #{v[1]}" }.join(', ')
"Grouping <#{data}>"
end

private

def write_attribute(name, val)
Expand Down
4 changes: 4 additions & 0 deletions lib/ransack/nodes/value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,10 @@ def cast_to_decimal(val)
end
end

def inspect
"Value <#{value}>"
end

def array_of_arrays?(val)
Array === val && Array === val.first
end
Expand Down
4 changes: 4 additions & 0 deletions lib/ransack/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ def method_missing(method_id, *args)
end
end

def inspect
"Ransack::Search<class: #{klass.name}, base: #{base.inspect}>"
end

private

def collapse_multiparameter_attributes!(attrs)
Expand Down
5 changes: 5 additions & 0 deletions spec/ransack/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ module Ransack
condition.attributes.first.name.should eq 'name'
condition.value.should eq ['Ernie', 'Bert']
end

it 'does not evaluate the query on #inspect' do
search = Search.new(Person, :children_id_in => [1, 2, 3])
search.inspect.should_not match /ActiveRecord/
end
end

describe '#result' do
Expand Down

0 comments on commit 953ec1b

Please sign in to comment.