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

#columnsObject (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



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

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_headerObject



74
75
76
# File 'app/components/express_admin/smart_table.rb', line 74

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

#cell_value(item, accessor) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/components/express_admin/smart_table.rb', line 112

def cell_value(item, accessor)
  value = if accessor.respond_to?(:call)
            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
            helpers.link_to item.send(attrib), resource_path(item)
          elsif attrib = accessor.to_s.match(/(\w+)_in_words/).try(:[], 1)
            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

            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)


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

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

#display_columnsObject



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

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

#hidden_column_cellObject



170
171
172
# File 'app/components/express_admin/smart_table.rb', line 170

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

#hidden_columns_header_indicatorObject



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

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

#row_class(item) ⇒ Object



100
101
102
103
104
105
106
# File 'app/components/express_admin/smart_table.rb', line 100

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



108
109
110
# File 'app/components/express_admin/smart_table.rb', line 108

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

#scroll_tableObject



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

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

#should_show_actions?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/components/express_admin/smart_table.rb', line 78

def should_show_actions?
  !!config[:show_actions]
end

#should_show_delete?(item) ⇒ Boolean

Returns:

  • (Boolean)


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

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_columnsObject



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

def specified_columns
  config[:columns]
end

#specified_columns?Boolean

Returns:

  • (Boolean)


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

def specified_columns?
  !!specified_columns
end