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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#columns ⇒ Object (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

Instance Method Details

#actions_column(item) ⇒ Object



124
125
126
127
128
129
130
# File 'app/components/express_admin/smart_table.rb', line 124

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



106
107
108
# File 'app/components/express_admin/smart_table.rb', line 106

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

#cell_value(item, accessor) ⇒ Object



144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
# File 'app/components/express_admin/smart_table.rb', line 144

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(/(\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+)_checkmark/).try(:[], 1)
            "<i class='ion-checkmark-round'></i>".html_safe if item.send(attrib)
          elsif attrib = accessor.to_s.match(/(\w+)_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



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

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)


185
186
187
# File 'app/components/express_admin/smart_table.rb', line 185

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

#display_columns ⇒ Object



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

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

#hidden_column_cell ⇒ Object



212
213
214
# File 'app/components/express_admin/smart_table.rb', line 212

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

#hidden_columns_header_indicator ⇒ Object



206
207
208
209
210
# File 'app/components/express_admin/smart_table.rb', line 206

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

#index_page ⇒ Object



96
97
98
# File 'app/components/express_admin/smart_table.rb', line 96

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

#pagination ⇒ Object



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

def pagination
  paginate collection, :route_set => route_set
end

#route_set ⇒ Object



87
88
89
# File 'app/components/express_admin/smart_table.rb', line 87

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

#row_class(item) ⇒ Object



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

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



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

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

#scroll_table ⇒ Object



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

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

#should_show_actions? ⇒ Boolean

Returns:

  • (Boolean)


110
111
112
# File 'app/components/express_admin/smart_table.rb', line 110

def should_show_actions?
  !!config[:show_actions]
end

#should_show_delete?(item) ⇒ Boolean

Returns:

  • (Boolean)


114
115
116
117
118
119
120
121
122
# File 'app/components/express_admin/smart_table.rb', line 114

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



202
203
204
# File 'app/components/express_admin/smart_table.rb', line 202

def specified_columns
  config[:columns]
end

#specified_columns? ⇒ Boolean

Returns:

  • (Boolean)


198
199
200
# File 'app/components/express_admin/smart_table.rb', line 198

def specified_columns?
  !!specified_columns
end

#specified_rows ⇒ Object



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

def specified_rows
  config[:rows] || MAX_ROWS_TO_SHOW_IDX
end

#table_classes ⇒ Object



83
84
85
# File 'app/components/express_admin/smart_table.rb', line 83

def table_classes
  'table striped'
end