Class: Generators::Hobo::Routes::Router
- Inherits:
-
Object
- Object
- Generators::Hobo::Routes::Router
- Defined in:
- lib/generators/hobo/routes/router.rb
Constant Summary collapse
- ID_REQUIREMENT =
specify that an id CANNOT be null - needed to disambiguate /models from /models/
"{ :id => %r([^#{ActionDispatch::Routing::SEPARATORS.join}]+) }"
Instance Attribute Summary collapse
-
#controller ⇒ Object
readonly
Returns the value of attribute controller.
-
#model ⇒ Object
readonly
Returns the value of attribute model.
-
#record ⇒ Object
readonly
Returns the value of attribute record.
-
#records ⇒ Object
readonly
Returns the value of attribute records.
-
#subsite ⇒ Object
readonly
Returns the value of attribute subsite.
Instance Method Summary collapse
- #basic_resources ⇒ Object
- #emit_hash(hash, prefix) ⇒ Object
- #emit_resources ⇒ Object
-
#index_action_routes ⇒ Object
deprecated.
- #index_actions ⇒ Object
-
#initialize(subsite, controller) ⇒ Router
constructor
A new instance of Router.
- #lifecycle_collection_actions ⇒ Object
- #lifecycle_member_actions ⇒ Object
-
#lifecycle_routes(subsite) ⇒ Object
deprecated.
- #owner_actions ⇒ Object
-
#owner_routes ⇒ Object
deprecated.
-
#reorder_routes ⇒ Object
deprecated.
-
#resource_routes ⇒ Object
deprecated.
- #resources_hash ⇒ Object
-
#show_action_routes ⇒ Object
deprecated.
- #show_actions ⇒ Object
-
#user_routes ⇒ Object
NOT deprecated.
-
#web_method_routes ⇒ Object
deprecated.
- #web_methods ⇒ Object
Constructor Details
#initialize(subsite, controller) ⇒ Router
Returns a new instance of Router.
11 12 13 14 15 16 17 18 |
# File 'lib/generators/hobo/routes/router.rb', line 11 def initialize(subsite, controller) raise ::Hobo::Error, "#{controller} is not a Hobo::Controller::Model" unless controller < ::Hobo::Controller::Model @subsite = subsite @controller = controller @model = controller.model @records = controller.controller_name @record = @records.singularize end |
Instance Attribute Details
#controller ⇒ Object (readonly)
Returns the value of attribute controller.
9 10 11 |
# File 'lib/generators/hobo/routes/router.rb', line 9 def controller @controller end |
#model ⇒ Object (readonly)
Returns the value of attribute model.
9 10 11 |
# File 'lib/generators/hobo/routes/router.rb', line 9 def model @model end |
#record ⇒ Object (readonly)
Returns the value of attribute record.
9 10 11 |
# File 'lib/generators/hobo/routes/router.rb', line 9 def record @record end |
#records ⇒ Object (readonly)
Returns the value of attribute records.
9 10 11 |
# File 'lib/generators/hobo/routes/router.rb', line 9 def records @records end |
#subsite ⇒ Object (readonly)
Returns the value of attribute subsite.
9 10 11 |
# File 'lib/generators/hobo/routes/router.rb', line 9 def subsite @subsite end |
Instance Method Details
#basic_resources ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/generators/hobo/routes/router.rb', line 56 def basic_resources actions = %w(index new edit show create update destroy).select {|action| controller.public_method_defined?(action)} if actions.length == 7 "resources :#{records}" else "resources :#{records}, :only => [#{actions.map{|a| ':'+a}.join(', ')}]" end end |
#emit_hash(hash, prefix) ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/generators/hobo/routes/router.rb', line 20 def emit_hash(hash, prefix) s = "" hash.each do |key, val| s << "#{prefix}#{key}" unless val.blank? s << " do\n" val.each do |sub| if sub.is_a?(Hash) s << emit_hash(sub, prefix + " ") else s << "#{prefix} #{sub}\n" end end s << "#{prefix}end\n" else s << "\n" end end s end |
#emit_resources ⇒ Object
122 123 124 125 |
# File 'lib/generators/hobo/routes/router.rb', line 122 def emit_resources "# #{@resource_hash.inspect}\n"+ "# #{owner_actions.inspect}" end |
#index_action_routes ⇒ Object
deprecated
128 129 130 131 132 |
# File 'lib/generators/hobo/routes/router.rb', line 128 def index_action_routes controller.index_actions.map do |action| link( "get '#{records}/#{action}(.:format)', :as => '#{action}_#{records}'", action ) end.compact end |
#index_actions ⇒ Object
65 66 67 68 69 |
# File 'lib/generators/hobo/routes/router.rb', line 65 def index_actions controller.index_actions.map do |action| "get '#{action}'" end end |
#lifecycle_collection_actions ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/generators/hobo/routes/router.rb', line 77 def lifecycle_collection_actions return [] unless defined? model::Lifecycle model::Lifecycle.creators.values.where.routable_for?(@subsite).*.name.map do |creator| ["post '#{creator}', :action => 'do_#{creator}'", "get '#{creator}'" ] end.flatten end |
#lifecycle_member_actions ⇒ Object
85 86 87 88 89 90 91 |
# File 'lib/generators/hobo/routes/router.rb', line 85 def lifecycle_member_actions return [] unless defined? model::Lifecycle model::Lifecycle.transitions.where.routable_for?(@subsite).*.name.map do |transition| ["put '#{transition}', :action => 'do_#{transition}'", "get '#{transition}'"] end.flatten end |
#lifecycle_routes(subsite) ⇒ Object
deprecated
135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/generators/hobo/routes/router.rb', line 135 def lifecycle_routes(subsite) return [] unless defined? model::Lifecycle routes = [] model::Lifecycle.creators.values.where.routable_for?(subsite).*.name.each do |creator| routes << link("post '#{records}/#{creator}(.:format)' => '#{records}#do_#{creator}', :as => 'do_#{record}_#{creator}'", creator, :post) routes << link("get '#{records}/#{creator}(.:format)' => '#{records}##{creator}', :as => '#{record}_#{creator}'", creator) end model::Lifecycle.transitions.where.routable_for?(subsite).*.name.each do |transition| routes << link("put '#{records}/:id/#{transition}(.:format)' => '#{records}#do_#{transition}', :as => 'do_#{record}_#{transition}'", transition, :put) routes << link("get '#{records}/:id/#{transition}(.:format)' => '#{records}##{transition}', :as => '#{record}_#{transition}'", transition) end routes.compact.uniq end |
#owner_actions ⇒ Object
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/generators/hobo/routes/router.rb', line 99 def owner_actions controller.owner_actions.map do |owner, actions| collection_refl = model.reverse_reflection(owner) raise ::Hobo::Error, "Hob routing error -- can't find reverse association for #{model}##{owner} " + "(e.g. the :has_many that corresponds to a :belongs_to)" if collection_refl.nil? collection = collection_refl.name owner_class = model.reflections[owner.to_s].klass.name.underscore owner = owner.to_s.singularize if model.reflections[owner.to_s].macro == :has_many collection_path = "#{owner_class.pluralize}/:#{owner}_id/#{collection}" routes = [] routes << "get '/', :on => :new, :action => 'new_for_#{owner}'" if actions.include?(:new) collection_routes = [] collection_routes << "get '/', :action => 'index_for_#{owner}'" if actions.include?(:index) collection_routes << "post '/', :action => 'create_for_#{owner}'" if actions.include?(:create) routes << {"collection" => collection_routes} unless collection_routes.empty? { "resources :#{owner_class.pluralize}, :as => :#{owner}, :only => []" => [ "resources :#{collection}, :only => []" => routes] } end end |
#owner_routes ⇒ Object
deprecated
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
# File 'lib/generators/hobo/routes/router.rb', line 163 def owner_routes routes = [] controller.owner_actions.each_pair do |owner, actions| collection_refl = model.reverse_reflection(owner) raise ::Hobo::Error, "Hob routing error -- can't find reverse association for #{model}##{owner} " + "(e.g. the :has_many that corresponds to a :belongs_to)" if collection_refl.nil? collection = collection_refl.name owner_class = model.reflections[owner.to_s].klass.name.underscore owner = owner.to_s.singularize if model.reflections[owner.to_s].macro == :has_many collection_path = "#{owner_class.pluralize}/:#{owner}_id/#{collection}" actions.each do |action| action_for_owner = "#{action}_for_#{owner}" case action when :index routes << link("get '#{collection_path}(.:format)' => '#{records}##{action_for_owner}', :as => '#{records}_for_#{owner}'", action_for_owner) when :new routes << link("get '#{collection_path}/new(.:format)' => '#{records}##{action_for_owner}', :as => 'new_#{record}_for_#{owner}'", action_for_owner) when :create routes << link("post '#{collection_path}(.:format)' => '#{records}##{action_for_owner}', :as => 'create_#{record}_for_#{owner}'", action_for_owner, :post) end end end routes.compact end |
#reorder_routes ⇒ Object
deprecated
204 205 206 |
# File 'lib/generators/hobo/routes/router.rb', line 204 def reorder_routes [ link("post '#{records}/reorder(.:format)', :as => 'reorder_#{records}'", 'reorder', :post) ].compact end |
#resource_routes ⇒ Object
deprecated
150 151 152 153 154 155 156 157 158 159 160 |
# File 'lib/generators/hobo/routes/router.rb', line 150 def resource_routes [ link("get '#{records}(.:format)' => '#{records}#index', :as => '#{records}'", 'index'), link("get '#{records}/new(.:format)' => '#{records}#new', :as => 'new_#{record}'", 'new'), link("get '#{records}/:id/edit(.:format)' => '#{records}#edit', :as => 'edit_#{record}'", 'edit'), link("get '#{records}/:id(.:format)' => '#{records}#show', :as => '#{record}', :constraints => #{ID_REQUIREMENT}", 'show'), link("post '#{records}(.:format)' => '#{records}#create', :as => 'create_#{record}'", 'create', :post), link("put '#{records}/:id(.:format)' => '#{records}#update', :as => 'update_#{record}', :constraints => #{ID_REQUIREMENT}", 'update', :put), link("delete '#{records}/:id(.:format)' => '#{records}#destroy', :as => 'destroy_#{record}', :constraints => #{ID_REQUIREMENT}", 'destroy', :delete) ].compact end |
#resources_hash ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/generators/hobo/routes/router.rb', line 41 def resources_hash collections = [] collections += index_actions collections += lifecycle_collection_actions collections << "post 'reorder'" if controller.public_method_defined?(:reorder) members = [] members += show_actions members += web_methods members += lifecycle_member_actions right = [] right << {"collection" => collections} unless collections.blank? right << {"member" => members} unless members.blank? {basic_resources => right} end |
#show_action_routes ⇒ Object
deprecated
197 198 199 200 201 |
# File 'lib/generators/hobo/routes/router.rb', line 197 def show_action_routes controller.show_actions.map do |action| link("get '#{records}/:id/#{action}(.:format)' => '#{records}##{action}', :as => '#{record}_#{action}'", action) end.compact end |
#show_actions ⇒ Object
71 72 73 74 75 |
# File 'lib/generators/hobo/routes/router.rb', line 71 def show_actions controller.show_actions.map do |action| "get '#{action}'" end end |
#user_routes ⇒ Object
NOT deprecated
209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/generators/hobo/routes/router.rb', line 209 def user_routes return [] unless controller < ::Hobo::Controller::UserBase prefix = records == "users" ? "" : "#{record}_" [ link("post '#{prefix}login(.:format)' => '#{records}#login', :as => '#{record}_login_post'", 'login'), link("get '#{prefix}login(.:format)' => '#{records}#login', :as => '#{record}_login'", 'login'), link("get '#{prefix}logout(.:format)' => '#{records}#logout', :as => '#{record}_logout'", 'logout'), link("get '#{prefix}forgot_password(.:format)' => '#{records}#forgot_password', :as => '#{record}_forgot_password'", 'forgot_password'), link("post '#{prefix}forgot_password(.:format)' => '#{records}#forgot_password', :as => '#{record}_forgot_password_post'", 'forgot_password'), ].compact end |
#web_method_routes ⇒ Object
deprecated
190 191 192 193 194 |
# File 'lib/generators/hobo/routes/router.rb', line 190 def web_method_routes controller.web_methods.map do |action| link("post '#{records}/:id/#{action}(.:format)' => '#{records}##{action}', :as => '#{record}_#{action}'", action, :post) end.compact end |
#web_methods ⇒ Object
93 94 95 96 97 |
# File 'lib/generators/hobo/routes/router.rb', line 93 def web_methods controller.web_methods.map do |action| "post '#{action}'" end end |