Module: Manageable::ApplicationHelper

Defined in:
app/helpers/manageable/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#manageable_attributes(record, options = {}, &block) ⇒ Object

Helper for custom attrtastic like builder



309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'app/helpers/manageable/application_helper.rb', line 309

def manageable_attributes(record, options = {}, &block)
  options[:html] ||= {}

  html_class = [ "attrtastic", record.class.to_s.underscore, options[:html][:class] ].compact.join(" ")

  output = tag(:div, { :class => html_class}, true)
  if block_given?
    output << capture(Helpers::AttributesBuilder.new(record, self), &block)
  else
    output << capture(Helpers::AttributesBuilder.new(record, self)) do |attr|
      attr.attributes
    end
  end
  output.safe_concat("</div>")
end

#manageable_breadcrumbs(options = {}) {|items| ... } ⇒ Object

Displays a breadcrumb trail

options - A hash of attributes to apply to the wrapping div tag

Example:

<div class="block">
  <div class="content">
    <h2><%= @news_item.title %></h2>
    <p><%= @news_item.content %></p>
  </div>
  <%= breadcrumbs do |b|
    b.item "Home", root_path
    b.item "News", news_path
    b.item "Awesome New Things", news_path(@news_item), :active => true
  %>
</div>

Returns the breadcrumb trail.

Yields:

  • (items)


187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# File 'app/helpers/manageable/application_helper.rb', line 187

def manageable_breadcrumbs(options = {})
  items = Helpers::NavigationBuilder.new
  yield items if block_given?

  options[:class] ||= ""
  options[:class] << " breadcrumb"
  options[:class].strip!

  ("div", options) do
    ("ul") do
      items.collect { |item|
        ("li", :class => item[:class]) do
          if item[:active]
            item[:label]
          else
            link_to(item[:label], item[:href])
          end
        end
      }.join("").html_safe
    end
  end
end

#manageable_button(body, url, html_options = {}) ⇒ Object

Creates a link_to button



229
230
231
232
233
234
235
236
# File 'app/helpers/manageable/application_helper.rb', line 229

def manageable_button(body, url, html_options = {})
  html_options[:class] = [html_options[:class], "button"].compact.join(" ")
  icon = manageable_icon(html_options.delete(:icon), :small, :alt => body) if html_options[:icon]

  link_to url, html_options do
    [icon, body].compact.join("&nbsp;").html_safe
  end
end

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

Create a content box.

options - A hash of options to apply to the box. &block - The content of the box, passed an instance of Helpers::BoxBuilder.

Valid options:

  • :headline – The headline to show in the box.

  • :class – A class to apply to the box.

  • :id – The ID to apply to the box.

Example:

<% content_box :headline => "My Box", :class => "alert", :id => "my_box" do |box| %>
  <% box.navigation do |nav| %>
    <% nav.item "List People", people_path, :active => true %>
    <% nav.item "New Person", new_person_path %>
    <% nav.item "Search", search_path(:type => "people") %>
  <% end %>

  <% box.breadcrumbs do |crumbs| %>
    <% crumbs.item "Home", root_path %>
    <% crumbs.item "People", people_path %>
    <% crumbs.item "Bob Jones", person_path(@person), :active => true %>
  <% end %>

  <p>This is a really neat box, which will be displayed with a headline and navigation.</p>
<% end %>

Returns the completed box, yields an instance of Helpers::BoxBuilder.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/manageable/application_helper.rb', line 32

def manageable_content_box(options = {}, &block)
  box_buffer = Helpers::BoxBuilder.new(self)
  box_content = capture(box_buffer, &block)

  options = {
    :id => nil,
    :class => []
  }.merge(options)

  block_class = ([ "block" ] + [ options[:class] ].flatten).join(" ")

  (:div, :class => block_class, :id => options[:id]) do
    block_out = box_buffer.buffers[:block_header].html_safe
    block_out << (:div, :class => "content") do
      content_out = ''.html_safe
      content_out = (:h2, options[:headline]) if options[:headline]
      content_out << (:div, box_content, :class => 'inner')
    end
    block_out << box_buffer.buffers[:block_footer].html_safe
  end
end

#manageable_controls(options = {}) {|items| ... } ⇒ Object

Creates a set of buttons

