Permalink
Browse files

added +ap attributes

  • Loading branch information...
1 parent ef22881 commit d7d4245d6e07bad0c628c7d1db1ebc31be547fc8 @ffmike ffmike committed Dec 22, 2008
View
8 generators/model_plus/USAGE
@@ -13,6 +13,14 @@ Description:
:has_many, :has_one, or :has_and_belongs_to_many, the model will contain
the appropriate association declaration.
+ You can also decorate a type with suffixes that specify further code
+ generation in the model:
+
+ title:string+a => Adds to an attr_accessible list
+ title:string+p => validates_presence_of :title
+ posted:boolean+ap => attr_accessible and
+ validates_inclusion_of :posted :in => [true, false]
+
This generates a model class in app/models, a unit test in test/unit,
a test fixture in test/fixtures/singular_name.yml, and a migration in
db/migrate.
View
6 generators/model_plus/model_plus_generator.rb
@@ -2,8 +2,12 @@ module Rails
module Generator
class GeneratedAttribute
+ def is_reference?
+ [ :references, :belongs_to ].include?(self.base_type)
+ end
+
def referenced_by?
- [ :has_one, :has_many, :has_and_belongs_to_many ].include?(self.type)
+ [ :has_one, :has_many, :has_and_belongs_to_many ].include?(self.base_type)
end
def base_type
View
12 generators/model_plus/templates/model.rb
@@ -1,9 +1,9 @@
class <%= class_name %> < ActiveRecord::Base
-<% attributes.select(&:reference?).each do |attribute| -%>
+<% attributes.select(&:is_reference?).each do |attribute| -%>
belongs_to :<%= attribute.name %>
<% end -%>
<% attributes.select(&:referenced_by?).each do |attribute| -%>
- <%= attribute.type %> :<%= attribute.name %>
+ <%= attribute.base_type %> :<%= attribute.name %>
<% end -%>
<% attr_accessible = [] %>
<% attributes.each do |attribute| -%>
@@ -12,4 +12,12 @@ class <%= class_name %> < ActiveRecord::Base
<% 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 -%>
end

0 comments on commit d7d4245

Please sign in to comment.