Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Stub fields in Rails model generator #58

Open
wants to merge 1 commit into from

2 participants

@ches

Greetings Pete,

I've updated the Rails model generator to create some defaults for fields, like so:

$ rails g model Foo title:string date:datetime
...
$ cat spec/support/blueprints.rb 
require 'machinist/active_record'

# Add your blueprints here.
#
# e.g.
#   Post.blueprint do
#     title { "Post #{sn}" }
#     body  { "Lorem ipsum..." }
#   end

Foo.blueprint do
  title { "MyString" }
  date { "2010-10-09 16:44:25" }
end

Thanks for creating Machinist :-)

@ches

Hey Pete,

Just perusing my old open pull requests and saw this, so thought I'd bump it. Still using this little hack in my projects :-)

@wulftone

I like this hack. Bump!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Oct 9, 2010
  1. @ches
This page is out of date. Refresh to see the latest.
Showing with 10 additions and 1 deletion.
  1. +10 −1 lib/generators/machinist/model/model_generator.rb
View
11 lib/generators/machinist/model/model_generator.rb
@@ -4,7 +4,16 @@ class ModelGenerator < Rails::Generators::NamedBase #:nodoc:
argument :attributes, :type => :array, :default => [], :banner => "field:type field:type"
def create_blueprint
- append_file "spec/support/blueprints.rb", "\n#{class_name}.blueprint do\n # Attributes here\nend\n"
+ if attributes.empty?
+ fields = " # Attributes here\n"
+ else
+ fields = ''
+ attributes.each do |a|
+ fields << " #{a.name} { #{a.default.inspect} }\n"
+ end
+ end
+
+ append_file "spec/support/blueprints.rb", "\n#{class_name}.blueprint do\n#{fields}end\n"
end
end
Something went wrong with that request. Please try again.