Module: Qadmin::Helper
- Defined in:
- lib/qadmin/helper.rb
Instance Method Summary collapse
- #admin_controls(name, options = {}, &block) ⇒ Object
- #admin_table(collection, options = {}) ⇒ Object
- #alt_rows ⇒ Object
- #fieldset(legend = nil, options = {}, &block) ⇒ Object
- #model_restful_query_parser(options = {}) ⇒ Object
- #sortable_column_header(attribute_name, text = nil, options = {}) ⇒ Object
- #yes?(boolean) ⇒ Boolean
Instance Method Details
#admin_controls(name, options = {}, &block) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/qadmin/helper.rb', line 13 def admin_controls(name, = {}, &block) return if respond_to?(:overlay?) && controller = [:controller] || name.to_s.tableize assumed_object = self.instance_variable_get "@#{name}" obj = [:object] || assumed_object || nil parent = [:parent] || false parent_link_attributes = parent ? {parent.class.to_s.foreign_key => parent.id} : {} general_link_attributes = {:controller => controller}.merge(parent_link_attributes) control_links = { :index => link_to(image_tag('admin/icon_list.png') + " Back to List", general_link_attributes.merge(:action => 'index')), :new => link_to(image_tag('admin/icon_new.png') + " New", general_link_attributes.merge(:action => 'new')), :edit => link_to(image_tag('admin/icon_edit.png') + " Edit", general_link_attributes.merge(:action => 'edit', :id => obj)), :show => link_to(image_tag('admin/icon_show.png') + " View", general_link_attributes.merge(:action => 'show', :id => obj)), :destroy => link_to(image_tag('admin/icon_destroy.png') + " Delete", general_link_attributes.merge(:action => 'destroy', :id => obj), :confirm => 'Are you sure?', :method => :delete), :ports => link_to(image_tag('admin/icon_export.png') + " Import/Export", general_link_attributes.merge(:action => 'ports')), :export => link_to(image_tag('admin/icon_export.png') + " Export", general_link_attributes.merge(:action => 'export')) } control_sets = { :index => [:new], :new => [:index], :edit => [:index,:new,:show,:destroy], :show => [:index,:new,:edit,:destroy] } control_set = ([:controls] || []).dup control_set.unshift(control_sets[[:for]]) if [:for] control_set << :ports if [:ports] controls = [control_set].flatten.collect {|c| control_links[c] }.compact html = "" html << %{<ul class="admin_controls">} controls.each {|control| html << li(control) } if block_given? html << capture(&block) html << %{</ul>} concat(html) else html << %{</ul>} html end end |
#admin_table(collection, options = {}) ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 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 |
# File 'lib/qadmin/helper.rb', line 77 def admin_table(collection, = {}) html = '<table>' html << '<tr>' attributes = [:attributes] || self.qadmin_configuration.display_columns model_column_types = SuperHash.new attributes.each do |attribute_name| column = self.qadmin_configuration.model_klass.columns.detect {|c| c.name == attribute_name.to_s } model_column_types[attribute_name] = column.type if column end attributes.each_with_index do |attribute, i| html << (i == 0 ? '<th class="first_col">' : '<th>') html << sortable_column_header(attribute) html << '</th>' end html << %{ <th>View</th> <th>Edit</th> <th>Delete</th> </tr> } collection.each do |instance| html << %{<tr id="#{dom_id(instance)}" #{alt_rows}>} attributes.each_with_index do |attribute, i| raw_value = instance.send(attribute) value = case model_column_types[attribute] when :boolean yes?(raw_value) when :text truncate(raw_value, :length => 30, :omission => ". . . #{link_to('More', send("#{model_instance_name}_path", instance))}") else h(raw_value) end if i == 0 html << %{<td class="first_col">#{link_to(value, send("#{model_instance_name}_path", instance))}</td>} else html << %{<td>#{value}</td>} end end html << %{<td>#{link_to(image_tag('admin/icon_show.png'), send("#{model_instance_name}_path", instance))}</td>} html << %{<td>#{link_to(image_tag('admin/icon_edit.png'), send("edit_#{model_instance_name}_path", instance))}</td>} html << %{<td>#{link_to(image_tag('admin/icon_destroy.png'), send("#{model_instance_name}_path", instance), :confirm => 'Are you sure?', :method => :delete)}</td>} html << '</tr>' end html << '</table>' end |
#alt_rows ⇒ Object
123 124 125 |
# File 'lib/qadmin/helper.rb', line 123 def alt_rows %{class="#{cycle('alt', '')}"} end |
#fieldset(legend = nil, options = {}, &block) ⇒ Object
4 5 6 7 8 9 10 11 |
# File 'lib/qadmin/helper.rb', line 4 def fieldset(legend = nil, = {}, &block) concat(content_tag(:fieldset, ) do html = '' html << content_tag(:legend, legend) if legend html << capture(&block) html end) end |
#model_restful_query_parser(options = {}) ⇒ Object
72 73 74 75 |
# File 'lib/qadmin/helper.rb', line 72 def model_restful_query_parser( = {}) query_param = [:query_param] || :query qadmin_configuration.model_klass.restful_query_parser(params[query_param], ) end |
#sortable_column_header(attribute_name, text = nil, options = {}) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/qadmin/helper.rb', line 58 def sortable_column_header(attribute_name, text = nil, = {}) link_text = text || self.qadmin_configuration.column_headers[attribute_name] || attribute_name.to_s.humanize return link_text unless qadmin_configuration.model_klass.can_query? query_parser = model_restful_query_parser() query_param = [:query_param] || :query logger.warn 'params:' + self.params[query_param].inspect logger.warn 'parser:' + query_parser.inspect sorting_this = query_parser.sort(attribute_name) logger.warn "sorting #{attribute_name}:" + sorting_this.inspect link_text << " #{image_tag("admin/icon_#{sorting_this.direction.downcase}.gif")}" if sorting_this query_parser.set_sort(attribute_name, sorting_this ? sorting_this.next_direction : 'desc') link_to link_text, self.params.dup.merge(query_param => query_parser.to_query_hash), :class => 'sortable_column_header' end |
#yes?(boolean) ⇒ Boolean
127 128 129 |
# File 'lib/qadmin/helper.rb', line 127 def yes?(boolean) boolean ? 'Yes' : 'No' end |