Last active
August 29, 2015 14:00
-
-
Save xixilive/11389507 to your computer and use it in GitHub Desktop.
Restful routing spec macor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| module RoutingMacors | |
| CRUD = { index: :get, show: :get, new: :get, edit: :get, update: :put, create: :post, destroy: :delete } | |
| def have_restful_resource *resources | |
| faked_id = Time.now.to_i.to_s | |
| options = resources.extract_options! | |
| collection_name = resources.last.to_s.pluralize | |
| ancestors = resources[0..-2].collect{|ancestor| [ancestor.to_s.pluralize, faked_id] }.join("/") | |
| actions = CRUD.keys | |
| actions.reject!{|a| [options[:except]].flatten.include?(a) } if options[:except].present? | |
| actions = actions & [options[:only]].flatten if options[:only].present? | |
| actions.uniq! | |
| paths = { | |
| index: "/#{ancestors}/#{collection_name}", | |
| show: "/#{ancestors}/#{collection_name}/#{faked_id}", | |
| new: "/#{ancestors}/#{collection_name}/new", | |
| create: "/#{ancestors}/#{collection_name}", | |
| edit: "/#{ancestors}/#{collection_name}/#{faked_id}/edit", | |
| update: "/#{ancestors}/#{collection_name}/#{faked_id}", | |
| destroy: "/#{ancestors}/#{collection_name}/#{faked_id}" | |
| } | |
| CRUD.keys.each do |action| | |
| controller_path = [options[:namespace], options[:scope], '/', collection_name].reject(&:blank?).join("/") | |
| controller_path.gsub!(/\/+/,'/') | |
| controller_path.gsub!(/\A\/+/,'') | |
| route_matcher = {controller: controller_path, action: action.to_s} | |
| resources[0..-2].each do |res| | |
| res_id = '%s_id' % res.to_s.singularize | |
| route_matcher.merge!( res_id => faked_id) | |
| end | |
| route_matcher.merge!(id: faked_id) if [:show, :edit, :update, :destroy].include?(action) | |
| verb, path = CRUD[action], [options[:scope], '/', paths[action]].join('/').gsub(/\/+/,'/') | |
| if actions.include?(action) | |
| # expectations for specified routings | |
| puts "\t\t#{verb.to_s.upcase} #{path}" | |
| expect(verb => path).to route_to(route_matcher) | |
| else | |
| # expect excepted routings NOT to be routable | |
| puts "\t\t!#{verb.to_s.upcase} #{path}" | |
| expect(verb => path).not_to route_to(route_matcher) | |
| end | |
| end | |
| end | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment