Skip to content

Instantly share code, notes, and snippets.

@mhenrixon
Created May 4, 2011 21:24
Show Gist options
  • Save mhenrixon/956067 to your computer and use it in GitHub Desktop.
Save mhenrixon/956067 to your computer and use it in GitHub Desktop.
active record extensions
class ActiveRecord::Base
def self.has_statuses(*status_names)
validates :status,
presence: true,
inclusion: { in: status_names.to_s}
status_names.each do |status_name|
scope "all_#{status_name}", where(status: status_name)
end
status_names.each do |status_name|
define_method "#{status_name}?" do
status == status_name.to_s
end
end
end
end
<li class="select optional error" id="order_status_input">
<label for="order_status">Status</label>
<select data-validate="true" id="order_status" name="order[status]">
<option value=""></option>
<option value="created">Created</option>
<option value="in_progress" selected="selected">In Progress</option>
<option value="approved">Approved</option>
<option value="rejected">Rejected</option>
<option value="shipped">Shipped</option>
<option value="delivered">Delivered</option>
</select>
<p class="inline-errors">is not included in the list</p>
</li>
var edit_order_2 =
{
"type":"Formtastic::SemanticFormBuilder",
"inline_error_class":"inline-errors",
"validators":
{
"order[status]":
{
"presence":
{
"message":"can't be blank"
},
"inclusion":
{
"message":"is not included in the list",
"in":"[ \"created\", \"in_progress\", \"approved\", \"rejected\", \"shipped\", \"delivered\" ]"
}
}
}
};
var edit_order_2 =
{
"type":"Formtastic::SemanticFormBuilder",
"inline_error_class":"inline-errors",
"validators":
{
"order[status]":
{
"presence":
{
"message":"can't be blank"
},
"inclusion":
{
"message":"is not included in the list",
"in":"[ :created, :in_progress, :approved, :rejected, :shipped, :delivered ]"
}
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment