Module: Admin::MasterHelper

Includes:
FormHelper, SidebarHelper, TableHelper, TypusHelper
Defined in:
app/helpers/admin/master_helper.rb

Instance Method Summary collapse

Methods included from TypusHelper

#applications, #display_flash_message, #header, #locales, #login_info, #page_title, #resources, #typus_block, #typus_message

Instance Method Details

#build_list(model, fields, items, resource = , link_options = {}, association = nil) ⇒ Object

If there’s a partial with a “microformat” of the data we want to display, this will be used, otherwise we use a default table which it’s build from the options defined on the yaml configuration file.



56
57
58
59
60
61
62
63
64
65
66
# File 'app/helpers/admin/master_helper.rb', line 56

def build_list(model, fields, items, resource = @resource[:self], link_options = {}, association = nil)

  template = "app/views/admin/#{resource}/_#{resource.singularize}.html.erb"

  if File.exist?(template)
    render :partial => template.gsub('/_', '/'), :collection => items, :as => :item
  else
    build_typus_table(model, fields, items, link_options, association)
  end

end

#build_pagination(pager, options = {}) ⇒ Object

Simple and clean pagination links



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
# File 'app/helpers/admin/master_helper.rb', line 76

def build_pagination(pager, options = {})

  options[:link_to_current_page] ||= true
  options[:always_show_anchors] ||= true

  # Calculate the window start and end pages
  options[:padding] ||= 2
  options[:padding] = options[:padding] < 0 ? 0 : options[:padding]

  page = params[:page].blank? ? 1 : params[:page].to_i
  current_page = pager.page(page)

  first = pager.first.number <= (current_page.number - options[:padding]) && pager.last.number >= (current_page.number - options[:padding]) ? current_page.number - options[:padding] : 1
  last = pager.first.number <= (current_page.number + options[:padding]) && pager.last.number >= (current_page.number + options[:padding]) ? current_page.number + options[:padding] : pager.last.number

  returning(String.new) do |html|
    # Print start page if anchors are enabled
    html << yield(1) if options[:always_show_anchors] and not first == 1
    # Print window pages
    first.upto(last) do |page|
      (current_page.number == page && !options[:link_to_current_page]) ? html << page.to_s : html << (yield(page)).to_s
    end
    # Print end page if anchors are enabled
    html << yield(pager.last.number).to_s if options[:always_show_anchors] and not last == pager.last.number
  end

end

(_params = params)



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
# File 'app/helpers/admin/master_helper.rb', line 9

def display_link_to_previous # (_params = params)

  options = {}
  options[:resource_from] = @resource[:class].typus_human_name
  options[:resource_to] = params[:resource].classify.humanize if params[:resource]

  editing = %w( edit update ).include?(params[:action])

  message = case
            when params[:resource] && editing
              _("You're updating a {{resource_from}} for {{resource_to}}.", 
                :resource_from =>  options[:resource_from], 
                :resource_to => options[:resource_to])
            when editing
              _("You're updating a {{resource_from}}.", 
                :resource_from => options[:resource_from])
            when params[:resource]
              _("You're adding a new {{resource_from}} to {{resource_to}}.", 
                :resource_from => options[:resource_from], 
                :resource_to => options[:resource_to])
            else
              _("You're adding a new {{resource_from}}.", 
                :resource_from => options[:resource_from] )
            end

  returning(String.new) do |html|
    html << <<-HTML
<div id="flash" class="notice">
<p>#{message} #{link_to _("Do you want to cancel it?"), params[:back_to]}</p>
</div>
    HTML
  end

end

#pagination(*args) ⇒ Object



68
69
70
71
# File 'app/helpers/admin/master_helper.rb', line 68

def pagination(*args)
  @options = args.extract_options!
  render 'admin/shared/pagination' if @items.prev || @items.next
end


44
45
46
47
48
49
# File 'app/helpers/admin/master_helper.rb', line 44

def remove_filter_link(filter = request.env['QUERY_STRING'])
  return unless filter && !filter.blank?
  <<-HTML
<small>#{link_to _("Remove filter")}</small>
  HTML
end