Module: AdminData::ApplicationHelper
- Defined in:
- app/helpers/admin_data/application_helper.rb
Instance Method Summary collapse
- #belongs_to_data(model, klass) ⇒ Object
- #breadcrum(&block) ⇒ Object
- #build_text_field(html, f, col) ⇒ Object
- #column_native(klass, column) ⇒ Object
- #column_title(klass, column) ⇒ Object
-
#columns_order(klass) ⇒ Object
AdminData.config.columns_order might not list all the columns of a given table.
- #form_field(klass, model, col, f) ⇒ Object
- #form_field_for_association_records(klass, col, f, html) ⇒ Object
- #form_field_for_habtm_records(klass, model, f, html) ⇒ Object
- #get_reflection_for_column(klass, col) ⇒ Object
- #get_sort_class(column) ⇒ Object
- #get_sort_order(column) ⇒ Object
- #get_sort_title_with_url(column, klass) ⇒ Object
-
#get_value_for_column(column, model, options = {}) ⇒ Object
uses truncate method options supports :limit which is applied if the column type is string or text.
- #habtm_data(model, klass) ⇒ Object
- #handle_column_type(col, html, model, column_value, f) ⇒ Object
- #has_many_data(model, klass) ⇒ Object
- #has_one(model, klass) ⇒ Object
- #parent_layout(layout) ⇒ Object
- #record_id(record) ⇒ Object
- #search_result_title(total_num_of_children, records) ⇒ Object
- #total_records_info(klass) ⇒ Object
Instance Method Details
#belongs_to_data(model, klass) ⇒ Object
128 129 130 131 132 133 134 135 136 137 138 139 140 |
# File 'app/helpers/admin_data/application_helper.rb', line 128 def belongs_to_data(model, klass) ActiveRecordUtil.new(klass).declared_belongs_to_association_names.map do |assoc_name| begin output = assoc_name if belongs_to_record = model.send(assoc_name) output = link_to(assoc_name, admin_data_path(:klass => belongs_to_record.class.name.underscore, :id => belongs_to_record.id)) end rescue => e Rails.logger.info Util.exception_info(e) end output end.join(', ') end |
#breadcrum(&block) ⇒ Object
163 164 165 |
# File 'app/helpers/admin_data/application_helper.rb', line 163 def breadcrum(&block) render(:partial => '/admin_data/shared/breadcrum', :locals => {:data => capture(&block)}) end |
#build_text_field(html, f, col) ⇒ Object
269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'app/helpers/admin_data/application_helper.rb', line 269 def build_text_field(html, f, col) = {:class => 'nice-field'} if AdminData.config.ignore_column_limit [:size] = 60 [:maxlength] = 255 else [:size] = (col && col.limit && col.limit < 60) ? col.limit : 60 [:maxlength] = col.limit if col.limit end html << f.text_field(col.name, ) html.join end |
#column_native(klass, column) ⇒ Object
86 87 88 |
# File 'app/helpers/admin_data/application_helper.rb', line 86 def column_native(klass, column) klass.send(:columns).select {|r| r.instance_variable_get('@name') == column}.first || column end |
#column_title(klass, column) ⇒ Object
30 31 32 |
# File 'app/helpers/admin_data/application_helper.rb', line 30 def column_title(klass, column) AdminData.config.column_headers[klass.name].try(:fetch,column.intern, nil) || column end |
#columns_order(klass) ⇒ Object
AdminData.config.columns_order might not list all the columns of a given table. However the listed columns should be at the front in the order mentioned. Primary key will also be the first column. Consumer might define a new column name that is not listed a column.
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'app/helpers/admin_data/application_helper.rb', line 38 def columns_order(klass) columns_symbol = klass.columns.map {|e| e.name.intern} # created_at and updated_at should be at the very end by default if columns_symbol.include? :created_at columns_symbol = (columns_symbol - [:created_at]) + [:created_at] end if columns_symbol.include? :updated_at columns_symbol = (columns_symbol - [:updated_at]) + [:updated_at] end if requested_order = AdminData.config.columns_order[klass.name] primary_key = klass.send(:primary_key).intern order = [primary_key] + requested_order order.uniq! # add the columns not covered by user at the end of the list sorted_columns = order + (columns_symbol - order) sorted_columns.map(&:to_s) else columns_symbol.map(&:to_s) end end |
#form_field(klass, model, col, f) ⇒ Object
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'app/helpers/admin_data/application_helper.rb', line 167 def form_field(klass, model, col, f) html = [] column_value = model.send(col.name) if klass.serialized_attributes.has_key?(col.name) return Util.get_serialized_value(html,column_value) end if col.primary html << model.new_record? ? '(auto)' : model.id elsif get_reflection_for_column(klass, col) && AdminData.config.display_assoc?( klass.name ) form_field_for_association_records(klass, col, f, html) else handle_column_type(col, html, model, column_value, f) end end |
#form_field_for_association_records(klass, col, f, html) ⇒ Object
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'app/helpers/admin_data/application_helper.rb', line 185 def form_field_for_association_records(klass, col, f, html) begin reflection = get_reflection_for_column(klass, col) # in some edge cases following code throws exception. investigating .. = reflection. if .keys.include?(:polymorphic) && .fetch(:polymorphic) build_text_field(html, f, col) else ref_klass = reflection.klass association_name = ref_klass.columns.map(&:name).include?('name') ? :name : ref_klass.primary_key all_for_dropdown = ref_klass.all(:order => "#{association_name} asc") html << f.collection_select(col.name, all_for_dropdown, :id, association_name, :include_blank => true) end html.join rescue Exception => e Rails.logger.info Util.exception_info(e) 'could not retrieve' # returning nil end end |
#form_field_for_habtm_records(klass, model, f, html) ⇒ Object
206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 |
# File 'app/helpers/admin_data/application_helper.rb', line 206 def form_field_for_habtm_records(klass, model, f, html) begin html = [] ActiveRecordUtil.new(klass).delcared_habtm_association_names.each do |k| assoc_klass = Util.get_class_name_for_habtm_association(model, k) html << "<div class='col_box'>" html << " <span class='col_name'>#{assoc_klass.table_name}</span>" html << " <span class='col_type'>[integer]</span>" html << "</div>" order_by = assoc_klass.columns.map(&:name).include?('name') ? :name : assoc_klass.primary_key all = assoc_klass.all(:order => order_by) selected = model.send(assoc_klass.table_name).map{|e| e.id} html << f.collection_select(assoc_klass.table_name, all, :id, order_by, {:include_blank => false, :selected => selected}, {:multiple => true, :size => (all.count > 10 ? 8 : 4)}) end html.join rescue Exception => e Rails.logger.info Util.exception_info(e) 'could not retrieve' # returning nil end end |
#get_reflection_for_column(klass, col) ⇒ Object
308 309 310 |
# File 'app/helpers/admin_data/application_helper.rb', line 308 def get_reflection_for_column(klass, col) klass.reflections.values.detect { |reflection| reflection.primary_key_name.to_sym == col.name.to_sym } end |
#get_sort_class(column) ⇒ Object
17 18 19 20 21 22 23 |
# File 'app/helpers/admin_data/application_helper.rb', line 17 def get_sort_class(column) sort_class = 'sortable' if column == @sort_by_column_name sort_class << ' ' + @sort_css end sort_class end |
#get_sort_order(column) ⇒ Object
4 5 6 7 8 9 10 |
# File 'app/helpers/admin_data/application_helper.rb', line 4 def get_sort_order(column) if column == @sort_by_column_name && @sort_order == 'desc' "#{column} asc" else "#{column} desc" end end |
#get_sort_title_with_url(column, klass) ⇒ Object
12 13 14 15 |
# File 'app/helpers/admin_data/application_helper.rb', line 12 def get_sort_title_with_url(column, klass) order = get_sort_order(column) link_to column_title(klass, column), admin_data_search_path(:klass => klass, :query => params[:query], :sortby => order) end |
#get_value_for_column(column, model, options = {}) ⇒ Object
uses truncate method options supports :limit which is applied if the column type is string or text. calls the inspect method to convert to a string if the column is serialized. TODO rspec test limit option
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
# File 'app/helpers/admin_data/application_helper.rb', line 286 def get_value_for_column(column, model, = {}) .reverse_merge!(:limit => 400) value = Util.custom_value_for_column(column, model) if column.is_a?(String) value elsif column.type == :datetime value.strftime('%d-%B-%Y %H:%M:%S %p') unless value.blank? elsif column.type == :string || column.type == :text value = value.inspect if model.class.serialized_attributes.keys.include?(column.name) return value if [:limit].blank? begin truncate(value,:length => [:limit]) rescue # truncate method failed '<actual data is not being shown because truncate method failed.>' end else value.to_s end end |
#habtm_data(model, klass) ⇒ Object
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'app/helpers/admin_data/application_helper.rb', line 142 def habtm_data(model, klass) ActiveRecordUtil.new(klass).declared_habtm_association_names.map do |assoc_name| begin count = model.send(assoc_name.intern).count label = assoc_name + '(' + count.to_s + ')' output = label if count > 0 then has_many_klass_name = ActiveRecordUtil.new(model.class).klass_for_association_type_and_name(:has_and_belongs_to_many, assoc_name).name.underscore output = link_to(label, admin_data_search_path( :klass => has_many_klass_name, :children => assoc_name, :base => klass.name.underscore, :model_id => model.id)) end rescue => e Rails.logger.info Util.exception_info(e) end output end.join(', ') end |
#handle_column_type(col, html, model, column_value, f) ⇒ Object
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'app/helpers/admin_data/application_helper.rb', line 231 def handle_column_type(col, html, model, column_value, f) case col.type when :text html << f.text_area(col.name, :rows => 6, :cols => 70) when :datetime if ['created_at', 'updated_at'].include?(col.name) html << '(auto)' else value = params[:action] == 'new' ? Time.now : column_value year_value = value.year if value datetime_selects = f.datetime_select(col.name, :include_blank => true) html << datetime_selects.gsub('type="hidden"', 'type="text" size="4" class="nice-field"') end when :date value = params[:action] == 'new' ? Time.now : column_value year_value = value.year if value date_selects = f.date_select(col.name, :discard_year => true, :include_blank => true) html << date_selects.gsub('type="hidden"', 'type="text" size="4" class="nice-field"') when :time # time_select method of rails is buggy and is causing problem # 1 error(s) on assignment of multiparameter attributes # # will try again this method with Rails 3 #html << f.time_select(col.name, :include_blank => true, :include_seconds => true) when :boolean html << f.select(col.name, [['True', true], ['False', false]], :include_blank => true) else build_text_field(html, f, col) end html.join end |
#has_many_data(model, klass) ⇒ Object
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'app/helpers/admin_data/application_helper.rb', line 107 def has_many_data(model, klass) array = ActiveRecordUtil.new(klass).declared_has_many_association_names.map do |m| begin count = model.send(m.intern).count label = m.to_s + '(' + count.to_s + ')' output = label if count > 0 has_many_klass_name = ActiveRecordUtil.new(model.class).klass_for_association_type_and_name(:has_many, m).name.underscore output = link_to(label, admin_data_search_path( :klass => has_many_klass_name, :children => m, :base => klass.name.underscore, :model_id => model.id)) end rescue => e Rails.logger.debug Util.exception_info(e) end output end array.join(', ') end |
#has_one(model, klass) ⇒ Object
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'app/helpers/admin_data/application_helper.rb', line 90 def has_one(model, klass) tmp = ActiveRecordUtil.new(klass).declared_has_one_association_names tmp.inject('') do |output, ho| begin label = ho if model.send(ho) output << link_to(label, admin_data_path(:klass => ho.underscore, :id => model.send(ho))) else output << label end rescue => e Rails.logger.debug Util.exception_info(e) end output end end |
#parent_layout(layout) ⇒ Object
25 26 27 28 |
# File 'app/helpers/admin_data/application_helper.rb', line 25 def parent_layout(layout) content_for(:layout, self.output_buffer) self.output_buffer = render(:file => "layouts/#{layout}") end |
#record_id(record) ⇒ Object
312 313 314 315 316 317 318 |
# File 'app/helpers/admin_data/application_helper.rb', line 312 def record_id(record) if record.respond_to?(:primary_key) record.send(:primary_key) else record.id end end |
#search_result_title(total_num_of_children, records) ⇒ Object
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# File 'app/helpers/admin_data/application_helper.rb', line 65 def search_result_title(total_num_of_children, records) output = [] if params[:base] label = params[:base].camelize + ' ID ' + params[:model_id] output << link_to(label, admin_data_path(:klass => params[:base], :id => params[:model_id])) output << 'has' output << pluralize(total_num_of_children, params[:klass]) elsif !params[:query].blank? || params[:adv_search] output << 'Search result:' output << pluralize(records.total_entries, 'record') output << 'found' else output << 'All ' output << params[:klass].camelize output << 'records' end output.join(' ').html_safe end |
#total_records_info(klass) ⇒ Object
61 62 63 |
# File 'app/helpers/admin_data/application_helper.rb', line 61 def total_records_info(klass) '(Total ' + pluralize(klass.count, 'record') + ' )' end |