Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added support to joins on AR #1

Merged
merged 1 commit into from
Apr 25, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions lib/data_table/active_record.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,27 @@
module DataTable
module ActiveRecord
module ClassMethods

def _find_objects params, fields, search_fields
self.where(_where_conditions params[:sSearch], search_fields).
includes(_discover_joins fields).
order(_order_fields params, fields).
paginate :page => _page(params), :per_page => _per_page(params)
end

def _discover_joins fields
joins = Set.new

fields.each { |it|
field = it.split('.')
if (field.size == 2) then
joins.add field[0].singularize.to_sym
end
}

joins.collect
end

def _where_conditions query, search_fields
return if query.blank?

Expand Down
11 changes: 10 additions & 1 deletion spec/active_record_data_table_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@
it "should find the objects required based on the params" do
params = {:sSearch => "answer", :iSortCol_0 => "0", :sSortDir_0 => "desc", :iDisplayLength => 10, :sEcho => 1}

mock(self)._discover_joins(%w(foo bar baz)) { [] }
mock(self)._where_conditions("answer", %w(foo bar)) { "where clause" }
mock(self)._order_fields(params, %w(foo bar baz)) { "order" }

mock(self).where("where clause") { mock!.order("order") { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } }
mock(self).where("where clause") { mock!.includes([]) { mock!.order("order") { mock!.paginate({:page => :page, :per_page => :per_page}) { :answer } } } }
mock(self)._page(params) { :page }
mock(self)._per_page(params) { :per_page }

Expand All @@ -33,6 +34,14 @@

end

context "#_discover_joins" do

it "should return the joins on the fields" do
_discover_joins(%w(foo.bar foz.ber baz)).should == [:foo, :foz]
end

end

context "#_order_fields" do

it "should find the field name and pass the sort direction" do
Expand Down