Module: ParentControl
- Extended by:
- ActiveSupport::Concern
- Included in:
- AbstractResourcesController
- Defined in:
- app/controllers/concerns/parent_control.rb
Instance Method Summary collapse
-
#find_parent(path = nil, parms = nil) ⇒ Object
/employees/1/teams /employees/1/teams/5 /employees/1/teams/5/attach /theatres/2/contacts/5/uris.js.
- #parent ⇒ Object
- #parent? ⇒ Boolean
- #parent_class ⇒ Object
- #parent_class=(val) ⇒ Object
-
#parent_url(options = {}) ⇒ Object
parent_url returns the parent url - /employees/1.
-
#recognise_parent(elems) ⇒ Object
{ :action=>“attach”, :user_id=>“2” }.
-
#recognise_path(path) ⇒ Object
[‘theatres’,‘5’,‘contacts’,‘2’,‘uris.js’].
-
#recognise_resource(elems) ⇒ Object
:action=>“attach”, :user_id=>“2”, :id=>“3”.
-
#set_parents ⇒ Object
build an array of the resource - particular to <SELECT>.
-
#update_parenthood ⇒ Object
# /employees/1/teams/new # /employees/1/teams/1/edit # /employees/1/teams/1/delete.
Instance Method Details
#find_parent(path = nil, parms = nil) ⇒ Object
/employees/1/teams /employees/1/teams/5 /employees/1/teams/5/attach /theatres/2/contacts/5/uris.js
54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'app/controllers/concerns/parent_control.rb', line 54 def find_parent path=nil, parms=nil path ||= request.path parms ||= params if parms[:parent].nil? #or params[:parent_id].nil? paths=path.split("/") paths.pop if %w{new edit show create update delete index}.include? paths[-1] return nil if (paths.size < 3) #or (paths.size==4 and %w{edit new}.include?( parms[:action])) recognise_path paths.join("/") else parms[:parent].classify.constantize.find(parms[:parent_id]) end end |
#parent ⇒ Object
10 11 12 |
# File 'app/controllers/concerns/parent_control.rb', line 10 def parent @parent ||= find_parent end |
#parent? ⇒ Boolean
23 24 25 |
# File 'app/controllers/concerns/parent_control.rb', line 23 def parent? !(%w{NilClass TrueClass FalseClass}.include? parent.class.to_s) end |
#parent_class ⇒ Object
15 16 17 |
# File 'app/controllers/concerns/parent_control.rb', line 15 def parent_class @parent_class ||= @parent.class end |
#parent_class=(val) ⇒ Object
19 20 21 |
# File 'app/controllers/concerns/parent_control.rb', line 19 def parent_class= val @parent_class = val end |
#parent_url(options = {}) ⇒ Object
parent_url returns the parent url - /employees/1
29 30 31 32 33 34 |
# File 'app/controllers/concerns/parent_control.rb', line 29 def parent_url ={} parent? ? url_for(@parent) : "" # parent? ? ( "/%s/%s" % [ @parent.class.table_name, @parent.id ] ) : "" rescue Exception => e scoop_from_error e end |
#recognise_parent(elems) ⇒ Object
{ :action=>“attach”, :user_id=>“2” }
90 91 92 93 94 95 96 97 98 |
# File 'app/controllers/concerns/parent_control.rb', line 90 def recognise_parent elems elems.delete :action arr = elems.keys.first.to_s.split("_") return nil unless arr.include? "id" arr.pop arr.join("_").singularize.classify.constantize.find(elems.values.first) rescue nil end |
#recognise_path(path) ⇒ Object
- ‘theatres’,‘5’,‘contacts’,‘2’,‘uris.js’
68 69 70 71 72 73 74 75 76 |
# File 'app/controllers/concerns/parent_control.rb', line 68 def recognise_path path path_elements = Rails.application.routes.recognize_path path.gsub /\..*$/,'' # "/admin/users/2/printers/3/attach" => {:controller=>"printers", :action=>"attach", :user_id=>"2", :id=>"3"} recognise_parent( recognise_resource( path_elements ) ) rescue Exception => e nil # return [ nil, nil, false ] if e.class.to_s == "ActionController::RoutingError" end |
#recognise_resource(elems) ⇒ Object
:action=>“attach”, :user_id=>“2”, :id=>“3”
79 80 81 82 83 84 85 86 87 |
# File 'app/controllers/concerns/parent_control.rb', line 79 def recognise_resource elems resource_class = elems.delete(:controller).singularize.classify.constantize resource = resource_class.find( elems.delete(:id) ) elems rescue Exception => e return elems if e.class.to_s == "ActiveRecord::RecordNotFound" resource_class = nil elems end |
#set_parents ⇒ Object
build an array of the resource - particular to <SELECT>
39 40 41 42 43 44 45 |
# File 'app/controllers/concerns/parent_control.rb', line 39 def set_parents @parents = [] return @parents unless resource_class.respond_to? :roots #( 'arraying') && resource? ) @parents = resource_class.({ order: 'name'}, resource.possible_parents) rescue Exception => e scoop_from_error e end |
#update_parenthood ⇒ Object
# /employees/1/teams/new # /employees/1/teams/1/edit # /employees/1/teams/1/delete
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'app/controllers/concerns/parent_control.rb', line 104 def update_parenthood if params[:parent] and params[:parent_id] # raise "Ups - is this ready for prime time yet?" parent = params[:parent].classify.constantize.find(params[:parent_id]) unless parent.blank? case params[:action] when "create" children = eval("parent.#{resource_name}") children << resource unless children.include? resource # when "edit" when "delete" children = eval("parent.#{resource_name}") children >> resource end end end true rescue false end |