Class: Sequel::Model

Inherits:
Object show all
Defined in:
lib/clevic/sequel_length_validation.rb,
lib/clevic/sequel_clevic.rb,
lib/clevic/sequel_ar_adapter.rb

Overview

This validates that strings going into varchar fields display meaningful warnings instead of incomprenensible native RDBMS errors.

Defined Under Namespace

Modules: Associations Classes: Errors

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.adaptorObject

return a class containing various db methods. Not sure if this is the right way to do it, but at least this way the model class namespace doesn’t get filled up with crud



161
162
163
# File 'lib/clevic/sequel_ar_adapter.rb', line 161

def adaptor
  @adaptor ||= Clevic::SequelAdaptor.new( self )
end

.belongs_to(name, options = nil, &block) ⇒ Object



136
137
138
139
140
141
142
143
# File 'lib/clevic/sequel_ar_adapter.rb', line 136

def belongs_to( name, options = nil, &block )
  # work around possible Sequel bug
  if options.nil?
    many_to_one( name, &block )
  else
    many_to_one( name, translate_options( options ), &block )
  end
end

.columns_hashObject



154
155
156
# File 'lib/clevic/sequel_ar_adapter.rb', line 154

def columns_hash
  db_schema
end

.has_many(name, options = nil, &block) ⇒ Object



145
146
147
148
149
150
151
152
# File 'lib/clevic/sequel_ar_adapter.rb', line 145

def has_many( name, options = nil, &block )
  # work around possible Sequel bug
  if options.nil?
    one_to_many( name, &block )
  else
    one_to_many( name, translate_options( options ), &block )
  end
end

.translate_options(options) ⇒ Object

for translating class methods for relations



127
128
129
130
131
132
133
134
# File 'lib/clevic/sequel_ar_adapter.rb', line 127

def translate_options( options )
  options[:key] = options[:foreign_key].andand.to_sym
  options.delete( :foreign_key )

  options[:class] = options[:class_name].andand.to_sym
  options.delete( :class_name )
  options
end

.varchar_columnsObject



9
10
11
12
13
14
# File 'lib/clevic/sequel_length_validation.rb', line 9

def self.varchar_columns
  @varchar_columns ||= columns.select do |col|
    db_type = db_schema[col][:db_type]
    db_type =~ /var/ && db_type =~ /char/
  end
end

Instance Method Details

#validateObject



16
17
18
19
20
21
22
# File 'lib/clevic/sequel_length_validation.rb', line 16

def validate
  super
  self.class.varchar_columns.each do |column|
    limit = self.class.meta[column].limit
    errors.add( column, "is longer than #{limit}" ) if self[column] && self[column].length > limit
  end
end