From 96b8283eba259184375397902f72b95501ee89ac Mon Sep 17 00:00:00 2001 From: Rodrigo Souto Date: Mon, 10 Sep 2012 16:48:41 -0300 Subject: [PATCH] [custom-forms] Controllers --- plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb | 145 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb | 45 +++++++++++++++++++++++++++++++++++++++++++++ plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 304 insertions(+), 0 deletions(-) create mode 100644 plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb create mode 100644 plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb create mode 100644 plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb diff --git a/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb new file mode 100644 index 0000000..b908810 --- /dev/null +++ b/plugins/custom_forms/controllers/custom_forms_plugin_myprofile_controller.rb @@ -0,0 +1,145 @@ +class CustomFormsPluginMyprofileController < MyProfileController + def index + @forms = CustomFormsPlugin::Form.from(profile) + end + + def create + @form = CustomFormsPlugin::Form.new(:profile => profile) + @fields = [] + @empty_field = CustomFormsPlugin::Field.new + if request.post? + begin + @form.update_attributes!(params[:form]) + params[:fields] = format_kind(params[:fields]) + params[:fields] = format_choices(params[:fields]) + params[:fields] = set_form_id(params[:fields], @form.id) + create_fields(new_fields(params)) + session['notice'] = _('Form created') + redirect_to :action => 'index' + rescue Exception => exception + logger.error(exception.to_s) + session['notice'] = _('Form could not be created') + end + end + end + + def edit + @form = CustomFormsPlugin::Form.find(params[:id]) + @fields = @form.fields + @empty_field = CustomFormsPlugin::TextField.new + if request.post? + begin + @form.update_attributes!(params[:form]) + params[:fields] = format_kind(params[:fields]) + params[:fields] = format_choices(params[:fields]) + remove_fields(params, @form) + create_fields(new_fields(params)) + update_fields(edited_fields(params)) + session['notice'] = _('Form updated') + redirect_to :action => 'index' + rescue Exception => exception + logger.error(exception.to_s) + session['notice'] = _('Form could not be updated') + end + end + end + + def remove + @form = CustomFormsPlugin::Form.find(params[:id]) + begin + @form.destroy + session[:notice] = _('Form removed') + rescue + session[:notice] = _('Form could not be removed') + end + redirect_to :action => 'index' + end + + def submissions + @form = CustomFormsPlugin::Form.find(params[:id]) + @submissions = @form.submissions + end + + def show_submission + @submission = CustomFormsPlugin::Submission.find(params[:id]) + @form = @submission.form + end + + private + + def new_fields(params) + result = params[:fields].map {|id, hash| hash.has_key?(:real_id) ? nil : hash}.compact + result.delete_if {|field| field[:name].blank?} + result + end + + def edited_fields(params) + params[:fields].map {|id, hash| hash.has_key?(:real_id) ? hash : nil}.compact + end + + def create_fields(fields) + fields.each do |field| + case field[:type] + when 'text_field' + CustomFormsPlugin::TextField.create!(field) + when 'select_field' + CustomFormsPlugin::SelectField.create!(field) + else + CustomFormsPlugin::Field.create!(field) + end + end + end + + def update_fields(fields) + fields.each do |field_attrs| + field = CustomFormsPlugin::Field.find(field_attrs.delete(:real_id)) + field.attributes = field_attrs + field.save! if field.changed? + end + end + + def format_kind(fields) + fields.each do |id, field| + next if field[:kind].blank? + kind = field.delete(:kind) + case kind + when 'radio' + field[:list] = false + field[:multiple] = false + when 'check_box' + field[:list] = false + field[:multiple] = true + when 'select' + field[:list] = true + field[:multiple] = false + when 'multiple_select' + field[:list] = true + field[:multiple] = true + end + end + fields + end + + def format_choices(fields) + fields.each do |id, field| + next if !field.has_key?(:choices) + field[:choices] = field[:choices].map {|key, value| value}.inject({}) do |result, choice| + hash = (choice[:name].blank? || choice[:value].blank?) ? {} : {choice[:name] => choice[:value]} + result.merge!(hash) + end + end + fields + end + + def remove_fields(params, form) + present_fields = params[:fields].map{|id, value| value}.collect {|field| field[:real_id]}.compact + form.fields.each {|field| field.destroy if !present_fields.include?(field.id.to_s) } + end + + def set_form_id(fields, form_id) + fields.each do |id, field| + field[:form_id] = form_id + end + fields + end +end diff --git a/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb b/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb new file mode 100644 index 0000000..61dcbdf --- /dev/null +++ b/plugins/custom_forms/controllers/custom_forms_plugin_profile_controller.rb @@ -0,0 +1,45 @@ +class CustomFormsPluginProfileController < ProfileController + + before_filter :has_access, :show + + def show + @form = CustomFormsPlugin::Form.find(params[:id]) + if user + @submission ||= CustomFormsPlugin::Submission.find_by_form_id_and_profile_id(@form.id,user.id) + @submission ||= CustomFormsPlugin::Submission.new(:form_id => @form.id, :profile_id => user.id) + else + @submission ||= CustomFormsPlugin::Submission.new(:form_id => @form.id) + end + if request.post? + begin + extend(CustomFormsPlugin::Helper) + answers = build_answers(params[:submission], @form) + failed_answers = answers.select {|answer| !answer.valid? } + if failed_answers.empty? + if !user + @submission.author_name = params[:author_name] + @submission.author_email = params[:author_email] + end + @submission.save! + answers.map {|answer| answer.submission = @submission; answer.save!} + else + @submission.valid? + failed_answers.each do |answer| + @submission.errors.add(answer.field.name.to_sym, answer.errors[answer.field.slug.to_sym]) + end + end + session[:notice] = _('Submission saved') + redirect_to :action => 'show' + rescue + session[:notice] = _('Submission could not be saved') + end + end + end + + private + + def has_access + form = CustomFormsPlugin::Form.find(params[:id]) + render_access_denied if !form.accessible_to(user) + end +end diff --git a/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb b/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb new file mode 100644 index 0000000..7c12989 --- /dev/null +++ b/plugins/custom_forms/test/functional/custom_forms_plugin_myprofile_controller_test.rb @@ -0,0 +1,114 @@ +require File.dirname(__FILE__) + '/../../../../test/test_helper' +require File.dirname(__FILE__) + '/../../controllers/custom_forms_plugin_myprofile_controller' + +# Re-raise errors caught by the controller. +class CustomFormsPluginMyprofileController; def rescue_action(e) raise e end; end + +class CustomFormsPluginMyprofileControllerTest < ActionController::TestCase + def setup + @controller = CustomFormsPluginMyprofileController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + @profile = create_user('profile').person + login_as(@profile.identifier) + environment = Environment.default + environment.enable_plugin(CustomFormsPlugin) + end + + attr_reader :profile + + should 'list forms associated with profile' do + another_profile = fast_create(Profile) + f1 = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software') + f2 = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Open Source') + f3 = CustomFormsPlugin::Form.create!(:profile => another_profile, :name => 'Open Source') + + get :index, :profile => profile.identifier + + assert_includes assigns(:forms), f1 + assert_includes assigns(:forms), f2 + assert_not_includes assigns(:forms), f3 + end + + should 'destroy form' do + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software') + assert CustomFormsPlugin::Form.exists?(form.id) + post :remove, :profile => profile.identifier, :id => form.id + assert !CustomFormsPlugin::Form.exists?(form.id) + end + + should 'create a form' do + format = '%Y-%m-%d %H:%M' + begining = Time.now.strftime(format) + ending = (Time.now + 1.day).strftime('%Y-%m-%d %H:%M') + assert_difference CustomFormsPlugin::Form, :count, 1 do + post :create, :profile => profile.identifier, + :form => { + :name => 'My Form', + :access => 'logged', + :begining => begining, + :ending => ending, + :description => 'Cool form'}, + :fields => { + 1 => { + :name => 'Name', + :default_value => 'Jack', + :type => 'text_field' + }, + 2 => { + :name => 'Color', + :list => '1', + :type => 'select_field', + :choices => { + 1 => {:name => 'Red', :value => 'red'}, + 2 => {:name => 'Blue', :value => 'blue'}, + 3 => {:name => 'Black', :value => 'black'} + } + } + } + end + + form = CustomFormsPlugin::Form.find_by_name('My Form') + assert_equal 'logged', form.access + assert_equal begining, form.begining.strftime(format) + assert_equal ending, form.ending.strftime(format) + assert_equal 'Cool form', form.description + assert_equal 2, form.fields.count + + f1 = form.fields.first + f2 = form.fields.last + + assert_equal 'Name', f1.name + assert_equal 'Jack', f1.default_value + assert f1.kind_of?(CustomFormsPlugin::TextField) + + assert_equal 'Color', f2.name + assert_equal 'red', f2.choices['Red'] + assert_equal 'blue', f2.choices['Blue'] + assert_equal 'black', f2.choices['Black'] + assert f2.list + assert f2.kind_of?(CustomFormsPlugin::SelectField) + end + + should 'edit a form' do + form = CustomFormsPlugin::Form.create!(:profile => profile, :name => 'Free Software') + field = CustomFormsPlugin::TextField.create!(:form => form, :name => 'License') + format = '%Y-%m-%d %H:%M' + begining = Time.now.strftime(format) + ending = (Time.now + 1.day).strftime('%Y-%m-%d %H:%M') + + post :edit, :profile => profile.identifier, :id => form.id, + :form => {:name => 'My Form', :access => 'logged', :begining => begining, :ending => ending, :description => 'Cool form'}, + :fields => {1 => {:real_id => field.id.to_s, :name => 'Source'}} + + form.reload + field.reload + + assert_equal 'logged', form.access + assert_equal begining, form.begining.strftime(format) + assert_equal ending, form.ending.strftime(format) + assert_equal 'Cool form', form.description + assert_equal 'Source', field.name + end +end + -- libgit2 0.21.2