Class: Stringex::ActsAsUrl::Configuration

### Home Classes Methods

### In Files

- lib/stringex/acts_as_url.rb

### Parent

Object

### Methods

- ::new
- #get_base_url!
- #get_conditions!
- #handle_duplicate_urls!

### Files

- MIT-LICENSE

### Class/Module Index

Quicksearch

- Stringex
- Stringex::ActsAsUrl
- Stringex::ActsAsUrl::ClassMethods
- Stringex::ActsAsUrl::Configuration

No matching classes.

# Stringex::ActsAsUrl::Configuration

### Attributes

allow_duplicates[RW]
allow_slash[RW]
attribute_to_urlify[RW]
duplicate_count_separator[RW]
exclude[RW]
only_when_blank[RW]
scope_for_url[RW]
sync_url[RW]
url_attribute[RW]
url_limit[RW]

### Public Class Methods

new(klass, options = {}) click to toggle source

    # File lib/stringex/acts_as_url.rb, line 11
    def initialize(klass, options = {})
      self.allow_slash = options[:allow_slash]
      self.allow_duplicates = options[:allow_duplicates]
      self.attribute_to_urlify = options[:attribute]
      self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
      self.exclude = options[:exclude] || []
      self.only_when_blank = options[:only_when_blank]
      self.scope_for_url = options[:scope]
      self.sync_url = options[:sync_url]
      self.url_attribute = options[:url_attribute] || "url"
      self.url_limit = options[:limit]
    end

### Public Instance Methods

get_base_url!(instance) click to toggle source

    # File lib/stringex/acts_as_url.rb, line 24
    def get_base_url!(instance)
      base_url = instance.send(url_attribute)
      if base_url.blank? || !only_when_blank
        root = instance.send(attribute_to_urlify).to_s
        base_url = root.to_url(:allow_slash => allow_slash, :limit => url_limit, :exclude => exclude)
      end
      instance.instance_variable_set "@acts_as_url_base_url", base_url
    end

get_conditions!(instance) click to toggle source

    # File lib/stringex/acts_as_url.rb, line 33
    def get_conditions!(instance)
      conditions = ["#{url_attribute} LIKE ?", instance.instance_variable_get("@acts_as_url_base_url") + '%']
      unless instance.new_record?
        conditions.first << " and id != ?"
        conditions << instance.id
      end
      if scope_for_url
        conditions.first << " and #{scope_for_url} = ?"
        conditions << instance.send(scope_for_url)
      end
      conditions
    end

handle_duplicate_urls!(instance) click to toggle source

    # File lib/stringex/acts_as_url.rb, line 46
    def handle_duplicate_urls!(instance)
      return if allow_duplicates

      base_url = instance.instance_variable_get("@acts_as_url_base_url")
      url_owners = instance.class.unscoped.find(:all, :conditions => get_conditions!(instance))
      if url_owners.any?{|owner| owner.send(url_attribute) == base_url}
        separator = duplicate_count_separator
        n = 1
        while url_owners.any?{|owner| owner.send(url_attribute) == "#{base_url}#{separator}#{n}"}
          n = n.succ
        end
        instance.send :write_attribute, url_attribute, "#{base_url}#{separator}#{n}"
      end
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.