Created
December 28, 2014 10:09
-
-
Save jtsagata/62d5ec5bab7301a413bf to your computer and use it in GitHub Desktop.
Add column definitions to a migration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| rails g model user name:string avatar:attached | |
| create_table :users do |t| | |
| t.string :name | |
| t.string :avatar_identifier | |
| t.string :avatar_extension | |
| t.integer :avatar_size | |
| end | |
| For more info look | |
| http://stackoverflow.com/questions/5202008/ruby-on-rails-custom-migration-generator |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # rails g model user name:string avatar:attached | |
| # | |
| # create_table :users do |t| | |
| # t.string :name | |
| # t.string :avatar_identifier | |
| # t.string :avatar_extension | |
| # t.integer :avatar_size | |
| # end | |
| # | |
| # For more info look | |
| # http://stackoverflow.com/questions/5202008/ruby-on-rails-custom-migration-generator | |
| require 'active_support' | |
| require 'active_record' | |
| class ExtraMigrationMethods | |
| module SchemaDefinitions | |
| module ExtraMethods | |
| def attachment(*args) | |
| options = args.extract_options! | |
| args.each do |col| | |
| column("#{col}_identifier", :string, options) | |
| column("#{col}_extension", :string, options) | |
| column("#{col}_size", :integer, options) | |
| end | |
| end | |
| def slug(*args) | |
| options = args.extract_options! | |
| options.merge!(null: false, index:true) | |
| args.each do |col| | |
| column("#{col}", :string, options) | |
| end | |
| end | |
| end | |
| def self.load! | |
| ::ActiveRecord::ConnectionAdapters::TableDefinition.class_eval { include ExtraMigrationMethods::SchemaDefinitions::ExtraMethods } | |
| end | |
| end | |
| end | |
| ActiveSupport.on_load :active_record do | |
| ExtraMigrationMethods::SchemaDefinitions.load! | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment