29: def acts_as_url(attribute, options = {})
30: cattr_accessor :attribute_to_urlify
31: cattr_accessor :scope_for_url
32: cattr_accessor :url_attribute
33: cattr_accessor :only_when_blank
34: cattr_accessor :duplicate_count_separator
35: cattr_accessor :allow_slash
36:
37: if options[:sync_url]
38: before_validation(:ensure_unique_url)
39: else
40: if defined?(ActiveModel::Callbacks)
41: before_validation(:ensure_unique_url, :on => :create)
42: else
43: before_validation_on_create(:ensure_unique_url)
44: end
45: end
46:
47: self.attribute_to_urlify = attribute
48: self.scope_for_url = options[:scope]
49: self.url_attribute = options[:url_attribute] || "url"
50: self.only_when_blank = options[:only_when_blank] || false
51: self.duplicate_count_separator = options[:duplicate_count_separator] || "-"
52: self.allow_slash = options[:allow_slash] || false
53:
54: class_eval "def \#{url_attribute}\nif !new_record? && errors[attribute_to_urlify].present?\nself.class.find(id).send(url_attribute)\nelse\nread_attribute(url_attribute)\nend\nend\n"
55: end