Module: Mongoid::ListField

Defined in:
lib/workarea/ext/mongoid/list_field.rb

Instance Method Summary collapse

Instance Method Details

#list_field(name) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# File 'lib/workarea/ext/mongoid/list_field.rb', line 3

def list_field(name)
  self.class_eval <<-RUBY
    def #{name}_list
      #{name}.join(', ') if #{name}.present?
    end

    def #{name}_list=(val)
      if val.blank?
        self.#{name} = []
      else
        self.#{name} = val.split(',').map(&:strip).delete_if(&:blank?)
      end
    end
  RUBY
end