From be09f864d90dde0f8d4d05947e6aab59dbbad3fc Mon Sep 17 00:00:00 2001 From: Chris Mytton Date: Fri, 1 Jul 2016 15:28:31 +0200 Subject: [PATCH] Move classes for reports page into separate file They were in the routes while I was extracting but they can't stay there long term, so moving them to their own file. --- app.rb | 144 +------------------------------------------------ lib/reports.rb | 142 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 144 insertions(+), 142 deletions(-) create mode 100644 lib/reports.rb diff --git a/app.rb b/app.rb index da530e1..e9d4e03 100644 --- a/app.rb +++ b/app.rb @@ -28,6 +28,7 @@ require 'app/jobs' require 'csv_export' require 'country_proxy' +require 'reports' helpers Helpers @@ -168,147 +169,6 @@ erb :term end -# Require including class to define a `stats` method which returns a hash with -# :female, :male and :total keys. -module GenderStats - def female - @female ||= stats[:female].to_f - end - - def male - @male ||= stats[:male].to_f - end - - def total - @total ||= stats[:total].to_f - end - - def female_percentage - @female_percentage ||= female / total * 100 - end - - def male_percentage - @male_percentage ||= male / total * 100 - end -end - -class LegislatureReport - extend Forwardable - include GenderStats - - # Delegate 'name' and 'slug' method calls to the 'legislature' object. - def_delegators :legislature, :name, :slug - - attr_reader :legislature - attr_reader :raw_stats - - def initialize(legislature, raw_stats) - @legislature = legislature - @raw_stats = raw_stats - end - - def stats - @stats ||= raw_stats[:totals][:overall] - end - - def groups - @groups ||= raw_stats[:totals][:parties].map { |slug, group_stats| GroupReport.new(slug, group_stats, raw_stats, legislative_periods) } - end - - def legislative_periods - @legislative_periods ||= legislature.legislative_periods.map { |lp| LegislativePeriodReport.new(lp, raw_stats) } - end -end - -class GroupReport - include GenderStats - - attr_reader :slug - attr_reader :stats - attr_reader :raw_stats - - def initialize(slug, stats, raw_stats, legislative_periods) - @slug = slug - @stats = stats - @raw_stats = raw_stats - @legislative_periods = legislative_periods - end - - def name - stats[:name] - end - - def id_slug - @id_slug ||= slug.to_s.sub('/', '-') - end - - def legislative_periods - @legislative_periods.map do |term| - term_stats = raw_stats[:terms]["term/#{term.slug}".to_sym] - group_stats = term_stats[:parties][slug] - GroupLegilativePeriodReport.new(term, group_stats) if group_stats - end.compact - end -end - -class GroupLegilativePeriodReport - include GenderStats - - attr_reader :term - attr_reader :stats - - def initialize(term, stats) - @term = term - @stats = stats - end - - def name - term.name - end -end - -class LegislativePeriodReport - extend Forwardable - include GenderStats - - # Delegate 'name' and 'slug' method calls to the 'legislative_period' object. - def_delegators :legislative_period, :name, :slug - - attr_reader :legislative_period - attr_reader :raw_stats - - def initialize(legislative_period, raw_stats) - @legislative_period = legislative_period - @raw_stats = raw_stats - end - - def legislative_period_stats - @legislative_period_stats ||= raw_stats[:terms]["term/#{legislative_period.slug}".to_sym] - end - - def stats - @stats ||= legislative_period_stats[:overall] - end - - def groups - @groups = legislative_period_stats[:parties].map { |_, group_stats| LegislativePeriodGroupReport.new(group_stats) } - end -end - -class LegislativePeriodGroupReport - include GenderStats - - attr_reader :stats - - def initialize(stats) - @stats = stats - end - - def name - stats[:name] - end -end - get '/reports/:country' do redirect to('/login') unless current_user @country = Everypolitician.country(slug: params[:country]) @@ -316,7 +176,7 @@ def name stats = Hash[stats_raw.map { |c| [c[:slug], c] }] @country_stats = stats[params[:country]] @legislature_stats = Hash[@country_stats[:legislatures].map {|l| [l[:slug], l]}] - @legislatures = @country.legislatures.map { |l| LegislatureReport.new(l, @legislature_stats[l.slug]) } + @legislatures = @country.legislatures.map { |l| Reports::LegislatureReport.new(l, @legislature_stats[l.slug]) } erb :report, :layout => :layout_page end diff --git a/lib/reports.rb b/lib/reports.rb new file mode 100644 index 0000000..7dbb708 --- /dev/null +++ b/lib/reports.rb @@ -0,0 +1,142 @@ +module Reports + # Require including class to define a `stats` method which returns a hash with + # :female, :male and :total keys. + module GenderStats + def female + @female ||= stats[:female].to_f + end + + def male + @male ||= stats[:male].to_f + end + + def total + @total ||= stats[:total].to_f + end + + def female_percentage + @female_percentage ||= female / total * 100 + end + + def male_percentage + @male_percentage ||= male / total * 100 + end + end + + class LegislatureReport + extend Forwardable + include GenderStats + + # Delegate 'name' and 'slug' method calls to the 'legislature' object. + def_delegators :legislature, :name, :slug + + attr_reader :legislature + attr_reader :raw_stats + + def initialize(legislature, raw_stats) + @legislature = legislature + @raw_stats = raw_stats + end + + def stats + @stats ||= raw_stats[:totals][:overall] + end + + def groups + @groups ||= raw_stats[:totals][:parties].map { |slug, group_stats| GroupReport.new(slug, group_stats, raw_stats, legislative_periods) } + end + + def legislative_periods + @legislative_periods ||= legislature.legislative_periods.map { |lp| LegislativePeriodReport.new(lp, raw_stats) } + end + end + + class GroupReport + include GenderStats + + attr_reader :slug + attr_reader :stats + attr_reader :raw_stats + + def initialize(slug, stats, raw_stats, legislative_periods) + @slug = slug + @stats = stats + @raw_stats = raw_stats + @legislative_periods = legislative_periods + end + + def name + stats[:name] + end + + def id_slug + @id_slug ||= slug.to_s.sub('/', '-') + end + + def legislative_periods + @legislative_periods.map do |term| + term_stats = raw_stats[:terms]["term/#{term.slug}".to_sym] + group_stats = term_stats[:parties][slug] + GroupLegilativePeriodReport.new(term, group_stats) if group_stats + end.compact + end + end + + class GroupLegilativePeriodReport + include GenderStats + + attr_reader :term + attr_reader :stats + + def initialize(term, stats) + @term = term + @stats = stats + end + + def name + term.name + end + end + + class LegislativePeriodReport + extend Forwardable + include GenderStats + + # Delegate 'name' and 'slug' method calls to the 'legislative_period' object. + def_delegators :legislative_period, :name, :slug + + attr_reader :legislative_period + attr_reader :raw_stats + + def initialize(legislative_period, raw_stats) + @legislative_period = legislative_period + @raw_stats = raw_stats + end + + def legislative_period_stats + @legislative_period_stats ||= raw_stats[:terms]["term/#{legislative_period.slug}".to_sym] + end + + def stats + @stats ||= legislative_period_stats[:overall] + end + + def groups + @groups = legislative_period_stats[:parties].map { |_, group_stats| LegislativePeriodGroupReport.new(group_stats) } + end + end + + class LegislativePeriodGroupReport + include GenderStats + + attr_reader :stats + + def initialize(stats) + @stats = stats + end + + def name + stats[:name] + end + end +end