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_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
- #search_result_title(total_num_of_children, records) ⇒ Object
- #total_records_info(klass) ⇒ Object
Instance Method Details
#belongs_to_data(model, klass) ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 |
# File 'app/helpers/admin_data/application_helper.rb', line 102 def belongs_to_data(model, klass) AdminData::ActiveRecordUtil.declared_belongs_to_association_names(klass).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 AdminData::Util.exception_info(e) end output end.join(', ') end |
#breadcrum(&block) ⇒ Object
137 138 139 |
# File 'app/helpers/admin_data/application_helper.rb', line 137 def breadcrum(&block) render(:partial => '/admin_data/shared/breadcrum', :locals => {:data => capture(&block)}) end |
#build_text_field(html, f, col) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 |
# File 'app/helpers/admin_data/application_helper.rb', line 243 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
60 61 62 |
# File 'app/helpers/admin_data/application_helper.rb', line 60 def column_native(klass, column) klass.send(:columns).select {|r| r.instance_variable_get('@name') == column}.first || column end |
#column_title(klass, column) ⇒ Object
4 5 6 |
# File 'app/helpers/admin_data/application_helper.rb', line 4 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.
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'app/helpers/admin_data/application_helper.rb', line 12 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
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'app/helpers/admin_data/application_helper.rb', line 141 def form_field(klass, model, col, f) html = [] column_value = model.send(col.name) if klass.serialized_attributes.has_key?(col.name) return AdminData::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.drop_down_for_associations[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
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'app/helpers/admin_data/application_helper.rb', line 159 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 AdminData::Util.exception_info(e) 'could not retrieve' # returning nil end end |
#form_field_for_habtm_records(klass, model, f, html) ⇒ Object
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'app/helpers/admin_data/application_helper.rb', line 180 def form_field_for_habtm_records(klass, model, f, html) begin html = [] AdminData::ActiveRecordUtil.delcared_habtm_association_names(klass).each do |k| assoc_klass = AdminData::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 AdminData::Util.exception_info(e) 'could not retrieve' # returning nil end end |
#get_reflection_for_column(klass, col) ⇒ Object
282 283 284 |
# File 'app/helpers/admin_data/application_helper.rb', line 282 def get_reflection_for_column(klass, col) klass.reflections.values.detect { |reflection| reflection.primary_key_name.to_sym == col.name.to_sym } 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
260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
# File 'app/helpers/admin_data/application_helper.rb', line 260 def get_value_for_column(column, model, = {}) .reverse_merge!(:limit => 400) value = AdminData::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
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
# File 'app/helpers/admin_data/application_helper.rb', line 116 def habtm_data(model, klass) AdminData::ActiveRecordUtil.declared_habtm_association_names(klass).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 = AdminData::ActiveRecordUtil.klass_for_association_type_and_name(model.class, :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 AdminData::Util.exception_info(e) end output end.join(', ') end |
#handle_column_type(col, html, model, column_value, f) ⇒ Object
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 240 |
# File 'app/helpers/admin_data/application_helper.rb', line 205 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
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'app/helpers/admin_data/application_helper.rb', line 81 def has_many_data(model, klass) array = AdminData::ActiveRecordUtil.declared_has_many_association_names(klass).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 = AdminData::ActiveRecordUtil.klass_for_association_type_and_name(model.class, :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 AdminData::Util.exception_info(e) end output end array.join(', ') end |
#has_one(model, klass) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'app/helpers/admin_data/application_helper.rb', line 64 def has_one(model, klass) tmp = AdminData::ActiveRecordUtil.declared_has_one_association_names(klass) 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 AdminData::Util.exception_info(e) end output end end |
#search_result_title(total_num_of_children, records) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/helpers/admin_data/application_helper.rb', line 39 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
35 36 37 |
# File 'app/helpers/admin_data/application_helper.rb', line 35 def total_records_info(klass) '(Total ' + pluralize(klass.count, 'record') + ' )' end |