diff --git a/app/controllers/codebooks_controller.rb b/app/controllers/codebooks_controller.rb index a430b89..4619f28 100644 --- a/app/controllers/codebooks_controller.rb +++ b/app/controllers/codebooks_controller.rb @@ -25,9 +25,9 @@ def show @next_section = sections[sections.index(@context.to_sym) + 1] if params[:id].split('_').last == "given" - @frequencies = Identity.histogram(@context.gsub("_given","").gsub("klass","class").gsub("_","-")) + @frequencies = Identity.histogram(@context) else - @frequencies = Code.histogram(@context.gsub("_exp","").gsub("klass","class").gsub("_","-")) + @frequencies = Code.histogram(@context) end if @context.include?("_exp") || @context.include?("_feel") diff --git a/app/models/code.rb b/app/models/code.rb index e50acb8..f5b27e7 100644 --- a/app/models/code.rb +++ b/app/models/code.rb @@ -1,25 +1,26 @@ class Code include ActiveGraph::Node - + property :name property :context - + before_validation :strip_whitespace before_validation :downcase - + validates :name, presence: true validates :context, presence: true validates_uniqueness_of :name, :scope => :context - + has_many :out, :personas, rel_class: :Experiences has_many :in, :categories, rel_class: :CategorizedAs def self.histogram(context) + context = context.gsub("_exp","").gsub("klass","class").gsub("_","-") where(context: context).query_as(:t).with('t, count{(t)-[:EXPERIENCES]-()} AS c').where('c > 0').order('c DESC').return('t.name, c').inject({}) {|h,t| h[t.values[0]] ||= 0; h[t.values[0]] += t.values[1]; h} end private - + def strip_whitespace self.name.strip! end @@ -27,4 +28,4 @@ def strip_whitespace def downcase self.name.downcase! end -end +end diff --git a/app/models/identity.rb b/app/models/identity.rb index 16690a4..dd59a1c 100644 --- a/app/models/identity.rb +++ b/app/models/identity.rb @@ -1,24 +1,25 @@ class Identity include ActiveGraph::Node - + property :name property :context - + before_validation :strip_whitespace - + validates :name, presence: true validates :context, presence: true has_many :out, :personas, rel_class: :IdentifiesWith - + def self.histogram(context) + context = context.gsub("_given","").gsub("klass","class").gsub("_","-") where(context: context).query_as(:i).with('i, count{(i)-[:IDENTIFIES_WITH]-(p:Persona)} AS c').return('i.name, c').order('c DESC').inject({}) {|h,i| h[i.values[0]] ||= 0; h[i.values[0]] += i.values[1]; h} end - + private - + def strip_whitespace self.name.strip! end - -end + +end