Module: Redde::IndexHelper

Included in:
Admin::BaseController, IndexBuilder::IndexCellBuilder
Defined in:
app/helpers/redde/index_helper.rb

Defined Under Namespace

Classes: IndexBuilder

Constant Summary collapse

IGNORED_COLUMNS =
%w(position created_at updated_at id)

Instance Method Summary collapse

Instance Method Details

#ancestry_options(items, symbol = :name, &block) ⇒ Object



111
112
113
114
115
116
117
118
119
120
# File 'app/helpers/redde/index_helper.rb', line 111

def ancestry_options(items, symbol = :name, &block)
  return ancestry_options(items, symbol) { |i| "#{'-' * i.depth} #{i.send(symbol)}" } unless block_given?

  result = []
  items.map do |item, sub_items|
    result << [yield(item), item.id]
    result += ancestry_options(sub_items, symbol) { |i| "#{'---' * i.depth} #{i.send(symbol)}" }
  end
  result
end

#ancestry_tree(obj_class, symbol) ⇒ Object



107
108
109
# File 'app/helpers/redde/index_helper.rb', line 107

def ancestry_tree(obj_class, symbol)
  ancestry_options(obj_class.unscoped.arrange(order: :position), symbol) { |i| "#{'--' * i.depth} #{i.send(symbol)}" }
end

#collectionObject



30
31
32
# File 'app/helpers/redde/index_helper.rb', line 30

def collection
  controller_name
end

#column_namesObject



42
43
44
45
46
47
# File 'app/helpers/redde/index_helper.rb', line 42

def column_names
  model_name
    .column_names
    .reject { |c| excluded_column_names.include?(c) }
    .sort { |a, b| sort_priority(a) <=> sort_priority(b) }
end

#excluded_column_namesObject



72
73
74
# File 'app/helpers/redde/index_helper.rb', line 72

def excluded_column_names
  %w(id created_at updated_at)
end

#form_column_namesObject



55
56
57
58
59
60
# File 'app/helpers/redde/index_helper.rb', line 55

def form_column_names
  return column_names.select { |i| !IGNORED_COLUMNS.include?(i) } unless defined?(model_name::FORM_COLUMNS)
  return model_name::FORM_COLUMNS if defined?(model_name::FORM_COLUMNS)
  res = model_name::INDEX_COLUMNS
  res.keys if model_name::INDEX_COLUMNS.is_a?(Hash)
end

#index_columnsObject



49
50
51
52
53
# File 'app/helpers/redde/index_helper.rb', line 49

def index_columns
  return column_names unless defined?(model_name::INDEX_COLUMNS)
  return model_name::INDEX_COLUMNS if model_name::INDEX_COLUMNS.is_a?(Array)
  model_name::INDEX_COLUMNS.keys
end

#index_value_for(item, column) ⇒ Object



133
134
135
136
137
# File 'app/helpers/redde/index_helper.rb', line 133

def index_value_for(item, column)
  "#{item.model_name}::INDEX_COLUMNS".constantize[column.to_sym].call(item)
rescue
  item.send(column)
end

#list_table(list, options = {}, &block) ⇒ Object

Raises:

  • (ArgumentError)


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'app/helpers/redde/index_helper.rb', line 14

def list_table(list, options = {}, &block)
  raise ArgumentError, "Missing block" unless block_given?
  builder = IndexBuilder.new(list, params)

  html = list.map do |item|
    cb = IndexBuilder::IndexCellBuilder.new(item, builder)
    (:tr, cb.handle + cb.visible + capture(cb, &block) + cb.del + cb.empty, 'data-href' => url_for(id: item, action: :edit), 'data-sortable-item' => "", id: "pos_#{item.id}")
  end.join.html_safe

  hb = IndexBuilder::IndexHeadCellBuilder.new(list)
   :table, class: ['list', options[:class]], style: options[:style], "data-sortable" => { handle: "[data-sortable-handle]" }.to_json do
    concat builder.thead( capture(hb, &block) )
    concat html
  end
end

#list_table_optionsObject



76
77
78
79
80
81
82
83
# File 'app/helpers/redde/index_helper.rb', line 76

def list_table_options
  {}.tap do |options|
    if column_names.include? 'position'
      options['class'] = 'sortable'
      options['data-sortable'] = ""
    end
  end
end

#model_nameObject



38
39
40
# File 'app/helpers/redde/index_helper.rb', line 38

def model_name
  record.camelize.constantize
end

#recordObject



34
35
36
# File 'app/helpers/redde/index_helper.rb', line 34

def record
  controller_name.singularize
end

#render_item_column(item, column) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'app/helpers/redde/index_helper.rb', line 122

def render_item_column(item, column)
  value = index_value_for(item, column).try(:html_safe)
  return 'Не задано' unless value.present?
  case value.class.name
  when 'Time' then l(value, format: '%d %b %Y, %H:%M')
  when 'Date' then l(value, format: '%d %b %Y')
  else
    value
  end
end

#show_tree(c) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
# File 'app/helpers/redde/index_helper.rb', line 94

def show_tree(c)
  link = link_to c.name, [:edit, :admin, c]
  edit = link_to 'Удал', [:admin, c], data: { confirm: 'Точно удалить?' },
                                      method: :delete, class: 'del'
  html = (:div, link + (:p, edit))
  if c.children.any?
    html << (:ol) do
      raw c.children.map { |ch| show_tree(ch) }.join('')
    end
  end
   :li, raw(html), id: "list_#{c.id}"
end

#sort_priority(column_name) ⇒ Object



62
63
64
65
66
67
68
69
70
# File 'app/helpers/redde/index_helper.rb', line 62

def sort_priority(column_name)
  case column_name
  when 'position' then 1
  when 'visible' then 2
  when 'name' then 3
  when 'title' then 3
  else 5
  end
end

#sort_table_options(item) ⇒ Object



85
86
87
88
89
90
91
92
# File 'app/helpers/redde/index_helper.rb', line 85

def sort_table_options(item)
  {}.tap do |options|
    if column_names.include?('position')
      options[:id] = "pos_#{item.id}"
      options['data-sortable-item'] = ""
    end
  end
end

#title_for(item) ⇒ Object



4
5
6
# File 'app/helpers/redde/index_helper.rb', line 4

def title_for(item)
  item.send(title_symbol_for(item))
end

#title_symbol_for(item) ⇒ Object



8
9
10
11
12
# File 'app/helpers/redde/index_helper.rb', line 8

def title_symbol_for(item)
  return 'title' if column_names.include?('title')
  return 'name' if column_names.include?('name')
  model_name.columns.select { |i| i.type == :string }.first
end