From c6222cdc2333d433b8f827205b5a0cbd0fb82e9d Mon Sep 17 00:00:00 2001 From: Andreas Stephanides Date: Fri, 7 Aug 2015 00:03:02 +0200 Subject: [PATCH] AutoCommit Fre Aug 7 00:03:02 CEST 2015 --- app/models/survey/choice.rb | 11 +++++++++-- app/models/survey/question.rb | 23 +++++++++++++++++++---- 2 files changed, 28 insertions(+), 6 deletions(-) diff --git a/app/models/survey/choice.rb b/app/models/survey/choice.rb index 9f2094e..6000935 100644 --- a/app/models/survey/choice.rb +++ b/app/models/survey/choice.rb @@ -5,9 +5,16 @@ class Survey::Choice < ActiveRecord::Base include ActionView::Helpers::TagHelper mount_uploader :picture, PictureUploader def to_s - self.text + self.text end def html - content_tag("i","", class: self.icon ) + self.text + content_tag("i","", class: self.icon ) + self.text + end + def attributes_for_copy + self.attributes.select{|k,v| ["text","sort", "picture", "icon"].include?(k)} + end + def copy_from_template + cpy = Survey::Choice.new(self.attributes_for_copy) + cpy.save end end diff --git a/app/models/survey/question.rb b/app/models/survey/question.rb index 1911f74..af75d55 100644 --- a/app/models/survey/question.rb +++ b/app/models/survey/question.rb @@ -1,17 +1,32 @@ # -*- coding: utf-8 -*- class Survey::Question < ActiveRecord::Base - attr_accessible :text, :title, :typ, :choice_ids + attr_accessible :text, :title, :typ, :choice_ids, :parent_type, :parent_id belongs_to :parent, polymorphic: true has_many :choices, dependent: :destroy has_many :answers, through: :choices include IsCommentable FLAG_ICONS={"delete" => "fa fa-trash", "template"=> "ffi1-cleaning1"} FLAG_CONFIRM={"delete"=> "Sicher loeschen?"} - scope :templates, ->{ where(flag_template:true)} + scope :templates, ->{ where(flag_template: true)} acts_as_flagable - def attributes_for_copy - self.attributes + + def copy_from_template_for(parent) + unless self.flag_template + return nil + else + cpy = Survey::Question.new(self.attributes_for_copy) + cpy.parent=parent + cpy.save + self.choices.each do |c| + cpy.choices << c.copy_from_template + end + end end + + def attributes_for_copy + self.attributes.select{|k,v| ["title","text","typ"].include?(k)} + end + def add_yesno_choices c=Survey::Choice.new(title: "Ja") c.save