Permalink
Browse files

Support for filters

  • Loading branch information...
1 parent c2028cf commit a22ff21c5f7788b931344cf0cdfa182a7a9995c4 @mshibuya committed Feb 21, 2012
Showing with 29 additions and 44 deletions.
  1. +21 −34 lib/rails_admin/adapters/mongoid.rb
  2. +8 −10 lib/rails_admin/adapters/mongoid/extension.rb
@@ -109,7 +109,7 @@ def properties
"Array" => :string,
"BigDecimal" => :string,
"Boolean" => :boolean,
- "Date" => :datetime,
+ "Date" => :date,
"DateTime" => :datetime,
"Float" => :float,
"Hash" => :string,
@@ -154,19 +154,16 @@ def accessible_by(*args)
def get_conditions_hash(model_config, query, filters)
query_statements = []
filters_statements = []
- values = []
conditions = {}
if query.present?
queryable_fields = model_config.list.fields.select(&:queryable?)
queryable_fields.each do |field|
searchable_columns = field.searchable_columns.flatten
searchable_columns.each do |field_infos|
- statement, value1, value2 = build_statement(field_infos[:column], field_infos[:type], query, field.search_operator)
+ statement = build_statement(field_infos[:column], field_infos[:type], query, field.search_operator)
if statement
query_statements << statement
- values << value1 unless value1.nil?
- values << value2 unless value2.nil?
end
end
end
@@ -176,34 +173,27 @@ def get_conditions_hash(model_config, query, filters)
conditions['$or'] = query_statements
end
-=begin
- # TODO: support filters
if filters.present?
@filterable_fields = model_config.list.fields.select(&:filterable?).inject({}){ |memo, field| memo[field.name.to_sym] = field.searchable_columns; memo }
filters.each_pair do |field_name, filters_dump|
filters_dump.each do |filter_index, filter_dump|
field_statements = []
@filterable_fields[field_name.to_sym].each do |field_infos|
- statement, value1, value2 = build_statement(field_infos[:column], field_infos[:type], filter_dump[:v], (filter_dump[:o] || 'default'))
+ statement = build_statement(field_infos[:column], field_infos[:type], filter_dump[:v], (filter_dump[:o] || 'default'))
if statement
field_statements << statement
- values << value1 unless value1.nil?
- values << value2 unless value2.nil?
end
end
- filters_statements << "(#{field_statements.join(' OR ')})" unless field_statements.empty?
+ filters_statements.push(field_statements) unless field_statements.empty?
end
end
end
unless filters_statements.empty?
- conditions[0] += " AND " unless conditions == [""]
- conditions[0] += "#{filters_statements.join(" AND ")}" # filters should all be true
+ conditions = { '$and' => [ conditions, filters_statements ].flatten }
end
- conditions += values
-=end
- conditions != {} ? { :conditions => conditions } : {}
+ conditions.any? ? { :conditions => conditions } : {}
end
def build_statement(column, type, value, operator)
@@ -216,32 +206,29 @@ def build_statement(column, type, value, operator)
# this operator/value has been discarded (but kept in the dom to override the one stored in the various links of the page)
return if operator == '_discard' || value == '_discard'
-=begin
- # TODO: support types
# filtering data with unary operator, not type dependent
if operator == '_blank' || value == '_blank'
- return ["(#{column} IS NULL OR #{column} = '')"]
+ return { column => [nil, ''] }
elsif operator == '_present' || value == '_present'
- return ["(#{column} IS NOT NULL AND #{column} != '')"]
+ return { column => {'$ne' => nil}, column => {'$ne' => ''} }
elsif operator == '_null' || value == '_null'
- return ["(#{column} IS NULL)"]
+ return { column => nil }
elsif operator == '_not_null' || value == '_not_null'
- return ["(#{column} IS NOT NULL)"]
+ return { column => {'$ne' => nil} }
elsif operator == '_empty' || value == '_empty'
- return ["(#{column} = '')"]
+ return { column => '' }
elsif operator == '_not_empty' || value == '_not_empty'
- return ["(#{column} != '')"]
+ return { column => {'$ne' => ''} }
end
# now we go type specific
case type
when :boolean
- return ["(#{column} IS NULL OR #{column} = ?)", false] if ['false', 'f', '0'].include?(value)
- return ["(#{column} = ?)", true] if ['true', 't', '1'].include?(value)
+ return { column => false } if ['false', 'f', '0'].include?(value)
+ return { column => true } if ['true', 't', '1'].include?(value)
when :integer, :belongs_to_association
return if value.blank?
- ["(#{column} = ?)", value.to_i] if value.to_i.to_s == value
+ { column => value.to_i } if value.to_i.to_s == value
when :string, :text
-=end
return if value.blank?
value = case operator
when 'default', 'like'
@@ -254,7 +241,6 @@ def build_statement(column, type, value, operator)
value.to_s
end
{ column => value }
-=begin
when :datetime, :timestamp, :date
return unless operator != 'default'
values = case operator
@@ -276,12 +262,13 @@ def build_statement(column, type, value, operator)
return if (value.blank? || value.match(/([0-9]{8})/).nil?)
[Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').beginning_of_day, Date.strptime(value.match(/([0-9]{8})/)[1], '%m%d%Y').end_of_day]
end
- ["(#{column} BETWEEN ? AND ?)", *values]
+ { column => { '$gte' => values[0], '$lte' => values[1] } }
when :enum
return if value.blank?
- ["(#{column} IN (?))", [value].flatten]
+ { column => [value].flatten }
+ else
+ { column => value }
end
-=end
end
private
@@ -299,7 +286,7 @@ def association_parent_model_lookup(association)
def association_foreign_type_lookup(association)
if association.polymorphic?
- association.foreign_type.try(:to_sym) || :"#{association.name}_type"
+ association.type.try(:to_sym) || :"#{association.name}_type"
end
end
@@ -308,7 +295,7 @@ def association_as_lookup(association)
end
def association_polymorphic_lookup(association)
- association.polymorphic? && association.polymorphic
+ association.polymorphic?
end
def association_parent_key_lookup(association)
@@ -10,17 +10,15 @@ def self.rails_admin(&block)
end
end
- module InstanceMethods
- def rails_admin_default_object_label_method
- self.new_record? ? "new #{self.class.to_s}" : "#{self.class.to_s} ##{self.id}"
- end
+ def rails_admin_default_object_label_method
+ self.new_record? ? "new #{self.class.to_s}" : "#{self.class.to_s} ##{self.id}"
+ end
- def safe_send(value)
- if self.attributes.find{ |k,v| k.to_s == value.to_s }
- self.read_attribute(value)
- else
- self.send(value)
- end
+ def safe_send(value)
+ if self.attributes.find{ |k,v| k.to_s == value.to_s }
+ self.read_attribute(value)
+ else
+ self.send(value)
end
end
end

0 comments on commit a22ff21

Please sign in to comment.