Stringex::ActsAsUrl::ClassMethods

Public Instance Methods

acts_as_url(attribute, options = {}) click to toggle source

Creates a callback to automatically create an url-friendly representation of the attribute argument. Example:

  act_as_url :title

will use the string contents of the title attribute to create the permalink. Note: you can also use a non-database-backed method to supply the string contents for the permalink. Just use that method’s name as the argument as you would an attribute.

The default attribute acts_as_url uses to save the permalink is url but this can be changed in the options hash. Available options are:

:url_attribute

The name of the attribute to use for storing the generated url string. Default is :url

:scope

The name of model attribute to scope unique urls to. There is no default here.

:only_when_blank

If true, the url generation will only happen when :url_attribute is blank. Default is false (meaning url generation will happen always)

:sync_url

If set to true, the url field will be updated when changes are made to the attribute it is based on. Default is false.

    # File lib/stringex/acts_as_url.rb, line 29
29:       def acts_as_url(attribute, options = {})
30:         cattr_accessor :attribute_to_urlify
31:         cattr_accessor :scope_for_url
32:         cattr_accessor :url_attribute # The attribute on the DB
33:         cattr_accessor :only_when_blank
34:         cattr_accessor :duplicate_count_separator
35:         cattr_accessor :allow_slash
36:         cattr_accessor :allow_duplicates
37:         cattr_accessor :url_limit
38: 
39:         if options[:sync_url]
40:           before_validation(:ensure_unique_url)
41:         else
42:           if defined?(ActiveModel::Callbacks)
43:             before_validation(:ensure_unique_url, :on => :create)
44:           else
45:             before_validation_on_create(:ensure_unique_url)
46:           end
47:         end
48: 
49:         self.attribute_to_urlify = attribute
50:         self.scope_for_url = options[:scope]
51:         self.url_attribute = options[:url_attribute] || "url"
52:         self.only_when_blank = options[:only_when_blank] || false
53:         self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
54:         self.allow_slash = options[:allow_slash] || false
55:         self.allow_duplicates = options[:allow_duplicates] || false
56:         self.url_limit = options[:limit] || nil
57: 
58:         class_eval           def #{url_attribute}            if !new_record? && errors[attribute_to_urlify].present?              self.class.find(id).send(url_attribute)            else              read_attribute(url_attribute)            end          end
59:       end
initialize_urls() click to toggle source

Initialize the url fields for the records that need it. Designed for people who add acts_as_url support once there’s already development/production data they’d like to keep around.

Note: This method can get very expensive, very fast. If you’re planning on using this on a large selection, you will get much better results writing your own version with using pagination.

    # File lib/stringex/acts_as_url.rb, line 76
76:       def initialize_urls
77:         find(:all, :conditions => {self.url_attribute => nil}).each do |instance|
78:           instance.send :ensure_unique_url
79:           instance.save
80:         end
81:       end

Disabled; run with --debug to generate this.

[Validate]

Generated with the Darkfish Rdoc Generator 1.1.6.