questionnaire_engine 0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (45) hide show
  1. checksums.yaml +7 -0
  2. data/Gemfile +9 -0
  3. data/MIT-LICENSE +20 -0
  4. data/README.md +234 -0
  5. data/Rakefile +30 -0
  6. data/app/models/survey/answer.rb +50 -0
  7. data/app/models/survey/attempt.rb +74 -0
  8. data/app/models/survey/option.rb +44 -0
  9. data/app/models/survey/options_type.rb +35 -0
  10. data/app/models/survey/predefined_value.rb +23 -0
  11. data/app/models/survey/question.rb +52 -0
  12. data/app/models/survey/questions_type.rb +27 -0
  13. data/app/models/survey/section.rb +47 -0
  14. data/app/models/survey/survey.rb +63 -0
  15. data/config/locales/en.yml +54 -0
  16. data/config/locales/pt-PT.yml +48 -0
  17. data/config/locales/pt.yml +48 -0
  18. data/lib/generators/survey/install_generator.rb +35 -0
  19. data/lib/generators/survey/survey_generator.rb +77 -0
  20. data/lib/generators/templates/active_admin.rb +85 -0
  21. data/lib/generators/templates/attempts_plain.rb +36 -0
  22. data/lib/generators/templates/attempts_views/_form.html.erb +61 -0
  23. data/lib/generators/templates/attempts_views/new.html.erb +10 -0
  24. data/lib/generators/templates/helper.rb +46 -0
  25. data/lib/generators/templates/migration.rb +56 -0
  26. data/lib/generators/templates/migration_add_head_number_to_options_table.rb +6 -0
  27. data/lib/generators/templates/migration_add_mandatory_to_questions_table.rb +6 -0
  28. data/lib/generators/templates/migration_add_types_to_questions_and_options.rb +13 -0
  29. data/lib/generators/templates/migration_create_predefined_values_table.rb +14 -0
  30. data/lib/generators/templates/migration_section.rb +22 -0
  31. data/lib/generators/templates/migration_update_survey_tables.rb +22 -0
  32. data/lib/generators/templates/rails_admin.rb +8 -0
  33. data/lib/generators/templates/survey_plain.rb +60 -0
  34. data/lib/generators/templates/survey_views/_form.html.erb +72 -0
  35. data/lib/generators/templates/survey_views/_option_fields.html.erb +26 -0
  36. data/lib/generators/templates/survey_views/_question_fields.html.erb +42 -0
  37. data/lib/generators/templates/survey_views/_section_fields.html.erb +29 -0
  38. data/lib/generators/templates/survey_views/edit.html.erb +2 -0
  39. data/lib/generators/templates/survey_views/index.html.erb +14 -0
  40. data/lib/generators/templates/survey_views/new.html.erb +2 -0
  41. data/lib/survey.rb +5 -0
  42. data/lib/survey/active_record.rb +15 -0
  43. data/lib/survey/engine.rb +6 -0
  44. data/lib/survey/version.rb +3 -0
  45. metadata +142 -0
