Created
November 2, 2009 00:14
-
-
Save anonymous/223815 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
| class Tag < ActiveRecord::Base | |
| belongs_to :taggable, :polymorphic => true | |
| validates_uniqueness_of :tag, :scope => [:taggable_type, :taggable_id] | |
| before_save :create_objects_for_each_tags | |
| after_save :return_objects | |
| def return_objects | |
| if @objects | |
| return @objects | |
| else | |
| return self | |
| end | |
| end | |
| attr_writer :tags | |
| def create_objects_for_each_tags | |
| if @tags.is_a? Array | |
| self.tag = @tags.pop | |
| @objects = @tags.map do |tag_name| | |
| clone = self.clone | |
| return clone.update_attributes!(:tag => tag_name) | |
| end | |
| elsif @tags.is_a? String | |
| self.tag = @tags | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment