I have only a vague idea on phrasing this, so question as needed:
I have a set of values I’m passing in my rails controller on a regular basis to a widget that differs slightly from page to page, from what I pass to it. This is is starting to get unwieldy for every controller, so I added a small class to help concatenate that process a bit (basic starting gist below).
#return dashcontroller hash from more succinct cues
module DashControl
class DashControl
attr_accessor :title, :instance, :actions
def initialize(title='default title', instance='default instance', actions={})
@title = title
@instance = instance
initialize_actions(actions)
end
def initialize_actions(actions)
actions.kind_of?(Hash) ? @actions = actions : initialize_tag(actions)
end
def initialize_tag(tag)
case tag
when :manage_default
@actions = {:statusactions => [],
:formactions => [ ['#tabaccount', 'addaccount'],
['#tabuser', 'addusers'],
['#tabadd','adddomain'] ],
:linkactions => [ [] ],
:filteractions => [ [] ] }
when :none
@actions = {}
#when
# @actions = {}
else
@actions = @actions
end
end
def dashcontroller
{:title => @title, :instance => @instance, :actions => @actions }
end
end
end
So basically I just need to pass an instance of this.dashcontroller and I get the hash I need with a lot less chaos in my controllers . The issue is with the @instance variable. I want to pass in the instance I’m using e.g. @book, @account, etc, and have it come out as @book, @account, etc. Instead, I get the contents of whatever I put into there as :instance => (contents of that instance). It doesn’t seem right to me as before I was just using e.g. @account, and then using that, but looking at it might not make any sort of difference in the widget, as I juggle things and work on my code-fu.
Basically my question is how to push an instance variable through a class like this, and still have it accessibile as it went in without having to do any backflips and transformations on the other side. There is probably a better way, but this is what I’m working with at the moment.
edit: pseudo-code
DashControl::DashControl.new("Catchy Title", @book, :none).dashcontroller
#=> {:title => "Catchy Title", :instance => @book, :actions => {} }
I think I can work with it, like I said its more an issue of my understanding of how things flow than an actual bug or anything difficult. I’d like to not have to do more gymnastics on the other end with the instance stuff, though the contents are there and that is all I really need, I just need some input on thinking it through to be less of a mess. I really need to refine what I’m sending through this, or use this to further refine what I’m sending on is the bottom line lesson to take away right now.
edit:
I ended up tossing this, but it was a learning experience…I went back the widget and I know more than when I originally set up the widget, so I’ve been able to set that up to take only the instance variable and bootstrap what it needs without adding another class, cleaning up my controllers and handing a lot back to the widget where I suspect it should/could have been to start.
Based on your code and example, this fits: