module URLAttributes::ActiveRecord

Public Instance Methods

url_attribute(attribute_name, before: :save) click to toggle source
# File lib/url_attributes/extensions/active_record.rb, line 16
def url_attribute(attribute_name, before: :save)
  validates attribute_name, :url => true, :if => "#{attribute_name}.present?"

  callback_method = :"before_#{before}"
    
  # Add "http://" to the URL if it's not already there.
  send(callback_method) do |record|
    url = record[attribute_name]
    if url.present? && !(url =~ /\A\s*https?:\/\//)
      url = "http://#{url.try(:strip)}"
    end
    record[attribute_name] = url
  end

end