Module: RoutingData

Defined in:
lib/routing_data.rb,
lib/routing_data/engine.rb,
lib/routing_data/version.rb,
lib/routing_data/view_helpers.rb

Defined Under Namespace

Modules: ViewHelpers Classes: Engine

Constant Summary collapse

VERSION =
"0.2.2"

Class Method Summary collapse

Class Method Details

.actions_for(controller) ⇒ Object

Returns array of actions for specific controller Example: RoutingData.actions_for(:pages) #=> [:index, :show, :new, :edit, :create, :update, :destroy]



47
48
49
# File 'lib/routing_data.rb', line 47

def self.actions_for(controller)
  controllers_actions_map[controller] || []
end

.controllersObject

Returns list of controllers



52
53
54
# File 'lib/routing_data.rb', line 52

def self.controllers
  @controllers ||= controllers_actions_map.keys
end

.controllers_actions_mapObject

Returns controllers -> actions map Example: RoutingData.controlles_actions_map #=>

posts: [:index, :show],
users: [:index, :show, :new, :create],
pages: [:index, :show, :new, :edit, :create, :update, :destroy]



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/routing_data.rb', line 25

def self.controllers_actions_map
  unless @controllers_actions_map
    @controllers_actions_map = {}

    routes.each do |route|
      controller = route.defaults[:controller]

      if controller
        controller = controller.to_sym
        @controllers_actions_map[controller] ||= []
        @controllers_actions_map[controller] << route.defaults[:action].to_sym
      end
    end
  end

  @controllers_actions_map
end

.routesObject

Returns Routes objects



7
8
9
# File 'lib/routing_data.rb', line 7

def self.routes
  @routes ||= Rails.application.routes.routes
end

.url_helpersObject

Returns url helpers module through which you can access url helpers in any part of your rails app.



12
13
14
# File 'lib/routing_data.rb', line 12

def self.url_helpers
  @url_helpers ||= Rails.application.routes.url_helpers
end

.url_helpers_data(opts = {}) ⇒ Object

Returns url helpers data hash.

url_helpers_data - returns full url helpers data. url_helpers_data(with_name_in: [‘user’, ‘post’, ‘forum’]) - returns data for url helpers with specific names. url_helpers_data(for_controllers: [‘posts’, ‘users’]) - returns url helpers data for PostsController & UsersController



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/routing_data.rb', line 61

def self.url_helpers_data(opts = {})
  unless @url_helpers_data
    @url_helpers_data = {}

    routes.to_a.each do |route|
      if route.name
        @url_helpers_data[route.name] = {
          name:       route.name,
          path:       route.path.spec.to_s,
          controller: route.defaults[:controller],
          action:     route.defaults[:action],
          parts:      route.parts
        }
      end
    end
  end

  if opts[:with_name_in]
    url_helpers_data = @url_helpers_data.select { |name, data| opts[:with_name_in].include? name }
  elsif opts[:for_controllers]
    url_helpers_data = @url_helpers_data.select { |name, data| opts[:for_controllers].include? data[:controller] }
  else
    url_helpers_data = @url_helpers_data
  end

  opts[:as_json] ? url_helpers_data.to_json : url_helpers_data
end