Class: ActiveExt::Core

Inherits:
Object
  • Object
show all
Defined in:
lib/active_ext/core.rb

Constant Summary collapse

RAILS_COLUMNS =

rails generated columns. Do not remove these

[:id, :created_at, :updated_at]
BETTER_NESTED_SET_COLUMNS =
[:parent_id, :lft, :rgt]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#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

Instance Attribute Details

#association_namesObject

Returns the value of attribute association_names.



5
6
7
# File 'lib/active_ext/core.rb', line 5

def association_names
  @association_names
end

#columnsObject

Returns the value of attribute columns.



4
5
6
# File 'lib/active_ext/core.rb', line 4

def columns
  @columns
end

#model_idObject (readonly)

Returns the value of attribute model_id.



3
4
5
# File 'lib/active_ext/core.rb', line 3

def model_id
  @model_id
end

Instance Method Details

#modelObject



55
56
57
# File 'lib/active_ext/core.rb', line 55

def model
  @model ||= @model_id.to_s.camelize.constantize
end

#non_excluded_columnsObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/active_ext/core.rb', line 63

def non_excluded_columns
  columns = []

  #add id column if showing it
  if self.options[:show_id]
    columns << core.columns[:id]
  end

  #build ext columns
  self.columns.each do |column|
    next if column.name.to_s =~ /(^id|created_at|updated_at)$/ || self.columns.exclude_column?(column.name)
    columns << column
  end

  #add timestamp columns if showing them
  if self.options[:show_timestamps]
    columns << self.columns[:created_at]
    columns << self.columns[:updated_at]
  end

  columns
end

#optionsObject



59
60
61
# File 'lib/active_ext/core.rb', line 59

def options
  @options
end