Module: HasHandleFallback::InstanceMethods

Defined in:
lib/has_handle_fallback.rb

Instance Method Summary collapse

Instance Method Details

#handleObject



99
100
101
102
# File 'lib/has_handle_fallback.rb', line 99

def handle
  raw = read_attribute self.class.has_handle_fallback_options[:handle_column]
  raw.present? ? raw : handle_fallback
end

#handle_fallbackObject



104
105
106
107
108
109
110
# File 'lib/has_handle_fallback.rb', line 104

def handle_fallback
  column = self.class.has_handle_fallback_options[:fallback_column]
  if column and fallback = read_attribute(column)
    fallback = fallback.split('@').first if fallback.to_s.include? '@'
    HasHandleFallback.str2handle fallback
  end
end

#handle_is_validObject



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/has_handle_fallback.rb', line 59

def handle_is_valid
  raw = read_attribute self.class.has_handle_fallback_options[:handle_column]
  
  # allow nils but not blanks
  if raw.blank? and (!raw.nil? or has_handle_fallback_options[:required])
    errors.add self.class.has_handle_fallback_options[:handle_column], "can't be blank"
  end
  
  # trapdoor for nil handles
  return if raw.nil?
  
  # don't allow all integer handles, because it looks like a database record id
  if raw =~ HasHandleFallback::RECORD_ID_REGEXP
    errors.add self.class.has_handle_fallback_options[:handle_column], "can't be entirely composed of integers"
  end
  
  # validates_format_of :handle, :with => HasHandleFallback::REGEXP, :allow_nil => true
  if has_handle_fallback_options[:validates_format] and raw !~ HasHandleFallback::REGEXP
    errors.add self.class.has_handle_fallback_options[:handle_column], "contains invalid characters"
  end
  
  # validates_length_of :handle, :in => HasHandleFallback::LENGTH_RANGE, :allow_nil => true
  unless HasHandleFallback::LENGTH_RANGE.include? raw.length
    errors.add self.class.has_handle_fallback_options[:handle_column], "must be #{HasHandleFallback::LENGTH_RANGE} characters in length"
  end
  
  if ValidatesDecencyOf.indecent? raw
    errors.add self.class.has_handle_fallback_options[:handle_column], "is indecent"
  end
  
  # validates_uniqueness_of :handle, :case_sensitive => false, :allow_nil => true
  if new_record? and self.class.exists? [ "#{self.class.quoted_table_name}.`#{self.class.has_handle_fallback_options[:handle_column]}` LIKE ?", raw ]
    errors.add self.class.has_handle_fallback_options[:handle_column], "isn't unique"
  end
  
  if !new_record? and self.class.exists? [ "#{self.class.quoted_table_name}.`#{self.class.primary_key}` <> ? AND #{self.class.quoted_table_name}.`#{self.class.has_handle_fallback_options[:handle_column]}` LIKE ?", id, raw ]
    errors.add self.class.has_handle_fallback_options[:handle_column], "isn't unique"
  end
end

#to_paramObject



112
113
114
115
116
117
118
119
120
121
# File 'lib/has_handle_fallback.rb', line 112

def to_param
  raw = read_attribute self.class.has_handle_fallback_options[:handle_column]
  if new_record?
    ''
  elsif raw.blank? or changes.include?(self.class.has_handle_fallback_options[:handle_column])
    id.to_s
  else
    raw
  end
end