Skip to content

Instantly share code, notes, and snippets.

@moretea
Created August 27, 2010 01:54
Show Gist options
  • Select an option

  • Save moretea/552627 to your computer and use it in GitHub Desktop.

Select an option

Save moretea/552627 to your computer and use it in GitHub Desktop.
def plugins=(next_plugin_names)
next_plugin_names.sort!
current_plugin_names = self.plugins.collect { |p| p.name }.sort!
# Nothing changed? Do nothing
return if current_plugin_names == next_plugin_names
# Add extra plugins, if there are more assigned, compared to before
next_plugin_names.each do |new|
if current_plugin_names.member? new
current_plugin_names.delete new
else
new_position = UserPlugin.select("user_plugins.position").where(:user_id => self).order('user_plugins.position DESC').limit(1).first.position + 1
self.plugins.create(:name => new, :position => new_position)
end
end
# Remove plugins not referenced
current_plugin_names.each do |remove|
UserPlugin.where(:name => remove).where(:user_id => self).destroy_all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment