module Stringex::ActsAsUrl::ActsAsUrlClassMethods
Attributes
Public Instance Methods
Source
# File lib/stringex/acts_as_url.rb 56 def acts_as_url(attribute, options = {}) 57 class_eval do 58 class << self 59 attr_accessor :acts_as_url_configuration 60 end 61 62 define_method :acts_as_url_configuration do 63 klass = self.class 64 while klass.acts_as_url_configuration.nil? 65 klass = klass.superclass 66 end 67 klass.acts_as_url_configuration 68 end 69 end 70 71 options[:attribute_to_urlify] = attribute 72 self.acts_as_url_configuration = Stringex::Configuration::ActsAsUrl.new(options) 73 74 acts_as_url_configuration.adapter.create_callbacks! self 75 end
Creates a callback to automatically create an url-friendly representation of the attribute
argument. Example:
acts_as_url :title
will use the string contents of the title
attribute to create the permalink. <strong>Note:</strong> 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:
:adapter
-
If specified, will indicate what ORM adapter to use. Default functionality is to use the first available adapter. This should work for most cases unless you are using multiple ORMs in a single project.
:allow_slash
-
If true, allows the generated url to contain slashes. Default is false.
:allow_duplicates
-
If true, allows duplicate urls instead of appending numbers to differentiate between urls. Default is false. See note on
:scope
. :duplicate_count_separator
-
String to use when forcing unique urls from non-unique strings. Default is “-”.
:duplicate_sequence
-
Supply an enumerator to generate the values used to generate unique urls (when
:allow_duplicates
is false). By default, generates positive integers in sequence from 1. <strong>Note:</strong> The sequence is restarted for each record (by calling rewind). :force_downcase
-
If false, allows generated url to contain uppercased letters. Default is false.
:exclude_list
-
List of complete strings that should not be transformed by
acts_as_url
. Default is empty. :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). :scope
-
The name of model attribute to scope unique urls to. There is no default here. <strong>Note:</strong> this will automatically act as if
:allow_duplicates
is set to true. :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.
:url_attribute
-
The name of the attribute to use for storing the generated url string. Default is
:url
. :limit
-
The maximum size a generated url should be. <strong>Note:</strong> this does not include the characters needed to enforce uniqueness on duplicate urls. Default is nil.
Source
# File lib/stringex/acts_as_url.rb 80 def included(base = nil, &block) 81 super 82 83 if base 84 base.send :include, Stringex::ActsAsUrl::ActsAsUrlInstanceMethods 85 base.send :extend, Stringex::ActsAsUrl::ActsAsUrlClassMethods 86 end 87 end
Some ORMs function as mixins not base classes and need to have a hook to reinclude and re-extend ActsAsUrl
methods
Source
# File lib/stringex/acts_as_url.rb 96 def initialize_urls 97 acts_as_url_configuration.adapter.initialize_urls! self 98 end
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.