Module: Stringex::ActsAsUrl::ClassMethods
- Defined in:
- lib/stringex/acts_as_url.rb
Overview
:doc:
Instance Method Summary collapse
-
#acts_as_url(attribute, options = {}) ⇒ Object
Creates a callback to automatically create an url-friendly representation of the
attribute
argument. -
#initialize_urls ⇒ Object
Initialize the url fields for the records that need it.
Instance Method Details
#acts_as_url(attribute, options = {}) ⇒ Object
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:
:allow_slash
-
If true, allow the generated url to contain slashes. Default is false.
:allow_duplicates
-
If true, allow duplicate urls instead of appending numbers to differentiate between urls. Default is false.
:duplicate_count_separator
-
String to use when forcing unique urls from non-unique strings. Default is “-”.
: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.
: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
. :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.
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/stringex/acts_as_url.rb', line 95 def acts_as_url(attribute, = {}) cattr_accessor :acts_as_url_configuration [:attribute] = attribute self.acts_as_url_configuration = ActsAsUrl::Configuration.new(self, ) if acts_as_url_configuration.sync_url before_validation(:ensure_unique_url) else if defined?(ActiveModel::Callbacks) before_validation(:ensure_unique_url, :on => :create) else before_validation_on_create(:ensure_unique_url) end end class_eval <<-"END" def #{acts_as_url_configuration.url_attribute} if !new_record? && errors[acts_as_url_configuration.attribute_to_urlify].present? self.class.find(id).send(acts_as_url_configuration.url_attribute) else read_attribute(acts_as_url_configuration.url_attribute) end end END end |
#initialize_urls ⇒ Object
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.
129 130 131 132 133 134 |
# File 'lib/stringex/acts_as_url.rb', line 129 def initialize_urls find_each(:conditions => {acts_as_url_configuration.url_attribute => nil}) do |instance| instance.send :ensure_unique_url instance.save end end |