# lib/widgets/widget.rb
# Superclass for our form element "widgets"
class Widget
include Merb::GlobalHelpers
# This is necessary to include Merb::GlobalHelpers
class_inheritable_accessor :_default_builder
# These are for convenience
attr_accessor :params
attr_accessor :session
def initialize(params = {}, session = {})
self.params = params
self.session = session
end
def value
end
end
# lib/widgets/weight_input_switcher.rb
class WeightInputSwitcher < Widget
attr_accessor :attribute_name, :weight_in_kg, :unit_preference
# View methods
def setup(attribute_name, weight_in_kg = nil, unit_preference = session[:unit_preference])
self.attribute_name = attribute_name
self.weight_in_kg = weight_in_kg
self.unit_preference = unit_preference
self
end
def switcher
html = "<div class='weight_input_switcher'>"
html += weight_input("lbs")
html += weight_input("kg")
html += weight_input("st")
html += "</div>"
html
end
def weight_input
html = "<span class='weight_input #{unit_preference}'>"
html += weight_input_field
html += "</span>"
end
def weight_input_field
if unit_preference == "st"
stone, lbs = if weight_in_kg.to_i == 0
["",""]
else
weight_input_value(weight_in_kg, unit_preference).split(" st ")
end
html = text_field attribute_name, :value => stone, :name => "weight_input[stone]", :class => "weight_input_field stone"
html += "<span class='weight_unit'>stone</span>"
html += text_field attribute_name, :value => lbs, :name => "weight_input[lbs]", :class => "weight_input_field stone lbs"
html += "<span class='weight_unit'>lbs</span>"
else
value = weight_input_value(weight_in_kg, unit_preference)
html = text_field attribute_name, :value => value, :name => "weight_input", :class => "weight_input_field"
html += "<span class='weight_unit'>" + unit_preference.to_s + "</span>"
end
html
end
# Parse methods
# We kinda cheat here and use params. Need to figure out a better way to do this.
def value
if params["weight_input"]["stone"]
"#{params["weight_input"]["stone"]} st #{params["weight_input"]["lbs"]} lbs"
else
"#{params["weight_input"]} #{Units.weight_full_to_abbreviation_map[unit_preference]}"
end
end
private
def weight_input_value(weight_in_kg, unit_preference)
begin
weight_in_kg.to_display_weight_without_unit(unit_preference)
rescue
""
end
end
end
# The following is added to global_helpers.rb
def widget(klass, *args)
klass.new(params, session).setup(*args)
end
# An example of using the "Widget" to display form elements
widget(WeightInputSwitcher, :weight, @current_user.current_weight).switcher
# An example of using the "Widget" to get the value of form elements
weight_widget = widget(WeightInputSwitcher, :weight, 0, "", user[:unit_preference])
weight = weight_widget.value
Tag Archive for view
Custom render function for Django view
def render(request, template, context=None):
return render_to_response(
template, context or {},context_instance=RequestContext(request)
)
django.views.generic.simple.direct_to_template takes request as the first parameter and does the same thing, in addition to providing URL parameters.
Here’s the source, from django/views/generic/simple.py:
def direct_to_template(request, template, extra_context=None, mimetype=None, **kwargs):
"""
Render a given template with any extra URL parameters in the context as
``{{ params }}``.
"""
if extra_context is None: extra_context = {}
dictionary = {'params': kwargs}
for key, value in extra_context.items():
if callable(value):
dictionary[key] = value()
else:
dictionary[key] = value
c = RequestContext(request, dictionary)
t = loader.get_template(template)
return HttpResponse(t.render(c), mimetype=mimetype)
Refresh an Oracle materialized view
BEGIN
dbms_mview.refresh('my_mview');
END;
Cairngorm VIEW
<?xml version="1.0" encoding="utf-8"?> <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="100%" height="100%"> <mx:Script> <![CDATA[ import com.adobe.cairngorm.control.CairngormEvent; import com.adobe.cairngorm.control.CairngormEventDispatcher; import ###MODEL###.AppModelLocator; [Bindable] private var model : AppModelLocator = AppModelLocator.getInstance(); ]]> </mx:Script> </mx:Canvas>
Check Parent Node When Last Child is Checked
Private Sub TreeView1_AfterCheck(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterCheck Dim blnUncheck As Boolean = False 'Check to see if a parent node exists. If Not e.Node.Parent Is Nothing Then 'Loop through the child nodes. For Each child As TreeNode In e.Node.Parent.Nodes 'Check to see if the current node is unchecked. If child.Checked = False Then 'Set the variable. blnUncheck = True End If Next 'Check the variable. If blnUncheck = False Then 'Check the parent node. e.Node.Parent.Checked = True Else 'Uncheck the parent node. e.Node.Parent.Checked = False End If End If End Sub
Form with 1:0-1 relationship
<% form_for :user, @display_user, :url => { :action => "update", :id => @display_user} do |f| %>
Mail: <%= f.text_field :mail %>
<% fields_for :adressdata, @display_user.adressdata do |adressdata_fields| %>
Lastname : <%= adressdata_fields.text_field :lastname %>
<% end %>
<% end %>
Simple Form
<% form_tag(:controller => "login", :action => "update", :id => @user) do %> <%= text_field 'user', 'yummid' %> <%= submit_tag 'Save' %> <% end %>
Month Table
function minimonth($d = "", $m = "", $y = "")
{
if(empty($d)) $d = date("j");
if(empty($m)) $m = date("m");
if(empty($y)) $y = date("Y");
$today = mktime(0, 0, 0, $m, $d, $y);
$first = mktime(0, 0, 0, $m, 1, $y);
$prev_month = date("n", strtotime("-1 month", $today));
$prev_year = date("Y", strtotime("-1 month", $today));
$next_month = date("n", strtotime("+1 month", $today));
$next_year = date("Y", strtotime("+1 month", $today));
$caption = date("F Y", $today);
$out = "<table id='minimonth' cellspacing='0'>";
$out .= "<tr><td class='caption'><a href='#' onclick='cd($prev_month, $prev_year)'>«</a></td><td class='caption' colspan='5'>$caption</td><td><a href='#' onclick='cd($next_month, $next_year)'>»</a></td></tr>";
$out .= "<tr class='week'><td>S</td><td>M</td><td>T</td><td>W</td><td>T</td><td>F</td><td>S</td></tr>";
$w = date("w", $first);
if($w > 0)
{
$out .= "<tr>";
for($i = 0; $i < $w; $i++) $out .= "<td></td>";
}
for($i = 1; $i <= date("t", $today); $i++)
{
if(date("w", mktime(0, 0, 0, $m, $i, $y)) == 0) $out .= "<tr>";
$out .= "<td id='d$i' class='day'><a href='#' onclick='pick($i)'>$i</a></td>";
if(date("w", mktime(0, 0, 0, $m, $i, $y)) == 6) $out .= "</tr>";
}
$w = date("w", mktime(0, 0, 0, $m, $i - 1, $y));
for($i = $w; $i < 6; $i++) $out .= "<td></td>";
$out .= "</tr></table>";
echo $out;
}