Sha256: ffe5fd51fae2339b3f20ba23120284c929bb6ccbfb77fe21e588a18afa2326c0

Contents?: true

Size: 1.42 KB

Versions: 7

Compression:

Stored size: 1.42 KB

Contents

module Noodall
  class Form
    include MongoMapper::Document
    plugin MongoMapper::Plugins::MultiParameterAttributes
    plugin Noodall::GlobalUpdateTime

    key :title, String, :required => true
    key :description, String
    key :email, String, :format => /^[-a-z0-9_+\.]+\@([-a-z0-9]+\.)+[a-z0-9]{2,4}$/i
    key :thank_you_message, :default => 'Thank you for getting in contact.'
    key :thank_you_email, :default => 'Thank you for getting in contact.'

    MANDATORY_FIELDS = ['Name','Email']
    many :fields, :class => Noodall::Field
    many :responses, :class => Noodall::FormResponse, :foreign_key => 'noodall_form_id' do
      def ham
        self.select {|r| r.spaminess < (self.class.defensio_config['spam_threshold'] || 0.75)}
      end
      def spam
        self.select {|r| r.spaminess >= (self.class.defensio_config['spam_threshold'] || 0.75)}
      end
    end

    before_save :create_mandatory_fields!

    timestamps!

    validates_associated :fields, :message => "have not had a name completed"

    def boolean_fields
      self.fields.select{|f| f.class == Noodall::CheckBox }
    end

    def required_fields
      self.fields.select{|f| f.required }
    end

    def create_mandatory_fields!
      MANDATORY_FIELDS.each do |mf|
        if fields.blank? or fields.select{|f| f.name == mf }.empty?
          self.fields << Noodall::TextField.new(:name => mf, :required => true)
        end
      end
    end

  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
noodall-form-builder-0.4.2 app/models/noodall/form.rb
noodall-form-builder-0.4.1 app/models/noodall/form.rb
noodall-form-builder-0.4.0 app/models/noodall/form.rb
noodall-form-builder-0.3.3 app/models/noodall/form.rb
noodall-form-builder-0.3.2 app/models/noodall/form.rb
noodall-form-builder-0.3.1 app/models/noodall/form.rb
noodall-form-builder-0.3.0 app/models/noodall/form.rb