module ActiveRecord::Acts::Vanity::InstanceMethods

Public Instance Methods

clear_vanity() click to toggle source
# File lib/acts_as_dasherize_vanity/active_record/acts/vanity.rb, line 59
def clear_vanity
  self[dasherize_vanity_column.to_sym] = nil
end
reset_vanity() click to toggle source
# File lib/acts_as_dasherize_vanity/active_record/acts/vanity.rb, line 62
def reset_vanity
  self.clear_vanity
  self.set_vanity
end
set_vanity() click to toggle source
# File lib/acts_as_dasherize_vanity/active_record/acts/vanity.rb, line 32
def set_vanity
  if self.send("#{initial_column}").present?
    tmp = self.send("#{initial_column}").downcase.tr(" ", "_").dasherize
    bp = self.class.where("#{dasherize_vanity_column} = '#{tmp}'").first
    if bp && bp.id != self.id
      tstr = false
      tstr_num = 1
      while tstr == false
        if bp = self.class.where("#{dasherize_vanity_column} = '#{tmp}-#{tstr_num.to_s}'").first and bp.id != self.id
          tstr_num += 1
        else
          tstr = true
          self[dasherize_vanity_column.to_sym] = tmp+"-"+tstr_num.to_s
        end
      end
    else  
      if self.send("#{initial_column}").present?
        self[dasherize_vanity_column.to_sym] = self.send("#{initial_column}").downcase.tr(" ", "_").dasherize  
      else  
        self[dasherize_vanity_column.to_sym] = nil
      end
    end
  end
end
vanity() click to toggle source
# File lib/acts_as_dasherize_vanity/active_record/acts/vanity.rb, line 56
def vanity
  self.class.columns_hash.include?('dasherize_vanity') ? read_attribute(:dasherize_vanity) : self.send(dasherize_vanity_column)
end