Method: ActiveExt::Core#initialize

Defined in:
lib/active_ext/core.rb

#initialize(model_id, options) ⇒ Core

Returns a new instance of Core.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/active_ext/core.rb', line 11

def initialize(model_id, options)
  @model_id = model_id.to_s.pluralize.singularize
  @options = options

  #get all attribute columns
  attribute_names = self.model.columns.collect { |c| c.name.to_sym }.sort_by { |c| c.to_s }

  #only include attribute columns we care about, do not remove rails generated columns
  if options[:only]
    valid_columns = []
    options[:only].each do |option|
      if option.is_a?(Hash)
        valid_columns << option.keys.first
      else
        valid_columns << option
      end
    end
    valid_columns = valid_columns | RAILS_COLUMNS
    attribute_names.delete_if { |item| !valid_columns.include?(item.to_sym) }
  end

  #if we are including associations get them
  unless options[:ignore_associations]
    @association_names = self.model.reflect_on_all_associations.collect { |a| a.name.to_sym }
    association_column_names = @association_names.sort_by { |c| c.to_s }
    attribute_names += association_column_names
  end

  @columns = ActiveExt::DataStructures::Columns.new(self.model, attribute_names)

  #exclude id columns and count columns and better nested set columns
  @columns.exclude(*@columns.find_all { |c| c.column and (c.column.primary or c.column.name =~ /(_id|_count)$/) }.collect { |c| c.name })
  #exclude nested set columns
  @columns.exclude(*@columns.find_all { |c| c.column and (BETTER_NESTED_SET_COLUMNS.include?(c.column.name.to_sym)) }.collect { |c| c.name })
  #exclude associations columns
  @columns.exclude(*self.model.reflect_on_all_associations.collect { |a| :"#{a.name}_type" if a.options[:polymorphic] }.compact)

  set_column_options
end