Sha256: 8c9161bdb45890d9b82ee222240a94f54e41a94817a42688839a1fe9a7e254ab

Contents?: true

Size: 1.84 KB

Versions: 5

Compression:

Stored size: 1.84 KB

Contents

module Wice

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

    # returns the main ActiveRecord model class
    attr_reader :default_model_class
    # 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

    # 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
    alias_method :get, :[]
    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.map(&:dup).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

5 entries across 5 versions & 1 rubygems

Version Path
wice_grid-7.1.4 lib/wice/table_column_matrix.rb
wice_grid-7.1.3 lib/wice/table_column_matrix.rb
wice_grid-7.1.2 lib/wice/table_column_matrix.rb
wice_grid-7.1.1 lib/wice/table_column_matrix.rb
wice_grid-7.1.0 lib/wice/table_column_matrix.rb