Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7543891
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T08:32:46+00:00 2026-05-30T08:32:46+00:00

I have only a vague idea on phrasing this, so question as needed: I

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T08:32:47+00:00Added an answer on May 30, 2026 at 8:32 am

    Based on your code and example, this fits:

    # No need to put a class in a namespace of the same name, just make the module a class
    # Also, if you inherit from a struct, it can save you a lot of typing. It defines the setters and getters for you.
    class DashControl < Struct.new(:title, :instance, :actions)
    
      # since it looks like you always access it the same way, create a class method
      # which does this initialization and invocation
      def self.for(*args)
        new(*args).dashcontroller
      end
    
      def initialize(title='default title', instance='default instance', actions=:none)
        # here, we can use our own defaults and normalization and pass the results up to the struct
        super title, instance, normalize(actions)
      end
    
      # didn't make sense to call this initialize_tag, as it was initializing actions
      # also there was already an initialize actions method which just checked for the case of a hash
      # but then elsewhere you checked for other things. Better to just put it all in one method and return it
      # (then you aren't setting it every time you want to ask it to calculate that value)
      # also using guard clauses (the if statements that return early) instead of the case, as they are easier to understand
      def normalize(actions)
        return Hash.new if actions == :none
        return actions unless actions == :manage_default
        default_actions
      end  
    
      # the value of default_actions is complicated and noisy, separate it out to its own method
      # this prevents it from cluttering the code around it, and also allows us to access,
      # and to do this without the side effects of setting values.
      def default_actions
        { :statusactions => [], 
          :formactions   => [ ['#tabaccount', 'addaccount'],
                              ['#tabuser', 'addusers'],
                              ['#tabadd','adddomain'] ],
          :linkactions   => [ [] ],
          :filteractions => [ [] ] }
      end
    
      # use the getters instead of the ivars (I consider this a generally best practice -- and you could have
      # done it before, since you declared the attr_accessor, even though I'm accessing it through the struct)
      def dashcontroller
        {:title => title, :instance => instance, :actions => actions }
      end
    end
    
    DashControl.for                                           # => {:title=>"default title", :instance=>"default instance", :actions=>{}}
    DashControl.for('Catchy Title', '@book', :none)           # => {:title=>"Catchy Title", :instance=>"@book", :actions=>{}}
    DashControl.for('Catchy Title', '@book', :manage_default) # => {:title=>"Catchy Title", :instance=>"@book", :actions=>{:statusactions=>[], :formactions=>[["#tabaccount", "addaccount"], ["#tabuser", "addusers"], ["#tabadd", "adddomain"]], :linkactions=>[[]], :filteractions=>[[]]}}
    DashControl.for('Catchy Title', '@book', a: 'b')          # => {:title=>"Catchy Title", :instance=>"@book", :actions=>{:a=>"b"}}
    DashControl.for('Catchy Title', '@book', 123)             # => {:title=>"Catchy Title", :instance=>"@book", :actions=>123}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I only have vague idea of what WCF services are, but since most jobs
I have only recently started working with the MVC approach, so I suppose this
I have only this in my mxml source code: <?xml version=1.0 encoding=utf-8?> <mx:Canvas xmlns:mx=http://www.adobe.com/2006/mxml
I feel like I have no idea what I'm doing.. I have a vague
This is a very wide-ranging/vague question, but here goes. Apologies in advance. The app
If I have a set of small string values, and I want to fetch
Simple question, I have code like this: class Context[A] { def t: A }
The idea here is that the user can only have one selection for an
I have this demo . The value only updates if I refresh the page,
This is going to be a vague and obscure question, which is probably due

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.