class Generators::Hobo::Routes::Router

Constants

ID_REQUIREMENT

specify that an id CANNOT be null - needed to disambiguate /models from /models/

Attributes

controller[R]
model[R]
record[R]
records[R]
subsite[R]

Public Instance Methods

basic_resources() click to toggle source
# 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) click to toggle source
# 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() click to toggle source
# File lib/generators/hobo/routes/router.rb, line 122
def emit_resources
  "# #{@resource_hash.inspect}\n"+
  "# #{owner_actions.inspect}"
end
index_action_routes() click to toggle source

deprecated

# 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() click to toggle source
# 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() click to toggle source
# 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() click to toggle source
# 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) click to toggle source

deprecated

# 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() click to toggle source
# 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() click to toggle source

deprecated

# 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() click to toggle source

deprecated

# 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() click to toggle source

deprecated

# 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() click to toggle source
# 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() click to toggle source

deprecated

# 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() click to toggle source
# File lib/generators/hobo/routes/router.rb, line 71
def show_actions
  controller.show_actions.map do |action|
    "get '#{action}'"
  end
end
user_routes() click to toggle source

NOT deprecated

# 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() click to toggle source

deprecated

# 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() click to toggle source
# File lib/generators/hobo/routes/router.rb, line 93
def web_methods
  controller.web_methods.map do |action|
    "post '#{action}'"
  end
end

Public Class Methods

new(subsite, controller) click to toggle source
# 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