Skip to content

Instantly share code, notes, and snippets.

@kevinpfromnm
Created August 19, 2011 17:18
Show Gist options
  • Save kevinpfromnm/1157385 to your computer and use it in GitHub Desktop.
Save kevinpfromnm/1157385 to your computer and use it in GitHub Desktop.
radio button select one tag
<def tag="select-one-radio" attrs='include-none, blank-message, options, sort, limit, text-method'><%
raise Hobo::PermissionDeniedError.new("Not allowed to edit #{this_field}") if !attributes[:disabled] && !can_edit?
blank_message ||= ht("#{this_type.name.underscore}.messages.none", :default=>"No #{this_type.model_name.human} available.")
limit ||= 100
options ||= begin
conditions = ActiveRecord::Associations::BelongsToAssociation.new(this_parent, this_field_reflection).send(:conditions)
order = this_field_reflection.klass.default_order
this_field_reflection.klass.all(:conditions => conditions, :limit => limit, :order => order).select {|x| can_view?(x)}
end
if text_method.nil?
select_options = options.map { |x| [x.to_s, x.id] }
else
select_options = options.map do |x|
[ text_method.split(".").inject(x) { |v, method| v.send(method) },
x.id ]
end
end
select_options = select_options.sort if sort
select_options.insert(0, [blank_message, ""]) if include_none || (this.nil? && include_none != false)
attributes = add_classes(attributes, "input", "belongs_to", type_and_field)
-%>
<radio-button-group options="&select_options" name="#{param_name_for_this(true)}" selected="&this.id" merge-attrs="&attributes.except :selected" />
</def>
<def tag="radio-button-group" attrs="options, name, selected"><%
selected ||= this -%>
<ul class="radio-button-group check-many" param>
<li repeat="&options" param>
<%= radio_button_tag name, this.last, selected == this.last %>
<%= label_tag "#{name}_#{this.first}", this.first %>
</li>
</ul>
</def>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment