Skip to content
Open
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
16 changes: 14 additions & 2 deletions lib/smart_listing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,10 @@ def setup params, cookies
else
# let's sort by all attributes
#
@collection = @collection.order(sort_keys.collect{|s| "#{s[1]} #{@sort[s[0]]}" if @sort[s[0]]}.compact) if @sort && !@sort.empty?
if @sort && !@sort.empty?
sorting = sort_keys.collect{|s| "#{s[1]} #{@sort[s[0]]}".strip if @sort[s[0]]}.compact
@collection = @collection.order(sorting)
end

if @options[:paginate] && @per_page > 0
@collection = @collection.page(@page).per(@per_page)
Expand Down Expand Up @@ -200,14 +203,23 @@ def set_param key, value, store = @params
end
end

def attribute_method?(klass, attr)
klass_name = klass.name.to_s.camelize

attr = attr.split('.')[1] if attr.starts_with?("#{klass_name}.")

klass.attribute_method?(attr)
end

def parse_sort sort_params
sort = nil

if @options[:sort_attributes] == :implicit
return sort if sort_params.blank?

sort_params.map do |attr, dir|
key = attr.to_s if @options[:array] || @collection.klass.attribute_method?(attr)
key = attr.to_s if @options[:array] || attribute_method?(@collection.klass, attr)

if key && ALLOWED_DIRECTIONS[dir.to_s]
sort ||= {}
sort[key] = dir.to_s
Expand Down