Last active
August 29, 2015 14:08
-
-
Save Sylvain303/46ead55ee995d56788e4 to your computer and use it in GitHub Desktop.
change name ActiveRecord can be used in migration to rename required records, here for redmine
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
def change(type, old_name, new_name) | |
case type | |
when :role | |
klass = Role | |
field = :name | |
when :group | |
klass = Group | |
field = :lastname | |
else | |
raise "unsupported type:#{type}" | |
end | |
old_o = klass.where(field.to_s => old_name).first | |
new_o = klass.where(field.to_s => new_name).first | |
assign = field.to_s + '=' | |
case | |
when (new_o and old_o.nil?) | |
return :allready | |
when (new_o.nil? and old_o) | |
old_o.method(assign.to_sym).call(new_name) | |
old_o.save | |
when (new_o.nil? and old_o.nil?) | |
raise "no old record" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment