Permalink
Browse files

Adding +i +n +u switches

  • Loading branch information...
1 parent 7111c0c commit 21e9f5fb6b3f17946ddf71359bf926b15975aa17 @ffmike ffmike committed Dec 22, 2008
Showing with 19 additions and 6 deletions.
  1. +19 −6 generators/model_plus/templates/model.rb
@@ -5,19 +5,32 @@ class <%= class_name %> < ActiveRecord::Base
<% attributes.select(&:referenced_by?).each do |attribute| -%>
<%= attribute.base_type %> :<%= attribute.name %>
<% end -%>
-<% attr_accessible = [] %>
+<% attr_accessible = present = bool_present = numerical = integral = unique = [] %>
<% attributes.each do |attribute| -%>
<% attr_accessible << ":" + attribute.name if attribute.type_attributes =~ /a/ -%>
+ <% numerical << ":" + attribute.name if attribute.type_attributes =~ /n/ -%>
+ <% integral << ":" + attribute.name if attribute.type_attributes =~ /i/ -%>
+ <% unique << ":" + attribute.name if attribute.type_attributes =~ /u/ -%>
+ <% present << ":" + attribute.name if attribute.type_attributes =~ /p/ && attribute.base_type != "boolean" -%>
+ <% bool_present << ":" + attribute.name if attribute.type_attributes =~ /p/ && attribute.base_type == "boolean" -%>
+%>
<% end -%>
<% if !attr_accessible.empty? -%>
attr_accessible <%= attr_accessible.join(", ") %>
<% end -%>
-<% present = [] %>
-<% attributes.each do |attribute| -%>
- <% present << ":" + attribute.name if attribute.type_attributes =~ /p/ && attribute.base_type != "boolean" -%>
- <%= "validates_inclusion_of :#{attribute.name}, :in => [true, false]" if attribute.type_attributes =~ /p/ && attribute.base_type == "boolean" %>
-<% end -%>
<% if !present.empty? -%>
validates_presence_of <%= present.join(", ") %>
<% end -%>
+<% if !bool_present.empty? -%>
+ validates_inclusion_of <%= bool_present.join(", ") %>, :in => [true, false]
+<% end -%>
+<% if !numerical.empty? -%>
+ validates_numericality_of <%= numerical.join(", ") %>
+<% end -%>
+<% if !integral.empty? -%>
+ validates_numericality_of <%= integral.join(", ") %>, :only_integer => true
+<% end -%>
+<% if !unique.empty? -%>
+ validates_uniqueness_of <%= unique.join(", ") %>
+<% end -%>
end

0 comments on commit 21e9f5f

Please sign in to comment.