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

#dynamic_segmentsObject

Return array of dynamic segments for example

:country_id, :dynamic], [:movie_id, :dynamic], [:id, :dynamic

]


48
49
50
51
52
# File 'lib/easycrumbs/collection.rb', line 48

def dynamic_segments
  @route.third.path.names.map do |segment|
    [segment.to_sym, :dynamic]
  end
end

#find_pathObject

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


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

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

#find_route(request = @request) ⇒ Object

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


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

def find_route(request = @request)
  routes = Rails.application.routes.router.send(:find_routes, request.env)
  raise EasyCrumbs::NotRecognized if routes.empty?
  routes.first
end

#last_controller_segmentObject

Returns last controller segment in segments


78
79
80
81
# File 'lib/easycrumbs/collection.rb', line 78

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

#make_breadcrumbs(options = {}) ⇒ Object

Return array of breadcrumbs object in right order


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

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


162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# File 'lib/easycrumbs/collection.rb', line 162

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
  result = []
  @route.first.to_s.split("/")[1...-1].inject([]) do |current_path, segment|
    current_path << segment
    request = ActionDispatch::Request.new("PATH_INFO"     => "/#{current_path.join("/")}",
                                          "REQUEST_METHOD"  => "GET")

    result << (find_route(request).second rescue nil)

    current_path
  end

  (result << @route.second).compact
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”>

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

def objects
  segments.map do |segment|
    if segment.second == :dynamic
      pick_model(segment)
    else
      pick_controller(segment)
    end
  end.compact
end

#pick_controller(segment) ⇒ Object

Returning controller object from static segment


70
71
72
73
74
75
# File 'lib/easycrumbs/collection.rb', line 70

def pick_controller(segment)
  #segment = last_controller_segment if segment.value == "new"
  #"#{segment.value.titlecase}Controller".constantize.new
  segment = last_controller_segment if segment.first == :new
  "#{segment.first.to_s.titlecase}Controller".constantize.new rescue nil
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)


85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/easycrumbs/collection.rb', line 85

def pick_model(segment)
  key = segment.first
  if key == :id
    model = @controller.singularize
  elsif key.to_s.include?("_id")
    model = key.to_s[0..-4]  # model_id without last 3 signs = model
  else
    return nil
  end

  model = model.titlecase.constantize
  model.find(@path[key])
end

#render(options = {}) ⇒ Object


187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/easycrumbs/collection.rb', line 187

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


145
146
147
148
149
150
# File 'lib/easycrumbs/collection.rb', line 145

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

#resolve_segmentsObject

Return array of all segments with :static and :dynamic annotation for examle:

[:countries, :static], [:country_id, :dynamic], [:movies, :static], [:movie_id, :dynamic], [:actors, :static

]


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

def resolve_segments
  @route.third.path.spec.to_s.split(/[\/(\)\.]/).map do |segment|
    [segment.delete(":").to_sym, :static] unless segment.blank?
  end.compact.map do |segment|
    if dynamic_segments.map(&:first).include? segment.first
      [segment.first, :dynamic]
    else
      segment
    end
  end
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


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

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
  resolve_segments
end