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

#columns ⇒ Object (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



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

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

#actions_header ⇒ Object



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

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

#cell_value(item, accessor) ⇒ Object



183
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
# File 'app/components/express_admin/smart_table.rb', line 183

def cell_value(item, accessor)
  value = if accessor.respond_to?(:call)
            begin
              accessor.call(item).try(: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
            helpers.link_to item.send(attrib), resource_path(item)
          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

#collection ⇒ Object



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

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)


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

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

#display_columns ⇒ Object



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

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

#hidden_column_cell ⇒ Object



264
265
266
# File 'app/components/express_admin/smart_table.rb', line 264

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

#hidden_columns_header_indicator ⇒ Object



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

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

#index_page ⇒ Object



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

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

#pagination ⇒ Object



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

def pagination
  paginate collection, :route_set => route_set
end

#route_set ⇒ Object



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

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

#row_class(item) ⇒ Object



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

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



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

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

#scroll_table ⇒ Object



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

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

#should_show_actions? ⇒ Boolean

Returns:

  • (Boolean)


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

def should_show_actions?
  !!config[:show_actions]
end

#should_show_delete?(item) ⇒ Boolean

Returns:

  • (Boolean)


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

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


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

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



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

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_columns ⇒ Object



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

def specified_columns
  config[:columns]
end

#specified_columns? ⇒ Boolean

Returns:

  • (Boolean)


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

def specified_columns?
  !!specified_columns
end

#specified_rows ⇒ Object



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

def specified_rows
  config[:rows] || MAX_ROWS_TO_SHOW_IDX
end

#table_classes ⇒ Object



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

def table_classes
  'table striped'
end