lib/netzke/basepack/search_panel.rb in netzke-basepack-0.6.3 vs lib/netzke/basepack/search_panel.rb in netzke-basepack-0.6.4
- old
+ new
@@ -1,59 +1,76 @@
module Netzke
module Basepack
- class SearchPanel < FormPanel
+ # == Configuration
+ # +load_last_preset+ - on load, tries to load the latest saved preset
+ class SearchPanel < Base
- js_properties :header => false,
- :bbar => false
+ js_base_class "Ext.form.FormPanel"
- # An override
- def normalize_field(f)
- f = if f.is_a?(Symbol) || f.is_a?(String)
- {:name => f.to_s, :operator => default_operator}
- else
- search_condition = f[:name]
- if search_condition.is_a?(MetaWhere::Column)
- {:name => search_condition.column, :operator => search_condition.method}
- else
- {:name => search_condition.to_s}
- end
- end
+ js_properties(
+ :padding => 5,
+ :auto_scroll => true
+ )
- f = super(f)
+ js_include :condition_field
- f[:disabled] = primary_key_attr?(f)
+ js_mixin :search_panel
- # Association field
- if f[:name].to_s.index("__")
- f[:xtype] ||= xtype_for_attr_type(:string)
- f[:attr_type] = :string
+ js_property :attribute_operators_map, {
+ :integer => [
+ ["eq", I18n.t('netzke.basepack.search_panel.equals')],
+ ["gt", I18n.t('netzke.basepack.search_panel.greater_than')],
+ ["lt", I18n.t('netzke.basepack.search_panel.less_than')]
+ ],
+ :text => [
+ ["contains", I18n.t('netzke.basepack.search_panel.contains')] # same as matches => %string%
+ ],
+ :string => [
+ ["contains", I18n.t('netzke.basepack.search_panel.contains')], # same as matches => %string%
+ ["matches", I18n.t('netzke.basepack.search_panel.matches')]
+ ],
+ :boolean => [
+ ["is_any", I18n.t('netzke.basepack.search_panel.is_true')],
+ ["is_true", I18n.t('netzke.basepack.search_panel.is_true')],
+ ["is_false", I18n.t('netzke.basepack.search_panel.is_false')]
+ ],
+ :datetime => [
+ ["eq", I18n.t('netzke.basepack.search_panel.date_equals')],
+ ["gt", I18n.t('netzke.basepack.search_panel.after')],
+ ["lt", I18n.t('netzke.basepack.search_panel.before')]
+ ]
+ }
+
+ # Builds default query search panel, where each field is presented
+ def default_query
+ data_class.column_names.map do |c|
+ column_type = data_class.columns_hash[c].type
+ operator = (self.class.js_property(:attribute_operators_map)[column_type] || []).first.try(:fetch, 0) || "matches"
+ {:attr => c, :attr_type => column_type, :operator => operator}
end
+ end
- f[:operator] ||= "gt" if [:datetime, :integer, :date].include?(f[:attr_type])
- f[:operator] ||= "eq" if f[:attr_type] == :boolean
- f[:operator] ||= default_operator
+ def data_class
+ @data_class ||= config[:model].constantize
+ end
- f[:field_label] = [f[:field_label], f[:operator]].join(" ")
- f.merge(:name => [f[:name], f[:operator]].join("__"))
+ def js_config
+ super.merge(
+ :attrs => attributes,
+ :attrs_hash => data_class.column_names.inject({}){ |hsh,c| hsh.merge(c => data_class.columns_hash[c].type) },
+ :query => (config[:load_last_preset] ? last_preset.try(:fetch, "query") : config[:query]) || []
+ )
end
- private
- def default_operator
- "like"
+ def attributes
+ data_class.column_names.map do |name|
+ [name, data_class.human_attribute_name(name)]
end
+ end
- # we need to correct the queries to cut off the condition suffixes, otherwise the FormPanel gets confused
- def get_combobox_options(params)
- column_name = params[:column]
- CONDITIONS.each { |c| column_name.sub!(/_#{c}$/, "") }
- super(:column => column_name)
- end
+ def last_preset
+ (state[:presets] || []).last
+ end
- def attr_type_to_xtype_map
- super.merge({
- :boolean => :tricheckbox
- })
- end
-
end
end
-end
\ No newline at end of file
+end