Class: ActionView::Base

Inherits:
Object
  • Object
show all
Includes:
Inflector
Defined in:
app/controllers/rails_ext.rb

Instance Method Summary collapse

Instance Method Details



90
91
92
# File 'app/controllers/rails_ext.rb', line 90

def breadcrumbs
  link_to_unless_current("Dashboard", :controller => "project", :action => "index")
end

#calendar(options) ⇒ Object

Renders a Calendar widget The view (or layout) must load the jscalendar and dateFormat javascripts Note that the value is posted back as a ymdHMS string

  • :name - The name of the (hidden) field containing the date

  • :time - The time to initialise the widget with

  • :editable - Whether or not to make the widget editable



229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'app/controllers/rails_ext.rb', line 229

def calendar(options)
  t = options[:time]
  name = options[:name]
  js_function = "change" + name.gsub(/:/, '_').gsub(/@/, '_').gsub(/\[/, '_').gsub(/\]/, '_')
  js_format = "format('%yyyy%%mm%%dd%%hh%%nn%%ss%')"
  js_date = "new Date(#{t.year}, #{t.month - 1}, #{t.day}, #{t.hour}, #{t.min}, #{t.sec})"
  <<EOF
<input type="hidden" id="#{options[:name]}" name="#{options[:name]}" value="">
<div id="#{options[:name]}_calendar"></div>
<script type="text/javascript">
  document.getElementById('#{options[:name]}').value = #{js_date}.#{js_format}
  function #{js_function}(calendar) {
    if (calendar.dateClicked) {
      document.getElementById('#{options[:name]}').value = calendar.date.#{js_format};
}
  };
  Calendar.setup(
    {
      flat         : "#{options[:name]}_calendar", // ID of the parent element
      flatCallback : #{js_function},           // our callback function
  showsTime    : true,
  date         : #{js_date}
    }
  );
</script>
EOF
end

#define_selected!(array) ⇒ Object

defines selected? => false on each object that doesn’t already have selected? defined.



170
171
172
173
174
175
176
177
178
# File 'app/controllers/rails_ext.rb', line 170

def define_selected!(array)
  array.each do |o|
    unless(o.respond_to?(:selected?))
      def o.selected?
        false
      end
    end
  end
end

#render_object(o, collection_name, edit) ⇒ Object

Creates a table rendering o‘s attributes. Uses a default rendering, but a custom template will be used if there is a “_<underscored_class_name>.rhtml” under the project directory



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
# File 'app/controllers/rails_ext.rb', line 184

def render_object(o, collection_name, edit)
  underscored_name = underscore(demodulize(o.class.name))
  template = File.expand_path(File.dirname(__FILE__) + "/../views/project/_#{underscored_name}.rhtml")
  if(File.exist?(template))
    render_partial(underscored_name, o)
  else
    r = "<table>\n"
    o.instance_variables.each do |attr_name|
      attr_anns = o.class.send(attr_name[1..-1])
      if(attr_anns && attr_anns[:description])
        # Only render attributes with :description annotations
        attr_value = o.instance_variable_get(attr_name)
        r << "  <tr>\n"
        r << "    <td width='25%'>#{attr_anns[:description]}</td>\n"
        html_value = text_or_input(edit, :name => "#{collection_name}[#{o.class.name}][#{attr_name}]", :value => attr_value)
        r << "    <td width='75%'>#{html_value}</td>\n"
        r << "  </tr>\n"
      end
    end
    r << "</table>"
    r
  end
end

#select_pane(description, name, array) ⇒ Object

Renders a pane (div) with a combo (select) that will Show one of the objects in the array (which are rendered with render_object). If one of the objects in the array respond to selected? and return true, it is preselected in the combo.



156
157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/rails_ext.rb', line 156

def select_pane(description, name, array)
  define_selected!(array)
  $pane_name = name
  $pane_description = description
  def array.name
    $pane_name
  end
  def array.description
    $pane_description
  end
  render_partial("select_pane", array)
end

#tab_pane(name, array) ⇒ Object

Renders a tab pane where each tab contains rendered objects



143
144
145
146
147
148
149
150
# File 'app/controllers/rails_ext.rb', line 143

def tab_pane(name, array)
  define_selected!(array)
  $pane_name = name
  def array.name
    $pane_name
  end
  render_partial("tab_pane", array)
end

#text_or_checkbox(options) ⇒ Object

Renders an editable or read-only element describing a boolean value.

Options:

  • :name - The name of the variable/attribute.

  • :value - True or False

  • :editable - True or False



112
113
114
115
116
117
118
119
120
121
122
# File 'app/controllers/rails_ext.rb', line 112

def text_or_checkbox(options)
  value = options.delete(:value)
  if(options.delete(:editable))
    options[:type] = "checkbox"
    options[:value] = "true"
    options[:checked] = "true" if value
    tag("input", options)
  else
    value ? "on" : "off"
  end
end

#text_or_input(input, options) ⇒ Object

Renders plain text (if input is true) or a text field if not.



95
96
97
98
99
100
101
102
103
104
# File 'app/controllers/rails_ext.rb', line 95

def text_or_input(input, options)
  if(input)
    options[:class] = "setting-input" unless options[:class]
    tag("input", options)
  elsif(options[:value] =~ /^http?:\/\//)
    ("a", options[:value], "href" => options[:value] ? options[:value] : "")
  else
    options[:value] ? options[:value] : ""
  end
end

#text_or_select(input, options) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/rails_ext.rb', line 124

def text_or_select(input, options)
  values = options.delete(:values)
  if(input)
    #options[:class] = "setting-input" unless options[:class]
    
    option_tags = "\n"
    values.each do |value|
      option_attrs = {:value => value.class.name}
      option_attrs[:selected] = "true" if value.selected?
      option_tag = ("option", value.name, option_attrs)
      option_tags << option_tag << "\n"
    end
    ("select", option_tags, options)
  else
    values.find {|v| v.selected?}.name
  end
end

#tip(options) ⇒ Object

Creates an image with a tooltip that will show on mouseover.

Options:

  • :txt - The text to put in the tooltip. Can be HTML.

  • :img - The image to display on the page. Defaults to ‘/images/16x16/about.png’



213
214
215
216
217
218
219
220
# File 'app/controllers/rails_ext.rb', line 213

def tip(options)
  tip = options.delete(:txt)
  options[:src] = options.delete(:img) || "/images/16x16/about.png"
  options[:onmouseover] = "Tooltip.show(event,#{tip})"
  options[:onmouseout] = "Tooltip.hide()"

  tag("img", options)
end