2011-03-31 00:31:39 +00:00
|
|
|
module Ransack
|
|
|
|
module Nodes
|
|
|
|
class Sort < Node
|
2011-04-10 00:55:28 +00:00
|
|
|
include Bindable
|
|
|
|
|
2015-03-18 01:59:00 +00:00
|
|
|
attr_reader :name, :dir, :ransacker_args
|
2011-03-31 00:31:39 +00:00
|
|
|
i18n_word :asc, :desc
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def extract(context, str)
|
2014-12-11 17:31:54 +00:00
|
|
|
return unless str
|
2011-03-31 00:31:39 +00:00
|
|
|
attr, direction = str.split(/\s+/,2)
|
2013-12-15 18:27:50 +00:00
|
|
|
self.new(context).build(name: attr, dir: direction)
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(params)
|
|
|
|
params.with_indifferent_access.each do |key, value|
|
2015-03-18 01:59:00 +00:00
|
|
|
if key.match(/^(name|dir|ransacker_args)$/)
|
2011-03-31 00:31:39 +00:00
|
|
|
self.send("#{key}=", value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2013-12-07 00:51:55 +00:00
|
|
|
bound? && attr &&
|
2014-10-09 21:48:38 +00:00
|
|
|
context.klassify(parent).ransortable_attributes(context.auth_object)
|
|
|
|
.include?(attr_name)
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def name=(name)
|
|
|
|
@name = name
|
2011-04-10 00:55:28 +00:00
|
|
|
context.bind(self, name) unless name.blank?
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def dir=(dir)
|
2014-10-03 23:25:14 +00:00
|
|
|
dir = dir.downcase if dir
|
2014-10-09 21:48:38 +00:00
|
|
|
@dir =
|
2015-08-29 20:52:57 +00:00
|
|
|
if ['asc'.freeze, 'desc'.freeze].freeze.include?(dir)
|
2014-10-09 21:48:38 +00:00
|
|
|
dir
|
|
|
|
else
|
2015-08-29 20:52:57 +00:00
|
|
|
'asc'.freeze
|
2014-10-09 21:48:38 +00:00
|
|
|
end
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
|
2015-03-18 01:59:00 +00:00
|
|
|
def ransacker_args=(ransack_args)
|
|
|
|
@ransacker_args = ransack_args
|
|
|
|
end
|
|
|
|
|
2011-03-31 00:31:39 +00:00
|
|
|
end
|
|
|
|
end
|
2013-10-24 15:56:45 +00:00
|
|
|
end
|