Class: ExpressAdmin::Components::Presenters::SmartTable

Inherits:
ExpressTemplates::Components::Configurable
  • Object
show all
Includes:
Helper, ExpressTemplates::Components::Capabilities::Resourceful
Defined in:
app/components/express_admin/smart_table.rb

Defined Under Namespace

Classes: Column

Constant Summary collapse

MAX_COLS_TO_SHOW_IDX =
5
MAX_ROWS_TO_SHOW_IDX =
10

Constants included from Helper

Helper::COLUMN_REGEX_LIST

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Helper

#accessor_match, #checkmark, #checkmark?, #link, #link?, #make_checkmark, #make_in_words, #make_link, #words, #words?

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



13
14
15
# File 'app/components/express_admin/smart_table.rb', line 13

def columns
  @columns
end

Instance Method Details

#actions_column(item) ⇒ Object



166
167
168
169
170
171
172
# File 'app/components/express_admin/smart_table.rb', line 166

def actions_column(item)
  td {
    if should_show_delete?(item)
      link_to 'Delete', resource_path(item), method: :delete, data: {confirm: 'Are you sure?'}, class: 'button smart-table-button small delete'
    end
  }
end

#actions_headerObject



148
149
150
# File 'app/components/express_admin/smart_table.rb', line 148

def actions_header
  th(class: 'actions') { 'Actions' }
end

#cell_value(item, accessor) ⇒ Object



186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/components/express_admin/smart_table.rb', line 186

def cell_value(item, accessor)
  value =
    if accessor.respond_to?(:call)
      begin
        accessor.call(item).to_s.html_safe
      rescue
        'Error: '+$!.to_s
      end
    elsif link?(accessor)
      make_link(item, accessor)
    elsif checkmark?(accessor)
      make_checkmark(item, accessor)
    elsif words?(accessor)
      make_in_words(item, accessor)
    else
      if relation_name = accessor.to_s.match(/(.*)_id$/).try(:[], 1)
        reflection = resource_class.reflect_on_association(relation_name.to_sym)
      end

      if reflection
        relation = item.send(relation_name)
        relation.try(:name) || relation.to_s
      else
        item.send(accessor)
      end
    end
  current_arbre_element.add_child value
end

#collectionObject



133
134
135
136
# File 'app/components/express_admin/smart_table.rb', line 133

def collection
  collections = super.kind_of?(Array) ? Kaminari.paginate_array(super) : super
  collections.page(index_page).per(specified_rows)
end

#columns_hidden?Boolean

Returns:

  • (Boolean)


219
220
221
# File 'app/components/express_admin/smart_table.rb', line 219

def columns_hidden?
  !specified_columns? && columns.size > MAX_COLS_TO_SHOW_IDX+1
end

#display_columnsObject



215
216
217
# File 'app/components/express_admin/smart_table.rb', line 215

def display_columns
  specified_columns? ? @columns : @columns.slice(1..MAX_COLS_TO_SHOW_IDX)
end

#hidden_column_cellObject



256
257
258
# File 'app/components/express_admin/smart_table.rb', line 256

def hidden_column_cell
  td(class: 'more-columns-indicator')
end

#hidden_columns_header_indicatorObject



250
251
252
253
254
# File 'app/components/express_admin/smart_table.rb', line 250

def hidden_columns_header_indicator
  th(class: 'more-columns-indicator') {
    "..."
  }
end

#index_pageObject



138
139
140
# File 'app/components/express_admin/smart_table.rb', line 138

def index_page
  helpers.params[:page] || 1 # Default page is 1
end

#paginationObject



121
122
123
# File 'app/components/express_admin/smart_table.rb', line 121

def pagination
  paginate collection, :route_set => route_set
end

#route_setObject



129
130
131
# File 'app/components/express_admin/smart_table.rb', line 129

def route_set
  namespace.nil? ? namespace : eval(namespace)
end

#row_class(item) ⇒ Object



174
175
176
177
178
179
180
# File 'app/components/express_admin/smart_table.rb', line 174

def row_class(item)
  if config[:row_class].try(:respond_to?, :call)
    config[:row_class].call(item)
  else
    item.eql?(resource) ? 'current' : ''
  end
end

#row_id(item) ⇒ Object



182
183
184
# File 'app/components/express_admin/smart_table.rb', line 182

def row_id(item)
  "#{collection_member_name}:#{item.to_param}"
end

#scroll_tableObject



142
143
144
145
146
# File 'app/components/express_admin/smart_table.rb', line 142

def scroll_table
  script {
    %Q($('\##{config[:id]}').scrollTableBody())
  }
end

#should_show_actions?Boolean

Returns:

  • (Boolean)


152
153
154
# File 'app/components/express_admin/smart_table.rb', line 152

def should_show_actions?
  !!config[:show_actions]
end

#should_show_delete?(item) ⇒ Boolean

Returns:

  • (Boolean)


156
157
158
159
160
161
162
163
164
# File 'app/components/express_admin/smart_table.rb', line 156

def should_show_delete?(item)
  if item.respond_to?(:can_delete?) && item.can_delete?
    true
  elsif !item.respond_to?(:can_delete?)
    true
  else
    false
  end
end


112
113
114
115
116
117
118
119
# File 'app/components/express_admin/smart_table.rb', line 112

def sort_link(column)
  uri = URI.parse(config[:id].to_s)
  table_column = column.table_column
  param_asc = helpers.params[:asc]
  sort_direction = param_asc.eql?(table_column) ? :desc : :asc
  uri.query = { sort_direction => table_column }.to_param
  uri.to_s
end

#sortable(column) ⇒ Object



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
# File 'app/components/express_admin/smart_table.rb', line 96

def sortable(column)
  table_column = column.table_column
  if config[:sortable].include?(column.title) && table_column.present?
    a(href: sort_link(column)) {
      text_node column.title
      if helpers.params[:asc].eql?(table_column)
        i(class: 'icon ion-ios-arrow-up')
      else
        i(class: 'icon ion-ios-arrow-down')
      end
    }
  else
    column.title
  end
end

#specified_columnsObject



246
247
248
# File 'app/components/express_admin/smart_table.rb', line 246

def specified_columns
  config[:columns]
end

#specified_columns?Boolean

Returns:

  • (Boolean)


242
243
244
# File 'app/components/express_admin/smart_table.rb', line 242

def specified_columns?
  !!specified_columns
end

#specified_rowsObject



260
261
262
# File 'app/components/express_admin/smart_table.rb', line 260

def specified_rows
  config[:rows] || MAX_ROWS_TO_SHOW_IDX
end

#table_classesObject



125
126
127
# File 'app/components/express_admin/smart_table.rb', line 125

def table_classes
  'table striped'
end