Module: Redde::IndexHelper

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

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



128
129
130
131
132
133
134
135
136
137
# File 'app/helpers/redde/index_helper.rb', line 128

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



124
125
126
# File 'app/helpers/redde/index_helper.rb', line 124

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

#collectionObject



47
48
49
# File 'app/helpers/redde/index_helper.rb', line 47

def collection
  controller_name
end

#column_namesObject



59
60
61
62
63
64
# File 'app/helpers/redde/index_helper.rb', line 59

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



89
90
91
# File 'app/helpers/redde/index_helper.rb', line 89

def excluded_column_names
  %w(id created_at updated_at)
end

#form_column_namesObject



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

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



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

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



150
151
152
153
# File 'app/helpers/redde/index_helper.rb', line 150

def index_value_for(item, column)
  return model_name::INDEX_COLUMNS[column.to_sym].call(item) if defined?(model_name::INDEX_COLUMNS) && model_name::INDEX_COLUMNS.is_a?(Hash) && model_name::INDEX_COLUMNS[column.to_sym].present?
  item.send(column)
end

#list_table(res_collection, &block) ⇒ Object



14
15
16
17
18
19
20
# File 'app/helpers/redde/index_helper.rb', line 14

def list_table(res_collection, &block)
  render layout: 'admin/redde/list', locals: { res_collection: res_collection } do
    res_collection.each do |item|
      concat list_table_row( item, &block )
    end
  end
end

#list_table_cell(item, column, &block) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/redde/index_helper.rb', line 30

def list_table_cell(item, column, &block)
  case column
  when 'position'
    (:td, "", class: 'list__cell _handle', 'data-sortable-handle' => "")
  when 'visible'
    (:td, link_to('', url_for(id: item, action: :update, record => { visible: !item.visible} ), class: ['list__eye', ('_disactive' if !item.visible)], data: { method: 'put' }), class: 'list__cell _eye')
  when 'title', 'name'
    (:td, link_to(item.send(column), url_for(id: item, action: :edit)), class: 'list__cell')
  else
    if block_given?
      capture(item, column, &block)
    else
       :td, item.send(column), class: 'list__cell'
    end
  end
end

#list_table_optionsObject



93
94
95
96
97
98
99
100
# File 'app/helpers/redde/index_helper.rb', line 93

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

#list_table_row(item, &block) ⇒ Object



22
23
24
25
26
27
28
# File 'app/helpers/redde/index_helper.rb', line 22

def list_table_row(item, &block)
  render layout: 'admin/redde/row', locals: { item: item } do
    index_columns.each do |column|
      concat list_table_cell(item, column, &block)
    end
  end
end

#model_nameObject



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

def model_name
  record.camelize.constantize
end

#recordObject



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

def record
  controller_name.singularize
end

#render_item_column(item, column) ⇒ Object



139
140
141
142
143
144
145
146
147
148
# File 'app/helpers/redde/index_helper.rb', line 139

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



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

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



79
80
81
82
83
84
85
86
87
# File 'app/helpers/redde/index_helper.rb', line 79

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



102
103
104
105
106
107
108
109
# File 'app/helpers/redde/index_helper.rb', line 102

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