lib/lolita/configuration/column.rb in lolita-3.1.17 vs lib/lolita/configuration/column.rb in lolita-3.1.18
- old
+ new
@@ -10,36 +10,64 @@
def initialize(dbi,*args,&block)
@dbi=dbi
self.set_attributes(*args)
self.instance_eval(&block) if block_given?
validate
- set_default_values
end
+ def value(record)
+ if self.name.to_s.match(/_id$/) && record.respond_to?(self.name.to_s.gsub(/_id$/,"").to_sym)
+ remote_record = record.send(self.name.to_s.gsub(/_id$/,"").to_sym)
+ if remote_record.respond_to?(:title)
+ remote_record.send(:title)
+ elsif remote_record.respond_to?(:name)
+ remote_record.send(:name)
+ else
+ record.send(self.name)
+ end
+ else
+ record.send(self.name)
+ end
+ end
+
def title(value=nil)
@title=value if value
@title||=@dbi.klass.human_attribute_name(@name.to_s)
@title
end
def sortable?
@sortable
end
-
- def currently_sorting?(params)
- @sortable && params[:sc].to_s==self.name.to_s
+
+ def current_sort_state(params)
+ @sortable && sort_pairs(params).detect{|pair| pair[0]==self.name.to_s} || []
end
- def sort_options(params)
- direction=if params[:sc].to_s==self.name.to_s
- params[:sd].to_s=="asc" ? "desc" : "asc"
+ def sort_params params
+ if @sortable
+ pairs = sort_pairs(params)
+ found_pair = false
+ pairs.each_with_index{|pair,index|
+ if pair[0] == self.name.to_s
+ pairs[index][1] = pair[1] == "asc" ? "desc" : "asc"
+ found_pair = true
+ end
+ }
+ unless found_pair
+ pairs << [self.name.to_s,"asc"]
+ end
+ (pairs.map{|pair| pair.join(",")}).join("|")
else
- "desc"
+ ""
end
- {:sc=>self.name,:sd=>direction}
end
-
+
+ def sort_pairs params
+ (params[:s] || "").split("|").map{|pair| pair.split(",")}
+ end
+
# Define format, for details see Lolita::Support::Formatter and Lolita::Support::Formater::Rails
def formatter(value=nil,&block)
if block_given?
@formatter=Lolita::Support::Formatter.new(value,&block)
elsif value || !@formatter
@@ -59,32 +87,23 @@
@formatter=Lolita::Support::Formatter::Rails.new(value)
end
end
def set_attributes(*args)
- if !args.empty?
- if args[0].is_a?(Hash)
- args[0].each{|m,value|
- self.send("#{m}=".to_sym,value)
- }
- elsif args[0].is_a?(Symbol) || args[0].is_a?(String)
- self.name=args[0].to_s
- if args[1].is_a?(Hash)
- args[1].each{|m,value|
- self.send("#{m}=".to_sym,value)
- }
- end
- else
- raise ArgumentError.new("Lolita::Configuration::Column arguments must be Hash or Symbol or String instead of #{args[0].class}")
+ options = args ? args.extract_options! : {}
+ if args[0].respond_to?(:field)
+ [:name,:type].each do |attr|
+ self.send(:"#{attr}=",args[0].send(attr))
end
+ elsif args[0]
+ self.name = args[0]
end
+ options.each do |attr_name,value|
+ self.send(:"#{attr_name}=",value)
+ end
end
private
-
- def set_default_values
- @sort_direction||=:desc
- end
def validate
raise ArgumentError.new("Column must have name.") unless self.name
end
end