Class: ExpressAdmin::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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columns ⇒ Object (readonly)

Returns the value of attribute columns.



9
10
11
# File 'app/components/express_admin/smart_table.rb', line 9

def columns
  @columns
end

Instance Method Details

#actions_column(item) ⇒ Object



82
83
84
85
86
87
88
# File 'app/components/express_admin/smart_table.rb', line 82

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



64
65
66
# File 'app/components/express_admin/smart_table.rb', line 64

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

#cell_value(item, accessor) ⇒ Object



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'app/components/express_admin/smart_table.rb', line 102

def cell_value(item, accessor)
  if accessor.respond_to?(:call)
    value = begin
        accessor.call(item).html_safe
      rescue
        'Error: '+$!.to_s
      end
  elsif attrib = accessor.to_s.match(/(\w+)_link$/).try(:[], 1)
    # TODO: only works with non-namespaced routes
    value = helpers.link_to item.send(attrib), resource_path(item)
  elsif attrib = accessor.to_s.match(/(\w+)_in_words/).try(:[], 1)
    value = item.send(attrib) ? (helpers.time_ago_in_words(item.send(attrib))+' ago') : 'never'
  else
    if relation_name = accessor.to_s.match(/(.*)_id$/).try(:[], 1)
      reflection = resource_class.reflect_on_association(relation_name.to_sym)
    end

    value = 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

#columns_hidden? ⇒ Boolean

Returns:

  • (Boolean)


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

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

#display_columns ⇒ Object



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

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

#hidden_column_cell ⇒ Object



160
161
162
# File 'app/components/express_admin/smart_table.rb', line 160

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

#hidden_columns_header_indicator ⇒ Object



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

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

#row_class(item) ⇒ Object



90
91
92
93
94
95
96
# File 'app/components/express_admin/smart_table.rb', line 90

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

#row_id(item) ⇒ Object



98
99
100
# File 'app/components/express_admin/smart_table.rb', line 98

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

#scroll_table ⇒ Object



58
59
60
61
62
# File 'app/components/express_admin/smart_table.rb', line 58

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

#should_show_actions? ⇒ Boolean

Returns:

  • (Boolean)


68
69
70
# File 'app/components/express_admin/smart_table.rb', line 68

def should_show_actions?
  !!config[:show_actions]
end

#should_show_delete?(item) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
76
77
78
79
80
# File 'app/components/express_admin/smart_table.rb', line 72

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

#specified_columns ⇒ Object



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

def specified_columns
  config[:columns]
end

#specified_columns? ⇒ Boolean

Returns:

  • (Boolean)


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

def specified_columns?
  !!specified_columns
end