Class: EasyCrumbs::Collection
- Inherits:
-
Object
- Object
- EasyCrumbs::Collection
- Defined in:
- lib/easycrumbs/collection.rb
Instance Attribute Summary collapse
-
#breadcrumbs ⇒ Object
readonly
Returns the value of attribute breadcrumbs.
-
#path ⇒ Object
readonly
Returns the value of attribute path.
-
#route ⇒ Object
readonly
Returns the value of attribute route.
Instance Method Summary collapse
-
#dynamic_segments ⇒ Object
Return array of dynamic segments for example [ :country_id, :dynamic], [:movie_id, :dynamic], [:id, :dynamic] ].
-
#find_path ⇒ Object
Return hash with path parameter for example: { :controller => ‘movies’, :action => ‘show’, :country_id => ‘23’, :id => ‘12’ }.
-
#find_route(request = @request) ⇒ Object
Finding route with given path and method Return ActionController:Routing::Route object.
-
#initialize(request, options = {}) ⇒ Collection
constructor
A new instance of Collection.
-
#last_controller_segment ⇒ Object
Returns last controller segment in segments.
-
#make_breadcrumbs(options = {}) ⇒ Object
Return array of breadcrumbs object in right order.
-
#make_pathes ⇒ Object
Retrun array of pathes for every segment for example: countries > 1 > movies > 2 > actors> 3.
-
#objects ⇒ Object
Retruning array of controllers and models objects from right segments for example [#<CountriesController:0x001>, #<Country:0x001 @name=“usa”>, #<MoviesController:0x001>, #<Movie:0x001 @name=“titanic”>].
-
#pick_controller(segment) ⇒ Object
Returning controller object from static segment.
-
#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).
- #render(options = {}) ⇒ Object
-
#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.
-
#resolve_segments ⇒ Object
Return array of all segments with :static and :dynamic annotation for examle: [[:countries, :static], [:country_id, :dynamic], [:movies, :static], [:movie_id, :dynamic], [:actors, :static] ].
-
#segments ⇒ Object
Select only static and dynamic segments from route.
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, = {}) @request = request @route = find_route @path = find_path @controller = @path[:controller] @action = @path[:action] @pathes = make_pathes @breadcrumbs = () end |
Instance Attribute Details
#breadcrumbs ⇒ Object (readonly)
Returns the value of attribute breadcrumbs.
3 4 5 |
# File 'lib/easycrumbs/collection.rb', line 3 def @breadcrumbs end |
#path ⇒ Object (readonly)
Returns the value of attribute path.
3 4 5 |
# File 'lib/easycrumbs/collection.rb', line 3 def path @path end |
#route ⇒ Object (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_segments ⇒ Object
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_path ⇒ Object
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_segment ⇒ Object
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 ( = {}) = [Breadcrumb.new(ApplicationController.new, )] objects.each_with_index do |object, index| .merge!({:action => @action}) if index == objects.size - 1 .merge!({:path => @pathes[index]}) << Breadcrumb.new(object, ) end end |
#make_pathes ⇒ Object
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 |
#objects ⇒ Object
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( = {}) [:separator] ||= " > " [:last_link] = true if [:last_link].nil? elements = @breadcrumbs.map do || if [:last_link] == false && == @breadcrumbs.last .name else link_to .name, .path end end elements.join([: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_segments ⇒ Object
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 |
#segments ⇒ Object
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 |