Sha256: 51943554dddd7cbde1a9f265c1215fa0fe8437f5254f1b66af46d7d9e849b666

Contents?: true

Size: 1.87 KB

Versions: 9

Compression:

Stored size: 1.87 KB

Contents

# encoding: utf-8
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?
        fail 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

9 entries across 9 versions & 2 rubygems

Version Path
wice_grid_ms-3.6.2 lib/wice/table_column_matrix.rb
wice_grid_ms-3.6.1 lib/wice/table_column_matrix.rb
wice_grid_ms-3.6.0 lib/wice/table_column_matrix.rb
wice_grid-3.6.0 lib/wice/table_column_matrix.rb
wice_grid-3.6.0.pre5 lib/wice/table_column_matrix.rb
wice_grid-3.6.0.pre4 lib/wice/table_column_matrix.rb
wice_grid-3.6.0.pre3 lib/wice/table_column_matrix.rb
wice_grid-3.6.0.pre2 lib/wice/table_column_matrix.rb
wice_grid-3.6.0.pre1 lib/wice/table_column_matrix.rb