Skip to content

Commit

Permalink
Refactor code to increase CodeClimate score.
Browse files Browse the repository at this point in the history
  • Loading branch information
jastix committed Jul 12, 2016
1 parent 3a15a6b commit 2349136
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 12 deletions.
4 changes: 3 additions & 1 deletion lib/route_translator/extensions/route_set.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ module ActionDispatch
module Routing
class RouteSet
def add_localized_route(mapping, path_ast, name, anchor, scope, path, controller, default_action, to, via, formatted, options_constraints, options)
RouteTranslator::Translator.translations_for(self, path, name, options_constraints, options, mapping) do |translated_name, translated_path, translated_options_constraints, translated_options|
route = RouteTranslator::Route.new(self, path, name, options_constraints, options, mapping)

RouteTranslator::Translator.translations_for(route) do |translated_name, translated_path, translated_options_constraints, translated_options|
translated_path_ast = ::ActionDispatch::Journey::Parser.parse(translated_path)
translated_mapping = ::ActionDispatch::Routing::Mapper::Mapping.build(scope, self, translated_path_ast, controller, default_action, to, via, formatted, translated_options_constraints, anchor, translated_options)

Expand Down
18 changes: 18 additions & 0 deletions lib/route_translator/route.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module RouteTranslator
class Route
attr_reader :route_set, :path, :name, :options_constraints, :options, :mapping

def initialize(route_set, path, name, options_constraints, options, mapping)
@route_set = route_set
@path = path
@name = name
@options_constraints = options_constraints
@options = options
@mapping = mapping
end

def scope
@scope ||= [:routes, :controllers].concat mapping.defaults[:controller].split('/').map(&:to_sym)
end
end
end
26 changes: 15 additions & 11 deletions lib/route_translator/translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

require File.expand_path('../translator/route_helpers', __FILE__)
require File.expand_path('../translator/path', __FILE__)
require File.expand_path('../route', __FILE__)

module RouteTranslator
module Translator
Expand Down Expand Up @@ -35,28 +36,31 @@ def translate_name(name, locale, named_routes_names)

module_function

def translations_for(route_set, path, name, options_constraints, options, mapping)
RouteTranslator::Translator::RouteHelpers.add name, route_set.named_routes
def translations_for(route)
RouteTranslator::Translator::RouteHelpers.add route.name, route.route_set.named_routes

scope = [:routes, :controllers].concat mapping.defaults[:controller].split('/').map(&:to_sym)
available_locales.each do |locale|
begin
translated_path = RouteTranslator::Translator::Path.translate(path, locale, scope)
translated_path = RouteTranslator::Translator::Path.translate(route.path, locale, route.scope)
rescue I18n::MissingTranslationData => e
raise e unless RouteTranslator.config.disable_fallback
next
end

translated_options_constraints = options_constraints.dup
translated_options = options.dup
yield translated_attributes(locale, translated_path, route)
end
end

translated_options_constraints[RouteTranslator.locale_param_key] = locale.to_s
translated_options[RouteTranslator.locale_param_key] = locale.to_s.gsub('native_', '') unless translated_options.include?(RouteTranslator.locale_param_key)
def translated_attributes(locale, translated_path, route)
translated_options_constraints = route.options_constraints.dup
translated_options = route.options.dup

translated_name = translate_name(name, locale, route_set.named_routes.names)
translated_options_constraints[RouteTranslator.locale_param_key] = locale.to_s
translated_options[RouteTranslator.locale_param_key] = locale.to_s.gsub('native_', '') unless translated_options.include?(RouteTranslator.locale_param_key)

yield translated_name, translated_path, translated_options_constraints, translated_options
end
translated_name = translate_name(route.name, locale, route.route_set.named_routes.names)

[translated_name, translated_path, translated_options_constraints, translated_options]
end

def route_name_for(args, old_name, suffix, kaller)
Expand Down

0 comments on commit 2349136

Please sign in to comment.