Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Add migration_version variable to migration generator for rails 5 com…
Browse files Browse the repository at this point in the history
…patibility (#2470)

* Add migration_version variable to migration generator for rails 5 compatibility

* Update syntax to satisfy hound
  • Loading branch information
jackellenberger authored and sidraval committed Jan 30, 2018
1 parent 76575c9 commit c0cf4b6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 9 additions & 1 deletion lib/generators/paperclip/paperclip_generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ def self.source_root
end

def generate_migration
migration_template "paperclip_migration.rb.erb", "db/migrate/#{migration_file_name}"
migration_template("paperclip_migration.rb.erb",
"db/migrate/#{migration_file_name}",
migration_version: migration_version)
end

def migration_name
Expand All @@ -27,4 +29,10 @@ def migration_file_name
def migration_class_name
migration_name.camelize
end

def migration_version
if Rails.version.start_with? "5"
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
end
end
end
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class <%= migration_class_name %> < ActiveRecord::Migration
class <%= migration_class_name %> < ActiveRecord::Migration<%= migration_version %>
def self.up
change_table :<%= table_name %> do |t|
<% attachment_names.each do |attachment| -%>
Expand Down

1 comment on commit c0cf4b6

@an-nasir
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This does not seems to work, I am on Rails 5.1 and created a migration for user to add avatar, it still created
class AddAttachmentAvatarToUsers < ActiveRecord::Migration
No rails version injected
In my project rails console showed these results for
Rails.version => 5.1.5

Rails.version.start_with? "5" => true
though
"[#{Rails::VERSION::MAJOR}.#{Rails::VERSION::MINOR}]"
This printed exact output as expected like "[5.1]"

Please sign in to comment.