noodall-form-builder 0.2.11 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Gemfile CHANGED
@@ -16,7 +16,8 @@ gem 'faker', '~> 0.3.1'
16
16
  gem 'defensio'
17
17
  gem 'email_spec'
18
18
  gem 'fastercsv'
19
-
19
+ gem 'system_timer'
20
+ gem 'bson_ext'
20
21
 
21
22
  if RUBY_VERSION < '1.9'
22
23
  gem "ruby-debug", ">= 0.10.3"
@@ -10,14 +10,16 @@ module Noodall
10
10
  @form = Noodall::Form.find(params[:form_id])
11
11
 
12
12
  @form_response = @form.responses.build(params[:form_response])
13
-
13
+
14
+ #If the form response hasn't been constructed properly (i.e. a random spam POST)
15
+ raise MongoMapper::DocumentNotFound, "Form response does not match form" unless @form_response.correct_fields?
16
+
14
17
  @form_response.ip = request.remote_ip
15
18
  @form_response.referrer = request.referer if @form_response.referrer.blank?
16
19
  @form_response.created_at = Time.zone.now
17
20
 
18
- respond_to do |format|
21
+ respond_to do |format|
19
22
  if @form_response.valid? and @form_response.save
20
-
21
23
  if @form_response.is_spam?
22
24
  logger.info "Form response was deemed to be spam: #{@form_response.inspect}"
23
25
  else
@@ -6,7 +6,7 @@ module Noodall
6
6
  key :name, String, :required => true
7
7
  key :label, String
8
8
  key :default, String
9
- key :required, Boolean, :default => false, :required => true
9
+ key :required, Boolean, :default => false
10
10
 
11
11
  embedded_in :form
12
12
 
@@ -12,7 +12,7 @@ module Noodall
12
12
 
13
13
  MANDATORY_FIELDS = ['Name','Email']
14
14
  many :fields, :class => Noodall::Field
15
- many :responses, :class => Noodall::FormResponse do
15
+ many :responses, :class => Noodall::FormResponse, :foreign_key => 'noodall_form_id' do
16
16
  def ham
17
17
  self.select {|r| r.spaminess < (self.class.defensio_config['spam_threshold'] || 0.75)}
18
18
  end
@@ -21,6 +21,13 @@ module Noodall
21
21
  def required_fields
22
22
  self.form.fields.select{ |f| f.required? }
23
23
  end
24
+
25
+ def correct_fields?
26
+ self.form.fields.each do |f|
27
+ return false unless self.respond_to?(f.name.downcase.to_sym)
28
+ end
29
+ return true
30
+ end
24
31
 
25
32
  def approve!
26
33
  self.approved = true
@@ -53,12 +60,13 @@ module Noodall
53
60
  def check_for_spam
54
61
  if self.defensio_signature.blank?
55
62
  status, response = self.class.defensio.post_document(self.defensio_attributes)
56
- return unless status == 200
63
+ return true unless status == 200
57
64
 
58
65
  self.defensio_signature = response['signature']
59
66
  self.spaminess = response['spaminess']
60
67
  self.approved = response['allow']
61
68
  end
69
+ return true
62
70
  end
63
71
 
64
72
  def self.defensio
@@ -91,20 +99,11 @@ module Noodall
91
99
  validate :custom_validation
92
100
 
93
101
  def custom_validation
94
- return if required_fields.nil? || !self.new_record?
102
+ return true if required_fields.nil? || !self.new_record?
95
103
  required_fields.each do |field|
96
- self.add_error(field.underscored_name.to_sym, "can't be empty") if self.send(field.underscored_name).blank?
97
- end
98
- end
99
-
100
- def method_missing(method)
101
- # If the form doesn't have a field that matches this method, act normally. Otherwise, return nil to show the field is empty.
102
- if form.fields.select{|f| f.underscored_name.to_sym == method}.empty?
103
- super
104
- else
105
- return nil
104
+ self.errors.add(field.underscored_name.to_sym, "can't be empty") if self.send(field.underscored_name).blank?
106
105
  end
106
+ return true if self.errors.empty?
107
107
  end
108
-
109
108
  end
110
109
  end
@@ -5,7 +5,7 @@
5
5
  <%= error_messages_for :object => form_response %>
6
6
  <% for field in form.fields %>
7
7
  <div class="form-wrap">
8
- <%= render :partial => "noodall/form_responses/fields/#{field._type.gsub(/^.*::/, '').downcase}", :locals => {:form_response => form_response, :f => f, :field => field} %>
8
+ <%= render :partial => "noodall/form_responses/fields/#{field_type(field)}", :locals => {:form_response => form_response, :f => f, :field => field} %>
9
9
  </div>
10
10
  <% end %>
11
11
 
@@ -36,9 +36,12 @@ Feature: Form Module
36
36
 
37
37
  Then the email address of the form should receive an email detailing the information submitted
38
38
  And they should receive an email confirming the request has been sent
39
-
40
-
41
-
39
+
40
+ Scenario: Bad data is POSTed
41
+ Given content exists with a form added via the contact module
42
+ Then some random fields are POSTed by a spam bot
43
+ #todo - better way to structure this feature?
44
+
42
45
  Scenario: Validation
43
46
  Given content exists with a form added via the contact module
44
47
  When a website visitor visits the content
@@ -65,7 +65,7 @@ When /^I click "([^\"]*)" on the forms row in the Form List$/ do |arg1|
65
65
  end
66
66
 
67
67
  Then /^I should receive a CSV file containing all the responses to that form$/ do
68
- CSV.parse(page.body).should have(10).things # 9 rows plus header
68
+ CSV.parse(page.source).should have(10).things # 9 rows plus header
69
69
  end
70
70
 
71
71
 
@@ -0,0 +1,5 @@
1
+ When /^some random fields are POSTed by a spam bot$/ do
2
+ lambda {
3
+ post noodall_form_form_responses_path(@_form), {:test => '123'}
4
+ }.should raise_error(MongoMapper::DocumentNotFound, "Form response does not match form")
5
+ end
@@ -1,5 +1,5 @@
1
1
  module Noodall
2
2
  module FormBuilder
3
- VERSION = "0.2.11"
3
+ VERSION = "0.3.0"
4
4
  end
5
5
  end
@@ -1 +0,0 @@
1
- require 'noodall/dragonfly'
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: noodall-form-builder
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 19
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 11
10
- version: 0.2.11
8
+ - 3
9
+ - 0
10
+ version: 0.3.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Steve England
@@ -17,7 +17,7 @@ autorequire:
17
17
  bindir: bin
18
18
  cert_chain: []
19
19
 
20
- date: 2011-07-18 00:00:00 +01:00
20
+ date: 2011-07-12 00:00:00 +01:00
21
21
  default_executable:
22
22
  dependencies:
23
23
  - !ruby/object:Gem::Dependency
@@ -116,6 +116,7 @@ files:
116
116
  - features/step_definitions/email_steps.rb
117
117
  - features/step_definitions/form_builder_steps.rb
118
118
  - features/step_definitions/sign_in_steps.rb
119
+ - features/step_definitions/spam_steps.rb
119
120
  - features/step_definitions/web_steps.rb
120
121
  - features/support/defensio_mock.rb
121
122
  - features/support/env.rb