Guest User

Untitled

a guest
Feb 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. # Models
  2.  
  3. class Agent < ActiveRecord::Base
  4. belongs_to :manager
  5. belongs_to :site
  6. belongs_to :workgroup
  7.  
  8. named_scope :of_workgroup, lambda { |workgroup| {:conditions => ['workgroup_id = ?', @workgroup.id] }
  9. end
  10.  
  11. class Manager < ActiveRecord::Base
  12. has_many :agents
  13.  
  14. named_scope :of_workgroup, lambda { |workgroup| {:conditions => ['workgroup_id = ?', @workgroup.id] }
  15. end
  16.  
  17. class Site < ActiveRecord::Base
  18. has_many :agents
  19. has_many :managers, :through => :agents, :uniq => true, :order => "managers.name"
  20. has_many :workgroups, :through => :agents, :uniq => true, :order => "workgroups.name"
  21. end
  22.  
  23. class Workgroup < ActiveRecord::Base
  24. has_many :agents
  25. has_many :managers, :through => :agents, :uniq => true, :order => "managers.name"
  26. has_many :sites, :through => :agents, :uniq => true, :order => "sites.name"
  27. end
  28.  
  29. # Controller
  30.  
  31. class WorkgroupsController < ApplicationController
  32. def show
  33. @workgroup = Workgroup.find(params[:id])
  34. end
  35. end
  36.  
  37. # Existing view
  38.  
  39. <% for site in @workgroup.sites %>
  40. <h2><em><%= site.name %></em></h2>
  41. <% for manager in site.managers %>
  42. <h3><%= manager.name %></h3>
  43. <ul>
  44. <% for agent in manager.agents %>
  45. <li><%= link_to agent.name, agent_path(agent.id) %></li>
  46. <% end %>
  47. </ul>
  48. <% end %>
  49. <% end %>
  50.  
  51. # It's a scoping problem, but I just can't put my finger on it right now.
  52. # Because workgroups can have more than one site, and sites can have more than one workgroup
  53. # not to mention that managers can have agents that span workgroups, this hierarchy breaks down.
  54.  
  55. # I don't think that I want to lock it in a certain way in the associations because I need the flexibility
  56. # later to look at a group of agents by site, manager, or workgroup independently.
  57.  
  58. # New view
  59.  
  60. <% for site in @workgroup.sites %>
  61. <h2><em><%= site.name %></em></h2>
  62. <% for manager in site.managers.of_workgroup(@workgroup) %>
  63. <h3><%= manager.name %></h3>
  64. <ul>
  65. <% for agent in manager.agents.of_workgroup(@workgroup) %>
  66. <li><%= link_to agent.name, agent_path(agent.id) %></li>
  67. <% end %>
  68. </ul>
  69. <% end %>
  70. <% end %>
  71.  
  72. # Looks ugly. Is there a proper way to do this?
  73.  
  74. # Ultimately it should look like this:
  75. # Workgroup
  76. # Site A
  77. # Manager 1
  78. # Agent
  79. # Agent
  80. # Agent
  81. # Manager 2
  82. # Agent
  83. # Agent
  84. # Agent
  85. # Site B
  86. # Manager 3
  87. # Agent
  88. # Agent
  89. # Agent
  90. # Manager 4
  91. # Agent
  92. # Agent
  93. # Agent
Add Comment
Please, Sign In to add comment