Commit 96b8283eba259184375397902f72b95501ee89ac

Authored by Rodrigo Souto
1 parent 81752a5c

[custom-forms] Controllers

plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb 0 → 100644
... ... @@ -0,0 +1,145 @@
  1 +class CustomFormsPluginMyprofileController < MyProfileController
  2 + def index
  3 + @forms = CustomFormsPlugin::Form.from(profile)
  4 + end
  5 +
  6 + def create
  7 + @form = CustomFormsPlugin::Form.new(:profile => profile)
  8 + @fields = []
  9 + @empty_field = CustomFormsPlugin::Field.new
  10 + if request.post?
  11 + begin
  12 + @form.update_attributes!(params[:form])
  13 + params[:fields] = format_kind(params[:fields])
  14 + params[:fields] = format_choices(params[:fields])
  15 + params[:fields] = set_form_id(params[:fields], @form.id)
  16 + create_fields(new_fields(params))
  17 + session['notice'] = _('Form created')
  18 + redirect_to :action => 'index'
  19 + rescue Exception => exception
  20 + logger.error(exception.to_s)
  21 + session['notice'] = _('Form could not be created')
  22 + end
  23 + end
  24 + end
  25 +
  26 + def edit
  27 + @form = CustomFormsPlugin::Form.find(params[:id])
  28 + @fields = @form.fields
  29 + @empty_field = CustomFormsPlugin::TextField.new
  30 + if request.post?
  31 + begin
  32 + @form.update_attributes!(params[:form])
  33 + params[:fields] = format_kind(params[:fields])
  34 + params[:fields] = format_choices(params[:fields])
  35 + remove_fields(params, @form)
  36 + create_fields(new_fields(params))
  37 + update_fields(edited_fields(params))
  38 + session['notice'] = _('Form updated')
  39 + redirect_to :action => 'index'
  40 + rescue Exception => exception
  41 + logger.error(exception.to_s)
  42 + session['notice'] = _('Form could not be updated')
  43 + end
  44 + end
  45 + end
  46 +
  47 + def remove
  48 + @form = CustomFormsPlugin::Form.find(params[:id])
  49 + begin
  50 + @form.destroy
  51 + session[:notice] = _('Form removed')
  52 + rescue
  53 + session[:notice] = _('Form could not be removed')
  54 + end
  55 + redirect_to :action => 'index'
  56 + end
  57 +
  58 + def submissions
  59 + @form = CustomFormsPlugin::Form.find(params[:id])
  60 + @submissions = @form.submissions
  61 + end
  62 +
  63 + def show_submission
  64 + @submission = CustomFormsPlugin::Submission.find(params[:id])
  65 + @form = @submission.form
  66 + end
  67 +
  68 + private
  69 +
  70 + def new_fields(params)
  71 + result = params[:fields].map {|id, hash| hash.has_key?(:real_id) ? nil : hash}.compact
  72 + result.delete_if {|field| field[:name].blank?}
  73 + result
  74 + end
  75 +
  76 + def edited_fields(params)
  77 + params[:fields].map {|id, hash| hash.has_key?(:real_id) ? hash : nil}.compact
  78 + end
  79 +
  80 + def create_fields(fields)
  81 + fields.each do |field|
  82 + case field[:type]
  83 + when 'text_field'
  84 + CustomFormsPlugin::TextField.create!(field)
  85 + when 'select_field'
  86 + CustomFormsPlugin::SelectField.create!(field)
  87 + else
  88 + CustomFormsPlugin::Field.create!(field)
  89 + end
  90 + end
  91 + end
  92 +
  93 + def update_fields(fields)
  94 + fields.each do |field_attrs|
  95 + field = CustomFormsPlugin::Field.find(field_attrs.delete(:real_id))
  96 + field.attributes = field_attrs
  97 + field.save! if field.changed?
  98 + end
  99 + end
  100 +
  101 + def format_kind(fields)
  102 + fields.each do |id, field|
  103 + next if field[:kind].blank?
  104 + kind = field.delete(:kind)
  105 + case kind
  106 + when 'radio'
  107 + field[:list] = false
  108 + field[:multiple] = false
  109 + when 'check_box'
  110 + field[:list] = false
  111 + field[:multiple] = true
  112 + when 'select'
  113 + field[:list] = true
  114 + field[:multiple] = false
  115 + when 'multiple_select'
  116 + field[:list] = true
  117 + field[:multiple] = true
  118 + end
  119 + end
  120 + fields
  121 + end
  122 +
  123 + def format_choices(fields)
  124 + fields.each do |id, field|
  125 + next if !field.has_key?(:choices)
  126 + field[:choices] = field[:choices].map {|key, value| value}.inject({}) do |result, choice|
  127 + hash = (choice[:name].blank? || choice[:value].blank?) ? {} : {choice[:name] => choice[:value]}
  128 + result.merge!(hash)
  129 + end
  130 + end
  131 + fields
  132 + end
  133 +
  134 + def remove_fields(params, form)
  135 + present_fields = params[:fields].map{|id, value| value}.collect {|field| field[:real_id]}.compact
  136 + form.fields.each {|field| field.destroy if !present_fields.include?(field.id.to_s) }
  137 + end
  138 +
  139 + def set_form_id(fields, form_id)
  140 + fields.each do |id, field|
  141 + field[:form_id] = form_id
  142 + end
  143 + fields
  144 + end
  145 +end
... ...
plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb 0 → 100644
... ... @@ -0,0 +1,45 @@
  1 +class CustomFormsPluginProfileController < ProfileController
  2 +
  3 + before_filter :has_access, :show
  4 +
  5 + def show
  6 + @form = CustomFormsPlugin::Form.find(params[:id])
  7 + if user
  8 + @submission ||= CustomFormsPlugin::Submission.find_by_form_id_and_profile_id(@form.id,user.id)
  9 + @submission ||= CustomFormsPlugin::Submission.new(:form_id => @form.id, :profile_id => user.id)
  10 + else
  11 + @submission ||= CustomFormsPlugin::Submission.new(:form_id => @form.id)
  12 + end
  13 + if request.post?
  14 + begin
  15 + extend(CustomFormsPlugin::Helper)
  16 + answers = build_answers(params[:submission], @form)
  17 + failed_answers = answers.select {|answer| !answer.valid? }
  18 + if failed_answers.empty?
  19 + if !user
  20 + @submission.author_name = params[:author_name]
  21 + @submission.author_email = params[:author_email]
  22 + end
  23 + @submission.save!
  24 + answers.map {|answer| answer.submission = @submission; answer.save!}
  25 + else
  26 + @submission.valid?
  27 + failed_answers.each do |answer|
  28 + @submission.errors.add(answer.field.name.to_sym, answer.errors[answer.field.slug.to_sym])
  29 + end
  30 + end
  31 + session[:notice] = _('Submission saved')
  32 + redirect_to :action => 'show'
  33 + rescue
  34 + session[:notice] = _('Submission could not be saved')
  35 + end
  36 + end
  37 + end
  38 +
  39 + private
  40 +
  41 + def has_access
  42 + form = CustomFormsPlugin::Form.find(params[:id])
  43 + render_access_denied if !form.accessible_to(user)
  44 + end
  45 +end
