Skip to content

Commit

Permalink
Merge pull request #118 from identity-research-lab/minor-refactorings
Browse files Browse the repository at this point in the history
Minor refactorings
  • Loading branch information
CoralineAda authored Aug 31, 2024
2 parents 6605766 + e79a041 commit 8a0900b
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 17 deletions.
2 changes: 1 addition & 1 deletion app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class QuestionsController < ApplicationController

def show
@question = Question.from(params[:id])
@responses = SurveyResponse.all.order(:created_at).reject{|sr| sr.read_attribute(@question.key).nil? }
@responses = SurveyResponse.where("#{@question.key} IS NOT NULL").order(:created_at)
end

end
12 changes: 6 additions & 6 deletions app/controllers/stats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@ class StatsController < ApplicationController

def index

sentiments = SurveyResponse.all.pluck(:sentiment)
@pronoun_sentiment_positive = (sentiments.select{|sentiment| sentiment == "positive"}.count / sentiments.count.to_f * 100).to_i
@pronoun_sentiment_neutral = (sentiments.select{|sentiment| sentiment == "neutral"}.count / sentiments.count.to_f * 100).to_i
@pronoun_sentiment_negative = (sentiments.select{|sentiment| sentiment == "negative"}.count / sentiments.count.to_f * 100).to_i

survey_response_count = SurveyResponse.count
question_count = Question::QUESTIONS.count
answer_count = survey_response_count * question_count
Expand All @@ -18,7 +13,12 @@ def index
node_count = ActiveGraph::Base.query('WITH count{()} AS ct RETURN ct').first.values.first
edge_count = ActiveGraph::Base.query('WITH count{()-[]-()} AS ct RETURN ct').first.values.first

@total_datapoints = survey_response_count + (question_count * survey_response_count) + identity_count + code_count + category_count + keyword_count + sentiments.compact.count + edge_count
sentiments = SurveyResponse.where("sentiment IS NOT NULL").pluck(:sentiment)
@pronoun_sentiment_positive = (sentiments.select{|sentiment| sentiment == "positive"}.count / sentiments.count.to_f * 100).to_i
@pronoun_sentiment_neutral = (sentiments.select{|sentiment| sentiment == "neutral"}.count / sentiments.count.to_f * 100).to_i
@pronoun_sentiment_negative = (sentiments.select{|sentiment| sentiment == "negative"}.count / sentiments.count.to_f * 100).to_i

@total_datapoints = survey_response_count + (question_count * survey_response_count) + identity_count + code_count + category_count + keyword_count + sentiments.count + edge_count

@stats = {
"Participant survey responses" => survey_response_count,
Expand Down
12 changes: 3 additions & 9 deletions app/controllers/survey_responses_controller.rb
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
class SurveyResponsesController < ApplicationController

def index
if @theme = params.permit(:theme)[:theme]
@responses = SurveyResponse.where("? = ANY (themes)", @theme).order(:created_at)
else
@responses = SurveyResponse.all.order(:created_at)
end
@responses = SurveyResponse.all.order(:created_at)
end

def show
@theme = params.permit(:theme)[:theme]
@response = SurveyResponse.find(params[:id])
@total_responses = SurveyResponse.all.count
@enqueued_at = params[:enqueued_at]

@previous_response = SurveyResponse.where("created_at < ?", @response.created_at).order("created_at DESC").limit(1).first
@next_response = SurveyResponse.where("created_at > ?", @response.created_at).order("created_at ASC").limit(1).first

persona = Persona.find_or_initialize_by(survey_response_id: @response.id)
@categories = persona.categories.sort{ |a,b| "#{a.context}.#{a.name}" <=> "#{b.context}.#{b.name}" }
@keywords = persona.keywords.sort{ |a,b| a.name <=> b.name }
@keywords = persona.keywords.order_by(:name)
end

def new
Expand All @@ -37,7 +31,7 @@ def update
sanitized_params[key] = value.join(",").split(",").reject(&:empty?).compact.map(&:strip).map(&:downcase)
end
success = @response.update(sanitized_params)
@question = Question.from(params["question"])
@question = Question.from(params[:survey_response].keys.first.gsub("_codes","").gsub("_id", "_given"))

respond_to do |format|
format.turbo_stream do
Expand Down
1 change: 1 addition & 0 deletions app/models/keyword.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ class Keyword
property :name

validates :name, presence: true
validates :name, uniqueness: true

has_many :in, :personas, rel_class: :ReflectsOn, dependent: :delete_orphans

Expand Down
1 change: 0 additions & 1 deletion app/views/survey_responses/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
<% end %>
<%= form_with model: response, data: { turbo_frame: "#{question.key}" } do |f| %>
<%= f.text_area question.codes_field, value: response.read_attribute(question.codes_field).try(:sort).try(:join, ', '), multiple: true, class: "#{@css_class}" %>
<%= hidden_field_tag "question", "#{question.key}" %>
<%= f.submit "Save", class: "small" %>
<% end %>
<% end %>

0 comments on commit 8a0900b

Please sign in to comment.