Class: ActiveadminDynamicTable::Configurator

Inherits:
Object
  • Object
show all
Defined in:
lib/activeadmin_dynamic_table/configurator.rb

Instance Method Summary collapse

Constructor Details

#initialize(context, settings) ⇒ Configurator

Returns a new instance of Configurator.



5
6
7
8
9
# File 'lib/activeadmin_dynamic_table/configurator.rb', line 5

def initialize(context, settings)
  @context = context
  @settings = settings
  @api_calls = []
end

Instance Method Details

#applicable_columnsObject



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/activeadmin_dynamic_table/configurator.rb', line 39

def applicable_columns
  applicable_columns = []

  settings.each do |config|
    applicable_column = @api_calls.detect do |register_call|
      register_call.key == config.column
    end

    applicable_columns << applicable_column if applicable_column.present?
  end

  applicable_columns
end

#columnsObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/activeadmin_dynamic_table/configurator.rb', line 27

def columns
  applicable_columns.each do |applicable_column|
    next @context.id_column *applicable_column.args if applicable_column.method == :id_column
    next @context.actions(applicable_column.args[1] || applicable_column.args[0]) if applicable_column.method == :actions
    next @context.index_column *applicable_column.args if applicable_column.method == :index_column

    # next @context.selectable_column *applicable_column.args if applicable_column.method == :selectable_column

    @context.public_send(applicable_column.method, *applicable_column.args, &applicable_column.block)
  end
end

#register_column(method, *args, &block) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/activeadmin_dynamic_table/configurator.rb', line 11

def register_column(method, *args, &block)
  options = args[1] || args[0]

  config = settings_hash[options[:key]] || ColumnSettings.new(options[:key])
  config.default_width = options[:width]

  api_call = RegisteredColumn.new method: method,
                                  key: options[:key],
                                  is_default: options[:default],
                                  args: args,
                                  block: block,
                                  config: config

  @api_calls << api_call
end

#registered_columnsObject



53
54
55
56
57
58
59
60
61
62
63
# File 'lib/activeadmin_dynamic_table/configurator.rb', line 53

def registered_columns
  @api_calls.map do |api_call|
    args = api_call.args

    {
      key: api_call.key,
      selected: selected?(api_call.key),
      args: args,
    }
  end
end

#width_for(key) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/activeadmin_dynamic_table/configurator.rb', line 65

def width_for(key)
  col = settings_hash[key]

  return ColumnSettings.new(nil).width if col.nil?

  col.width
end