From f2b99ae1361434e59a10c6a538e2ca1539b50802 Mon Sep 17 00:00:00 2001 From: Luca Guidi Date: Mon, 11 May 2015 14:11:14 +0200 Subject: [PATCH] Cleanup per #51 --- Gemfile | 2 +- lib/lotus/routing/nested.rb | 42 ------------------- lib/lotus/routing/resource.rb | 25 +++++------ lib/lotus/routing/resource/action.rb | 16 +++---- lib/lotus/routing/resource/nested.rb | 39 +++++++++++++++++ lib/lotus/routing/resource/options.rb | 4 -- lib/lotus/routing/resources.rb | 3 +- lib/lotus/routing/resources/action.rb | 4 +- ...outes_test.rb => nested_resources_test.rb} | 0 ...outes_test.rb => nested_resources_test.rb} | 0 10 files changed, 65 insertions(+), 70 deletions(-) delete mode 100644 lib/lotus/routing/nested.rb create mode 100644 lib/lotus/routing/resource/nested.rb rename test/integration/{nested_routes_test.rb => nested_resources_test.rb} (100%) rename test/{nested_routes_test.rb => nested_resources_test.rb} (100%) diff --git a/Gemfile b/Gemfile index 0de3626..212a6d7 100644 --- a/Gemfile +++ b/Gemfile @@ -6,6 +6,6 @@ if !ENV['TRAVIS'] gem 'yard', require: false end -gem 'lotus-utils', '~> 0.4', require: false, github: 'lotus/utils', branch: 'master' +gem 'lotus-utils', '~> 0.4', require: false, github: 'lotus/utils', branch: '0.4.x' gem 'simplecov', require: false gem 'coveralls', require: false diff --git a/lib/lotus/routing/nested.rb b/lib/lotus/routing/nested.rb deleted file mode 100644 index 425300f..0000000 --- a/lib/lotus/routing/nested.rb +++ /dev/null @@ -1,42 +0,0 @@ -module Lotus - module Routing - # Helper class to calculate nested path - # - # @api private - # @since x.x.x - class Nested - # @api private - # @since x.x.x - SLASH = '/'.freeze - - # @api private - # @since x.x.x - def initialize(resource_name, resource) - @resource_name = resource_name.to_s.split(SLASH) - @resource = resource - @path = [] - end - - # @api private - # @since x.x.x - def calculate_nested_path - _calculate(@resource_name.dup, @resource) - end - - # @api private - # @since x.x.x - def nested_path - @path.reverse!.pop - @resource_name.zip(@path).flatten.join - end - - private - - def _calculate(param_wildcard, resource = nil) - return if resource.nil? - @path << resource.wildcard_param(param_wildcard.pop) - _calculate(param_wildcard, resource.parent_resource) - end - end - end -end \ No newline at end of file diff --git a/lib/lotus/routing/resource.rb b/lib/lotus/routing/resource.rb index 2c73b3a..a2f1965 100644 --- a/lib/lotus/routing/resource.rb +++ b/lib/lotus/routing/resource.rb @@ -17,7 +17,7 @@ class Resource # @api private # @since x.x.x - SLASH = '/'.freeze + NESTED_ROUTES_SEPARATOR = '/'.freeze # Set of default routes # @@ -49,15 +49,15 @@ class Resource # @api private # @since x.x.x - attr_reader :parent_resource + attr_reader :parent # @api private # @since 0.1.0 - def initialize(router, name, options = {}, parent_resource = nil, &blk) - @router = router - @name = name - @parent_resource = parent_resource - @options = Options.new(self.class.actions, options.merge(name: @name)) + def initialize(router, name, options = {}, parent = nil, &blk) + @router = router + @name = name + @parent = parent + @options = Options.new(self.class.actions, options.merge(name: @name)) generate(&blk) end @@ -79,20 +79,21 @@ def resource(name, options = {}, &blk) _resource(Resource, name, options, &blk) end - # Return slash, no wildcard param + # Return separator # # @api private # @since x.x.x def wildcard_param(route_param = nil) - SLASH + NESTED_ROUTES_SEPARATOR end private + # @api private + # @since x.x.x def _resource(klass, name, options, &blk) - merged_options = options.merge(separator: @options[:separator], namespace: @options[:namespace]) - nested_name = "#{@name}#{Resource::Action::NESTED_ROUTES_SEPARATOR}#{name}" - klass.new(@router, nested_name, merged_options, self, &blk) + options = options.merge(separator: @options[:separator], namespace: @options[:namespace]) + klass.new(@router, [@name, name].join(NESTED_ROUTES_SEPARATOR), options, self, &blk) end def generate(&blk) diff --git a/lib/lotus/routing/resource/action.rb b/lib/lotus/routing/resource/action.rb index 10c16da..8021ea9 100644 --- a/lib/lotus/routing/resource/action.rb +++ b/lib/lotus/routing/resource/action.rb @@ -1,7 +1,7 @@ require 'lotus/utils/string' require 'lotus/utils/path_prefix' require 'lotus/utils/class_attribute' -require 'lotus/routing/nested' +require 'lotus/routing/resource/nested' module Lotus module Routing @@ -48,6 +48,7 @@ class Action # @param router [Lotus::Router] # @param action [Lotus::Routing::Resource::Action] # @param options [Hash] + # @param resource [Lotus::Routing::Resource, Lotus::Routing::Resources] # # @api private # @@ -60,6 +61,7 @@ def self.generate(router, action, options = {}, resource = nil) # # @param router [Lotus::Router] # @param options [Hash] + # @param resource [Lotus::Routing::Resource, Lotus::Routing::Resources] # @param blk [Proc] # # @api private @@ -86,6 +88,8 @@ def generate(&blk) # Resource name # + # @return [String] + # # @api private # @since 0.1.0 # @@ -98,7 +102,7 @@ def generate(&blk) # # # 'identity' is the name passed in the @options def resource_name - @options[:name] + @resource_name ||= @options[:name].to_s end # Namespace @@ -254,9 +258,9 @@ def controller_name # @api private # @since x.x.x def _singularized_as - resource_name.to_s.split(NESTED_ROUTES_SEPARATOR).map do |name| + resource_name.split(NESTED_ROUTES_SEPARATOR).map do |name| Lotus::Utils::String.new(name).singularize - end.join(self.class.named_route_separator) + end end # Create nested rest path @@ -264,9 +268,7 @@ def _singularized_as # @api private # @since x.x.x def _nested_rest_path - nested = Nested.new(resource_name, @resource) - nested.calculate_nested_path - nested.nested_path + Nested.new(resource_name, @resource).to_path end end diff --git a/lib/lotus/routing/resource/nested.rb b/lib/lotus/routing/resource/nested.rb new file mode 100644 index 0000000..ecbc006 --- /dev/null +++ b/lib/lotus/routing/resource/nested.rb @@ -0,0 +1,39 @@ +module Lotus + module Routing + class Resource + # Helper class to calculate nested path + # + # @api private + # @since x.x.x + class Nested + # @api private + # @since x.x.x + SEPARATOR = '/'.freeze + + # @api private + # @since x.x.x + def initialize(resource_name, resource) + @resource_name = resource_name.to_s.split(SEPARATOR) + @resource = resource + @path = [] + _calculate(@resource_name.dup, @resource) + end + + # @api private + # @since x.x.x + def to_path + @path.reverse!.pop + @resource_name.zip(@path).flatten.join + end + + private + + def _calculate(param_wildcard, resource = nil) + return if resource.nil? + @path << resource.wildcard_param(param_wildcard.pop) + _calculate(param_wildcard, resource.parent) + end + end + end + end +end diff --git a/lib/lotus/routing/resource/options.rb b/lib/lotus/routing/resource/options.rb index e6fea1c..6f1d311 100644 --- a/lib/lotus/routing/resource/options.rb +++ b/lib/lotus/routing/resource/options.rb @@ -13,10 +13,6 @@ class Options # @since 0.1.0 attr_reader :actions - # @api private - # @since x.x.x - attr_reader :options - # Initialize the options for: # * Lotus::Router#resource # * Lotus::Router#resources diff --git a/lib/lotus/routing/resources.rb b/lib/lotus/routing/resources.rb index 57f345d..f93a441 100644 --- a/lib/lotus/routing/resources.rb +++ b/lib/lotus/routing/resources.rb @@ -41,8 +41,7 @@ class Resources < Resource # @api private # @since x.x.x def wildcard_param(route_param = nil) - sigularized_param = Lotus::Utils::String.new(route_param).singularize - "#{SLASH}:#{sigularized_param}_id#{SLASH}" + "/:#{ Lotus::Utils::String.new(route_param).singularize }_id/" end end end diff --git a/lib/lotus/routing/resources/action.rb b/lib/lotus/routing/resources/action.rb index 88fbd8f..cf7e0b6 100644 --- a/lib/lotus/routing/resources/action.rb +++ b/lib/lotus/routing/resources/action.rb @@ -38,7 +38,7 @@ module PluralizedAction # @api private # @since x.x.x def as - Lotus::Utils::String.new(super).pluralize + Lotus::Utils::String.new(super).pluralize end end @@ -50,7 +50,7 @@ def as # @see Lotus::Router#resources class CollectionAction < Resource::CollectionAction def as(action_name) - Lotus::Utils::String.new(super(action_name)).pluralize + Lotus::Utils::String.new(super(action_name)).pluralize end end diff --git a/test/integration/nested_routes_test.rb b/test/integration/nested_resources_test.rb similarity index 100% rename from test/integration/nested_routes_test.rb rename to test/integration/nested_resources_test.rb diff --git a/test/nested_routes_test.rb b/test/nested_resources_test.rb similarity index 100% rename from test/nested_routes_test.rb rename to test/nested_resources_test.rb