diff --git a/app/controllers/super_admin/themes_controller.rb b/app/controllers/super_admin/themes_controller.rb index 5c8560e..6fbf25d 100644 --- a/app/controllers/super_admin/themes_controller.rb +++ b/app/controllers/super_admin/themes_controller.rb @@ -68,10 +68,10 @@ def extract @theme = Theme.find(extract_params[:id]) @answers = @theme.answers - @answers = @answers.where(plan_id: extract_params[:plan_id]) if extract_params[:plan_id] - @answers = @answers.where(question_id: extract_params[:question_id]) if extract_params[:question_id] - @answers = @answers.since(extract_params[:start_date]) - @answers = @answers.until(extract_params[:end_date]) + + extract_filtering_params.each do |key, value| + @answers = @answers.public_send(key, value) if value + end render format: :json end @@ -86,5 +86,9 @@ def extract_params params.permit(:id, :plan_id, :question_id, :start_date, :end_date) end + + def extract_filtering_params + extract_params.slice(:plan_id, :question_id, :start_date, :end_date) + end end end diff --git a/app/models/answer.rb b/app/models/answer.rb index ccde9b5..fbc8aac 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -30,6 +30,8 @@ :question, :user, :plan, :question_options, :notes, :note_ids, :id, :as => [:default, :admin] + scope :plan_id, ->(id) { where('answers.plan_id = ?', id) } + scope :question_id, ->(id) { where('answers.question_id = ?', id) } scope :since, ->(date) { where('answers.created_at >= ?', date) if date } scope :until, ->(date) { where('answers.created_at <= ?', date) if date }