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
|
|
|
|
|
2011-04-11 16:04:31 +00:00
|
|
|
attr_reader :name, :dir
|
2011-03-31 00:31:39 +00:00
|
|
|
i18n_word :asc, :desc
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def extract(context, str)
|
|
|
|
attr, direction = str.split(/\s+/,2)
|
|
|
|
self.new(context).build(:name => attr, :dir => direction)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build(params)
|
|
|
|
params.with_indifferent_access.each do |key, value|
|
|
|
|
if key.match(/^(name|dir)$/)
|
|
|
|
self.send("#{key}=", value)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
self
|
|
|
|
end
|
|
|
|
|
|
|
|
def valid?
|
2011-04-18 17:59:47 +00:00
|
|
|
bound? && attr
|
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)
|
|
|
|
@dir = %w(asc desc).include?(dir) ? dir : 'asc'
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|