Class: ModelExplorer::Select

Inherits:
Object
  • Object
show all
Defined in:
lib/model_explorer/select.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, selects) ⇒ Select

Returns a new instance of Select.



5
6
7
8
# File 'lib/model_explorer/select.rb', line 5

def initialize(model, selects)
  @model = model
  @selects = selects
end

Instance Attribute Details

#modelObject (readonly)

Returns the value of attribute model.



3
4
5
# File 'lib/model_explorer/select.rb', line 3

def model
  @model
end

#selectsObject (readonly)

Returns the value of attribute selects.



3
4
5
# File 'lib/model_explorer/select.rb', line 3

def selects
  @selects
end

Instance Method Details

#columnsObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/model_explorer/select.rb', line 10

def columns
  column_names = model.column_names

  return column_names if selects.blank?

  selected_columns = column_names & selects

  if selected_columns.empty?
    ["#{model.table_name}.*"]
  else
    ([model.primary_key] + selected_columns).uniq
  end
end

#to_aObject



24
25
26
# File 'lib/model_explorer/select.rb', line 24

def to_a
  columns.map { |column| "#{model.table_name}.#{column}" }
end