Class: EasyCrumbs::Collection

Inherits:
Object
  • Object
show all
Defined in:
lib/easycrumbs/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(request, options = {}) ⇒ Collection

Returns a new instance of Collection.



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/easycrumbs/collection.rb', line 5

def initialize(request, options = {})
  @request = request

  @route = find_route
  @path = find_path
  @controller = @path[:controller]
  @action = @path[:action]

  @pathes = make_pathes
  @breadcrumbs = make_breadcrumbs(options)
end

Instance Attribute Details

Returns the value of attribute breadcrumbs.



3
4
5
# File 'lib/easycrumbs/collection.rb', line 3

def breadcrumbs
  @breadcrumbs
end

#pathObject (readonly)

Returns the value of attribute path.



3
4
5
# File 'lib/easycrumbs/collection.rb', line 3

def path
  @path
end

#routeObject (readonly)

Returns the value of attribute route.



3
4
5
# File 'lib/easycrumbs/collection.rb', line 3

def route
  @route
end

Instance Method Details

#find_pathObject

Return hash with path parameter for example: { :controller => ‘movies’, :action => ‘show’, :country_id => ‘23’, :id => ‘12’ }



30
31
32
# File 'lib/easycrumbs/collection.rb', line 30

def find_path
  @route.recognize(request_path, :method => request_method)
end

#find_routeObject

Finding route with given path and method Return ActionController:Routing::Route object



19
20
21
22
23
24
25
# File 'lib/easycrumbs/collection.rb', line 19

def find_route
  routes = ActionController::Routing::Routes.routes.select do |route|
    route.recognize(request_path, :method => request_method) != nil
  end
  raise EasyCrumbs::NotRecognized if routes.empty?
  routes.first
end

#last_controller_segmentObject

Returns last controller segment in segments



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

def last_controller_segment
  segments.select{ |seg| seg.is_a?(ActionController::Routing::StaticSegment) && seg.value != "new"}.last
end

#make_breadcrumbs(options = {}) ⇒ Object

Return array of breadcrumbs object in right order



83
84
85
86
87
88
89
90
91
# File 'lib/easycrumbs/collection.rb', line 83

def make_breadcrumbs(options = {})
  breadcrumbs = [Breadcrumb.new(ApplicationController.new, options)]
  objects.each_with_index do |object, index|
    options.merge!({:action => @action}) if index == objects.size - 1
    options.merge!({:path => @pathes[index]})
    breadcrumbs << Breadcrumb.new(object, options)
  end
  breadcrumbs
end

#make_pathesObject

Retrun array of pathes for every segment for example: countries > 1 > movies > 2 > actors> 3

=> ‘index’, :controller => ‘countries’, => ‘show’, :controller => ‘countries’, :id => 1, => ‘index’, :controller => ‘movies’, :country_id => 1, => ‘show’, :controller => ‘movies’, :country_id => 1, :id => 2, => ‘index’, :controller => ‘actors’, :country_id => 1, :movie_id => 2, => ‘update’, :controller => ‘actors’, :country_id => 1, :movie_id => 2, :id => 3



132
133
134
135
136
137
138
139
140
141
142
143
# File 'lib/easycrumbs/collection.rb', line 132

def make_pathes
  path = {}
  segments.map do |segment|
    if segment.is_a? ActionController::Routing::DynamicSegment
      path.merge! path_for_model(segment)
      result = repaired_model_path(path)
    else
      result = path.merge! path_for_controller(segment)
    end
    result.dup
  end
end

#objectsObject

Retruning array of controllers and models objects from right segments for example

#<CountriesController:0x001>, #<Country:0x001 @name=“usa”>, #<MoviesController:0x001>, #<Movie:0x001 @name=“titanic”>


72
73
74
75
76
77
78
79
80
# File 'lib/easycrumbs/collection.rb', line 72

def objects
  segments.map do |segment|
    if segment.is_a? ActionController::Routing::DynamicSegment
      pick_model(segment)
    else
      pick_controller(segment)
    end
  end
end

#path_for_controller(segment) ⇒ Object

Retrun parameters for path of controller



105
106
107
108
109
110
111
# File 'lib/easycrumbs/collection.rb', line 105

def path_for_controller(segment)
  if segment.value == "new"
    {:action => "new", :controller => last_controller_segment.value}
  else
    {:action => 'index', :controller => segment.value}
  end
end

#path_for_model(segment) ⇒ Object

Retrurn parameters for path of model If it is last object then action is equal to request action



95
96
97
98
99
100
101
102
# File 'lib/easycrumbs/collection.rb', line 95

def path_for_model(segment)
  key = segment.key
  if key == :id
    {:action => @action, :id => @path[key]}
  else
    {:action => 'show', key => @path[key]}
  end
end

#pick_controller(segment) ⇒ Object

Returning controller object from static segment



46
47
48
49
# File 'lib/easycrumbs/collection.rb', line 46

def pick_controller(segment)
  segment = last_controller_segment if segment.value == "new"
  "#{segment.value.titlecase}Controller".constantize.new
end

#pick_model(segment) ⇒ Object

Retrung model object from dynamic segment If key has not model name then it is taken from current controller(it is taken from path)



58
59
60
61
62
63
64
65
66
67
# File 'lib/easycrumbs/collection.rb', line 58

def pick_model(segment)
  key = segment.key
  if key == :id
    model = @controller.singularize
  else
    model = key.to_s[0..-4]  # model_id without last 3 signs = model
  end
  model = model.titlecase.constantize
  model.find(@path[key])
end

#render(options = {}) ⇒ Object



145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/easycrumbs/collection.rb', line 145

def render(options = {})
  options[:separator] ||= " > "
  options[:last_link] = true if options[:last_link].nil?

  elements = @breadcrumbs.map do |breadcrumb|
    if options[:last_link] == false && breadcrumb == @breadcrumbs.last
      breadcrumb.name
    else
      link_to breadcrumb.name, breadcrumb.path
    end
  end
  elements.join(options[:separator])
end

#repaired_model_path(path) ⇒ Object

If controller name is connected with object then parameter should be :id instead of :object_id => ‘movies’, :movie_id => 1 will be => ‘movies’, :id => 1



115
116
117
118
119
120
# File 'lib/easycrumbs/collection.rb', line 115

def repaired_model_path(path)
  path = path.dup
  object_param = "#{path[:controller].singularize}_id".to_sym
  id = path.delete(object_param)
  id.nil? ? path : path.merge({:id => id})
end

#segmentsObject

Select only static and dynamic segments from route. Static segments points at controllers and dynamic points at models. It is given in right order If last segment is equal to member action then it should be deleted. for example movies/123/edit should not return “edit” segment



37
38
39
40
41
42
43
# File 'lib/easycrumbs/collection.rb', line 37

def segments
  segments = @route.segments.select do |segment|
    [ActionController::Routing::DynamicSegment, ActionController::Routing::StaticSegment].include? segment.class
  end
  segments.pop if segments.last.is_a?(ActionController::Routing::StaticSegment) && segments.last.value == @action && segments.last.value != 'new'
  segments
end