Skip to content

Commit 3311c75

Browse files
author
Francesc Esplugas
committed
Refactored typus_table_header.
1 parent 5aa9d7d commit 3311c75

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

app/helpers/admin/table_helper.rb

Lines changed: 26 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -105,37 +105,34 @@ def build_typus_table(model, fields, items, link_options = {}, association = nil
105105

106106
end
107107

108-
# OPTIMIZE: Move html code to partial.
109108
def typus_table_header(model, fields)
110-
returning(String.new) do |html|
111-
headers = []
112-
fields.each do |key, value|
113-
114-
content = key.end_with?('_id') ? key : model.human_attribute_name(key)
115-
116-
if (model.model_fields.map(&:first).collect { |i| i.to_s }.include?(key) || model.reflect_on_all_associations(:belongs_to).map(&:name).include?(key.to_sym)) && params[:action] == 'index'
117-
sort_order = case params[:sort_order]
118-
when 'asc' then ['desc', '↓']
119-
when 'desc' then ['asc', '↑']
120-
else
121-
[nil, nil]
122-
end
123-
order_by = model.reflect_on_association(key.to_sym).primary_key_name rescue key
124-
switch = sort_order.last if params[:order_by].eql?(order_by)
125-
options = { :order_by => order_by, :sort_order => sort_order.first }
126-
content = link_to "#{content} #{switch}", params.merge(options)
127-
end
128109

129-
headers << "<th>#{content}</th>"
110+
headers = fields.map do |key, value|
111+
112+
content = key.end_with?('_id') ? key : model.human_attribute_name(key)
113+
114+
if (model.model_fields.map(&:first).collect { |i| i.to_s }.include?(key) || model.reflect_on_all_associations(:belongs_to).map(&:name).include?(key.to_sym)) && params[:action] == 'index'
115+
sort_order = case params[:sort_order]
116+
when 'asc' then ['desc', '&darr;']
117+
when 'desc' then ['asc', '&uarr;']
118+
else
119+
[nil, nil]
120+
end
121+
order_by = model.reflect_on_association(key.to_sym).primary_key_name rescue key
122+
switch = sort_order.last if params[:order_by].eql?(order_by)
123+
options = { :order_by => order_by, :sort_order => sort_order.first }
124+
content = link_to "#{content} #{switch}", params.merge(options)
125+
end
126+
127+
content
128+
129+
end
130+
131+
headers << "&nbsp;" if @current_user.can?('delete', model)
132+
133+
render "admin/helpers/table_header",
134+
:headers => headers
130135

131-
end
132-
headers << "<th>&nbsp;</th>" if @current_user.can?('delete', model)
133-
html << <<-HTML
134-
<tr>
135-
#{headers.join("\n")}
136-
</tr>
137-
HTML
138-
end
139136
end
140137

141138
# OPTIMIZE: Refactor (Remove rescue)
@@ -255,4 +252,4 @@ def typus_table_boolean_field(attribute, item)
255252

256253
end
257254

258-
end
255+
end
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<tr>
2+
<% headers.each do |header| %>
3+
<th><%= header %></th>
4+
<% end %>
5+
</tr>

0 commit comments

Comments
 (0)