module BackStack::ClassMethods

Public Instance Methods

backstack(edges) click to toggle source

This is the “macro” you put at the top of your controllers

# File lib/backstack.rb, line 27
def backstack(edges)

  # Note: its a little hard to follow but @bs_graph is a instance
  # variable of the class ApplicationController::Base (not an
  # object of AC::B).  There will only be one in the rails runtime,
  # and may behave differently depending on whether we're in dev
  # or prod mode, because dev mode reloads classes with each
  # request, and prod hopefully doesn't.  We'll write this module
  # to work correctly under both circumstances.

  # In rails we're going to use the string "controller#action" to
  # identify the page.  We're NOT going to let bs_add_edges
  # normalize that because it might be used for other frameworks.
  # If the user didn't pass in the controller we'll add it here.
  # The complete value should look like "controller#action", for
  # both keys and values.
  normalizer = lambda {|x| bs_action_normal(controller_name, x) }

  # Add new edges to existing graph, and extract out the labels.
  # bs_add_edges will also accumulate labels for us.
  @bs_graph, @bs_labels = bs_add_edges(@bs_graph, @bs_labels, 
                                      edges, normalizer)

end
bs_action_normal(controller, x) click to toggle source

Normalize controller and action to “controller#action”, unless second param already has “#”. This is up here in the class methods because normalizing controller/actions is utilized both here and in the ApplicationControllers objects

# File lib/backstack.rb, line 22
def bs_action_normal(controller, x)
  x.to_s.index("#") ? "#{x}" : "#{controller}##{x}"
end
get_bs_graph() click to toggle source

So ApplicationController methods can reach @bs_graph and @bs_labels. I wanted to use @@bs_graph, so both AC::B and the instance of AC could reach it, but this mysteriously breaks rails routing! So here are getters than we can use and access from the AC instance as self.class.get_bs_graph. RoR can be a real pit of dispair sometimes.

# File lib/backstack.rb, line 58
def get_bs_graph
  @bs_graph || {}
end
get_bs_labels() click to toggle source
# File lib/backstack.rb, line 62
def get_bs_labels
  @bs_labels || {}
end