@@ -0,0 +1,77 @@
1
+ module Survey
2
+ class SurveyGenerator < Rails::Generators::Base
3
+
4
+ source_root File.expand_path("../../templates", __FILE__)
5
+
6
+ TEMPLATES = ["active_admin", "rails_admin", "plain", "routes"]
7
+
8
+ argument :arguments,
9
+ :type => :array,
10
+ :default => [],
11
+ :banner => "< #{TEMPLATES.join("|")} > [options]"
12
+
13
+ def create_resolution
14
+ strategy = arguments.first
15
+ if TEMPLATES.include? strategy
16
+ send("generate_#{strategy}_resolution")
17
+ success_message(strategy)
18
+ else
19
+ error_message(strategy)
20
+ end
21
+ end
22
+
23
+ private
24
+
25
+ def generate_active_admin_resolution
26
+ copy_file "active_admin.rb", "app/admin/survey.rb"
27
+ end
28
+
29
+ def generate_rails_admin_resolution
30
+ copy_file "rails_admin.rb", "config/initializers/survey_rails_admin.rb"
31
+ end
32
+
33
+ def generate_plain_resolution
34
+ scope = get_scope
35
+ template "survey_plain.rb", "app/controllers/#{scope}/surveys_controller.rb"
36
+ template "attempts_plain.rb", "app/controllers/#{scope}/attempts_controller.rb"
37
+ template "helper.rb", "app/helpers/#{scope}/surveys_helper.rb"
38
+ directory "survey_views", "app/views/#{scope}/surveys", :recursive => true
39
+ directory "attempts_views", "app/views/#{scope}/attempts", :recursive => true
40
+ generate_routes_for(scope)
41
+ end
42
+
43
+ def generate_routes_resolution
44
+ generate_routes_for(get_scope)
45
+ end
46
+
47
+ # Error Handlers
48
+ def error_message(argument)
49
+ error_message = <<-CONTENT
50
+ This Resolution: '#{argument}' is not supported by Survey:
51
+ We only support Active Admin, Refinery and Active Scaffold
52
+ CONTENT
53
+ say error_message, :red
54
+ end
55
+
56
+ def success_message(argument)
57
+ say "Generation of #{argument.capitalize} Template Complete :) enjoy Survey", :green
58
+ end
59
+
60
+ def generate_routes_for(namespace, conditional=nil)
61
+ content = <<-CONTENT
62
+
63
+ namespace :#{namespace} do
64
+ resources :surveys
65
+ resources :attempts, :only => [:new, :create]
66
+ end
67
+ CONTENT
68
+ inject_into_file "config/routes.rb", "\n#{content}",
69
+ :after => "#{Rails.application.class.to_s}.routes.draw do"
70
+ end
71
+
72
+ def get_scope
73
+ arguments.size == 1 ? "admin" : arguments[1].split(":").last
74
+ end
75
+
76
+ end
77
+ end
@@ -0,0 +1,85 @@
1
+ ActiveAdmin.register Survey::Survey do
2
+ menu :label => I18n.t("surveys")
3
+
4
+ filter :name,
5
+ :as => :select,
6
+ :collection => proc {
7
+ Survey::Survey.select("distinct(name)").collect { |c|
8
+ [c.name, c.name]
9
+ }
10
+ }
11
+ filter :active,
12
+ :as => :select,
13
+ :collection => ["true", "false"]
14
+
15
+ filter :created_at
16
+
17
+ index do
18
+ column :name
19
+ column :description
20
+ column :active
21
+ column :attempts_number
22
+ column :finished
23
+ column :created_at
24
+ default_actions
25
+ end
26
+
27
+ form do |f|
28
+ f.inputs I18n.t("survey_details") do
29
+ f.input :name
30
+ f.input :locale_name
31
+ f.input :description
32
+ f.input :locale_description
33
+ f.input :active, :as => :select, :collection => ["true", "false"]
34
+ f.input :attempts_number
35
+ end
36
+
37
+ f.inputs I18n.t("sections") do
38
+ f.has_many :sections do |s|
39
+ s.input :head_number
40
+ s.input :locale_head_number
41
+ s.input :name
42
+ s.input :locale_name
43
+ s.input :description
44
+ s.input :locale_description
45
+
46
+ s.has_many :questions do |q|
47
+ q.input :head_number
48
+ q.input :locale_head_number
49
+ q.input :text
50
+ q.input :locale_text
51
+ q.input :description
52
+ q.input :locale_description
53
+ q.input :questions_type_id, :as => :select, :collection => Survey::QuestionsType.questions_types_title
54
+ q.input :mandatory
55
+
56
+ q.inputs I18n.t("predefined_values") do
57
+ q.has_many :predefined_values do |p|
58
+ p.input :head_number
59
+ p.input :name
60
+ p.input :locale_name
61
+ end
62
+ end
63
+
64
+ q.has_many :options do |a|
65
+ a.input :head_number
66
+ a.input :text
67
+ a.input :locale_text
68
+ a.input :options_type_id, :as => :select, :collection => Survey::OptionsType.options_types_title
69
+ a.input :correct
70
+ end
71
+ end
72
+ end
73
+ end
74
+
75
+ f.buttons
76
+ end
77
+
78
+ if Rails::VERSION::MAJOR >= 4
79
+ controller do
80
+ def permitted_params
81
+ params.permit!
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,36 @@
1
+ class <%= get_scope.capitalize %>::AttemptsController < ApplicationController
2
+
3
+ helper "<%= get_scope%>/surveys"
4
+
5
+ def new
6
+ @survey = Survey::Survey.active.last
7
+ @attempt = @survey.attempts.new
8
+ @attempt.answers.build
9
+ @participant = current_user # you have to decide what to do here
10
+ end
11
+
12
+ def create
13
+ @survey = Survey::Survey.active.last
14
+ @attempt = @survey.attempts.new(attempt_params)
15
+ @attempt.participant = current_user # you have to decide what to do here
16
+ if @attempt.valid? and @attempt.save
17
+ redirect_to view_context.new_attempt_path, alert: I18n.t("attempts_controller.#{action_name}")
18
+ else
19
+ flash.now[:error] = @attempt.errors.full_messages.join(', ')
20
+ render :action => :new
21
+ end
22
+ end
23
+
24
+ #######
25
+ private
26
+ #######
27
+
28
+ # Rails 4 Strong Params
29
+ def attempt_params
30
+ if Rails::VERSION::MAJOR < 4
31
+ params[:survey_attempt]
32
+ else
33
+ params.require(:survey_attempt).permit(answers_attributes: [:question_id, :option_id, :option_text, :option_number, :predefined_value_id])
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,61 @@
1
+ <h1><%= @survey.name %></h1>
2
+ <p><%= @survey.description %></p>
3
+ <%= form_for(@attempt, :url => attempt_scope(@attempt)) do |f| %>
4
+ <%= f.fields_for :answers do |builder| %>
5
+ <ul>
6
+ <% seq = 0 %>
7
+ <% @survey.sections.each do |section| %>
8
+ <p><span><%= "#{section.head_number} : " if section.head_number %></span><%= section.name%></p>
9
+ <p><%= section.description if section.description %></p>
10
+ <% section.questions.each do |question| %>
11
+ <% seq += 1 %>
12
+ <li>
13
+ <p><span><%= "#{question.head_number} : " if question.head_number %></span><%= question.text %></p>
14
+ <p><%= question.description if question.description %></p>
15
+ <% question.options.each do |option| %>
16
+
17
+ <% if [Survey::OptionsType.multi_choices, Survey::OptionsType.multi_choices_with_text, Survey::OptionsType.multi_choices_with_number].include? option.options_type_id %>
18
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][question_id]", question.id %>
19
+ <%= check_box_tag "survey_attempt[answers_attributes][#{seq}][option_id]", option.id %>
20
+
21
+ <% if option.options_type_id == Survey::OptionsType.multi_choices_with_text %>
22
+ <%= text_field_tag "survey_attempt[answers_attributes][#{seq}][option_text]", "" %>
23
+ <% elsif option.options_type_id == Survey::OptionsType.multi_choices_with_number %>
24
+ <%= number_field_tag "survey_attempt[answers_attributes][#{seq}][option_number]", "", class: "form-control" %>
25
+ <% end %>
26
+ <% seq += 1 %>
27
+ <% elsif [Survey::OptionsType.single_choice, Survey::OptionsType.single_choice_with_text].include? option.options_type_id %>
28
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][question_id]", question.id %>
29
+ <%= radio_button_tag "survey_attempt[answers_attributes][#{seq}][option_id]", option.id %>
30
+
31
+ <% if option.options_type_id == Survey::OptionsType.single_choice_with_text %>
32
+ <%= text_field_tag "survey_attempt[answers_attributes][#{seq}][option_text]", "" %>
33
+ <% elsif option.options_type_id == Survey::OptionsType.single_choice_with_number %>
34
+ <%= number_field_tag "survey_attempt[answers_attributes][#{seq}][option_number]", "", class: "form-control" %>
35
+ <% end %>
36
+ <% elsif option.options_type_id == Survey::OptionsType.number %>
37
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][question_id]", question.id %>
38
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][option_id]", option.id %>
39
+ <%= number_field_tag "survey_attempt[answers_attributes][#{seq}][option_number]", "", :style => "width: 40px;" %>
40
+ <% seq += 1 %>
41
+ <% elsif option.options_type_id == Survey::OptionsType.text %>
42
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][question_id]", question.id %>
43
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][option_id]", option.id %>
44
+ <%= text_field_tag "survey_attempt[answers_attributes][#{seq}][option_text]", "" %>
45
+ <% seq += 1 %>
46
+ <% elsif option.options_type_id == Survey::OptionsType.large_text %>
47
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][question_id]", question.id %>
48
+ <%= hidden_field_tag "survey_attempt[answers_attributes][#{seq}][option_id]", option.id %>
49
+ <%= text_area_tag "survey_attempt[answers_attributes][#{seq}][option_text]", "" %>
50
+ <% seq += 1 %>
51
+ <% end %>
52
+
53
+ <%= option.text %> <br/>
54
+ <% end -%>
55
+ </li>
56
+ <% end -%>
57
+ <% end -%>
58
+ </ul>
59
+ <% end -%>
60
+ <%= f.submit "Submit" %>
61
+ <% end -%>
@@ -0,0 +1,10 @@
1
+ <h3><%= flash[:alert]%></h3>
2
+ <h3><%= flash[:error]%></h3>
3
+
4
+ <% if @survey.avaliable_for_participant?(@participant) %>
5
+ <%= render 'form' %>
6
+ <% else %>
7
+ <p>
8
+ <%= @participant.name %> spent all the possible attempts to answer this Survey
9
+ </p>
10
+ <% end -%>
@@ -0,0 +1,46 @@
1
+ module <%= get_scope.capitalize %>
2
+ module SurveysHelper
3
+ def link_to_remove_field(name, f)
4
+ f.hidden_field(:_destroy) +
5
+ link_to_function(raw(name), "removeField(this)", :id =>"remove-attach")
6
+ end
7
+
8
+ def new_attempt_path
9
+ new_<%= get_scope %>_attempt_path
10
+ end
11
+
12
+ def new_survey_path
13
+ new_<%= get_scope %>_survey_path
14
+ end
15
+
16
+ def edit_survey_path(resource)
17
+ edit_<%= get_scope %>_survey_path(resource)
18
+ end
19
+
20
+ def attempt_scope(resource)
21
+ if action_name =~ /new|create/
22
+ <%= get_scope %>_attempts_path(resource)
23
+ elsif action_name =~ /edit|update/
24
+ <%= get_scope %>_attempt_path(resource)
25
+ end
26
+ end
27
+
28
+ def survey_scope(resource)
29
+ if action_name =~ /new|create/
30
+ <%= get_scope %>_surveys_path(resource)
31
+ elsif action_name =~ /edit|update/
32
+ <%= get_scope %>_survey_path(resource)
33
+ end
34
+ end
35
+
36
+ def link_to_add_field(name, f, association)
37
+ new_object = f.object.class.reflect_on_association(association).klass.new
38
+ fields = f.fields_for(association, new_object,:child_index => "new_#{association}") do |builder|
39
+ render(association.to_s.singularize + "_fields", :f => builder)
40
+ end
41
+ link_to_function(name, "addField(this, \"#{association}\", \"#{escape_javascript(fields)}\")",
42
+ :id=>"add-attach",
43
+ :class=>"btn btn-small btn-info")
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,56 @@
1
+ class CreateSurvey < ActiveRecord::Migration
2
+ def self.up
3
+
4
+ # survey surveys logic
5
+ create_table :survey_surveys do |t|
6
+ t.string :name
7
+ t.text :description
8
+ t.integer :attempts_number, :default => 0
9
+ t.boolean :finished, :default => false
10
+ t.boolean :active, :default => true
11
+
12
+ t.timestamps
13
+ end
14
+
15
+ create_table :survey_questions do |t|
16
+ t.integer :survey_id
17
+ t.string :text
18
+
19
+ t.timestamps
20
+ end
21
+
22
+ create_table :survey_options do |t|
23
+ t.integer :question_id
24
+ t.integer :weight, :default => 0
25
+ t.string :text
26
+ t.boolean :correct
27
+
28
+ t.timestamps
29
+ end
30
+
31
+ # survey answer logic
32
+ create_table :survey_attempts do |t|
33
+ t.belongs_to :participant, :polymorphic => true
34
+ t.integer :survey_id
35
+ t.boolean :winner
36
+ t.integer :score
37
+ end
38
+
39
+ create_table :survey_answers do |t|
40
+ t.integer :attempt_id
41
+ t.integer :question_id
42
+ t.integer :option_id
43
+ t.boolean :correct
44
+ t.timestamps
45
+ end
46
+ end
47
+
48
+ def self.down
49
+ drop_table :survey_surveys
50
+ drop_table :survey_questions
51
+ drop_table :survey_options
52
+
53
+ drop_table :survey_attempts
54
+ drop_table :survey_answers
55
+ end
56
+ end
@@ -0,0 +1,6 @@
1
+ class AddHeadNumberToOptionsTable < ActiveRecord::Migration
2
+ def change
3
+ #Survey Options table
4
+ add_column :survey_options, :head_number, :string
5
+ end
6
+ end
@@ -0,0 +1,6 @@
1
+ class AddMandatoryToQuestionsTable < ActiveRecord::Migration
2
+ def change
3
+ #Survey Questions table
4
+ add_column :survey_questions, :mandatory, :boolean, :default => false
5
+ end
6
+ end
@@ -0,0 +1,13 @@
1
+ class AddTypesToQuestionsAndOptions < ActiveRecord::Migration
2
+ def change
3
+ #Survey Questions table
4
+ add_column :survey_questions, :questions_type_id, :integer
5
+
6
+ #Survey Options table
7
+ add_column :survey_options, :options_type_id, :integer
8
+
9
+ #Survey Answers table
10
+ add_column :survey_answers, :option_text, :text
11
+ add_column :survey_answers, :option_number, :integer
12
+ end
13
+ end
@@ -0,0 +1,14 @@
1
+ class CreatePredefinedValuesTable < ActiveRecord::Migration
2
+ def change
3
+ create_table :survey_predefined_values do |t|
4
+ t.string :head_number
5
+ t.string :name
6
+ t.string :locale_name
7
+ t.integer :question_id
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ add_column :survey_answers, :predefined_value_id, :integer
13
+ end
14
+ end
@@ -0,0 +1,22 @@
1
+ class CreateSections < ActiveRecord::Migration
2
+ def self.up
3
+ create_table :survey_sections do |t|
4
+ t.string :head_number
5
+ t.string :name
6
+ t.text :description
7
+ t.integer :survey_id
8
+
9
+ t.timestamps
10
+ end
11
+
12
+ remove_column :survey_questions, :survey_id
13
+ add_column :survey_questions, :section_id, :integer
14
+ end
15
+
16
+ def self.down
17
+ drop_table :survey_sections
18
+
19
+ remove_column :survey_questions, :section_id
20
+ add_column :survey_questions, :survey_id, :integer
21
+ end
22
+ end