Class: ActionView::Base

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

Instance Method Summary collapse

Instance Method Details



97
98
99
# File 'app/controllers/rails_ext.rb', line 97

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



239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'app/controllers/rails_ext.rb', line 239

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.



177
178
179
180
181
182
183
184
185
# File 'app/controllers/rails_ext.rb', line 177

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



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/controllers/rails_ext.rb', line 191

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
    # workaround for RoR bug. 'hash' form params must have at least one value.
    r << "<tr><td></td><td><input type='hidden' name ='#{collection_name}[#{o.class.name}][__dummy]'></td></tr>" if o.instance_variables.empty?

    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.



163
164
165
166
167
168
169
170
171
172
173
174
# File 'app/controllers/rails_ext.rb', line 163

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



150
151
152
153
154
155
156
157
# File 'app/controllers/rails_ext.rb', line 150

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



119
120
121
122
123
124
125
126
127
128
129
# File 'app/controllers/rails_ext.rb', line 119

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.



102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/rails_ext.rb', line 102

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



131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/controllers/rails_ext.rb', line 131

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’



223
224
225
226
227
228
229
230
# File 'app/controllers/rails_ext.rb', line 223

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