Module: ControllerHelpers

Included in:
ApplicationControllerInstanceMethodsAndHelpers
Defined in:
app/helpers/controller_helpers.rb

Instance Method Summary collapse

Instance Method Details

#add_css(s) ⇒ Object



99
100
101
102
103
# File 'app/helpers/controller_helpers.rb', line 99

def add_css(s)
  if !@layout[:css].include? s     
    @layout[:css] << s
  end
end

#add_js(js) ⇒ Object



104
105
106
107
108
# File 'app/helpers/controller_helpers.rb', line 104

def add_js(js)
  if !@layout[:js].include? js
    @layout[:js] << js 
  end
end

#app_controllersObject



117
118
119
# File 'app/helpers/controller_helpers.rb', line 117

def app_controllers
  Dir['app/controllers/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize}
end

#app_modelsObject



120
121
122
# File 'app/helpers/controller_helpers.rb', line 120

def app_models
  Dir['app/models/*.rb'].map {|f| File.basename(f, '.*').camelize.constantize }
end

#controller_resourceObject



56
57
58
59
60
61
62
# File 'app/helpers/controller_helpers.rb', line 56

def controller_resource 
  begin
    instance_variable_get "@#{controller_resource_var}"
  rescue
    'error'
  end
end

#controller_resource_nameObject



63
64
65
66
67
68
69
70
71
# File 'app/helpers/controller_helpers.rb', line 63

def controller_resource_name
  if controller_resource
    if controller_resource.respond_to? :name
      controller_resource.titleize 
    else
      controller_resource.id
    end
  end
end

#controller_resource_varObject



49
50
51
52
53
54
55
# File 'app/helpers/controller_helpers.rb', line 49

def controller_resource_var
  if params[:action] == 'index'
    params[:controller]
  else
    params[:controller].singularize
  end
end

#controller_titleObject



46
47
48
# File 'app/helpers/controller_helpers.rb', line 46

def controller_title 
  controller_resource_var.titleize
end

#default_view_titleObject

gets @resource or @resource for all actions in controller before filter is set per controller, thus overrideable.



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'app/helpers/controller_helpers.rb', line 5

def default_view_title                                                               #sets h1 and <title> defaults to something sensible, in the absense of explicit setting
  case params[:action]
  when 'new'
    title_h1 "New #{controller_title}"
  when 'create'
    title_h1 "New #{controller_title} (with errors)"
  when 'show'
    title_h1 "#{controller_title}: #{controller_resource_name}"
  when 'edit'
    title_h1 "Editing #{controller_title}: #{controller_resource_name}"
  when 'update'
    title_h1 "Editing #{controller_title}: #{controller_resource_name} (with errors)"
  when 'index'
    title_h1 "Listing #{params[:controller].titleize}"
  else
    "#{params[:controller].titleize}: #{params[:action].titleize}"
  end
end

#display_flash(type = nil) ⇒ Object

———– LAYOUT ————–



73
74
75
76
77
78
79
80
81
82
# File 'app/helpers/controller_helpers.rb', line 73

def display_flash (type = nil)
  
  if type.nil?                                                                        #show all flash keys
    html = flash.collect { |type, val| display_flash(type) }.join("\n")
  else
    return flash[type].blank? ? "" : "<p class='flash flash_#{type}'>#{flash[type]}</p> \n"
  end
  
  html.html_safe
end

#get_resourceObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/helpers/controller_helpers.rb', line 23

def get_resource                                                                      
  a       = params[:action].to_sym
  c       = params[:controller]
  klass   = c.classify.constantize 
  if [:show, :edit, :update, :destroy].include? a

    instance_variable_set "@#{controller_resource_var}", klass.find(params[:id])

  elsif a == :new

    instance_variable_set "@#{controller_resource_var}", klass.new 

  elsif a == :create

    instance_variable_set "@#{controller_resource_var}", klass.new(params[c.singularize]) 

  elsif a == :index

    instance_variable_set "@#{controller_resource_var}", klass.all 

  end
  
end

#h1(st) ⇒ Object

allow templates to set page h1



86
87
88
# File 'app/helpers/controller_helpers.rb', line 86

def h1 (st)                                                                            #allow templates to set page h1
  @layout[:h1] = st
end

#h1_emph(st) ⇒ Object

uniform way to make part of the h1 stand out



93
94
95
# File 'app/helpers/controller_helpers.rb', line 93

def h1_emph(st)                                                                        #uniform way to make part of the h1 stand out
  "<em>#{st}</em>"
end

#h1_name(st) ⇒ Object

uniform way of displaying model names in h1



96
97
98
# File 'app/helpers/controller_helpers.rb', line 96

def h1_name(st)                                                                        #uniform way of displaying model names in h1 
  h1_emph "#{st.titleize}"
end

#layout_chooserObject

put one of these in the controller to override on a per controller basis



109
110
111
# File 'app/helpers/controller_helpers.rb', line 109

def layout_chooser #put one of these in the controller to override on a per controller basis
  (['index', 'new', 'create', 'update', 'edit', 'destroy'].include? params[:action]) ? 'admin' : 'application'
end

#strip_tags(str) ⇒ Object

———– UTILS ————–



114
115
116
# File 'app/helpers/controller_helpers.rb', line 114

def strip_tags (str)
  str.gsub(/<\/?[^>]*>/, "")
end

#title(st) ⇒ Object

allow templates to set page title



83
84
85
# File 'app/helpers/controller_helpers.rb', line 83

def title (st)                                                                         #allow templates to set page title
  @layout[:title] = strip_tags st 
end

#title_h1(st) ⇒ Object

allow templates to set page title and h1 at same time



89
90
91
92
# File 'app/helpers/controller_helpers.rb', line 89

def title_h1 (st)                                                                      #allow templates to set page title and h1 at same time
  h1(st)
  title(st)
end