Sha256: 596593ba36c0215f43e31d3283068841150d4157d07554687b94715bd90f5ca3

Contents?: true

Size: 1.64 KB

Versions: 25

Compression:

Stored size: 1.64 KB

Contents

# The Tag model. This model is automatically generated and added to your app if you run the tagging generator included with has_many_polymorphs.

class Tag < ActiveRecord::Base

  DELIMITER = " " # Controls how to split and join tagnames from strings. You may need to change the <tt>validates_format_of parameters</tt> if you change this.

  # If database speed becomes an issue, you could remove these validations and rescue the ActiveRecord database constraint errors instead.
  validates_presence_of :name
  validates_uniqueness_of :name, :case_sensitive => false
  
  # Change this validation if you need more complex tag names.
  validates_format_of :name, :with => /^[a-zA-Z0-9\_\-]+$/, :message => "can not contain special characters"
  
  # Set up the polymorphic relationship.
  has_many_polymorphs :taggables, 
    :from => [<%= taggable_models.join(", ") %>], 
    :through => :taggings, 
    :dependent => :destroy,
<% if options[:self_referential] -%>    :as => :<%= parent_association_name -%>,
<% end -%>
    :skip_duplicates => false, 
    :parent_extend => proc {
      # Defined on the taggable models, not on Tag itself. Return the tagnames associated with this record as a string.
      def to_s
        self.map(&:name).sort.join(Tag::DELIMITER)
      end
    }
    
  # Callback to strip extra spaces from the tagname before saving it. If you allow tags to be renamed later, you might want to use the <tt>before_save</tt> callback instead.
  def before_create 
    self.name = name.downcase.strip.squeeze(" ")
  end
  
  # Tag::Error class. Raised by ActiveRecord::Base::TaggingExtensions if something goes wrong.
  class Error < StandardError
  end
    
end

Version data entries

25 entries across 25 versions & 4 rubygems

Version Path
radiant-tags-extension-1.6.10 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
johnsbrn-has_many_polymorphs-2.13.1 generators/tagging/templates/tag.rb
johnsbrn-has_many_polymorphs-2.13.3 generators/tagging/templates/tag.rb
johnsbrn-has_many_polymorphs-2.13.4 generators/tagging/templates/tag.rb
johnsbrn-has_many_polymorphs-2.13 generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.9 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.8 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.7 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.6 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.5 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.4 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.3 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.2 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.1 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.6.0 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
radiant-tags-extension-1.5.1 vendor/plugins/has_many_polymorphs/generators/tagging/templates/tag.rb
has_many_polymorphs-2.10 generators/tagging/templates/tag.rb
has_many_polymorphs-2.2 generators/tagging/templates/tag.rb
has_many_polymorphs-2.12 generators/tagging/templates/tag.rb
has_many_polymorphs-2.11 generators/tagging/templates/tag.rb