options - A hash of attributes to apply to the wrapping div tag

Example:

<div class="block">
  <div class="content">
    <%= controls do |c|
      c.item "Copy", copy_person_path(person), :icon => "copy_person"
      c.item "Delete", person_path(person), :method => :delete
    end %>
  </div>
</div>

Returns a set of controls to be displayed.

Yields:

  • (items)


154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/helpers/manageable/application_helper.rb', line 154

def manageable_controls(options = {})
  options[:class] ||= ""
  options[:class] << " control"
  options[:class].strip!

  items = Helpers::NavigationBuilder.new
  yield items if block_given?

  ("div", options) do
    items.collect { |item|
      manageable_button(item[:label], item[:href], item[:link_options].merge(:icon => item[:icon]))
    }.join("").html_safe
  end
end


343
344
345
# File 'app/helpers/manageable/application_helper.rb', line 343

def manageable_footer
  (:p, "Manageable - Activo Theme by David Francisco / [email protected] / activo.dmfranc.com")
end

#manageable_headObject

Default customizable helpers



327
328
329
# File 'app/helpers/manageable/application_helper.rb', line 327

def manageable_head
  content_for(:head)
end

#manageable_icon(name, size = :small, options = {}) ⇒ Object

Display an icon

name - The icon to display size - One of :small or :large (optional) options - A hash to be passed to the image_tag helper (optional)

Example:

manageable_icon("add")
# => image_tag("/assets/manageable/icons/16x16/add.png", :alt => "Add")
manageable_icon("new_item", :large)
# => image_tag("/assets/manageable/icons/32x32/new_item.png, :alt => "New Item")

Returns an image tag, ready to be displayed in a template.



83
84
85
86
87
88
89
90
91
92
# File 'app/helpers/manageable/application_helper.rb', line 83

def manageable_icon(name, size = :small, options = {})
  return "" if name.nil?

  dimension = ( (size == :small) ? "16" : "32" ).html_safe
  options[:alt] ||= name.capitalize.gsub("_", " ")

  image_tag(asset_path("manageable/icons/#{dimension}x#{dimension}/#{name}.png"), {
    :alt => options[:alt]
  })
end

#manageable_javascriptsObject



331
332
333
# File 'app/helpers/manageable/application_helper.rb', line 331

def manageable_javascripts
  content_for(:javascripts)
end

#manageable_logoObject



339
340
341
# File 'app/helpers/manageable/application_helper.rb', line 339

def 
  (:h1, "Manageable")
end

#manageable_main_navigation(menu) ⇒ Object



354
355
356
357
358
# File 'app/helpers/manageable/application_helper.rb', line 354

def manageable_main_navigation(menu)
   menu.item "Main Page", "#"
   menu.item "Active", "#", :class => "active"
   menu.item "Login", "#"
end

#manageable_navigation(options = {}) {|menu| ... } ⇒ Object

Yields:

  • (menu)


94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'app/helpers/manageable/application_helper.rb', line 94

def manageable_navigation(options = {}, &block)
  options[:class] ||= ""
  options[:class].strip!

  options[:ul_class] ||= "wat-cf"
  options[:ul_class].strip!

  menu = Helpers::NavigationBuilder.new
  yield menu if block_given?

  ("div", options) do
    ("ul", "", :class => options[:ul_class]) do
      menu.collect { |item|
        ("li", :class => item[:class]) do
          link_to(item[:label], item[:href], item[:link_options])
        end
      }.join("").html_safe
    end
  end
end

#manageable_page_title(title = nil) ⇒ Object

Get or set the page title

title - The title to set. (optional)

Example:

page_title("Hello, world!")
# => "Hello, world!"
page_title
# => "Hello, world!"

Returns the page title, first setting it if title is not nil.



65
66
67
68
# File 'app/helpers/manageable/application_helper.rb', line 65

def manageable_page_title(title = nil)
  @title = title unless title.nil?
  @title || "Untitled Page"
end

#manageable_pagination(options = {}) ⇒ Object

Prints a pagination block. Accepts the following options:

current_page num_pages param_name



243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
# File 'app/helpers/manageable/application_helper.rb', line 243

