From 13283e0e6505abd3c37215c79edb0494b8bf9ff0 Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Fri, 7 Aug 2015 01:03:02 +0200 Subject: [PATCH] AutoCommit Fre Aug 7 01:03:02 CEST 2015 --- app/controllers/survey/questions_controller.rb | 7 +++++-- app/helpers/survey/questions_helper.rb | 2 +- app/models/survey/choice.rb | 1 + app/models/survey/question.rb | 3 ++- app/views/survey/questions/_form.html.erb | 2 ++ app/views/survey/questions/_new_question.html.erb | 14 +++++++++++++- app/views/survey/questions/show.html.erb | 2 +- config/routes.rb | 2 +- 8 files changed, 26 insertions(+), 7 deletions(-) diff --git a/app/controllers/survey/questions_controller.rb b/app/controllers/survey/questions_controller.rb index 4bcf2fc..b7cbad3 100644 --- a/app/controllers/survey/questions_controller.rb +++ b/app/controllers/survey/questions_controller.rb @@ -31,7 +31,9 @@ class Survey::QuestionsController < ApplicationController # GET /survey/questions/1.json def create_from_template @template = Survey::Question.find(params[:id]) - + parent= params[:parent_type].constantize.find(params[:parent_id]) + @survey_question = @template.copy_from_template_for(parent) + render action: :show end def show @@ -46,7 +48,8 @@ class Survey::QuestionsController < ApplicationController # GET /survey/questions/new.json def new @survey_question = Survey::Question.new - @commentable=params[:commentable_type].constantize.find(params[:commentable_id]) + @parent=params[:parent_type].constantize.find(params[:parent_id]) + @survey_question.parent=@parent respond_to do |format| format.html # new.html.erb end diff --git a/app/helpers/survey/questions_helper.rb b/app/helpers/survey/questions_helper.rb index b2757f1..5ac1928 100644 --- a/app/helpers/survey/questions_helper.rb +++ b/app/helpers/survey/questions_helper.rb @@ -1,5 +1,5 @@ module Survey::QuestionsHelper def new_question_for(obj) - render partial: "survey/questions/new_question", locals: {question_templates: Survey::Question.templates} + render partial: "survey/questions/new_question", locals: {question_templates: Survey::Question.templates, parent: obj} end end diff --git a/app/models/survey/choice.rb b/app/models/survey/choice.rb index 6000935..22f8dbf 100644 --- a/app/models/survey/choice.rb +++ b/app/models/survey/choice.rb @@ -16,5 +16,6 @@ class Survey::Choice < ActiveRecord::Base def copy_from_template cpy = Survey::Choice.new(self.attributes_for_copy) cpy.save + cpy end end diff --git a/app/models/survey/question.rb b/app/models/survey/question.rb index af75d55..26ad1b3 100644 --- a/app/models/survey/question.rb +++ b/app/models/survey/question.rb @@ -2,7 +2,7 @@ class Survey::Question < ActiveRecord::Base attr_accessible :text, :title, :typ, :choice_ids, :parent_type, :parent_id belongs_to :parent, polymorphic: true - has_many :choices, dependent: :destroy + has_many :choices, dependent: :destroy, class_name: "Survey::Choice" has_many :answers, through: :choices include IsCommentable FLAG_ICONS={"delete" => "fa fa-trash", "template"=> "ffi1-cleaning1"} @@ -20,6 +20,7 @@ class Survey::Question < ActiveRecord::Base self.choices.each do |c| cpy.choices << c.copy_from_template end + cpy end end diff --git a/app/views/survey/questions/_form.html.erb b/app/views/survey/questions/_form.html.erb index 8a95873..94d24d3 100644 --- a/app/views/survey/questions/_form.html.erb +++ b/app/views/survey/questions/_form.html.erb @@ -3,6 +3,8 @@ <%= f.input :title %> <%= f.input :text %> + <%= f.input :parent_type, as: :hidden %> + <%= f.input :parent_id, as: :hidden %> <%= f.input :typ %> <% end %> diff --git a/app/views/survey/questions/_new_question.html.erb b/app/views/survey/questions/_new_question.html.erb index 4d0b8f0..72c1945 100644 --- a/app/views/survey/questions/_new_question.html.erb +++ b/app/views/survey/questions/_new_question.html.erb @@ -1,3 +1,15 @@ <% question_templates.each do |q| %> -<%= link_to q.title, new_survey_question_path() %> +<%= link_to q.title, create_from_template_survey_question_path(q, params: {parent_type: parent.class.to_s, parent_id: parent.id.to_s}) %> <% end %> + diff --git a/app/views/survey/questions/show.html.erb b/app/views/survey/questions/show.html.erb index edf9f8b..4a57ce5 100644 --- a/app/views/survey/questions/show.html.erb +++ b/app/views/survey/questions/show.html.erb @@ -9,4 +9,4 @@ <%= render_comments_for(@survey_question) %> <%= link_to 'Edit', edit_survey_question_path(@survey_question) %> | -<%= link_to 'Back', survey_questions_path %> +<%= link_to 'Back', polymorphic_path(@survey_question.parent) %> diff --git a/config/routes.rb b/config/routes.rb index 938e6c6..8416b83 100755 --- a/config/routes.rb +++ b/config/routes.rb @@ -16,7 +16,7 @@ get :answer put :answer get 'flag' - + get :create_from_template end end resources :choices