Sha256: f7dc681231dbb5c27b000c666dffd060a7c55ecec597631ba890a88fcba4c7cb

Contents?: true

Size: 1.85 KB

Versions: 4

Compression:

Stored size: 1.85 KB

Contents

module Wice

  # a matrix for all declared columns
  class TableColumnMatrix < Hash #:nodoc:

    # a structure to hold generates Arels for all column filters
    attr_reader :generated_conditions

    # init a matrix of all columns
    def initialize  #:nodoc:
      super
      @generated_conditions = []
      @by_table_names = HashWithIndifferentAccess.new
    end

    # add an Arel for a column
    def add_condition(column, conditions)
      @generated_conditions << [column, conditions] unless conditions.blank?
    end

    # returns a list of all Arels
    def conditions
      @generated_conditions.collect { |_, cond| cond }
    end

    # returns Arels for one model
    alias_method :get, :[]

    # returns the main ActiveRecord model class
    attr_reader :default_model_class

    # sets the main ActiveRecord model class
    def default_model_class=(model)  #:nodoc:
      init_columns_of_table(model) unless key?(model)
      @default_model_class = model
    end

    # returns Arels for one model
    def [](model)  #:nodoc:
      init_columns_of_table(model) unless key?(model)
      get(model)
    end

    def get_column_by_model_class_and_column_name(model_class, column_name)  #:nodoc:
      self[model_class][column_name]
    end

    def get_column_in_default_model_class_by_column_name(column_name)  #:nodoc:
      if @default_model_class.nil?
        raise WiceGridException.new("Cannot search for this column(#{column_name}) in a default model(#{@default_model_class}) as the default model is not set")
      end

      self[@default_model_class][column_name]
    end

    def init_columns_of_table(model) #:nodoc:
      self[model] = HashWithIndifferentAccess.new(model.columns.index_by(&:name))
      @by_table_names[model.table_name] = self[model]
      self[model].each_value { |c| c.model = model }
    end

    alias_method :<<, :init_columns_of_table
  end
end

Version data entries

4 entries across 4 versions & 2 rubygems

Version Path
pwice_grid-5.0.1 lib/wice/table_column_matrix.rb
pwice_grid-5.0.0 lib/wice/table_column_matrix.rb
wice_grid-4.1.0 lib/wice/table_column_matrix.rb
wice_grid-3.6.2 lib/wice/table_column_matrix.rb