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

Inherits:
ExpressTemplates::Components::Configurable
  • Object
show all
Includes:
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
COLUMN_REGEX_LIST =
{
  link: /(\w+)_link$/,
  checkmark: /(\w+)_checkmark/,
  in_words: /(\w+)_in_words/
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



17
18
19
# File 'app/components/express_admin/smart_table.rb', line 17

def columns
  @columns
end

Instance Method Details

#actions_column(item) ⇒ Object



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

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 secondary'
    end
  }
end

#actions_headerObject



146
147
148
# File 'app/components/express_admin/smart_table.rb', line 146

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

#cell_value(item, accessor) ⇒ Object



184
185
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
214
215
216
217
218
219
220
221
# File 'app/components/express_admin/smart_table.rb', line 184

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 attrib = accessor.to_s.match(COLUMN_REGEX_LIST[:link]).try(:[], 1)
            # TODO: only works with non-namespaced routes
            if item.send(attrib).present?
              helpers.link_to item.send(attrib), resource_path(item)
            end
          elsif attrib = accessor.to_s.match(COLUMN_REGEX_LIST[:checkmark]).try(:[], 1)
            "<i class='ion-checkmark-round'></i>".html_safe if item.send(attrib)
          elsif attrib = accessor.to_s.match(COLUMN_REGEX_LIST[:in_words]).try(:[], 1)
            if item.send(attrib)
              if item.send(attrib) < DateTime.now
                "#{helpers.time_ago_in_words(item.send(attrib))} ago"
              else
                "in #{helpers.time_ago_in_words(item.send(attrib))}"
              end
            else
              'never'
            end
          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



131
132
133
134
# File 'app/components/express_admin/smart_table.rb', line 131

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)


227
228
229
# File 'app/components/express_admin/smart_table.rb', line 227

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

#display_columnsObject



223
224
225
# File 'app/components/express_admin/smart_table.rb', line 223

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

#hidden_column_cellObject



267
268
269
# File 'app/components/express_admin/smart_table.rb', line 267

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

#hidden_columns_header_indicatorObject



261
262
263
264
265
# File 'app/components/express_admin/smart_table.rb', line 261

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

#index_pageObject



136
137
138
# File 'app/components/express_admin/smart_table.rb', line 136

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

#paginationObject



119
120
121
# File 'app/components/express_admin/smart_table.rb', line 119

def pagination
  paginate collection, :route_set => route_set
end

#route_setObject



127
128
129
# File 'app/components/express_admin/smart_table.rb', line 127

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

#row_class(item) ⇒ Object



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

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



180
181
182
# File 'app/components/express_admin/smart_table.rb', line 180

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

#scroll_tableObject



140
141
142
143
144
# File 'app/components/express_admin/smart_table.rb', line 140

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

#should_show_actions?Boolean

Returns:

  • (Boolean)


150
151
152
# File 'app/components/express_admin/smart_table.rb', line 150

def should_show_actions?
  !!config[:show_actions]
end

#should_show_delete?(item) ⇒ Boolean

Returns:

  • (Boolean)


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

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


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

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



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

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



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

def specified_columns
  config[:columns]
end

#specified_columns?Boolean

Returns:

  • (Boolean)


253
254
255
# File 'app/components/express_admin/smart_table.rb', line 253

def specified_columns?
  !!specified_columns
end

#specified_rowsObject



271
272
273
# File 'app/components/express_admin/smart_table.rb', line 271

def specified_rows
  config[:rows] || MAX_ROWS_TO_SHOW_IDX
end

#table_classesObject



123
124
125
# File 'app/components/express_admin/smart_table.rb', line 123

def table_classes
  'table striped'
end