class <%= model_name %>Query < Query self.queried_class = <%= model_name %> def initialize_available_filters <%- db_columns.each do |column_name, column_options| -%> <%- if project? && column_name == 'project_id' -%> if project.nil? add_available_filter '<%= column_name %>', name: <%= model_name %>.human_attribute_name(:<%= column_name %>), type: :<%= column_options[:query_type] %>, values: all_projects_values end <%- else -%> add_available_filter '<%= column_name %>', name: <%= model_name %>.human_attribute_name(:<%= column_name %>), type: :<%= column_options[:query_type] %> <%- end -%> <%- end -%> <%- if acts_as_customizable? -%> add_custom_fields_filters(<%= model_name %>CustomField) <%- end -%> end def available_columns return @available_columns if @available_columns @available_columns = [] group = l("label_filter_group_#{self.class.name.underscore}") <%- db_columns.each do |column_name, column_options| -%> @available_columns << QueryColumn.new(:<%= column_options[:query_column_name] || column_name %>, caption: <%= model_name %>.human_attribute_name(:<%= column_name %>), title: <%= model_name %>.human_attribute_name(:<%= column_name %>), group: group) <%- end -%> <%- if acts_as_customizable? -%> @available_columns += <%= model_name %>CustomField.visible.collect { |cf| QueryCustomFieldColumn.new(cf) } <%- end -%> @available_columns end def initialize(attributes=nil, *args) super attributes self.filters ||= { "<%= db_columns.first[0] %>" => {:operator => "*", :values => []} } end def default_columns_names super.presence || <%= db_columns.collect{|column_name, column_options| (column_options[:query_column_name] || column_name).to_s}[0..3].to_s %>.flat_map{|c| [c.to_s, c.to_sym]} end def <%= model_name_pluralize_underscored %>(options={}) order_option = [group_by_sort_order, (options[:order] || sort_clause)].flatten.reject(&:blank?) scope = <%= model_name %>.visible. where(statement). includes(((options[:include] || [])).uniq). where(options[:conditions]). order(order_option). joins(joins_for_order_statement(order_option.join(','))). limit(options[:limit]). offset(options[:offset]) if has_custom_field_column? scope = scope.preload(:custom_values) end <%= model_name_pluralize_underscored %> = scope.to_a <%= model_name_pluralize_underscored %> rescue ::ActiveRecord::StatementInvalid => e raise StatementInvalid.new(e.message) end end