Created
December 23, 2014 23:15
-
-
Save josephrexme/04a8740950bc3988c1f3 to your computer and use it in GitHub Desktop.
How I display a list of child departments on my dashboard index
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DashboardController < ApplicationController | |
before_action :require_login | |
before_filter :init | |
def index | |
@departments.each do |department| | |
childfetch = Department.where(parent: "#{department.name}").select('name') | |
instance_variable_set("@#{department.name}Children".gsub(' ','_'), childfetch.map { |child| "#{child.name}" }.join(',') ) | |
end | |
end | |
def office | |
end | |
def preferences | |
end | |
def company | |
end | |
private | |
def require_login | |
unless session[:user_id].present? | |
redirect_to root_url | |
end | |
end | |
def init | |
@departments = Department.where(parent: '') | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module DashboardHelper | |
def instancevars(arg) | |
instance_variable_get("@#{arg}Children".gsub(' ','_')) | |
end | |
def nameslug(arg) | |
arg.downcase[0,5] | |
end | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<ul class="accord-style"> | |
<% @departments.each do |department| %> | |
<li <%= instancevars(department.name).present? ? "data-accord=#{nameslug(department.name)}":'' %>> | |
<%= "#{department.name}" %> | |
</li> | |
<% if(instancevars(department.name).present?) %> | |
<ul id="<%= nameslug(department.name) %>"> | |
<% instancevars(department.name).split(',').each do |child| %> | |
<li><a href=""><%= "#{child}" %></a></li> | |
<% end %> | |
</ul> | |
<% end %> | |
<% end %> | |
</ul> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment