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 +2 -1
- data/app/controllers/noodall/form_responses_controller.rb +5 -3
- data/app/models/noodall/field.rb +1 -1
- data/app/models/noodall/form.rb +1 -1
- data/app/models/noodall/form_response.rb +12 -13
- data/app/views/noodall/form_responses/_form.html.erb +1 -1
- data/features/form_module.feature +6 -3
- data/features/step_definitions/form_builder_steps.rb +1 -1
- data/features/step_definitions/spam_steps.rb +5 -0
- data/lib/noodall/form_builder/version.rb +1 -1
- data/spec/dummy/config/initializers/dragonfly.rb +0 -1
- metadata +6 -5
data/Gemfile
CHANGED
@@ -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
|
data/app/models/noodall/field.rb
CHANGED
data/app/models/noodall/form.rb
CHANGED
@@ -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.
|
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
|
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.
|
68
|
+
CSV.parse(page.source).should have(10).things # 9 rows plus header
|
69
69
|
end
|
70
70
|
|
71
71
|
|
@@ -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:
|
4
|
+
hash: 19
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
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-
|
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
|