diff options
author | Aleksey Magusev <lexmag@gmail.com> | 2012-06-25 22:01:31 +0400 |
---|---|---|
committer | Aleksey Magusev <lexmag@gmail.com> | 2012-06-27 18:12:28 +0400 |
commit | 94b230e189e7241b96dcbfb41ceabedf7cfd14ac (patch) | |
tree | 58663586bb6cf80f19c36f20575b1ef53e393cc0 /railties/lib | |
parent | 51b055b6d11f439ce16c30167ad807d2a7b70532 (diff) | |
download | rails-94b230e189e7241b96dcbfb41ceabedf7cfd14ac.tar.gz rails-94b230e189e7241b96dcbfb41ceabedf7cfd14ac.tar.bz2 rails-94b230e189e7241b96dcbfb41ceabedf7cfd14ac.zip |
Add polymorphic option to model generator
For instance,
$ rails g model Product supplier:references{polymorphic}
generate model with `belongs_to :supplier, polymorphic: true` association and appropriate migration.
Also fix model_generator_test.rb#L196 and #L201
Diffstat (limited to 'railties/lib')
-rw-r--r-- | railties/lib/rails/generators/generated_attribute.rb | 14 |
1 files changed, 12 insertions, 2 deletions
diff --git a/railties/lib/rails/generators/generated_attribute.rb b/railties/lib/rails/generators/generated_attribute.rb index 25d0161e4c..d480fc12b5 100644 --- a/railties/lib/rails/generators/generated_attribute.rb +++ b/railties/lib/rails/generators/generated_attribute.rb @@ -35,7 +35,7 @@ module Rails private - # parse possible attribute options like :limit for string/text/binary/integer or :precision/:scale for decimals + # parse possible attribute options like :limit for string/text/binary/integer, :precision/:scale for decimals or :polymorphic for references/belongs_to # when declaring options curly brackets should be used def parse_type_and_options(type) case type @@ -43,6 +43,8 @@ module Rails return $1, :limit => $2.to_i when /decimal\{(\d+)[,.-](\d+)\}/ return :decimal, :precision => $1.to_i, :scale => $2.to_i + when /(references|belongs_to)\{polymorphic\}/ + return $1, :polymorphic => true else return type, {} end @@ -92,13 +94,21 @@ module Rails end def index_name - reference? ? "#{name}_id" : name + if reference? + polymorphic? ? %w(id type).map { |t| "#{name}_#{t}" } : "#{name}_id" + else + name + end end def reference? self.class.reference?(type) end + def polymorphic? + self.attr_options.has_key?(:polymorphic) + end + def has_index? @has_index end |