Skip to content

Commit

Permalink
Add specs for relation with association
Browse files Browse the repository at this point in the history
  • Loading branch information
rnice01 committed Oct 28, 2017
1 parent 9b41f73 commit dadf9f2
Show file tree
Hide file tree
Showing 2 changed files with 211 additions and 172 deletions.
152 changes: 76 additions & 76 deletions lib/administrate/order.rb
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
Loading

0 comments on commit dadf9f2

Please sign in to comment.