2014-12-03 10 views
5

Jest to z ActiveAdmin 0.4.3. Nasza aplikacja uruchamia ankiety, które mogą mieć dowolną liczbę zapytań o ankietę. Gdy użytkownik wypełnia ankietę, tworzona jest instancja UserSurveyComment, którą jest has_many SurveyComments, po jednym dla każdego z ankietowych ankiet.Eksportuj dynamiczną liczbę kolumn w ActiveAdmin CSV

Wynikiem tego jest fakt, że dla każdej ankiety wszystkie instancje UserSurveyComment będą miały taką samą liczbę elementów SurveyComments, ale między ankietami ta liczba może być różna.

Czy to możliwe, w odniesieniu do wywozu ActiveAdmin CSV do obsługi UserSurveyComments w ten sposób, tak, że nie są kolumny dla użytkownika, badania, a następnie każdy SurveyComment z kolei? Eksport jest określany na podstawie badania, dlatego każdy wiersz ma te same kolumny, ale poszczególne produkty mogą mieć inny numer.

Co chciałbym zrobić coś takiego jak

survey.survey_questions.each do |sq| 
    column "Question" { |q| q.survey_comments.where(survey_question_id: sq.id).first.submitted_text } 
end 

... ale w instancji ActiveAdmin.CSVBuilder, nie wydaje się być sposobem na dotarcie do badania.

Może łatwiej mi to po prostu zrobić w moim własnym kontrolerze?

+0

Zostawię to pytanie otwarte, ale w końcu zrobiłem własnego eksportu CSV w moim kontrolera, a pozostały ActiveAdmin zewnątrz. – pjmorse

Odpowiedz

2

Rozumiem twój model jest podobny do

class Survey < ActiveRecord::Base 
    has_many :user_survey_comments 
    has_many :survey_questions 
end 

class SurveyQuestion < ActiveRecord::Base 

    attr_accessor :name 

    belongs_to :survey 
    has_many :survey_comments 
end 

class UserSurveyComments < ActiveRecord::Base 
    belongs_to :survey 
    has_many :survey_comments 
end 

class SurveyComments < ActiveRecord::Base 

    attr_accessor :content 

    belongs_to :user_survey_comments 
    belongs_to :survey_question 
end 

Wewnątrz bloku csv, @collection zawiera listę obiektów filtrowanych na wyjściu. W konfiguracji można zarejestrować UserSurveyComment w podobny sposób, jak następuje:

ActiveAdmin.register UserSurveyComment do 
    csv do 

    column(:survey) 

    visited_surveys = Set[] 

    @collection.each do |user_survey_comment| 

     next if visited_surveys.include?(user_survey_comment.survey) 
     visited_surveys.add(user_survey_comment.survey) 

     user_survey_comment.survey.survey_questions do |question| 
     column(question.name) do |user_survey_comment| 
      user_survey_comment 
      .survey_comments 
      .find_by(survey_question_id=question.id) 
      .try(:response){''} 
     end 
     end 
    end 
    end 
end 
Powiązane problemy