spanish_inquisition 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/README.textile +3 -1
- data/lib/spanish_inquisition.rb +5 -0
- data/lib/spanish_inquisition/attributes.rb +51 -0
- data/lib/spanish_inquisition/presenter.rb +52 -8
- data/lib/spanish_inquisition/presenters/question_presenter.rb +26 -0
- data/lib/spanish_inquisition/question.rb +7 -0
- data/spanish_inquisition.gemspec +1 -1
- data/spec/spanish_inquisition/attributes_spec.rb +56 -0
- metadata +11 -15
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 4ed41d5bda96b89d370071a7673504bb4d866977
|
4
|
+
data.tar.gz: 63240287af68ad7ea31e3cf77263b71bd896e205
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 8db82ae49a050f989964efb852890d64be8e7fcf3412bcacc471207ee1085ec90d3d5a4959a2fe84744391f0f7962cbc8d09eeed9594391c810c9744c2436a14
|
7
|
+
data.tar.gz: b80da4adcd29e5dc3602aede46ccc4ad2c3f3b308c3806242427c808ff102b338d18b7cb910265cfbaf07cc0ad0e45c34b4840ba4933a218ad9b134ad4e4a760
|
data/README.textile
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
-
h1.
|
1
|
+
h1. Spanish Inquisition
|
2
2
|
|
3
3
|
Nobody expects the Spanish Inquisition!
|
4
4
|
|
5
|
+
This is a simple and opinionated survey library for Rails. It generates the form using Formtastic, and it does not deal with persistence - the surveys are plain Ruby objects, not ActiveRecord objects. It fulfills our needs at Inspire9 nicely - and perhaps it's what you're looking for too.
|
6
|
+
|
5
7
|
h2. Installation
|
6
8
|
|
7
9
|
It's a gem - so, like any other gem, add it to your app's Gemfile:
|
data/lib/spanish_inquisition.rb
CHANGED
@@ -14,9 +14,14 @@ module SpanishInquisition
|
|
14
14
|
@surveys ||= {}
|
15
15
|
end
|
16
16
|
|
17
|
+
def self.defaults
|
18
|
+
@defaults ||= {}
|
19
|
+
end
|
20
|
+
|
17
21
|
module Presenters; end
|
18
22
|
end
|
19
23
|
|
24
|
+
require 'spanish_inquisition/attributes'
|
20
25
|
require 'spanish_inquisition/page'
|
21
26
|
require 'spanish_inquisition/presenter'
|
22
27
|
require 'spanish_inquisition/presenters/page_presenter'
|
@@ -0,0 +1,51 @@
|
|
1
|
+
require 'date'
|
2
|
+
|
3
|
+
class SpanishInquisition::Attributes
|
4
|
+
def initialize(values, types)
|
5
|
+
@values = translate values, types
|
6
|
+
end
|
7
|
+
|
8
|
+
def [](key)
|
9
|
+
@values[key]
|
10
|
+
end
|
11
|
+
|
12
|
+
def to_hash
|
13
|
+
@values
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def translate(values, types)
|
19
|
+
types.keys.inject({}) do |hash, key|
|
20
|
+
case types[key]
|
21
|
+
when :date
|
22
|
+
hash[key] = translate_date key, values
|
23
|
+
when :location
|
24
|
+
hash[key] = values[key]
|
25
|
+
hash[:lat] = values[:lat]
|
26
|
+
hash[:lng] = values[:lng]
|
27
|
+
hash[:state] = values[:state]
|
28
|
+
hash[:country] = values[:country]
|
29
|
+
else
|
30
|
+
hash[key] = values[key]
|
31
|
+
end
|
32
|
+
|
33
|
+
hash
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def translate_date(key, values)
|
38
|
+
case values[key]
|
39
|
+
when String
|
40
|
+
Date.parse values[key]
|
41
|
+
when NilClass
|
42
|
+
parts = ["#{key}(1i)".to_sym, "#{key}(2i)".to_sym, "#{key}(3i)".to_sym]
|
43
|
+
|
44
|
+
unless parts.any? { |part| values[part].nil? || values[part].empty? }
|
45
|
+
Date.new *parts.collect { |part| values[part].to_i }
|
46
|
+
end
|
47
|
+
else
|
48
|
+
values[key]
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -6,6 +6,9 @@ class SpanishInquisition::Presenter
|
|
6
6
|
include ActiveModel::Conversion
|
7
7
|
include ActiveModel::Validations
|
8
8
|
|
9
|
+
LOCATION_IDENTIFIERS = [:lat, :lng, :state, :country]
|
10
|
+
REQUIRED_LOCATION_IDENTIFIERS = [:lat, :lng]
|
11
|
+
|
9
12
|
validate :required_answers
|
10
13
|
|
11
14
|
delegate :heading, :description, to: :survey
|
@@ -16,16 +19,18 @@ class SpanishInquisition::Presenter
|
|
16
19
|
|
17
20
|
def initialize(identifier, attributes = {})
|
18
21
|
@survey = SpanishInquisition.surveys[identifier]
|
19
|
-
@attributes =
|
22
|
+
@attributes = SpanishInquisition::Attributes.new(
|
23
|
+
attributes || {}, question_types
|
24
|
+
)
|
20
25
|
|
21
26
|
raise SpanishInquisition::SurveyNotFound if @survey.nil?
|
22
27
|
end
|
23
28
|
|
24
29
|
def answers
|
25
30
|
questions.inject({}) do |hash, question|
|
26
|
-
|
27
|
-
hash[
|
28
|
-
end
|
31
|
+
question_identifiers(question).each do |identifier|
|
32
|
+
hash[identifier] = attributes[identifier]
|
33
|
+
end if question.capture?(attributes)
|
29
34
|
|
30
35
|
hash
|
31
36
|
end
|
@@ -45,9 +50,15 @@ class SpanishInquisition::Presenter
|
|
45
50
|
}.join.html_safe
|
46
51
|
end
|
47
52
|
|
53
|
+
def respond_to?(method, include_all = false)
|
54
|
+
survey.question_identifiers.include?(method) ||
|
55
|
+
(LOCATION_IDENTIFIERS.include?(method) && location_question?) ||
|
56
|
+
super
|
57
|
+
end
|
58
|
+
|
48
59
|
def to_json
|
49
60
|
questions.collect { |question|
|
50
|
-
SpanishInquisition::Presenters::QuestionPresenter.new(nil, question).to_json(attributes)
|
61
|
+
SpanishInquisition::Presenters::QuestionPresenter.new(nil, question).to_json(attributes.to_hash)
|
51
62
|
}
|
52
63
|
end
|
53
64
|
|
@@ -58,11 +69,22 @@ class SpanishInquisition::Presenter
|
|
58
69
|
def invalid_question?(question)
|
59
70
|
question.capture?(attributes) &&
|
60
71
|
question.required? &&
|
61
|
-
|
72
|
+
question_identifiers(question, true).any? { |identifier|
|
73
|
+
attributes[identifier].blank?
|
74
|
+
}
|
75
|
+
end
|
76
|
+
|
77
|
+
def location_question?
|
78
|
+
survey.pages.collect(&:questions).flatten.any? { |question|
|
79
|
+
question.style == :location
|
80
|
+
}
|
62
81
|
end
|
63
82
|
|
64
83
|
def method_missing(method, *arguments, &block)
|
65
|
-
return attributes[method] if survey.question_identifiers.include?
|
84
|
+
return attributes[method] if survey.question_identifiers.include? method
|
85
|
+
if LOCATION_IDENTIFIERS.include?(method) && location_question?
|
86
|
+
return attributes[method]
|
87
|
+
end
|
66
88
|
|
67
89
|
super
|
68
90
|
end
|
@@ -71,11 +93,33 @@ class SpanishInquisition::Presenter
|
|
71
93
|
@questions ||= survey.pages.collect(&:questions).flatten
|
72
94
|
end
|
73
95
|
|
96
|
+
def question_identifiers(question, required = false)
|
97
|
+
return [question.identifier] unless question.style == :location
|
98
|
+
|
99
|
+
if required
|
100
|
+
[question.identifier] + REQUIRED_LOCATION_IDENTIFIERS
|
101
|
+
else
|
102
|
+
[question.identifier] + LOCATION_IDENTIFIERS
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def question_types
|
107
|
+
questions.inject({}) do |hash, question|
|
108
|
+
hash[question.identifier] = question.type
|
109
|
+
hash
|
110
|
+
end
|
111
|
+
end
|
112
|
+
|
74
113
|
def required_answers
|
75
114
|
questions.each do |question|
|
76
115
|
next unless invalid_question?(question)
|
77
116
|
|
78
|
-
|
117
|
+
case question.style
|
118
|
+
when :location
|
119
|
+
errors.add question.identifier, 'must be selected from the prompted options'
|
120
|
+
else
|
121
|
+
errors.add question.identifier, 'must be answered'
|
122
|
+
end
|
79
123
|
end
|
80
124
|
end
|
81
125
|
end
|
@@ -7,10 +7,32 @@ class SpanishInquisition::Presenters::QuestionPresenter
|
|
7
7
|
case question.style
|
8
8
|
when :one
|
9
9
|
form.input question.identifier, as: :radio, collection: question.answers, label: question.text
|
10
|
+
when :many
|
11
|
+
form.input question.identifier, as: :check_boxes, collection: question.answers, label: question.text
|
12
|
+
when :file
|
13
|
+
form.input question.identifier, as: :file, label: question.text
|
14
|
+
when :password
|
15
|
+
form.input question.identifier, as: :password, label: question.text
|
16
|
+
when :date_select
|
17
|
+
form.input question.identifier,
|
18
|
+
{as: :date_select, label: question.text}.merge(date_options)
|
19
|
+
when :datetime_select
|
20
|
+
form.input question.identifier,
|
21
|
+
{as: :datetime_select, label: question.text}.merge(date_options)
|
22
|
+
when :time_select
|
23
|
+
form.input question.identifier, as: :time_select, label: question.text
|
10
24
|
when :string
|
11
25
|
form.input question.identifier, as: :string, label: question.text
|
12
26
|
when :text
|
13
27
|
form.input question.identifier, as: :text, label: question.text
|
28
|
+
when :location
|
29
|
+
[
|
30
|
+
form.input(question.identifier, as: :string, label: question.text, input_html: { class: 'location' }),
|
31
|
+
form.input(:lat, as: :hidden, input_html: {class: 'lat'}),
|
32
|
+
form.input(:lng, as: :hidden, input_html: {class: 'lng'}),
|
33
|
+
form.input(:state, as: :hidden, input_html: {class: 'state'}),
|
34
|
+
form.input(:country, as: :hidden, input_html: {class: 'country'})
|
35
|
+
].join(' ').html_safe
|
14
36
|
else
|
15
37
|
raise "Unknown question style: #{question.style}"
|
16
38
|
end
|
@@ -28,4 +50,8 @@ class SpanishInquisition::Presenters::QuestionPresenter
|
|
28
50
|
private
|
29
51
|
|
30
52
|
attr_reader :form, :question
|
53
|
+
|
54
|
+
def date_options
|
55
|
+
SpanishInquisition.defaults.dup
|
56
|
+
end
|
31
57
|
end
|
data/spanish_inquisition.gemspec
CHANGED
@@ -0,0 +1,56 @@
|
|
1
|
+
module SpanishInquisition
|
2
|
+
end
|
3
|
+
|
4
|
+
require './lib/spanish_inquisition/attributes'
|
5
|
+
|
6
|
+
describe SpanishInquisition::Attributes do
|
7
|
+
describe '#[]' do
|
8
|
+
it "returns provided attribute values" do
|
9
|
+
SpanishInquisition::Attributes.new(
|
10
|
+
{:foo => 'bar'}, {:foo => :string}
|
11
|
+
)[:foo].should == 'bar'
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns dates for date params" do
|
15
|
+
SpanishInquisition::Attributes.new({
|
16
|
+
:"created_on(1i)" => '2013',
|
17
|
+
:"created_on(2i)" => '6',
|
18
|
+
:"created_on(3i)" => '11'
|
19
|
+
}, {:created_on => :date})[:created_on].should == Date.new(2013, 6, 11)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "ignores date params if any are blank" do
|
23
|
+
SpanishInquisition::Attributes.new({
|
24
|
+
:"created_on(1i)" => '2013',
|
25
|
+
:"created_on(2i)" => '6',
|
26
|
+
:"created_on(3i)" => ''
|
27
|
+
}, {:created_on => :date})[:created_on].should be_nil
|
28
|
+
|
29
|
+
SpanishInquisition::Attributes.new({
|
30
|
+
:"created_on(1i)" => '2013',
|
31
|
+
:"created_on(2i)" => '',
|
32
|
+
:"created_on(3i)" => '11'
|
33
|
+
}, {:created_on => :date})[:created_on].should be_nil
|
34
|
+
|
35
|
+
SpanishInquisition::Attributes.new({
|
36
|
+
:"created_on(1i)" => '',
|
37
|
+
:"created_on(2i)" => '6',
|
38
|
+
:"created_on(3i)" => '11'
|
39
|
+
}, {:created_on => :date})[:created_on].should be_nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it "translates string date values" do
|
43
|
+
SpanishInquisition::Attributes.new(
|
44
|
+
{:created_on => '2013-06-11'},
|
45
|
+
{:created_on => :date}
|
46
|
+
)[:created_on].should == Date.new(2013, 6, 11)
|
47
|
+
end
|
48
|
+
|
49
|
+
it "accepts date values" do
|
50
|
+
SpanishInquisition::Attributes.new(
|
51
|
+
{:created_on => Date.new(2013, 6, 11)},
|
52
|
+
{:created_on => :date}
|
53
|
+
)[:created_on].should == Date.new(2013, 6, 11)
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,20 +1,18 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: spanish_inquisition
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
5
|
-
prerelease:
|
4
|
+
version: 1.1.0
|
6
5
|
platform: ruby
|
7
6
|
authors:
|
8
7
|
- Pat Allan
|
9
8
|
autorequire:
|
10
9
|
bindir: bin
|
11
10
|
cert_chain: []
|
12
|
-
date:
|
11
|
+
date: 2014-02-07 00:00:00.000000000 Z
|
13
12
|
dependencies:
|
14
13
|
- !ruby/object:Gem::Dependency
|
15
14
|
name: activemodel
|
16
15
|
requirement: !ruby/object:Gem::Requirement
|
17
|
-
none: false
|
18
16
|
requirements:
|
19
17
|
- - ~>
|
20
18
|
- !ruby/object:Gem::Version
|
@@ -22,7 +20,6 @@ dependencies:
|
|
22
20
|
type: :runtime
|
23
21
|
prerelease: false
|
24
22
|
version_requirements: !ruby/object:Gem::Requirement
|
25
|
-
none: false
|
26
23
|
requirements:
|
27
24
|
- - ~>
|
28
25
|
- !ruby/object:Gem::Version
|
@@ -30,7 +27,6 @@ dependencies:
|
|
30
27
|
- !ruby/object:Gem::Dependency
|
31
28
|
name: formtastic
|
32
29
|
requirement: !ruby/object:Gem::Requirement
|
33
|
-
none: false
|
34
30
|
requirements:
|
35
31
|
- - ~>
|
36
32
|
- !ruby/object:Gem::Version
|
@@ -38,7 +34,6 @@ dependencies:
|
|
38
34
|
type: :runtime
|
39
35
|
prerelease: false
|
40
36
|
version_requirements: !ruby/object:Gem::Requirement
|
41
|
-
none: false
|
42
37
|
requirements:
|
43
38
|
- - ~>
|
44
39
|
- !ruby/object:Gem::Version
|
@@ -57,6 +52,7 @@ files:
|
|
57
52
|
- README.textile
|
58
53
|
- Rakefile
|
59
54
|
- lib/spanish_inquisition.rb
|
55
|
+
- lib/spanish_inquisition/attributes.rb
|
60
56
|
- lib/spanish_inquisition/page.rb
|
61
57
|
- lib/spanish_inquisition/presenter.rb
|
62
58
|
- lib/spanish_inquisition/presenters/page_presenter.rb
|
@@ -65,29 +61,29 @@ files:
|
|
65
61
|
- lib/spanish_inquisition/survey.rb
|
66
62
|
- lib/spanish_inquisition/survey_not_found.rb
|
67
63
|
- spanish_inquisition.gemspec
|
64
|
+
- spec/spanish_inquisition/attributes_spec.rb
|
68
65
|
homepage: https://github.com/inspire9/spanish_inquisition
|
69
66
|
licenses: []
|
67
|
+
metadata: {}
|
70
68
|
post_install_message:
|
71
69
|
rdoc_options: []
|
72
70
|
require_paths:
|
73
71
|
- lib
|
74
72
|
required_ruby_version: !ruby/object:Gem::Requirement
|
75
|
-
none: false
|
76
73
|
requirements:
|
77
|
-
- -
|
74
|
+
- - '>='
|
78
75
|
- !ruby/object:Gem::Version
|
79
76
|
version: '0'
|
80
77
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
81
|
-
none: false
|
82
78
|
requirements:
|
83
|
-
- -
|
79
|
+
- - '>='
|
84
80
|
- !ruby/object:Gem::Version
|
85
81
|
version: '0'
|
86
82
|
requirements: []
|
87
83
|
rubyforge_project:
|
88
|
-
rubygems_version: 1.
|
84
|
+
rubygems_version: 2.1.11
|
89
85
|
signing_key:
|
90
|
-
specification_version:
|
86
|
+
specification_version: 4
|
91
87
|
summary: Simple Ruby survey DSL
|
92
|
-
test_files:
|
93
|
-
|
88
|
+
test_files:
|
89
|
+
- spec/spanish_inquisition/attributes_spec.rb
|