... ...
plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,114 @@
  1 +require File.dirname(__FILE__) + '/../../../../test/test_helper'
  2 +require File.dirname(__FILE__) + '/../../controllers/custom_forms_plugin_myprofile_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class CustomFormsPluginMyprofileController; def rescue_action(e) raise e end; end
  6 +
  7 +class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase
  8 + def setup
  9 + @controller = CustomFormsPluginMyprofileController.new
  10 + @request = ActionController::TestRequest.new
  11 + @response = ActionController::TestResponse.new
  12 + @profile = create_user('profile').person
  13 + login_as(@profile.identifier)
  14 + environment = Environment.default
  15 + environment.enable_plugin(CustomFormsPlugin)
  16 + end
  17 +
  18 + attr_reader :profile
  19 +
  20 + should 'list forms associated with profile' do
  21 + another_profile = fast_create(Profile)
  22 + f1 = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software')
  23 + f2 = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Open Source')
  24 + f3 = CustomFormsPlugin::Form.create!(:profile => another_profile, :name => 'Open Source')
  25 +
  26 + get :index, :profile => profile.identifier
  27 +
  28 + assert_includes assigns(:forms), f1
  29 + assert_includes assigns(:forms), f2
  30 + assert_not_includes assigns(:forms), f3
  31 + end
  32 +
  33 + should 'destroy form' do
  34 + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software')
  35 + assert CustomFormsPlugin::Form.exists?(form.id)
  36 + post :remove, :profile => profile.identifier, :id => form.id
  37 + assert !CustomFormsPlugin::Form.exists?(form.id)
  38 + end
  39 +
  40 + should 'create a form' do
  41 + format = '%Y-%m-%d %H:%M'
  42 + begining = Time.now.strftime(format)
  43 + ending = (Time.now + 1.day).strftime('%Y-%m-%d %H:%M')
  44 + assert_difference CustomFormsPlugin::Form, :count, 1 do
  45 + post :create, :profile => profile.identifier,
  46 + :form => {
  47 + :name => 'My Form',
  48 + :access => 'logged',
  49 + :begining => begining,
  50 + :ending => ending,
  51 + :description => 'Cool form'},
  52 + :fields => {
  53 + 1 => {
  54 + :name => 'Name',
  55 + :default_value => 'Jack',
  56 + :type => 'text_field'
  57 + },
  58 + 2 => {
  59 + :name => 'Color',
  60 + :list => '1',
  61 + :type => 'select_field',
  62 + :choices => {
  63 + 1 => {:name => 'Red', :value => 'red'},
  64 + 2 => {:name => 'Blue', :value => 'blue'},
  65 + 3 => {:name => 'Black', :value => 'black'}
  66 + }
  67 + }
  68 + }
  69 + end
  70 +
  71 + form = CustomFormsPlugin::Form.find_by_name('My Form')
  72 + assert_equal 'logged', form.access
  73 + assert_equal begining, form.begining.strftime(format)
  74 + assert_equal ending, form.ending.strftime(format)
  75 + assert_equal 'Cool form', form.description
  76 + assert_equal 2, form.fields.count
  77 +
  78 + f1 = form.fields.first
  79 + f2 = form.fields.last
  80 +
  81 + assert_equal 'Name', f1.name
  82 + assert_equal 'Jack', f1.default_value
  83 + assert f1.kind_of?(CustomFormsPlugin::TextField)
  84 +
  85 + assert_equal 'Color', f2.name
  86 + assert_equal 'red', f2.choices['Red']
  87 + assert_equal 'blue', f2.choices['Blue']
  88 + assert_equal 'black', f2.choices['Black']
  89 + assert f2.list
  90 + assert f2.kind_of?(CustomFormsPlugin::SelectField)
  91 + end
  92 +
  93 + should 'edit a form' do
  94 + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software')
  95 + field = CustomFormsPlugin::TextField.create!(:form => form, :name => 'License')
  96 + format = '%Y-%m-%d %H:%M'
  97 + begining = Time.now.strftime(format)
  98 + ending = (Time.now + 1.day).strftime('%Y-%m-%d %H:%M')
  99 +
  100 + post :edit, :profile => profile.identifier, :id => form.id,
  101 + :form => {:name => 'My Form', :access => 'logged', :begining => begining, :ending => ending, :description => 'Cool form'},
  102 + :fields => {1 => {:real_id => field.id.to_s, :name => 'Source'}}
  103 +
  104 + form.reload
  105 + field.reload
  106 +
  107 + assert_equal 'logged', form.access
  108 + assert_equal begining, form.begining.strftime(format)
  109 + assert_equal ending, form.ending.strftime(format)
  110 + assert_equal 'Cool form', form.description
  111 + assert_equal 'Source', field.name
  112 + end
  113 +end
  114 +
... ...