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

Allow to set a table alias on columns #389

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 9 additions & 1 deletion lib/ajax-datatables-rails/datatable/column.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,15 @@ def source
end

def table
model.respond_to?(:arel_table) ? model.arel_table : model
if model.respond_to?(:arel_table)
table_alias.present? ? model.arel_table.alias(table_alias) : model.arel_table
else
model
end
end

def table_alias
@table_alias ||= @view_column[:table_alias]
end

def model
Expand Down
47 changes: 47 additions & 0 deletions spec/ajax-datatables-rails/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,4 +220,51 @@ def paginate_records(records)
end
end
end

describe 'Named join' do
describe '#data' do
let(:datatable) { NamedJoinDatatable.new(group_sample_params) }
let(:admin) { create(:user, username: 'johndoe', email: '[email protected]') }

before do
user_1 = create(:user, username: 'johndoe', email: '[email protected]')

group_1 = create(:group, admin: nil, name: 'Super Group')
user_2 = create(:user, username: 'sandbo', email: '[email protected]', group: group_1)

user_3 = create(:user, username: 'msmith', email: '[email protected]')
create(:group, admin: user_2, name: 'First group', users: [user_1, user_3])

user_4 = create(:user, username: 'anna', email: '[email protected]')
create(:group, admin: user_4, name: 'Awesome group', users: [user_4])

create(:user, username: 'hans', email: '[email protected]')
end

it 'returns list' do
group_names = datatable.data.map { |i| i[:group_name] }
admin_names = datatable.data.map { |i| i[:group_admin] }
expect(group_names).to eq ["Awesome group", nil, "First group", "First group", "Super Group"]
expect(admin_names).to eq ["anna", nil, "sandbo", "sandbo", nil]
end

it 'global search' do
datatable.params[:search][:value] = 'sand'
user_names = datatable.data.map { |i| i[:username] }
expect(user_names).to eq ["johndoe", "msmith", "sandbo"]
end

it 'sorts list asc' do
datatable.params[:order]['0'] = { column: '2', dir: 'asc' }
admin_names = datatable.data.map { |i| i[:group_admin] }
expect(admin_names).to eq ["anna", "sandbo", "sandbo", nil, nil]
end

it 'sort list desc' do
datatable.params[:order]['0'] = { column: '2', dir: 'desc' }
admin_names = datatable.data.map { |i| i[:group_admin] }
expect(admin_names).to eq [nil, nil, "sandbo", "sandbo", "anna"]
end
end
end
end
24 changes: 24 additions & 0 deletions spec/ajax-datatables-rails/datatable/column_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@
end
end

describe '#table_alias' do
it 'returns nil as the table_alias' do
expect(column.table_alias).to eq nil
end
end

describe '#table' do
context 'with ActiveRecord ORM' do
it 'returns the corresponding AR table' do
Expand Down Expand Up @@ -219,4 +225,22 @@
expect { datatable.to_json }.to raise_error(AjaxDatatablesRails::Error::InvalidSearchColumn).with_message("Check that column 'foo' exists in view_columns")
end
end

describe 'table name overwrites for named joins' do
let(:datatable) { NamedJoinDatatable.new(group_sample_params) }

let(:column) { datatable.datatable.columns.last }

describe '#source' do
it 'returns the data source from view_column' do
expect(column.source).to eq 'User.username'
end
end

describe '#table_alias' do
it 'returns the data source from table_alias' do
expect(column.table_alias).to eq 'admin'
end
end
end
end
8 changes: 8 additions & 0 deletions spec/factories/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

FactoryBot.define do
factory :group do |f|
f.name { Faker::Team.name }
f.admin { build(:user) }
end
end
26 changes: 26 additions & 0 deletions spec/support/datatables/named_join_datatable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

class NamedJoinDatatable < AjaxDatatablesRails::ActiveRecord
def view_columns
@view_columns ||= {
username: { source: 'User.username' },
group_name: { source: 'Group.name' },
group_admin: { source: 'User.username', table_alias: 'admin' }
}
end

def data
records.map do |record|
{
username: record.username,
group_name: record.group&.name,
group_admin: record.group&.admin&.username,
}
end
end

def get_raw_records
User.left_outer_joins(group: :admin)
.where(admin: { id: [nil, 1..] }) # Hack to force alias in Rails
end
end
35 changes: 35 additions & 0 deletions spec/support/helpers/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,41 @@ def sample_params
)
end

def group_sample_params
ActionController::Parameters.new(
{
'draw' => '1',
'columns' => {
'0' => {
'data' => 'username', 'name' => '', 'searchable' => 'true', 'orderable' => 'true',
'search' => {
'value' => '', 'regex' => 'false'
}
},
'1' => {
'data' => 'group_name', 'name' => '', 'searchable' => 'true', 'orderable' => 'true',
'search' => {
'value' => '', 'regex' => 'false'
}
},
'2' => {
'data' => 'group_admin', 'name' => '', 'searchable' => 'true', 'orderable' => 'false',
'search' => {
'value' => '', 'regex' => 'false'
}
}
},
'order' => {
'0' => { 'column' => '0', 'dir' => 'asc' },
},
'start' => '0', 'length' => '10', 'search' => {
'value' => '', 'regex' => 'false'
},
'_' => '1423364387185'
}
)
end

def sample_params_json
hash_params = sample_params.to_unsafe_h
hash_params['columns'] = hash_params['columns'].values
Expand Down
6 changes: 6 additions & 0 deletions spec/support/models/group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# frozen_string_literal: true

class Group < ActiveRecord::Base
belongs_to :admin, class_name: 'User'
has_many :users
end
3 changes: 3 additions & 0 deletions spec/support/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# frozen_string_literal: true

class User < ActiveRecord::Base
belongs_to :group, optional: true
has_many :admin_groups, foreign_key: :admin_id

def full_name
"#{first_name} #{last_name}"
end
Expand Down
8 changes: 8 additions & 0 deletions spec/support/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,14 @@
t.string :first_name
t.string :last_name
t.integer :post_id
t.integer :group_id

t.timestamps null: false
end

create_table :groups, force: true do |t|
t.string :name
t.integer :admin_id

t.timestamps null: false
end
Expand Down