Module: Behi::Trimify::ClassMethods

Defined in:
lib/trimify.rb

Instance Method Summary collapse

Instance Method Details

#trimify(*args) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/trimify.rb', line 9

def trimify(*args)
  return if args.empty?
  return unless table_exists?

  options = { :nilify => true }
  options.merge!(args.pop) if args.last.kind_of? Hash

  unless self.included_modules.include?(Trimify::InstanceMethods)
    include Trimify::InstanceMethods
    write_inheritable_attribute :trimify_attributes, {}
    class_inheritable_reader :trimify_attributes
  end

  attributes = args.map(&:to_sym)
  col_options = {}
  columns = content_columns.reject { |c| c.type != :string }.map { |c| c.name.to_sym }.select { |c| args.include?(c) }
  columns.each { |col| col_options[col] = options }
  write_inheritable_hash :trimify_attributes, col_options

  class_eval "before_validation :trimify"
end