Created
August 27, 2010 01:54
-
-
Save moretea/552627 to your computer and use it in GitHub Desktop.
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 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