def manageable_pagination(options = {})
  current_page = options[:current_page] || 1
  num_pages    = options[:num_pages] || 1
  outer_window = options[:outer_window] || 4
  page_param   = options[:param_name] || :page

  if current_page <= num_pages
    previous_page = current_page - 1
    next_page     = current_page + 1
    left_window   = ((current_page - outer_window)...current_page).to_a.select{|i| i > 0}
    right_window  = ((current_page + 1)..(current_page + outer_window)).to_a.select{|i| i <= num_pages}

    elements = []

    if 1 != current_page
      # First
      elements <<  {:value => t("manageable.pagination.first"), :href => params.merge(page_param => 1)}

      # Previous
      elements <<  {:value => t("manageable.pagination.previous"), :href => params.merge(page_param => previous_page)}
    end

    # Left Gap
    if left_window.first && left_window.first != 1
      elements <<  {:value => t("manageable.pagination.gap")}
    end

    # Left window
    left_window.each do |i|
      elements <<  {:value => i, :href => params.merge(page_param => i)}
    end

    # Current Page
    elements <<  {:value => current_page, :html => {:class => "current"}}

    # Right window
    right_window.each do |i|
      elements <<  {:value => i, :href => params.merge(page_param => i)}
    end

    # Right Gap
    if right_window.last && right_window.last != num_pages
      elements <<  {:value => t("manageable.pagination.gap")}
    end

    if num_pages != current_page
      # Next
      elements <<  {:value => t("manageable.pagination.next"), :href => params.merge(page_param => next_page)}

      # Last
      elements <<  {:value => t("manageable.pagination.last"), :href => params.merge(page_param => num_pages)}
    end

     :div, :class => "pagination" do
      elements.map do |options|
        if options[:href]
          link_to options[:value], options[:href]
        else
          (:span, options[:value], options[:html])
        end
      end.join.html_safe
    end
  end
end

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

Displays a secondary naviagtion menu

options - A hash of attributes to apply to the wrapping div tag

Example:

<div class="block">
  <%= secondary_navigation do |nav|
    nav.item "List People", people_path, :active => true
    nav.item "New Person", new_person_path
    nav.item "Search", search_path(:type => "people")
  end %>
  <div class="content">
    <h2 class="title">List People</h2>
  </div>
</div>

Returns a secondary navigation block to be displayed.



132
133
134
135
136
137
# File 'app/helpers/manageable/application_helper.rb', line 132

def manageable_secondary_navigation(options = {}, &block)
  options[:class] ||= ""
  options[:class] << " secondary-navigation"

  manageable_navigation(options, &block)
end

#manageable_sidebarObject



335
336
337
# File 'app/helpers/manageable/application_helper.rb', line 335

def manageable_sidebar
  content_for(:sidebar)
end

#manageable_sortable(column, title = nil, options = {}) ⇒ Object

Links to a sortable column. column: - The column to link to title: - The link title options: - Additional link_to options



214
215
216
217
218
219
220
221
222
223
224
225
226
# File 'app/helpers/manageable/application_helper.rb', line 214

def manageable_sortable(column, title = nil, options = {})
  title ||= column.titleize

  if respond_to?(:sort_column) && respond_to?(:sort_direction)
    css_class = column && sort_column && column.to_sym == sort_column.to_sym ? "sort_#{sort_direction}" : nil
    direction = column && sort_column && column.to_sym == sort_column.to_sym && sort_direction == "asc" ? "desc" : "asc"
    options[:class] = [options[:class], css_class].compact.join(" ")

    link_to title, params.merge(:sort => column, :direction => direction, :page => nil), options
  else
    title
  end
end

#manageable_user_navigation(menu) ⇒ Object



347
348
349
350
351
352
# File 'app/helpers/manageable/application_helper.rb', line 347

def manageable_user_navigation(menu)
  menu.item image_tag("manageable/session/home.png", :alt => "Dashboard", :"original-title" => "Dashboard"), "#"
  menu.item image_tag("manageable/session/account.png", :alt => "Profile", :"original-title" => "Profile"), "#"
  menu.item image_tag("manageable/session/config.png", :alt => "Preferences", :"original-title" => "Preferences"), "#"
  menu.item image_tag("manageable/session/logout.png", :alt => "Logout", :"original-title" => "Logout"), "#"
end