Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
order clause refactoring, allow to use custom sql ordering strategies
- added ability to use custom class for handling ordering per resource eg ``` ActiveAdmin.register Post do config.order_clause = MyOrderClause end ``` - added dsl to customize ordering strategy per column eg ``` ActiveAdmin.register Post do order_by(:full_name) do |order_clause| ['COALESCE(NULLIF(last_name, ''), first_name), first_name', order_clause.order].join(' ') end end ```
- Loading branch information
Showing
14 changed files
with
178 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,48 @@ | ||
module ActiveAdmin | ||
class OrderClause | ||
attr_reader :field, :order | ||
attr_reader :field, :order, :active_admin_config | ||
|
||
def initialize(clause) | ||
def initialize(active_admin_config, clause) | ||
clause =~ /^([\w\_\.]+)(->'\w+')?_(desc|asc)$/ | ||
@column = $1 | ||
@op = $2 | ||
@order = $3 | ||
|
||
@active_admin_config = active_admin_config | ||
@field = [@column, @op].compact.join | ||
end | ||
|
||
def valid? | ||
@field.present? && @order.present? | ||
end | ||
|
||
def to_sql(active_admin_config) | ||
table = active_admin_config.resource_column_names.include?(@column) ? active_admin_config.resource_table_name : nil | ||
table_column = (@column =~ /\./) ? @column : | ||
[table, active_admin_config.resource_quoted_column_name(@column)].compact.join(".") | ||
def apply(chain) | ||
chain.reorder(sql) | ||
end | ||
|
||
def to_sql | ||
[table_column, @op, ' ', @order].compact.join | ||
end | ||
|
||
def table | ||
active_admin_config.resource_column_names.include?(@column) ? active_admin_config.resource_table_name : nil | ||
end | ||
|
||
def table_column | ||
(@column =~ /\./) ? @column : | ||
[table, active_admin_config.resource_quoted_column_name(@column)].compact.join(".") | ||
end | ||
|
||
def sql | ||
custom_sql || to_sql | ||
end | ||
|
||
protected | ||
|
||
def custom_sql | ||
if active_admin_config.ordering[@column].present? | ||
active_admin_config.ordering[@column].call(self) | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
module ActiveAdmin | ||
class Resource | ||
module Ordering | ||
|
||
def ordering | ||
@ordering ||= {}.with_indifferent_access | ||
end | ||
|
||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
require 'rails_helper' | ||
|
||
module ActiveAdmin | ||
describe Resource, "Ordering" do | ||
describe "#order_by" do | ||
|
||
let(:application) { ActiveAdmin::Application.new } | ||
let(:namespace) { ActiveAdmin::Namespace.new application, :admin } | ||
let(:resource_config) { ActiveAdmin::Resource.new namespace, Post } | ||
let(:dsl){ ActiveAdmin::ResourceDSL.new(resource_config, Post) } | ||
|
||
it "should register the ordering in the config" do | ||
dsl.run_registration_block do | ||
order_by(:age) do |order_clause| | ||
if order_clause.order == 'desc' | ||
[order_clause.to_sql, 'NULLS LAST'].join(' ') | ||
end | ||
end | ||
end | ||
expect(resource_config.ordering.size).to eq(1) | ||
end | ||
|
||
|
||
it "should allow to setup custom ordering class" do | ||
MyOrderClause = Class.new(ActiveAdmin::OrderClause) | ||
dsl.run_registration_block do | ||
config.order_clause = MyOrderClause | ||
end | ||
expect(resource_config.order_clause).to eq(MyOrderClause) | ||
expect(application.order_clause).to eq(ActiveAdmin::OrderClause) | ||
|
||
end | ||
|
||
end | ||
end | ||
end | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters