Sha256: 6ce29608dfbe96790a74fd6ca270a3544b1361462e72e8d7a071df54b6bcf5dd

Contents?: true

Size: 1.08 KB

Versions: 1

Compression:

Stored size: 1.08 KB

Contents

module UrlField
  def self.included(base) 
    base.extend UrlFieldMethod
  end

  module UrlFieldMethod
    def url_field(*args)

      args.each do |field_name|
        before_validation :"clean_#{field_name}_url_field"                    # before_save :clean_url_url_field
      end

      class_eval do        
        
        args.each do |field_name|
          define_method(:"clean_#{field_name}_url_field") do              # def clean_url_url_field
            self.send("#{field_name}=", send("cleaned_#{field_name}"))    #   self.url = cleaned_url
          end                                                             # end
        end
        
        private

        args.each do |field_name|
          define_method("cleaned_#{field_name}") do
            return nil if send(field_name).nil? or send(field_name).blank?
            return "http://#{send(field_name)}" unless send(field_name).match(/https?:\/\/.*$/)
            send(field_name)
          end
        end
      end
    end
  end
end

if Object.const_defined?("ActiveRecord")
  ActiveRecord::Base.send(:include, UrlField)
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
url_field-0.0.2 lib/url_field.rb