Module: Qadmin::Helper

Defined in:
lib/qadmin/helper.rb

Instance Method Summary collapse

Instance Method Details

#admin_controls(name, options = {}, &block) ⇒ Object



4
5
6
7
8
9
10
11
12
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
# File 'lib/qadmin/helper.rb', line 4

def admin_controls(name, options = {}, &block)
  return if respond_to?(:overlay?) && overlay?
  controller     = options[:controller] || name.to_s.tableize
  assumed_object = self.instance_variable_get "@#{name}"
  obj            = options[:object] || assumed_object || nil
  parent         = options[: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 = (options[:controls] || []).dup
  control_set.unshift(control_sets[options[:for]]) if options[:for]
  control_set << :ports if options[: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



68
69
70
71
72
73
74
75
76
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
# File 'lib/qadmin/helper.rb', line 68

def admin_table(collection, options = {})
  html = '<table>'
  html <<	'<tr>'
  attributes = options[: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_rowsObject



114
115
116
# File 'lib/qadmin/helper.rb', line 114

def alt_rows
  %{class="#{cycle('alt', '')}"}
end

#fieldset(legend = nil, options = {}, &block) ⇒ Object



151
152
153
154
155
156
157
158
# File 'lib/qadmin/helper.rb', line 151

def fieldset(legend = nil, options = {}, &block)
  concat((:fieldset, options) do
    html = ''
    html << (:legend, legend) if legend
    html << capture(&block)
    html
  end)
end

#li(content, *args) ⇒ Object



122
123
124
# File 'lib/qadmin/helper.rb', line 122

def li(content, *args)
   :li, content, args
end

#model_restful_query_parser(options = {}) ⇒ Object



63
64
65
66
# File 'lib/qadmin/helper.rb', line 63

def model_restful_query_parser(options = {})
  query_param = options[:query_param] || :query
  qadmin_configuration.model_klass.restful_query_parser(params[query_param], options)
end

#simple_admin_menu(*controllers) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'lib/qadmin/helper.rb', line 126

def simple_admin_menu(*controllers)
  html = %{<div id="menu">
		<ul>}
		simple_menu(*controllers) do |name,controller|
	  li(link_to(name, :controller => controller))
	end
	html <<	%{</ul>
		<div class="clear"></div>
	</div>}
end

#simple_menu(*controllers, &block) ⇒ Object



137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/qadmin/helper.rb', line 137

def simple_menu(*controllers, &block)
  return unless controllers
  html = ""
  controllers.each do |controller_pair|
    if controller_pair.is_a? Array
      name, controller = controller_pair[0], controller_pair[1]
    else
      name, controller = controller_pair, controller_pair
    end
    html << yield(name,controller)
  end
  html
end

#sortable_column_header(attribute_name, text = nil, options = {}) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/qadmin/helper.rb', line 49

def sortable_column_header(attribute_name, text = nil, options = {})
  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(options)
  query_param = options[: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

Returns:

  • (Boolean)


118
119
120
# File 'lib/qadmin/helper.rb', line 118

def yes?(boolean)
  boolean ? 'Yes' : 'No'
end