-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add specs for relation with association
- Loading branch information
Showing
2 changed files
with
211 additions
and
172 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,78 +1,78 @@ | ||
module Administrate | ||
class Order | ||
def initialize(attribute = nil, direction = nil) | ||
@attribute = attribute | ||
@direction = direction || :asc | ||
end | ||
|
||
def apply(relation) | ||
return order_by_association(relation) if | ||
!reflect_association(relation).nil? | ||
|
||
return relation.reorder("#{attribute} #{direction}") if | ||
relation.columns_hash.keys.include?(attribute.to_s) | ||
|
||
relation | ||
end | ||
|
||
def ordered_by?(attr) | ||
attr.to_s == attribute.to_s | ||
end | ||
|
||
def order_params_for(attr) | ||
{ | ||
order: attr, | ||
direction: reversed_direction_param_for(attr) | ||
} | ||
end | ||
|
||
attr_reader :direction | ||
|
||
private | ||
|
||
attr_reader :attribute | ||
|
||
def reversed_direction_param_for(attr) | ||
if ordered_by?(attr) | ||
opposite_direction | ||
else | ||
:asc | ||
end | ||
end | ||
|
||
def opposite_direction | ||
direction.to_sym == :asc ? :desc : :asc | ||
end | ||
|
||
def order_by_association(relation) | ||
return order_by_count(relation) if has_many_attribute?(relation) | ||
|
||
return order_by_id(relation) if belongs_to_attribute?(relation) | ||
|
||
relation | ||
end | ||
|
||
def order_by_count(relation) | ||
relation. | ||
left_joins(attribute.to_sym). | ||
group(:id). | ||
reorder("COUNT(#{attribute}.id) #{direction}") | ||
end | ||
|
||
def order_by_id(relation) | ||
relation.reorder("#{attribute}_id #{direction}") | ||
end | ||
|
||
def has_many_attribute?(relation) | ||
reflect_association(relation).macro == :has_many | ||
end | ||
|
||
def belongs_to_attribute?(relation) | ||
reflect_association(relation).macro == :belongs_to | ||
end | ||
|
||
def reflect_association(relation) | ||
relation.klass.reflect_on_association(attribute.to_s) | ||
end | ||
end | ||
class Order | ||
def initialize(attribute = nil, direction = nil) | ||
@attribute = attribute | ||
@direction = direction || :asc | ||
end | ||
|
||
def apply(relation) | ||
return order_by_association(relation) if | ||
!reflect_association(relation).nil? | ||
|
||
return relation.reorder("#{attribute} #{direction}") if | ||
relation.columns_hash.keys.include?(attribute.to_s) | ||
|
||
relation | ||
end | ||
|
||
def ordered_by?(attr) | ||
attr.to_s == attribute.to_s | ||
end | ||
|
||
def order_params_for(attr) | ||
{ | ||
order: attr, | ||
direction: reversed_direction_param_for(attr) | ||
} | ||
end | ||
|
||
attr_reader :direction | ||
|
||
private | ||
|
||
attr_reader :attribute | ||
|
||
def reversed_direction_param_for(attr) | ||
if ordered_by?(attr) | ||
opposite_direction | ||
else | ||
:asc | ||
end | ||
end | ||
|
||
def opposite_direction | ||
direction.to_sym == :asc ? :desc : :asc | ||
end | ||
|
||
def order_by_association(relation) | ||
return order_by_count(relation) if has_many_attribute?(relation) | ||
|
||
return order_by_id(relation) if belongs_to_attribute?(relation) | ||
|
||
relation | ||
end | ||
|
||
def order_by_count(relation) | ||
relation. | ||
left_joins(attribute.to_sym). | ||
group(:id). | ||
reorder("COUNT(#{attribute}.id) #{direction}") | ||
end | ||
|
||
def order_by_id(relation) | ||
relation.reorder("#{attribute}_id #{direction}") | ||
end | ||
|
||
def has_many_attribute?(relation) | ||
reflect_association(relation).macro == :has_many | ||
end | ||
|
||
def belongs_to_attribute?(relation) | ||
reflect_association(relation).macro == :belongs_to | ||
end | ||
|
||
def reflect_association(relation) | ||
relation.klass.reflect_on_association(attribute.to_s) | ||
end | ||
end | ||
end |
Oops, something went wrong.