Sha256: d86438ed900620182c48b8ebfea3283731b7747670358b82da6a66d7f0879b61

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

module Clevic
  class FilterCommand
    # TODO seems like either filter_indexes, filter_conditions can be
    # passed. Better docs needed, and better api.
    def initialize( table_view, filter_indexes, filter_conditions )
      @table_view = table_view
      @filter_conditions = filter_conditions
      @filter_indexes = filter_indexes
      
      # Better make the status message now, before the indexes become invalid
      @status_message =
      if filter_indexes.empty?
        # no indexes, so use filter_conditions.
        "Filtered on #{filter_conditions.inspect}"
      else
        "Filtered on #{filter_indexes.first.field.label} = #{filter_indexes.first.display_value}"
      end
    end
    
    # Do the filtering. Return true if successful, false otherwise.
    def doit
      begin
        # store current AR conditions
        @stored_conditions = @table_view.model.cache_table.find_options
        
        # store auto_new
        @auto_new = @table_view.model.auto_new
        
        # reload cache table with new conditions
        @table_view.model.auto_new = false
        @table_view.model.reload_data( @filter_conditions )
      rescue Exception => e
        puts
        puts e.message
        puts e.backtrace
        false
      end
      true
    end
    
    def undo
      # restore auto_new
      @table_view.model.auto_new = @auto_new
      
      # reload cache table with stored AR conditions
      @table_view.model.reload_data( @stored_conditions )
    end
    
    # return a message based on the conditions
    def status_message
      @status_message
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
clevic-0.13.0.b3 lib/clevic/filter_command.rb