88
89
90
91
92
93
94
95
96
97
98
99
100
101
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
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
180
181
182
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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
# File 'lib/gridion/grid_helper.rb', line 88
def initialize_grid_binding(grid_binding)
with_output_buffer do
grid_binding. do |klass, collection, options={}|
table_tag = options[:table_tag]||"table"
table_row_tag=options[:table_row_tag]||"tr"
=options[:table_header_tag]||"th"
=options[:table_header_wrapper_tag]||"thead"
table_body_wrapper_tag=options[:table_body_wrapper_tag]||"tbody"
skip_field_class=options[:skip_field_class]||false
field_class_prefix=options[:field_class_prefix]||""
table_classes=[options[:class]].flatten.compact
safe_concat("<#{table_tag} class=\"#{klass.name.downcase} #{table_classes.join(' ')}\">")
columns=options[:columns]||klass.column_names
safe_concat("<#{}>") if .present?
safe_concat("<#{table_row_tag} class=\"header\">")
(columns).each do |col|
col_label=klass.human_attribute_name(col)
col_label = sort_link(options[:q], col, col_label) if defined?(:sort_link) && options.has_key?(:q) && options[:q].present?
safe_concat("<#{} class=\"header_cell #{field_class_prefix + '_' + col.to_s.parameterize.underscore unless skip_field_class}\">#{col_label}</#{}>")
end
safe_concat("<#{} class=\"actions\">#{I18n.t("gridion.headers.actions", "Actions")}</#{}>") unless options[:actions].blank?
safe_concat("<#{} class=\"children\"></#{}>") if options.has_key?(:children)
safe_concat("</#{table_row_tag}>")
safe_concat("</#{}>") if .present?
safe_concat("<#{table_body_wrapper_tag}>") if table_body_wrapper_tag.present?
end
grid_binding.row do |klass, object, options={}|
table_tag = options[:table_tag]||"table"
table_row_tag=options[:table_row_tag]||"tr"
table_cell_tag=options[:table_cell_tag]||"td"
skip_field_class=options[:skip_field_class]||false
field_class_prefix=options[:field_class_prefix]||""
object_list=
if options.has_key?(:parent)
([options[:parent]] + [object]).flatten
else
[object]
end
if options.has_key?(:namespace)
namespaces=options[:namespace]
object_list = ([namespaces] + object_list).flatten
end
result =""
aux_columns={}
aux_columns=options[:aux_columns].with_indifferent_access if options.has_key?(:aux_columns)
formats=(options[:formats]||{}).with_indifferent_access
actions=options[:actions]
columns=options[:columns]||klass.column_names
row_id="#{klass.name}_#{object.id}"
result << "<#{table_row_tag} id=\"#{row_id}\" data-id=\"#{object.id}\" class=\"#{options[:row_is_even] ? 'even' : 'odd'}\">"
(columns).each do |col|
if aux_columns[col].present?
value=aux_columns[col].call(object, options)
else
value=object.send(col)
if formats.has_key?(col)
if formats[col]==:currency
value=number_to_currency(value)
elsif formats[col].kind_of?(Hash)
if formats[col].has_key?(:date)
value=value.try(:to_date).try(:to_s, formats[col][:date])
elsif formats[col].has_key?(:datetime)
value=value.try(:to_s, formats[col][:datetime])
end
elsif formats[col].kind_of?(Proc)
value=formats[col].call(value)
end
end
end
result << "<#{table_cell_tag} #{"class=\"#{field_class_prefix + '_' + col}\"" unless skip_field_class}>#{value}</#{table_cell_tag}>"
end
if actions.present?
result << "<#{table_cell_tag} class=\"actions\">"
if actions.kind_of?(Proc)
result << actions.call(object, options)
elsif actions.kind_of?(Array)
result << "#{link_to I18n.t("gridion.actions.show", default: "Show"), object_list, :class=>%w{action_link show}}" if actions.include?(:show)
result << "#{link_to I18n.t("gridion.actions.edit", default: "Edit"), [:edit]+ object_list, :class=>%w{action_link edit}}" if actions.include?(:edit)
result << "#{link_to I18n.t("gridion.actions.delete", default: "Delete"), object_list, :class=>%w{action_link delete}, :method=>:delete, :confirm=>'Are you sure?'}" if actions.include?(:delete)
elsif actions.kind_of?(Hash)
result << "#{link_to I18n.t("gridion.actions.show", default: "Show"), object_list, {:class=>%w{action_link show}}.merge(actions[:show]||{})}" if actions.has_key?(:show)
result << "#{link_to I18n.t("gridion.actions.edit", default: "Edit"), [:edit]+ object_list, {:class=>%w{action_link edit}}.merge(actions[:edit]||{})}" if actions.include?(:edit)
result << "#{link_to I18n.t("gridion.actions.delete", default: "Delete"), object_list, {:class=>%w{action_link delete}, :method=>:delete, :confirm=>'Are you sure?'}.merge(actions[:delete]||{})}" if actions.include?(:delete)
end
result << "</#{table_cell_tag}>"
end
if options.has_key?(:children)
result << "<#{table_cell_tag} class=\"children\">"
children_hash=
if options[:children].kind_of?(Array)
options[:children].each_with_object({}) {|child, h| h[child]=child.to_s.singularize.classify.constantize.model_name.human.pluralize }
else
options[:children].keys.each_with_object({}) {|child, h| h[child]=options[:children][child]||child.to_s.singularize.classify.constantize.model_name.human.pluralize}
end
children_hash.keys.each do |child|
label= children_hash[child]
label||= child.to_s.singularize.classify.constantize.model_name.human.pluralize
result << link_to(label, [namespaces, object, child].flatten, :class=>"child_link #{child.to_s}")
end
result << "</#{table_cell_tag}>"
end
result << "</#{table_row_tag}>"
safe_concat(result)
end
grid_binding.paginator do |klass, collection, options|
table_tag = options[:table_tag]
table_row_tag=options[:table_row_tag]
table_cell_tag=options[:table_cell_tag]
paginate_param_name=options[:paginate_param_name]||"page"
colspans=(options[:columns]||klass.column_names).count + 1 + (options[:actions].blank? ? 0 : 1) result = ""
result << "<#{table_row_tag} class=\"paginator\">"
result << "<#{table_cell_tag} colspan=\"#{colspans}\">"
result << paginate(collection, param_name: paginate_param_name)
result << "</#{table_cell_tag}>"
result << "</#{table_row_tag}>"
safe_concat(result)
end
grid_binding. do |klass, collection, options={}|
table_tag = options[:table_tag]
table_row_tag=options[:table_row_tag]
table_cell_tag=options[:table_cell_tag]
table_body_wrapper_tag=options[:table_body_wrapper_tag]
safe_concat("</#{table_body_wrapper_tag}>") if table_body_wrapper_tag.present?
safe_concat("</#{table_tag}>")
end
end
end
|