Module: Puffer::Extensions::Mapper

Extended by:
ActiveSupport::Concern
Defined in:
lib/puffer/extensions/mapper31.rb,
lib/puffer/extensions/mapper32.rb

Overview

Extends rails mapper to provide resources nesting tree structure with request params. Route defaults contains :puffer key with node index in resources tree nodes array. See Puffer::Resource::Tree

Resource tree depends on routes resources nesting.

ex:

namespace :admin do
  resources :users do
    resource :profile
    resources :articles
  end
  resources :orders
end

will produce:

[
  {:scope => 'admin', :current => :users, :children => [:profile, :articles], :ancestors => []},
  {:scope => 'admin', :current => :profile, :children => [], :ancestors => [:users]},
  {:scope => 'admin', :current => :articles, :children => [], :ancestors => [:users]},
  {:scope => 'admin', :current => :orders, :children => [], :ancestors => []},
]

a complete tree structure with nodes array to acces with node index (Puffer::Resource::Tree#to_struct).

Also this mapper extension adds come aaditional routes for every pufer controller and namespace.

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#namespace_with_puffer(path, options = {}) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/puffer/extensions/mapper32.rb', line 41

def namespace_with_puffer path, options = {}
  namespace_without_puffer path, options do
    yield

    if ::Rails.application.routes.resources_tree.any? {|node| node.scope == @scope[:module].to_sym}
      old, @scope[:module] = @scope[:module], 'admin'
      root :to => 'dashboard#index'
      @scope[:module] = old
    end
  end
end

#resource_scope_with_puffer(kind, resource, &block) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/puffer/extensions/mapper32.rb', line 53

def resource_scope_with_puffer kind, resource, &block
  controller = "#{[@scope[:module], resource.controller].compact.join("/")}_controller".camelize.constantize rescue nil
  if controller && controller.puffer?
    singular = kind == :resource
    name = (singular ? resource.singular : resource.plural).to_sym

    resource_node = ::Rails.application.routes.resources_tree.append_node swallow_nil{@scope[:defaults][:puffer]},
      :name => name, :scope => @scope[:module].to_sym, :controller => controller, :singular => singular

    defaults :puffer => resource_node do
      resource_scope_without_puffer kind, resource do
        block.call if block

        member do
          controller._members.each do |action|
            send *action.route
          end
        end

        collection do
          controller._collections.each do |action|
            send *action.route
          end
          get '/event/:fieldset/:field/:event(/:identifer)', :action => :event, :as => :event
        end
      end
    end
  else
    resource_scope_without_puffer kind, resource, &block
  end
end