1module PageTitleHelper
 
2  class PageTitleBuilder
 
3    class << self
 
4      def build(template)
 
5        new(template).build
 
6      end
 
7    end
 
8
 
 9    attr_reader :template
 
 
11    delegate :assigns, to: :template
 
12    delegate :params, to: :template
 
13    delegate :translate, to: :template
 
14    delegate :[], :has_key?, to: :assigns
 
  • Complexity 1 » saikuro
16    def initialize(template)
 
17      @template = template
 
18    end
 
  • Complexity 1 » saikuro
20    def build
 
21      translate key, options
 
22    end
 
 
24    private
 
  • Complexity 1 » saikuro
26    def controller
 
27      @controller ||= params[:controller].tr('/', '_')
 
28    end
 
  • Complexity 1 » saikuro
30    def action
 
31      @action ||= params[:action]
 
32    end
 
  • Complexity 1 » saikuro
34    def key
 
35      @key ||= :"#{controller}.#{action}"
 
36    end
 
  • TooManyStatements - has approx 9 statements » reek
  • Complexity 7 » saikuro
38    def options
  • Block cyclomatic complexity is 6. It should be 4 or less. » roodi
39      {}.tap do |opts|
 
40        opts[:scope]        = :page_titles
 
41        opts[:default]      = [:"#{controller}.default", :default]
 
42        opts[:constituency] = constituency.name if constituency?
 
43        opts[:postcode]     = formatted_postcode if postcode?
 
 
45        if postcode?
 
46          opts[:count] = 1
 
47        else
 
48          opts[:count] = 0
 
49        end
 
 
51        if petition?
 
52          opts[:petition] = petition.action
 
 
54          if controller == 'sponsors'
 
55            opts[:creator] = petition.creator.name
 
56          end
 
57        end
 
58      end
 
59    end
 
 
61    %w[constituency petition postcode].each do |object|
 
62      define_method :"#{object}?" do
 
63        send(:"#{object}").present?
 
64      end
 
 
66      define_method :"#{object}" do
 
67        send(:[], object)
 
68      end
 
69    end
 
  • Complexity 1 » saikuro
71    def formatted_postcode
 
72      postcode.gsub(/\A(.+)(.{3})\z/, '\1 \2')
 
73    end
 
74  end
 
  • Complexity 1 » saikuro
76  def page_title
 
77    PageTitleBuilder.build(self)
 
78  end
 
79end